📄 hostconfig.java
字号:
/**
* Process the START event for an associated Host.
*
* @param event The lifecycle event that has occurred
*/
public void lifecycleEvent(LifecycleEvent event) {
if (event.getType().equals("check"))
check();
// Identify the host we are associated with
try {
host = (Host) event.getLifecycle();
if (host instanceof StandardHost) {
int hostDebug = ((StandardHost) host).getDebug();
if (hostDebug > this.debug) {
this.debug = hostDebug;
}
setDeployXML(((StandardHost) host).isDeployXML());
setUnpackWARs(((StandardHost) host).isUnpackWARs());
setXmlNamespaceAware(((StandardHost) host).getXmlNamespaceAware());
setXmlValidation(((StandardHost) host).getXmlValidation());
}
} catch (ClassCastException e) {
log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e);
return;
}
// Process the event that has occurred
if (event.getType().equals(Lifecycle.START_EVENT))
start();
else if (event.getType().equals(Lifecycle.STOP_EVENT))
stop();
}
// ------------------------------------------------------ Protected Methods
/**
* Return a File object representing the "application root" directory
* for our associated Host.
*/
protected File appBase() {
if (appBase != null) {
return appBase;
}
File file = new File(host.getAppBase());
if (!file.isAbsolute())
file = new File(System.getProperty("catalina.base"),
host.getAppBase());
try {
appBase = file.getCanonicalFile();
} catch (IOException e) {
appBase = file;
}
return (appBase);
}
/**
* Return a File object representing the "configuration root" directory
* for our associated Host.
*/
protected File configBase() {
if (configBase != null) {
return configBase;
}
File file = new File(System.getProperty("catalina.base"), "conf");
Container parent = host.getParent();
if ((parent != null) && (parent instanceof Engine)) {
file = new File(file, parent.getName());
}
file = new File(file, host.getName());
try {
configBase = file.getCanonicalFile();
} catch (IOException e) {
configBase = file;
}
return (configBase);
}
/**
* Deploy applications for any directories or WAR files that are found
* in our "application root" directory.
*/
protected void deployApps() {
if (!(host instanceof Deployer))
return;
// Initialize the deployer
((Deployer) host).findDeployedApps();
File appBase = appBase();
if (!appBase.exists() || !appBase.isDirectory())
return;
File configBase = configBase();
if (configBase.exists() && configBase.isDirectory()) {
String configFiles[] = configBase.list();
deployDescriptors(configBase, configFiles);
}
String files[] = appBase.list();
deployWARs(appBase, files);
deployDirectories(appBase, files);
}
/**
* Deploy XML context descriptors.
*/
protected void deployDescriptors(File configBase, String[] files) {
if (!deployXML)
return;
for (int i = 0; i < files.length; i++) {
if (files[i].equalsIgnoreCase("META-INF"))
continue;
if (files[i].equalsIgnoreCase("WEB-INF"))
continue;
if (deployed.contains(files[i]))
continue;
File dir = new File(configBase, files[i]);
if (files[i].toLowerCase().endsWith(".xml")) {
deployed.add(files[i]);
// Calculate the context path and make sure it is unique
String file = files[i].substring(0, files[i].length() - 4);
String contextPath = "/" + file.replace('#', '/');
if (file.equals("ROOT")) {
contextPath = "";
}
// Assume this is a configuration descriptor and deploy it
log.debug(sm.getString("hostConfig.deployDescriptor", files[i]));
try {
if (host.findChild(contextPath) != null) {
if ((deployed.contains(file))
|| (deployed.contains(file + ".war"))) {
// If this is a newly added context file and
// it overrides a context with a simple path,
// that was previously deployed by the auto
// deployer, undeploy the context
((Deployer) host).remove(contextPath);
} else {
continue;
}
}
URL config =
new URL("file", null, dir.getCanonicalPath());
((Deployer) host).install(config, null);
} catch (Throwable t) {
log.error(sm.getString("hostConfig.deployDescriptor.error",
files[i]), t);
}
}
}
}
/**
* Deploy WAR files.
*/
protected void deployWARs(File appBase, String[] files) {
for (int i = 0; i < files.length; i++) {
if (files[i].equalsIgnoreCase("META-INF"))
continue;
if (files[i].equalsIgnoreCase("WEB-INF"))
continue;
if (deployed.contains(files[i]))
continue;
File dir = new File(appBase, files[i]);
if (files[i].toLowerCase().endsWith(".war")) {
deployed.add(files[i]);
// Calculate the context path and make sure it is unique
String contextPath = "/" + files[i];
int period = contextPath.lastIndexOf(".");
if (period >= 0)
contextPath = contextPath.substring(0, period);
if (contextPath.equals("/ROOT"))
contextPath = "";
if (host.findChild(contextPath) != null)
continue;
// Checking for a nested /META-INF/context.xml
JarFile jar = null;
JarEntry entry = null;
InputStream istream = null;
BufferedOutputStream ostream = null;
File xml = new File
(configBase, files[i].substring
(0, files[i].lastIndexOf(".")) + ".xml");
if (!xml.exists()) {
try {
jar = new JarFile(dir);
entry = jar.getJarEntry("META-INF/context.xml");
if (entry != null) {
istream = jar.getInputStream(entry);
ostream =
new BufferedOutputStream
(new FileOutputStream(xml), 1024);
byte buffer[] = new byte[1024];
while (true) {
int n = istream.read(buffer);
if (n < 0) {
break;
}
ostream.write(buffer, 0, n);
}
ostream.flush();
ostream.close();
ostream = null;
istream.close();
istream = null;
entry = null;
jar.close();
jar = null;
deployDescriptors(configBase(), configBase.list());
return;
}
} catch (Exception e) {
// Ignore and continue
if (ostream != null) {
try {
ostream.close();
} catch (Throwable t) {
;
}
ostream = null;
}
if (istream != null) {
try {
istream.close();
} catch (Throwable t) {
;
}
istream = null;
}
entry = null;
if (jar != null) {
try {
jar.close();
} catch (Throwable t) {
;
}
jar = null;
}
}
}
if (isUnpackWARs()) {
// Expand and deploy this application as a directory
log.debug(sm.getString("hostConfig.expand", files[i]));
URL url = null;
String path = null;
try {
url = new URL("jar:file:" +
dir.getCanonicalPath() + "!/");
path = ExpandWar.expand(host, url);
} catch (IOException e) {
// JAR decompression failure
log.warn(sm.getString
("hostConfig.expand.error", files[i]));
continue;
} catch (Throwable t) {
log.error(sm.getString
("hostConfig.expand.error", files[i]), t);
continue;
}
try {
if (path != null) {
url = new URL("file:" + path);
((Deployer) host).install(contextPath, url);
}
} catch (Throwable t) {
log.error(sm.getString
("hostConfig.expand.error", files[i]), t);
}
} else {
// Deploy the application in this WAR file
log.info(sm.getString("hostConfig.deployJar", files[i]));
try {
URL url = new URL("file", null,
dir.getCanonicalPath());
url = new URL("jar:" + url.toString() + "!/");
((Deployer) host).install(contextPath, url);
} catch (Throwable t) {
log.error(sm.getString("hostConfig.deployJar.error",
files[i]), t);
}
}
}
}
}
/**
* Deploy directories.
*/
protected void deployDirectories(File appBase, String[] files) {
for (int i = 0; i < files.length; i++) {
if (files[i].equalsIgnoreCase("META-INF"))
continue;
if (files[i].equalsIgnoreCase("WEB-INF"))
continue;
if (deployed.contains(files[i]))
continue;
File dir = new File(appBase, files[i]);
if (dir.isDirectory()) {
deployed.add(files[i]);
// Make sure there is an application configuration directory
// This is needed if the Context appBase is the same as the
// web server document root to make sure only web applications
// are deployed and not directories for web space.
File webInf = new File(dir, "/WEB-INF");
if (!webInf.exists() || !webInf.isDirectory() ||
!webInf.canRead())
continue;
// Calculate the context path and make sure it is unique
String contextPath = "/" + files[i];
if (files[i].equals("ROOT"))
contextPath = "";
if (host.findChild(contextPath) != null)
continue;
// Deploy the application in this directory
if( log.isDebugEnabled() )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -