在filter的配置下面添加上如下的配置 <listener> <listener-class>kick.load.resource.LoadResourceListener</listener-class> </listener> 三.LoadResourceTimerTask類(lèi) /* * Created on 2005-9-6 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package kick.load.resource;
public class LoadResourceTimerTask extends TimerTask { private ServletContext context = null;
/** * <p> * The resources object for our internal resources. * </p> */ protected MessageResources internal = null;
/** * <p> * The Digester used to produce ModuleConfig objects from a Struts * configuration file. * </p> * * @since Struts 1.1 */ protected Digester configDigester = null;
/** * <p> * The Java base name of our internal resources. * </p> * * @since Struts 1.1 */ protected String internalName = "org.apache.struts.action.ActionResources";
/** * <p> * The set of public identifiers, and corresponding resource names, for the * versions of the configuration file DTDs that we know about. There * <strong>MUST </strong> be an even number of Strings in this list! * </p> */ protected String registrations[] = { "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN", "/org/apache/struts/resources/struts-config_1_0.dtd", "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN", "/org/apache/struts/resources/struts-config_1_1.dtd", "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN", "/org/apache/struts/resources/struts-config_1_2.dtd", "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN", "/org/apache/struts/resources/web-app_2_2.dtd", "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN", "/org/apache/struts/resources/web-app_2_3.dtd" };
/** * <p> * The JDBC data sources that has been configured for this module, if any, * keyed by the servlet context attribute under which they are stored. * </p> */ protected FastHashMap dataSources = new FastHashMap();
/** * <p> * Comma-separated list of context-relative path(s) to our configuration * resource(s) for the default module. * </p> */ protected String config = "/WEB-INF/struts-config.xml";
List l = w.getServlets(); if (l.size() > 0) { initParams = ((Servlet) l.get(0)).getInitParams(); } initParams = ((Servlet) ((List) w.getServlets()).get(0)).getInitParams(); try { input.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
/** * <p> * Gracefully release any configDigester instance that we have created. * </p> * * @since Struts 1.1 */ protected void destroyConfigDigester() {
configDigester = null;
}
/** * <p> * Saves a String[] of module prefixes in the ServletContext under * Globals.MODULE_PREFIXES_KEY. <strong>NOTE </strong>- the "" prefix for * the default module is not included in this list. * </p> * * @param context * The servlet context. * @since Struts 1.2 */ protected void initModulePrefixes(ServletContext context) { ArrayList prefixList = new ArrayList();
Enumeration names = context.getAttributeNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (!name.startsWith(Globals.MODULE_KEY)) { continue; }
/** * <p> * Initialize the data sources for the specified module. * </p> * * @param config * ModuleConfig information for this module * * @exception ServletException * if initialization cannot be performed * @since Struts 1.1 */ protected void initModuleDataSources(ModuleConfig config) throws ServletException {
// :FIXME: Document UnavailableException?
if (log.isDebugEnabled()) { log.debug("Initializing module path '" + config.getPrefix() + "' data sources"); }
ServletContextWriter scw = new ServletContextWriter(getServletContext()); DataSourceConfig dscs[] = config.findDataSourceConfigs(); if (dscs == null) { dscs = new DataSourceConfig[0]; }
dataSources.setFast(false); for (int i = 0; i < dscs.length; i++) { if (log.isDebugEnabled()) { log.debug("Initializing module path '" + config.getPrefix() + "' data source '" + dscs[i].getKey() + "'"); } DataSource ds = null; try { ds = (DataSource) RequestUtils.applicationInstance(dscs[i].getType()); BeanUtils.populate(ds, dscs[i].getProperties()); ds.setLogWriter(scw);
/** * <p> * Initialize the module configuration information for the specified module. * </p> * * @param prefix * Module prefix for this module * @param paths * Comma-separated list of context-relative resource path(s) for * this modules's configuration resource(s) * * @exception ServletException * if initialization cannot be performed * @since Struts 1.1 */ protected ModuleConfig initModuleConfig(String prefix, String paths) throws ServletException {
if (log.isDebugEnabled()) { log.debug("Initializing module path '" + prefix + "' configuration from '" + paths + "'"); }
// Parse the configuration for this module ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); ModuleConfig config = factoryObject.createModuleConfig(prefix);
// Configure the Digester instance we will use Digester digester = initConfigDigester();
// Process each specified resource path while (paths.length() > 0) { digester.push(config); String path = null; int comma = paths.indexOf(','); if (comma >= 0) { path = paths.substring(0, comma).trim(); paths = paths.substring(comma + 1); } else { path = paths.trim(); paths = ""; }
// Force creation and registration of DynaActionFormClass instances // for all dynamic form beans we wil be using FormBeanConfig fbs[] = config.findFormBeanConfigs(); for (int i = 0; i < fbs.length; i++) { if (fbs[i].getDynamic()) { fbs[i].getDynaActionFormClass(); } }
return config; }
/** * <p> * Create (if needed) and return a new <code>Digester</code> instance that * has been initialized to process Struts module configuration files and * configure a corresponding <code>ModuleConfig</code> object (which must * be pushed on to the evaluation stack before parsing begins). * </p> * * @exception ServletException * if a Digester cannot be configured * @since Struts 1.1 */ protected Digester initConfigDigester() throws ServletException {
// :FIXME: Where can ServletException be thrown?
// Do we have an existing instance? if (configDigester != null) { return (configDigester); }
// Create a new Digester instance with standard capabilities configDigester = new Digester(); configDigester.setNamespaceAware(true); configDigester.setValidating(false); configDigester.setUseContextClassLoader(true); configDigester.addRuleSet(new ConfigRuleSet());
for (int i = 0; i < registrations.length; i += 2) { URL url = this.getClass().getResource(registrations[i + 1]); if (url != null) { configDigester.register(registrations[i], url.toString()); } }
this.addRuleSets();
// Return the completely configured Digester instance return (configDigester); }
/** * <p> * Add any custom RuleSet instances to configDigester that have been * specified in the <code>rulesets</code> init parameter. * </p> * * @throws ServletException */ private void addRuleSets() throws ServletException {
// If the config isn't in the servlet context, try the class loader // which allows the config files to be stored in a jar if (url == null) { url = getClass().getResource(path); }
if (url == null) { String msg = internal.getMessage("configMissing", path); log.error(msg); throw new UnavailableException(msg); }
InputSource is = new InputSource(url.toExternalForm()); input = url.openStream(); is.setByteStream(input); digester.parse(is);
} 四.另外還有幾個(gè)Digester用來(lái)解析web.xml和struts-config.xml文件的類(lèi) /* * Created on 2005-9-6 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package kick.load.resource;
public class WebApp { private List servlets = new ArrayList();
public void addServlet(Servlet servlet){ servlets.add(servlet); }
/** * @return Returns the servlets. */ public List getServlets() { return servlets; } /** * @param servlets The servlets to set. */ public void setServlets(List servlets) { this.servlets = servlets; } public void print(){ for(int i = 0;i<servlets.size();i++){ ((Servlet)servlets.get(i)).print(); } } }
/* * Created on 2005-9-6 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package kick.load.resource;