📄 actionservlet.java
字号:
digester.parse(is);
getServletContext().setAttribute(Globals.MODULE_KEY + prefix, config);
} catch (MalformedURLException e) {
handleConfigException(paths, e);
} catch (IOException e) {
handleConfigException(paths, e);
} catch (SAXException e) {
handleConfigException(paths, e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
throw new UnavailableException(e.getMessage());
}
}
}
}
/**
* Simplifies exception handling in the parseModuleConfigFile() method.
* @param paths
* @param e
* @throws UnavailableException
*/
private void handleConfigException(String paths, Exception e)
throws UnavailableException {
log.error(internal.getMessage("configParse", paths), e);
throw new UnavailableException(internal.getMessage("configParse", paths));
}
/**
* <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
* @deprecated use initModuleDataSources(ModuleConfig)
*/
protected void initApplicationDataSources(ModuleConfig config) throws ServletException {
initModuleDataSources(config);
}
/**
* <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 {
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());
if (ds instanceof GenericDataSource) {
((GenericDataSource) ds).open();
}
ds.setLogWriter(scw);
} catch (Exception e) {
log.error(internal.getMessage("dataSource.init", dscs[i].getKey()), e);
throw new UnavailableException
(internal.getMessage("dataSource.init", dscs[i].getKey()));
}
getServletContext().setAttribute
(dscs[i].getKey() + config.getPrefix(), ds);
dataSources.put(dscs[i].getKey(), ds);
}
dataSources.setFast(true);
// Call deprecated method for backwards compatibility
if ("".equals(config.getPrefix())) {
initDataSources();
}
}
/**
* <p>Initialize the plug ins for the specified module.</p>
*
* @param config ModuleConfig information for this module
*
* @exception ServletException if initialization cannot be performed
* @deprecated use {@link #initModulePlugIns(ModuleConfig)}
* @since Struts 1.1
*/
protected void initApplicationPlugIns
(ModuleConfig config) throws ServletException {
initModulePlugIns(config);
}
/**
* <p>Initialize the plug ins 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 initModulePlugIns
(ModuleConfig config) throws ServletException {
if (log.isDebugEnabled()) {
log.debug("Initializing module path '" + config.getPrefix() + "' plug ins");
}
PlugInConfig plugInConfigs[] = config.findPlugInConfigs();
PlugIn plugIns[] = new PlugIn[plugInConfigs.length];
getServletContext().setAttribute(Globals.PLUG_INS_KEY + config.getPrefix(), plugIns);
for (int i = 0; i < plugIns.length; i++) {
try {
plugIns[i] =
(PlugIn)RequestUtils.applicationInstance(plugInConfigs[i].getClassName());
BeanUtils.populate(plugIns[i], plugInConfigs[i].getProperties());
// Pass the current plugIn config object to the PlugIn.
// The property is set only if the plugin declares it.
// This plugin config object is needed by Tiles
try {
PropertyUtils.setProperty(
plugIns[i],
"currentPlugInConfigObject",
plugInConfigs[i]);
} catch (Exception e) {
// FIXME Whenever we fail silently, we must document a valid reason
// for doing so. Why should we fail silently if a property can't be set on
// the plugin?
}
plugIns[i].init(this, (ModuleConfig) config);
} catch (ServletException e) {
throw e;
} catch (Exception e) {
String errMsg =
internal.getMessage(
"plugIn.init",
plugInConfigs[i].getClassName());
log(errMsg, e);
throw new UnavailableException(errMsg);
}
}
}
/**
* <p>Initialize the application MessageResources for the specified
* module.</p>
*
* @param config ModuleConfig information for this module
*
* @exception ServletException if initialization cannot be performed
* @since Struts 1.1
* @deprecated use initModuleMessageResources()
*/
protected void initApplicationMessageResources(ModuleConfig config) throws ServletException {
initModuleMessageResources(config);
}
/**
* <p>Initialize the application MessageResources 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 initModuleMessageResources(ModuleConfig config)
throws ServletException {
MessageResourcesConfig mrcs[] = config.findMessageResourcesConfigs();
for (int i = 0; i < mrcs.length; i++) {
if ((mrcs[i].getFactory() == null)
|| (mrcs[i].getParameter() == null)) {
continue;
}
if (log.isDebugEnabled()) {
log.debug(
"Initializing module path '"
+ config.getPrefix()
+ "' message resources from '"
+ mrcs[i].getParameter()
+ "'");
}
String factory = mrcs[i].getFactory();
MessageResourcesFactory.setFactoryClass(factory);
MessageResourcesFactory factoryObject =
MessageResourcesFactory.createFactory();
MessageResources resources =
factoryObject.createResources(mrcs[i].getParameter());
resources.setReturnNull(mrcs[i].getNull());
getServletContext().setAttribute(
mrcs[i].getKey() + config.getPrefix(),
resources);
}
}
/**
* <p>Create (if needed) and return a new Digester instance that has been
* initialized to process Struts module configuraiton files and
* configure a corresponding ModuleConfig 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 {
// Do we have an existing instance?
if (configDigester != null) {
return (configDigester);
}
// Check the status of the "validating" initialization parameter
boolean validating = true;
String value = getServletConfig().getInitParameter("validating");
if ("false".equalsIgnoreCase(value)
|| "no".equalsIgnoreCase(value)
|| "n".equalsIgnoreCase(value)
|| "0".equalsIgnoreCase(value)) {
validating = false;
}
// Create a new Digester instance with standard capabilities
configDigester = new Digester();
configDigester.setNamespaceAware(true);
configDigester.setValidating(validating);
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());
}
}
// Add any custom RuleSet instances that have been specified
String rulesets = getServletConfig().getInitParameter("rulesets");
if (rulesets == null) {
rulesets = "";
}
rulesets = rulesets.trim();
String ruleset = null;
while (rulesets.length() > 0) {
int comma = rulesets.indexOf(",");
if (comma < 0) {
ruleset = rulesets.trim();
rulesets = "";
} else {
ruleset = rulesets.substring(0, comma).trim();
rulesets = rulesets.substring(comma + 1).trim();
}
if (log.isDebugEnabled()) {
log.debug("Configuring custom Digester Ruleset of type " + ruleset);
}
try {
RuleSet instance = (RuleSet) RequestUtils.applicationInstance(ruleset);
configDigester.addRuleSet(instance);
} catch (Exception e) {
log.error("Exception configuring custom Digester RuleSet", e);
throw new ServletException(e);
}
}
// Return the completely configured Digester instance
return (configDigester);
}
/**
* Initialize data sources for the default module. This method
* signature is maintained only for backwards compatibility, and will
* be removed in a subsequent release.
*
* @deprecated Replaced by initApplicationDataSources() that takes
* an ModuleConfig argument. This method does nothing.
*/
protected void initDataSources() throws javax.servlet.ServletException {
; // Implementation has been replaced in initApplicationDataSources()
}
/**
* Initialize our internal MessageResources bundle.
*
* @exception ServletException if we cannot initialize these resources
*/
protected void initInternal() throws ServletException {
try {
internal = MessageResources.getMessageResources(internalName);
} catch (MissingResourceException e) {
log.error("Cannot load internal resources from '" + internalName + "'",
e);
throw new UnavailableException
("Cannot load internal resources from '" + internalName + "'");
}
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -