版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a040600145/article/details/68944725
1.HostConfig在start中会调用deployApps方法扫描appBase目录下的apps,加载DocBase/META-INF/context.xml.
- protected void deployApps() {
- File appBase = appBase();
- File configBase = configBase();
- String[] filteredAppPaths = filterAppPaths(appBase.list());
- // Deploy XML descriptors from configBase
- deployDescriptors(configBase, configBase.list());
- // Deploy WARs
- deployWARs(appBase, filteredAppPaths);
- // Deploy expanded folders
- deployDirectories(appBase, filteredAppPaths);
- }
- protected void deployDescriptor(ContextName cn, File contextXml) {
- DeployedApplication deployedApp =
- new DeployedApplication(cn.getName(), true);
- long startTime = 0;
- // Assume this is a configuration descriptor and deploy it
- if(log.isInfoEnabled()) {
- startTime = System.currentTimeMillis();
- log.info(sm.getString("hostConfig.deployDescriptor",
- contextXml.getAbsolutePath()));
- }
- Context context = null;
- boolean isExternalWar = false;
- boolean isExternal = false;
- File expandedDocBase = null;
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(contextXml);
- synchronized (digesterLock) {
- try {
- context = (Context) digester.parse(fis);
- } catch (Exception e) {
- log.error(sm.getString(
- "hostConfig.deployDescriptor.error",
- contextXml.getAbsolutePath()), e);
- context = new FailedContext();
- } finally {
- digester.reset();
- }
- }
- Class<?> clazz = Class.forName(host.getConfigClass());
- LifecycleListener listener =
- (LifecycleListener) clazz.newInstance();
- context.addLifecycleListener(listener);
- context.setConfigFile(contextXml.toURI().toURL());
- context.setName(cn.getName());
- context.setPath(cn.getPath());
- context.setWebappVersion(cn.getVersion());
- // Add the associated docBase to the redeployed list if it's a WAR
- if (context.getDocBase() != null) {
- File docBase = new File(context.getDocBase());
- if (!docBase.isAbsolute()) {
- docBase = new File(appBase(), context.getDocBase());
- }
- // If external docBase, register .xml as redeploy first
- if (!docBase.getCanonicalPath().startsWith(
- appBase().getAbsolutePath() + File.separator)) {
- isExternal = true;
- deployedApp.redeployResources.put(
- contextXml.getAbsolutePath(),
- Long.valueOf(contextXml.lastModified()));
- deployedApp.redeployResources.put(docBase.getAbsolutePath(),
- Long.valueOf(docBase.lastModified()));
- if (docBase.getAbsolutePath().toLowerCase(Locale.ENGLISH).endsWith(".war")) {
- isExternalWar = true;
- }
- } else {
- log.warn(sm.getString("hostConfig.deployDescriptor.localDocBaseSpecified",
- docBase));
- // Ignore specified docBase
- context.setDocBase(null);
- }
- }
- host.addChild(context);
- } catch (Throwable t) {
- ExceptionUtils.handleThrowable(t);
- log.error(sm.getString("hostConfig.deployDescriptor.error",
- contextXml.getAbsolutePath()), t);
- } finally {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- // Ignore
- }
- }
- // Get paths for WAR and expanded WAR in appBase
- // default to appBase dir + name
- expandedDocBase = new File(appBase(), cn.getBaseName());
- if (context.getDocBase() != null
- && !context.getDocBase().toLowerCase(Locale.ENGLISH).endsWith(".war")) {
- // first assume docBase is absolute
- expandedDocBase = new File(context.getDocBase());
- if (!expandedDocBase.isAbsolute()) {
- // if docBase specified and relative, it must be relative to appBase
- expandedDocBase = new File(appBase(), context.getDocBase());
- }
- }
- boolean unpackWAR = unpackWARs;
- if (unpackWAR && context instanceof StandardContext) {
- unpackWAR = ((StandardContext) context).getUnpackWAR();
- }
- // Add the eventual unpacked WAR and all the resources which will be
- // watched inside it
- if (isExternalWar) {
- if (unpackWAR) {
- deployedApp.redeployResources.put(expandedDocBase.getAbsolutePath(),
- Long.valueOf(expandedDocBase.lastModified()));
- addWatchedResources(deployedApp, expandedDocBase.getAbsolutePath(), context);
- } else {
- addWatchedResources(deployedApp, null, context);
- }
- } else {
- // Find an existing matching war and expanded folder
- if (!isExternal) {
- File warDocBase = new File(expandedDocBase.getAbsolutePath() + ".war");
- if (warDocBase.exists()) {
- deployedApp.redeployResources.put(warDocBase.getAbsolutePath(),
- Long.valueOf(warDocBase.lastModified()));
- } else {
- // Trigger a redeploy if a WAR is added
- deployedApp.redeployResources.put(
- warDocBase.getAbsolutePath(),
- Long.valueOf(0));
- }
- }
- if (unpackWAR) {
- deployedApp.redeployResources.put(expandedDocBase.getAbsolutePath(),
- Long.valueOf(expandedDocBase.lastModified()));
- addWatchedResources(deployedApp,
- expandedDocBase.getAbsolutePath(), context);
- } else {
- addWatchedResources(deployedApp, null, context);
- }
- if (!isExternal) {
- // For external docBases, the context.xml will have been
- // added above.
- deployedApp.redeployResources.put(
- contextXml.getAbsolutePath(),
- Long.valueOf(contextXml.lastModified()));
- }
- }
- // Add the global redeploy resources (which are never deleted) at
- // the end so they don't interfere with the deletion process
- addGlobalRedeployResources(deployedApp);
- }
- if (host.findChild(context.getName()) != null) {
- deployed.put(context.getName(), deployedApp);
- }
- if (log.isInfoEnabled()) {
- log.info(sm.getString("hostConfig.deployDescriptor.finished",
- contextXml.getAbsolutePath(), Long.valueOf(System.currentTimeMillis() - startTime)));
- }
- }
- protected void deployWAR(ContextName cn, File war) {
- // Checking for a nested /META-INF/context.xml
- JarFile jar = null;
- InputStream istream = null;
- FileOutputStream fos = null;
- BufferedOutputStream ostream = null;
- File xml = new File(appBase(),
- cn.getBaseName() + "/META-INF/context.xml");
- boolean xmlInWar = false;
- try {
- jar = new JarFile(war);
- JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
- if (entry != null) {
- xmlInWar = true;
- }
- } catch (IOException e) {
- /* Ignore */
- } finally {
- if (jar != null) {
- try {
- jar.close();
- } catch (IOException ioe) {
- // Ignore;
- }
- jar = null;
- }
- }
- Context context = null;
- try {
- if (deployXML && xml.exists() && unpackWARs && !copyXML) {
- synchronized (digesterLock) {
- try {
- context = (Context) digester.parse(xml);
- } catch (Exception e) {
- log.error(sm.getString(
- "hostConfig.deployDescriptor.error",
- war.getAbsolutePath()), e);
- } finally {
- digester.reset();
- if (context == null) {
- context = new FailedContext();
- }
- }
- }
- context.setConfigFile(xml.toURI().toURL());
- } else if (deployXML && xmlInWar) {
- synchronized (digesterLock) {
- try {
- jar = new JarFile(war);
- JarEntry entry =
- jar.getJarEntry(Constants.ApplicationContextXml);
- istream = jar.getInputStream(entry);
- context = (Context) digester.parse(istream);
- } catch (Exception e) {
- log.error(sm.getString(
- "hostConfig.deployDescriptor.error",
- war.getAbsolutePath()), e);
- } finally {
- digester.reset();
- if (istream != null) {
- try {
- istream.close();
- } catch (IOException e) {
- /* Ignore */
- }
- istream = null;
- }
- if (jar != null) {
- try {
- jar.close();
- } catch (IOException e) {
- /* Ignore */
- }
- jar = null;
- }
- if (context == null) {
- context = new FailedContext();
- }
- context.setConfigFile(
- UriUtil.buildJarUrl(war, Constants.ApplicationContextXml));
- }
- }
- } else if (!deployXML && xmlInWar) {
- // Block deployment as META-INF/context.xml may contain security
- // configuration necessary for a secure deployment.
- log.error(sm.getString("hostConfig.deployDescriptor.blocked",
- cn.getPath(), Constants.ApplicationContextXml,
- new File(configBase(), cn.getBaseName() + ".xml")));
- } else {
- context = (Context) Class.forName(contextClass).newInstance();
- }
- } catch (Throwable t) {
- ExceptionUtils.handleThrowable(t);
- log.error(sm.getString("hostConfig.deployWar.error",
- war.getAbsolutePath()), t);
- } finally {
- if (context == null) {
- context = new FailedContext();
- }
- }
- boolean copyThisXml = false;
- if (deployXML) {
- if (host instanceof StandardHost) {
- copyThisXml = ((StandardHost) host).isCopyXML();
- }
- // If Host is using default value Context can override it.
- if (!copyThisXml && context instanceof StandardContext) {
- copyThisXml = ((StandardContext) context).getCopyXML();
- }
- if (xmlInWar && copyThisXml) {
- // Change location of XML file to config base
- xml = new File(configBase(), cn.getBaseName() + ".xml");
- try {
- jar = new JarFile(war);
- JarEntry entry =
- jar.getJarEntry(Constants.ApplicationContextXml);
- istream = jar.getInputStream(entry);
- fos = new FileOutputStream(xml);
- ostream = new BufferedOutputStream(fos, 1024);
- byte buffer[] = new byte[1024];
- while (true) {
- int n = istream.read(buffer);
- if (n < 0) {
- break;
- }
- ostream.write(buffer, 0, n);
- }
- ostream.flush();
- } catch (IOException e) {
- /* Ignore */
- } finally {
- if (ostream != null) {
- try {
- ostream.close();
- } catch (IOException ioe) {
- // Ignore
- }
- ostream = null;
- }
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException ioe) {
- // Ignore
- }
- fos = null;
- }
- if (istream != null) {
- try {
- istream.close();
- } catch (IOException ioe) {
- // Ignore
- }
- istream = null;
- }
- if (jar != null) {
- try {
- jar.close();
- } catch (IOException ioe) {
- // Ignore;
- }
- jar = null;
- }
- }
- }
- }
- DeployedApplication deployedApp = new DeployedApplication(cn.getName(),
- xml.exists() && deployXML && copyThisXml);
- long startTime = 0;
- // Deploy the application in this WAR file
- if(log.isInfoEnabled()) {
- startTime = System.currentTimeMillis();
- log.info(sm.getString("hostConfig.deployWar",
- war.getAbsolutePath()));
- }
- try {
- // Populate redeploy resources with the WAR file
- deployedApp.redeployResources.put
- (war.getAbsolutePath(), Long.valueOf(war.lastModified()));
- if (deployXML && xml.exists() && copyThisXml) {
- deployedApp.redeployResources.put(xml.getAbsolutePath(),
- Long.valueOf(xml.lastModified()));
- } else {
- // In case an XML file is added to the config base later
- deployedApp.redeployResources.put(
- (new File(configBase(),
- cn.getBaseName() + ".xml")).getAbsolutePath(),
- Long.valueOf(0));
- }
- Class<?> clazz = Class.forName(host.getConfigClass());
- LifecycleListener listener =
- (LifecycleListener) clazz.newInstance();
- context.addLifecycleListener(listener);
- context.setName(cn.getName());
- context.setPath(cn.getPath());
- context.setWebappVersion(cn.getVersion());
- context.setDocBase(cn.getBaseName() + ".war");
- host.addChild(context);
- } catch (Throwable t) {
- ExceptionUtils.handleThrowable(t);
- log.error(sm.getString("hostConfig.deployWar.error",
- war.getAbsolutePath()), t);
- } finally {
- // If we're unpacking WARs, the docBase will be mutated after
- // starting the context
- boolean unpackWAR = unpackWARs;
- if (unpackWAR && context instanceof StandardContext) {
- unpackWAR = ((StandardContext) context).getUnpackWAR();
- }
- if (unpackWAR && context.getDocBase() != null) {
- File docBase = new File(appBase(), cn.getBaseName());
- deployedApp.redeployResources.put(docBase.getAbsolutePath(),
- Long.valueOf(docBase.lastModified()));
- addWatchedResources(deployedApp, docBase.getAbsolutePath(),
- context);
- if (deployXML && !copyThisXml && (xmlInWar || xml.exists())) {
- deployedApp.redeployResources.put(xml.getAbsolutePath(),
- Long.valueOf(xml.lastModified()));
- }
- } else {
- // Passing null for docBase means that no resources will be
- // watched. This will be logged at debug level.
- addWatchedResources(deployedApp, null, context);
- }
- // Add the global redeploy resources (which are never deleted) at
- // the end so they don't interfere with the deletion process
- addGlobalRedeployResources(deployedApp);
- }
- deployed.put(cn.getName(), deployedApp);
- if (log.isInfoEnabled()) {
- log.info(sm.getString("hostConfig.deployWar.finished",
- war.getAbsolutePath(), Long.valueOf(System.currentTimeMillis() - startTime)));
- }
- }
- protected void deployDescriptor(ContextName cn, File contextXml) {
- DeployedApplication deployedApp =
- new DeployedApplication(cn.getName(), true);
- long startTime = 0;
- // Assume this is a configuration descriptor and deploy it
- if(log.isInfoEnabled()) {
- startTime = System.currentTimeMillis();
- log.info(sm.getString("hostConfig.deployDescriptor",
- contextXml.getAbsolutePath()));
- }
- Context context = null;
- boolean isExternalWar = false;
- boolean isExternal = false;
- File expandedDocBase = null;
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(contextXml);
- synchronized (digesterLock) {
- try {
- context = (Context) digester.parse(fis);
- } catch (Exception e) {
- log.error(sm.getString(
- "hostConfig.deployDescriptor.error",
- contextXml.getAbsolutePath()), e);
- context = new FailedContext();
- } finally {
- digester.reset();
- }
- }
- Class<?> clazz = Class.forName(host.getConfigClass());
- LifecycleListener listener =
- (LifecycleListener) clazz.newInstance();
- context.addLifecycleListener(listener);
- context.setConfigFile(contextXml.toURI().toURL());
- context.setName(cn.getName());
- context.setPath(cn.getPath());
- context.setWebappVersion(cn.getVersion());
- // Add the associated docBase to the redeployed list if it's a WAR
- if (context.getDocBase() != null) {
- File docBase = new File(context.getDocBase());
- if (!docBase.isAbsolute()) {
- docBase = new File(appBase(), context.getDocBase());
- }
- // If external docBase, register .xml as redeploy first
- if (!docBase.getCanonicalPath().startsWith(
- appBase().getAbsolutePath() + File.separator)) {
- isExternal = true;
- deployedApp.redeployResources.put(
- contextXml.getAbsolutePath(),
- Long.valueOf(contextXml.lastModified()));
- deployedApp.redeployResources.put(docBase.getAbsolutePath(),
- Long.valueOf(docBase.lastModified()));
- if (docBase.getAbsolutePath().toLowerCase(Locale.ENGLISH).endsWith(".war")) {
- isExternalWar = true;
- }
- } else {
- log.warn(sm.getString("hostConfig.deployDescriptor.localDocBaseSpecified",
- docBase));
- // Ignore specified docBase
- context.setDocBase(null);
- }
- }
- host.addChild(context);
- } catch (Throwable t) {
- ExceptionUtils.handleThrowable(t);
- log.error(sm.getString("hostConfig.deployDescriptor.error",
- contextXml.getAbsolutePath()), t);
- } finally {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- // Ignore
- }
- }
- // Get paths for WAR and expanded WAR in appBase
- // default to appBase dir + name
- expandedDocBase = new File(appBase(), cn.getBaseName());
- if (context.getDocBase() != null
- && !context.getDocBase().toLowerCase(Locale.ENGLISH).endsWith(".war")) {
- // first assume docBase is absolute
- expandedDocBase = new File(context.getDocBase());
- if (!expandedDocBase.isAbsolute()) {
- // if docBase specified and relative, it must be relative to appBase
- expandedDocBase = new File(appBase(), context.getDocBase());
- }
- }
- boolean unpackWAR = unpackWARs;
- if (unpackWAR && context instanceof StandardContext) {
- unpackWAR = ((StandardContext) context).getUnpackWAR();
- }
- // Add the eventual unpacked WAR and all the resources which will be
- // watched inside it
- if (isExternalWar) {
- if (unpackWAR) {
- deployedApp.redeployResources.put(expandedDocBase.getAbsolutePath(),
- Long.valueOf(expandedDocBase.lastModified()));
- addWatchedResources(deployedApp, expandedDocBase.getAbsolutePath(), context);
- } else {
- addWatchedResources(deployedApp, null, context);
- }
- } else {
- // Find an existing matching war and expanded folder
- if (!isExternal) {
- File warDocBase = new File(expandedDocBase.getAbsolutePath() + ".war");
- if (warDocBase.exists()) {
- deployedApp.redeployResources.put(warDocBase.getAbsolutePath(),
- Long.valueOf(warDocBase.lastModified()));
- } else {
- // Trigger a redeploy if a WAR is added
- deployedApp.redeployResources.put(
- warDocBase.getAbsolutePath(),
- Long.valueOf(0));
- }
- }
- if (unpackWAR) {
- deployedApp.redeployResources.put(expandedDocBase.getAbsolutePath(),
- Long.valueOf(expandedDocBase.lastModified()));
- addWatchedResources(deployedApp,
- expandedDocBase.getAbsolutePath(), context);
- } else {
- addWatchedResources(deployedApp, null, context);
- }
- if (!isExternal) {
- // For external docBases, the context.xml will have been
- // added above.
- deployedApp.redeployResources.put(
- contextXml.getAbsolutePath(),
- Long.valueOf(contextXml.lastModified()));
- }
- }
- // Add the global redeploy resources (which are never deleted) at
- // the end so they don't interfere with the deletion process
- addGlobalRedeployResources(deployedApp);
- }
- if (host.findChild(context.getName()) != null) {
- deployed.put(context.getName(), deployedApp);
- }
- if (log.isInfoEnabled()) {
- log.info(sm.getString("hostConfig.deployDescriptor.finished",
- contextXml.getAbsolutePath(), Long.valueOf(System.currentTimeMillis() - startTime)));
- }
- }
2.ContextConfig初始化时Override为否时时会依次加载conf/context.xml,HostConfig/Basecontext.xml.default,configFile配置文件。
- protected void init() {
- // Called from StandardContext.init()
- Digester contextDigester = createContextDigester();
- contextDigester.getParser();
- if (log.isDebugEnabled())
- log.debug(sm.getString("contextConfig.init"));
- context.setConfigured(false);
- ok = true;
- contextConfig(contextDigester);
- createWebXmlDigester(context.getXmlNamespaceAware(), context.getXmlValidation());
- }
- protected void contextConfig(Digester digester) {
- // Open the default context.xml file, if it exists
- if (defaultContextXml == null && context instanceof StandardContext) {
- defaultContextXml = ((StandardContext) context).getDefaultContextXml();
- }
- // set the default if we don't have any overrides
- if (defaultContextXml == null)
- getDefaultContextXml();
- if (!context.getOverride()) {
- File defaultContextFile = new File(defaultContextXml);
- if (!defaultContextFile.isAbsolute()) {
- defaultContextFile = new File(getBaseDir(), defaultContextXml);
- }
- if (defaultContextFile.exists()) {
- try {
- URL defaultContextUrl = defaultContextFile.toURI().toURL();
- processContextConfig(digester, defaultContextUrl);
- } catch (MalformedURLException e) {
- log.error(sm.getString("contextConfig.badUrl", defaultContextFile), e);
- }
- }
- File hostContextFile = new File(getHostConfigBase(), Constants.HostContextXml);
- if (hostContextFile.exists()) {
- try {
- URL hostContextUrl = hostContextFile.toURI().toURL();
- processContextConfig(digester, hostContextUrl);
- } catch (MalformedURLException e) {
- log.error(sm.getString("contextConfig.badUrl", hostContextFile), e);
- }
- }
- }
- if (context.getConfigFile() != null)
- processContextConfig(digester, context.getConfigFile());
- }
3.war包的解压
context在start时会触发Lifecycle.BEFORE_START_EVENT,ContextConfig调用fixDocBase解压war包
- protected void fixDocBase() throws IOException {
- Host host = (Host) context.getParent();
- String appBase = host.getAppBase();
- File canonicalAppBase = new File(appBase);
- if (canonicalAppBase.isAbsolute()) {
- canonicalAppBase = canonicalAppBase.getCanonicalFile();
- } else {
- canonicalAppBase = new File(getBaseDir(), appBase).getCanonicalFile();
- }
- String docBase = context.getDocBase();
- if (docBase == null) {
- // Trying to guess the docBase according to the path
- String path = context.getPath();
- if (path == null) {
- return;
- }
- ContextName cn = new ContextName(path, context.getWebappVersion());
- docBase = cn.getBaseName();
- }
- File file = new File(docBase);
- if (!file.isAbsolute()) {
- docBase = (new File(canonicalAppBase, docBase)).getPath();
- } else {
- docBase = file.getCanonicalPath();
- }
- file = new File(docBase);
- String origDocBase = docBase;
- ContextName cn = new ContextName(context.getPath(), context.getWebappVersion());
- String pathName = cn.getBaseName();
- boolean unpackWARs = true;
- if (host instanceof StandardHost) {
- unpackWARs = ((StandardHost) host).isUnpackWARs();
- if (unpackWARs && context instanceof StandardContext) {
- unpackWARs = ((StandardContext) context).getUnpackWAR();
- }
- }
- if (docBase.toLowerCase(Locale.ENGLISH).endsWith(".war") && !file.isDirectory()) {
- URL war = UriUtil.buildJarUrl(new File(docBase));
- if (unpackWARs) {
- docBase = ExpandWar.expand(host, war, pathName);
- file = new File(docBase);
- docBase = file.getCanonicalPath();
- if (context instanceof StandardContext) {
- ((StandardContext) context).setOriginalDocBase(origDocBase);
- }
- } else {
- ExpandWar.validate(host, war, pathName);
- }
- } else {
- File docDir = new File(docBase);
- if (!docDir.exists()) {
- File warFile = new File(docBase + ".war");
- if (warFile.exists()) {
- URL war = UriUtil.buildJarUrl(warFile);
- if (unpackWARs) {
- docBase = ExpandWar.expand(host, war, pathName);
- file = new File(docBase);
- docBase = file.getCanonicalPath();
- } else {
- docBase = warFile.getCanonicalPath();
- ExpandWar.validate(host, war, pathName);
- }
- }
- if (context instanceof StandardContext) {
- ((StandardContext) context).setOriginalDocBase(origDocBase);
- }
- }
- }
- if (docBase.startsWith(canonicalAppBase.getPath() + File.separatorChar)) {
- docBase = docBase.substring(canonicalAppBase.getPath().length());
- docBase = docBase.replace(File.separatorChar, '/');
- if (docBase.startsWith("/")) {
- docBase = docBase.substring(1);
- }
- } else {
- docBase = docBase.replace(File.separatorChar, '/');
- }
- context.setDocBase(docBase);
- }