webappcontainer.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,204 行 · 第 1/2 页

JAVA
1,204
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.server.webapp;import com.caucho.config.ConfigException;import com.caucho.lifecycle.Lifecycle;import com.caucho.loader.ClassLoaderListener;import com.caucho.loader.DynamicClassLoader;import com.caucho.loader.Environment;import com.caucho.loader.EnvironmentClassLoader;import com.caucho.loader.EnvironmentListener;import com.caucho.log.Log;import com.caucho.make.AlwaysModified;import com.caucho.server.cluster.Server;import com.caucho.server.deploy.DeployContainer;import com.caucho.server.deploy.DeployGenerator;import com.caucho.server.dispatch.DispatchBuilder;import com.caucho.server.dispatch.DispatchServer;import com.caucho.server.dispatch.ErrorFilterChain;import com.caucho.server.dispatch.ExceptionFilterChain;import com.caucho.server.dispatch.Invocation;import com.caucho.server.dispatch.InvocationDecoder;import com.caucho.server.e_app.EarConfig;import com.caucho.server.e_app.EarDeployController;import com.caucho.server.e_app.EarDeployGenerator;import com.caucho.server.e_app.EarSingleDeployGenerator;import com.caucho.server.host.Host;import com.caucho.server.log.AbstractAccessLog;import com.caucho.server.log.AccessLog;import com.caucho.server.session.SessionManager;import com.caucho.server.util.CauchoSystem;import com.caucho.server.rewrite.RewriteDispatch;import com.caucho.util.L10N;import com.caucho.util.LruCache;import com.caucho.vfs.Path;import com.caucho.vfs.Vfs;import javax.annotation.PostConstruct;import javax.servlet.FilterChain;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.UnavailableException;import javax.servlet.http.HttpServletResponse;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.logging.Level;import java.util.logging.Logger;/** * Resin's webApp implementation. */public class WebAppContainer  implements DispatchBuilder, ClassLoaderListener, EnvironmentListener{  static final L10N L = new L10N(WebApp.class);  static final Logger log = Log.open(WebAppContainer.class);  // The owning dispatch server  private DispatchServer _dispatchServer;  // The context class loader  private EnvironmentClassLoader _classLoader;  // The root directory.  private Path _rootDir;  // The document directory.  private Path _docDir;  // dispatch mapping  private RewriteDispatch _rewriteDispatch;  private WebApp _errorWebApp;  // List of default ear webApp configurations  private ArrayList<EarConfig> _earDefaultList    = new ArrayList<EarConfig>();  private DeployContainer<EarDeployController> _earDeploy;  private DeployContainer<WebAppController> _appDeploy;  private WebAppExpandDeployGenerator _warGenerator;  private boolean _hasWarGenerator;  // LRU cache for the webApp lookup  private LruCache<String,WebAppController> _uriToAppCache    = new LruCache<String,WebAppController>(8192);  // List of default webApp configurations  private ArrayList<WebAppConfig> _webAppDefaultList    = new ArrayList<WebAppConfig>();  private AbstractAccessLog _accessLog;  private ErrorPageManager _errorPageManager;  private long _startWaitTime = 10000L;  private Throwable _configException;  // lifecycle  private final Lifecycle _lifecycle = new Lifecycle();  /**   * Creates the webApp with its environment loader.   */  public WebAppContainer()  {    this((EnvironmentClassLoader) Thread.currentThread().getContextClassLoader());  }  /**   * Creates the webApp with its environment loader.   */  public WebAppContainer(EnvironmentClassLoader loader)  {    _rootDir = Vfs.lookup();    _docDir = Vfs.lookup();    _classLoader = loader;    /*    Environment.addEnvironmentListener(this, loader);    Environment.addClassLoaderListener(this, loader);    */    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(loader);            _errorPageManager = new ErrorPageManager(getErrorWebApp());      _errorPageManager.setWebAppContainer(this);      // These need to be in the proper class loader so they can      // register themselves with the environment      _earDeploy = new DeployContainer<EarDeployController>();      _appDeploy = new DeployContainer<WebAppController>();      _warGenerator = new WebAppExpandDeployGenerator(_appDeploy, this);    } finally {      thread.setContextClassLoader(oldLoader);    }  }  /**   * Sets the dispatch server.   */  public void setDispatchServer(DispatchServer server)  {    _dispatchServer = server;  }  /**   * Gets the dispatch server.   */  public DispatchServer getDispatchServer()  {    return _dispatchServer;  }  /**   * Gets the class loader.   */  public ClassLoader getClassLoader()  {    return _classLoader;  }  /**   * sets the class loader.   */  public void setEnvironmentClassLoader(EnvironmentClassLoader loader)  {    _classLoader = loader;  }  /**   * Returns the owning host.   */  public Host getHost()  {    return null;  }  /**   * Gets the root directory.   */  public Path getRootDirectory()  {    return _rootDir;  }  /**   * Sets the root directory.   */  public void setRootDirectory(Path path)  {    _rootDir = path;    Vfs.setPwd(path, getClassLoader());  }  /**   * Gets the document directory.   */  public Path getDocumentDirectory()  {    return _docDir;  }  /**   * Sets the document directory.   */  public void setDocumentDirectory(Path path)  {    _docDir = path;  }  /**   * Sets the document directory.   */  public void setDocDir(Path path)  {    setDocumentDirectory(path);  }  /**   * Sets the access log.   */  public AbstractAccessLog createAccessLog()  {    if (_accessLog == null)      _accessLog = new AccessLog();    return _accessLog;  }  /**   * Sets the access log.   */  public void setAccessLog(AbstractAccessLog log)  {    _accessLog = log;    Environment.setAttribute("caucho.server.access-log", log);  }  /**   * Adds an error page   */  public void addErrorPage(ErrorPage errorPage)  {    _errorPageManager.addErrorPage(errorPage);  }  /**   * Returns the error page manager   */  public ErrorPageManager getErrorPageManager()  {    return _errorPageManager;  }  /**   * Sets a configuration exception.   */  public void setConfigException(Throwable e)  {    _configException = e;  }  /**   * Returns the webApp generator   */  public DeployContainer<WebAppController> getWebAppGenerator()  {    return _appDeploy;  }  /**   * Returns the container's session manager.   */  public SessionManager getSessionManager()  {    return null;  }  /**   * Adds rewrite-dispatch.   */  public RewriteDispatch createRewriteDispatch()  {    if (_rewriteDispatch == null) {      _rewriteDispatch = new RewriteDispatch((Server) getDispatchServer());    }    return _rewriteDispatch;  }  /**   * Returns true if modified.   */  public boolean isModified()  {    return _lifecycle.isDestroyed() || _classLoader.isModified();  }  /**   * Adds an webApp.   */  public void addWebApp(WebAppConfig config)    throws Exception  {    if (config.getURLRegexp() != null) {      DeployGenerator<WebAppController> deploy	= new WebAppRegexpDeployGenerator(_appDeploy, this, config);      _appDeploy.add(deploy);      return;    }    // server/10f6    /*    WebAppController oldEntry      = _appDeploy.findController(config.getContextPath());    if (oldEntry != null && oldEntry.getSourceType().equals("single")) {      throw new ConfigException(L.l("duplicate web-app '{0}' forbidden.",				    config.getId()));    }    */    WebAppSingleDeployGenerator deploy      = new WebAppSingleDeployGenerator(_appDeploy, this, config);        deploy.deploy();    _appDeploy.add(deploy);    clearCache();  }  /**   * Removes an webApp.   */  void removeWebApp(WebAppController entry)  {    _appDeploy.remove(entry.getContextPath());    clearCache();  }  /**   * Adds a web-app default   */  public void addWebAppDefault(WebAppConfig init)  {    _webAppDefaultList.add(init);  }  /**   * Returns the list of web-app defaults   */  public ArrayList<WebAppConfig> getWebAppDefaultList()  {    return _webAppDefaultList;  }  /**   * Sets the war-expansion   */  public WebAppExpandDeployGenerator createWarDeploy()  {    return new WebAppExpandDeployGenerator(_appDeploy, this);  }  /**   * Sets the war-expansion   */  public WebAppExpandDeployGenerator createWebAppDeploy()  {    return createWarDeploy();  }  /**   * Sets the war-expansion   */  public void addWebAppDeploy(WebAppExpandDeployGenerator deploy)    throws ConfigException  {    addWarDeploy(deploy);  }  /**   * Sets the war-expansion   */  public void addWarDeploy(WebAppExpandDeployGenerator webAppDeploy)    throws ConfigException  {    assert webAppDeploy.getContainer() == this;    if (! _hasWarGenerator) {      _hasWarGenerator = true;      _warGenerator = webAppDeploy;    }    _appDeploy.add(webAppDeploy);  }  /**   * Sets the war-expansion   */  public void addDeploy(DeployGenerator deploy)    throws ConfigException  {    if (deploy instanceof WebAppExpandDeployGenerator)      addWebAppDeploy((WebAppExpandDeployGenerator) deploy);    else      _appDeploy.add(deploy);  }  /**   * Removes a web-app-generator.   */  public void removeWebAppDeploy(DeployGenerator deploy)  {    _appDeploy.remove(deploy);  }  /**   * Updates a WebApp deploy   */  public void updateWebAppDeploy(String name)    throws Throwable  {    clearCache();    _appDeploy.update();    WebAppController controller = _appDeploy.update(name);    if (controller != null) {      Throwable configException = controller.getConfigException();      if (configException != null)	throw configException;    }  }  /**   * Adds an enterprise webApp.   */  public void addApplication(EarConfig config)  {    DeployGenerator<EarDeployController> deploy = new EarSingleDeployGenerator(_earDeploy, this, config);    _earDeploy.add(deploy);  }  /**   * Updates an ear deploy   */  public void updateEarDeploy(String name)    throws Throwable  {    clearCache();    _earDeploy.update();    EarDeployController entry = _earDeploy.update(name);    if (entry != null) {      entry.start();      Throwable configException = entry.getConfigException();      if (configException != null)	throw configException;    }  }  /**   * Updates an ear deploy   */  public void expandEarDeploy(String name)  {    clearCache();    _earDeploy.update();    EarDeployController entry = _earDeploy.update(name);    if (entry != null)      entry.start();  }  /**   * Start an ear   */  public void startEarDeploy(String name)  {    clearCache();    _earDeploy.update();    EarDeployController entry = _earDeploy.update(name);    if (entry != null)      entry.start();  }  /**   * Adds an ear default   */  public void addEarDefault(EarConfig config)  {    _earDefaultList.add(config);  }  /**   * Returns the list of ear defaults   */  public ArrayList<EarConfig> getEarDefaultList()  {    return _earDefaultList;  }  /**   * Sets the ear-expansion   */  public EarDeployGenerator createEarDeploy()    throws Exception  {    return new EarDeployGenerator(_earDeploy, this);  }  /**   * Adds the ear-expansion   */  public void addEarDeploy(EarDeployGenerator earDeploy)    throws Exception  {    _earDeploy.add(earDeploy);    // server/26cc - _appDeploy must be added first, because the    // _earDeploy addition will automaticall register itself    _appDeploy.add(new WebAppEarDeployGenerator(_appDeploy, this, earDeploy));    /*    _earDeploy.add(earDeploy);    */  }  /**   * Returns the URL for the container.   */  public String getURL()  {    return "";  }  /**   * Returns the host name for the container.   */  public String getHostName()  {    return "";  }  // backwards compatibility  /**   * Sets the war-dir for backwards compatibility.   */  public void setWarDir(Path warDir)    throws ConfigException  {    _warGenerator.setPath(warDir);    if (! _hasWarGenerator) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?