📄 contextconfig.java
字号:
// Process the application web.xml file
synchronized (webDigester) {
try {
if (altDDName != null) {
url = new File(altDDName).toURL();
} else {
url = servletContext.getResource(
Constants.ApplicationWebXml);
}
if( url!=null ) {
InputSource is = new InputSource(url.toExternalForm());
is.setByteStream(stream);
if (context instanceof StandardContext) {
((StandardContext) context).setReplaceWelcomeFiles(true);
}
webDigester.push(context);
webDigester.setErrorHandler(new ContextErrorHandler());
if(log.isDebugEnabled()) {
log.debug("Parsing application web.xml file at " + url.toExternalForm());
}
webDigester.parse(is);
if (parseException != null) {
ok = false;
}
} else {
log.info("No web.xml, using defaults " + context );
}
} catch (SAXParseException e) {
log.error(sm.getString("contextConfig.applicationParse", url.toExternalForm()), e);
log.error(sm.getString("contextConfig.applicationPosition",
"" + e.getLineNumber(),
"" + e.getColumnNumber()));
ok = false;
} catch (Exception e) {
log.error(sm.getString("contextConfig.applicationParse", url.toExternalForm()), e);
ok = false;
} finally {
webDigester.reset();
parseException = null;
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
log.error(sm.getString("contextConfig.applicationClose"), e);
}
}
}
webRuleSet.recycle();
long t2=System.currentTimeMillis();
if (context instanceof StandardContext) {
((StandardContext) context).setStartupTime(t2-t1);
}
}
/**
* Set up an Authenticator automatically if required, and one has not
* already been configured.
*/
protected synchronized void authenticatorConfig() {
// Does this Context require an Authenticator?
SecurityConstraint constraints[] = context.findConstraints();
if ((constraints == null) || (constraints.length == 0))
return;
LoginConfig loginConfig = context.getLoginConfig();
if (loginConfig == null) {
loginConfig = DUMMY_LOGIN_CONFIG;
context.setLoginConfig(loginConfig);
}
// Has an authenticator been configured already?
if (context instanceof Authenticator)
return;
if (context instanceof ContainerBase) {
Pipeline pipeline = ((ContainerBase) context).getPipeline();
if (pipeline != null) {
Valve basic = pipeline.getBasic();
if ((basic != null) && (basic instanceof Authenticator))
return;
Valve valves[] = pipeline.getValves();
for (int i = 0; i < valves.length; i++) {
if (valves[i] instanceof Authenticator)
return;
}
}
} else {
return; // Cannot install a Valve even if it would be needed
}
// Has a Realm been configured for us to authenticate against?
if (context.getRealm() == null) {
log.error(sm.getString("contextConfig.missingRealm"));
ok = false;
return;
}
/*
* First check to see if there is a custom mapping for the login
* method. If so, use it. Otherwise, check if there is a mapping in
* org/apache/catalina/startup/Authenticators.properties.
*/
Valve authenticator = null;
if (customAuthenticators != null) {
authenticator = (Valve)
customAuthenticators.get(loginConfig.getAuthMethod());
}
if (authenticator == null) {
// Load our mapping properties if necessary
if (authenticators == null) {
try {
InputStream is=this.getClass().getClassLoader().getResourceAsStream("org/apache/catalina/startup/Authenticators.properties");
if( is!=null ) {
authenticators = new Properties();
authenticators.load(is);
} else {
log.error(sm.getString(
"contextConfig.authenticatorResources"));
ok=false;
return;
}
} catch (IOException e) {
log.error(sm.getString(
"contextConfig.authenticatorResources"), e);
ok = false;
return;
}
}
// Identify the class name of the Valve we should configure
String authenticatorName = null;
authenticatorName =
authenticators.getProperty(loginConfig.getAuthMethod());
if (authenticatorName == null) {
log.error(sm.getString("contextConfig.authenticatorMissing",
loginConfig.getAuthMethod()));
ok = false;
return;
}
// Instantiate and install an Authenticator of the requested class
try {
Class authenticatorClass = Class.forName(authenticatorName);
authenticator = (Valve) authenticatorClass.newInstance();
} catch (Throwable t) {
log.error(sm.getString(
"contextConfig.authenticatorInstantiate",
authenticatorName),
t);
ok = false;
}
}
if (authenticator != null && context instanceof ContainerBase) {
Pipeline pipeline = ((ContainerBase) context).getPipeline();
if (pipeline != null) {
((ContainerBase) context).addValve(authenticator);
if (log.isDebugEnabled()) {
log.debug(sm.getString(
"contextConfig.authenticatorConfigured",
loginConfig.getAuthMethod()));
}
}
}
}
/**
* Create (if necessary) and return a Digester configured to process the
* web application deployment descriptor (web.xml).
*/
protected static Digester createWebDigester() {
Digester webDigester =
createWebXmlDigester(xmlNamespaceAware, xmlValidation);
return webDigester;
}
/**
* Create (if necessary) and return a Digester configured to process the
* web application deployment descriptor (web.xml).
*/
public static Digester createWebXmlDigester(boolean namespaceAware,
boolean validation) {
Digester webDigester = DigesterFactory.newDigester(xmlValidation,
xmlNamespaceAware,
webRuleSet);
return webDigester;
}
/**
* Create (if necessary) and return a Digester configured to process the
* context configuration descriptor for an application.
*/
protected Digester createContextDigester() {
Digester digester = new Digester();
digester.setValidating(false);
RuleSet contextRuleSet = new ContextRuleSet("", false);
digester.addRuleSet(contextRuleSet);
RuleSet namingRuleSet = new NamingRuleSet("Context/");
digester.addRuleSet(namingRuleSet);
return digester;
}
protected String getBaseDir() {
Container engineC=context.getParent().getParent();
if( engineC instanceof StandardEngine ) {
return ((StandardEngine)engineC).getBaseDir();
}
return System.getProperty("catalina.base");
}
/**
* Process the default configuration file, if it exists.
* The default config must be read with the container loader - so
* container servlets can be loaded
*/
protected void defaultWebConfig() {
long t1=System.currentTimeMillis();
// Open the default web.xml file, if it exists
if( defaultWebXml==null && context instanceof StandardContext ) {
defaultWebXml=((StandardContext)context).getDefaultWebXml();
}
// set the default if we don't have any overrides
if( defaultWebXml==null ) getDefaultWebXml();
File file = new File(this.defaultWebXml);
if (!file.isAbsolute()) {
file = new File(getBaseDir(),
this.defaultWebXml);
}
InputStream stream = null;
InputSource source = null;
try {
if ( ! file.exists() ) {
// Use getResource and getResourceAsStream
stream = getClass().getClassLoader()
.getResourceAsStream(defaultWebXml);
if( stream != null ) {
source = new InputSource
(getClass().getClassLoader()
.getResource(defaultWebXml).toString());
}
if( stream== null ) {
// maybe embedded
stream = getClass().getClassLoader()
.getResourceAsStream("web-embed.xml");
if( stream != null ) {
source = new InputSource
(getClass().getClassLoader()
.getResource("web-embed.xml").toString());
}
}
if( stream== null ) {
log.info("No default web.xml");
}
} else {
source =
new InputSource("file://" + file.getAbsolutePath());
stream = new FileInputStream(file);
context.addWatchedResource(file.getAbsolutePath());
}
} catch (Exception e) {
log.error(sm.getString("contextConfig.defaultMissing")
+ " " + defaultWebXml + " " + file , e);
}
if (webDigester == null){
webDigester = createWebDigester();
}
if (stream != null) {
processDefaultWebConfig(webDigester, stream, source);
webRuleSet.recycle();
}
long t2=System.currentTimeMillis();
if( (t2-t1) > 200 )
log.debug("Processed default web.xml " + file + " " + ( t2-t1));
stream = null;
source = null;
String resourceName = getHostConfigPath(Constants.HostWebXml);
file = new File(getConfigBase(), resourceName);
try {
if ( ! file.exists() ) {
// Use getResource and getResourceAsStream
stream = getClass().getClassLoader()
.getResourceAsStream(resourceName);
if( stream != null ) {
source = new InputSource
(getClass().getClassLoader()
.getResource(resourceName).toString());
}
} else {
source =
new InputSource("file://" + file.getAbsolutePath());
stream = new FileInputStream(file);
}
} catch (Exception e) {
log.error(sm.getString("contextConfig.defaultMissing")
+ " " + resourceName + " " + file , e);
}
if (stream != null) {
processDefaultWebConfig(webDigester, stream, source);
webRuleSet.recycle();
}
}
/**
* Process a default web.xml.
*/
protected void processDefaultWebConfig(Digester digester, InputStream stream,
InputSource source) {
if (log.isDebugEnabled())
log.debug("Processing context [" + context.getName()
+ "] web configuration resource " + source.getSystemId());
// Process the default web.xml file
synchronized (digester) {
try {
source.setByteStream(stream);
if (context instanceof StandardContext)
((StandardContext) context).setReplaceWelcomeFiles(true);
digester.setClassLoader(this.getClass().getClassLoader());
digester.setUseContextClassLoader(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -