webapp.java

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

JAVA
2,683
字号
	  if (log.isLoggable(Level.FINE))	    log.log(Level.WARNING, e1.toString(), e1);	  else	    log.warning(e1.getMessage());	}	else {	  log.log(Level.WARNING, e1.toString(), e1);	}      }    }        if (_configException == null)      _configException = e;    // server/13l8    if (e != null) { // && _invocationDependency != null) {      // _invocationDependency.add      // _invocationDependency.clearModified();      _classLoader.addDependency(AlwaysModified.create());    }  }  /**   * Gets the config exception.   */  public Throwable getConfigException()  {    return _configException;  }  /**   * Returns the current cluster.   */  public Cluster getCluster()  {    return Cluster.getCluster(getClassLoader());  }  /**   * Returns true if should ignore client disconnect.   */  public boolean isIgnoreClientDisconnect()  {    DispatchServer server = getDispatchServer();    if (server == null)      return true;    else      return server.isIgnoreClientDisconnect();  }  /**   * Sets the delay time waiting for requests to end.   */  public void setShutdownWaitMax(Period wait)  {    _shutdownWaitTime = wait.getPeriod();    Resin resin = Resin.getLocal();    if (resin != null &&        resin.getShutdownWaitMax() < _shutdownWaitTime) {      log.warning(L.l("web-app shutdown-wait-max '{0}' is longer than resin shutdown-wait-max '{1}'.",                      _shutdownWaitTime,                      resin.getShutdownWaitMax()));    }  }  /**   * Sets the delay time waiting for a restart   */  public void setActiveWaitTime(Period wait)  {    _activeWaitTime = wait.getPeriod();  }  /**   * Sets the delay time waiting for requests to end.   */  public void setIdleTime(Period idle)  {    _idleTime = idle.getPeriod();  }  /**   * Backwards compatability for config-file.   */  public void addConfigFile(Path path)    throws Exception  {    com.caucho.config.core.ResinImport rImport;    rImport = new com.caucho.config.core.ResinImport();    rImport.setPath(path);    rImport.setOptional(true);    rImport.setParent(this);    rImport.init();    log.config("<config-file> is deprecated.  Please use resin:import.");  }  /**   * Returns true if the webApp is active.   */  public String getState()  {    return _lifecycle.getStateName();  }  /**   * Returns true if it's init.   */  public boolean isInit()  {    return _lifecycle.isInit() || _configException != null;  }  /**   * Returns true if it's in the middle of initializing   */  public boolean isInitializing()  {    return _lifecycle.isBeforeActive();  }  /**   * Returns true if the webApp is active.   */  public boolean isActive()  {    return _lifecycle.isActive();  }  /**   * Returns true if it's closed.   */  public boolean isClosed()  {    return _lifecycle.isDestroyed();  }  static ThreadLocal<ServletRequest> getRequestThreadLocal()  {    return _requestThreadLocal;  }  public static ServletRequest getThreadRequest()  {    return _requestThreadLocal.get();  }  /**   * Initializes.   */  @PostConstruct  public void init()    throws Exception  {    if (! _lifecycle.toInitializing())      return;    try {      _classLoader.setId("web-app:" + getURL());      _invocationDependency.setCheckInterval(getEnvironmentClassLoader().getDependencyCheckInterval());      if (_tempDir == null)        _tempDir = (Path) Environment.getLevelAttribute("caucho.temp-dir");      if (_tempDir == null) {        _tempDir = getAppDir().lookup("WEB-INF/tmp");        if (getAppDir().lookup("WEB-INF").isDirectory())          _tempDir.mkdirs();      }      else        _tempDir.mkdirs();      setAttribute("javax.servlet.context.tempdir", new File(_tempDir.getNativePath()));      FilterChainBuilder securityBuilder = _constraintManager.getFilterBuilder();      if (securityBuilder != null)        _filterMapper.addTopFilter(securityBuilder);      _cache = (AbstractCache) Environment.getAttribute("caucho.server.cache");      for (int i = 0; i < _appGenerators.size(); i++)        _parent.addDeploy(_appGenerators.get(i));      _classLoader.setId("web-app:" + getURL());      WebBeansContainer webBeans = WebBeansContainer.getCurrent();      try {	ServletAuthenticator auth	  = webBeans.getByType(ServletAuthenticator.class);;	setAttribute("caucho.authenticator", auth);      } catch (Exception e) {	log.fine(e.toString());      }      WebAppController parent = null;      if (_controller != null)        parent = _controller.getParent();      if (_isInheritSession && parent != null &&          _sessionManager != parent.getWebApp().getSessionManager()) {        SessionManager sessionManager = _sessionManager;        _sessionManager = parent.getWebApp().getSessionManager();        if (sessionManager != null)          sessionManager.close();      }      if (getSessionManager() != null)        getSessionManager().init();      _characterEncoding = CharacterEncoding.getLocalEncoding();      for (int i = 0; i < _resourceValidators.size(); i++) {        Validator validator = _resourceValidators.get(i);        validator.validate();      }    } finally {      _lifecycle.toInit();    }  }  public WebAppAdmin getAdmin()  {    return _controller.getAdmin();  }  public void start()  {    if (! _lifecycle.isAfterInit())      throw new IllegalStateException(L.l("webApp must be initialized before starting.  Currently in state {0}.", _lifecycle.getStateName()));    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    boolean isOkay = true;    try {      thread.setContextClassLoader(_classLoader);      if (! _lifecycle.toStarting())        return;      isOkay = false;      if (_accessLog == null)        _accessLog = _accessLogLocal.get();      long interval = _classLoader.getDependencyCheckInterval();      _invocationDependency.setCheckInterval(interval);      if (_parent != null)        _invocationDependency.add(_parent.getWebAppGenerator());      // Sets the last modified time so the app won't immediately restart      _invocationDependency.clearModified();      _classLoader.clearModified();      String serverId = (String) new EnvironmentLocal("caucho.server-id").get();      if (serverId != null)        setAttribute("caucho.server-id", serverId);      _classLoader.start();      // configuration exceptions discovered by resources like      // the persistence manager      if (_configException == null)        _configException = Environment.getConfigException();      try {	if (getSessionManager() != null)	  getSessionManager().start();      } catch (Throwable e) {        log.log(Level.WARNING, e.toString(), e);      }      _jspApplicationContext.addELResolver(new WebBeansELResolver());            ServletContextEvent event = new ServletContextEvent(this);      for (Listener listener : _listeners) {        try {          addListenerObject(listener.createListenerObject(), false);        } catch (Exception e) {          throw ConfigException.create(e);        }      }      for (int i = 0; i < _webAppListeners.size(); i++) {        ServletContextListener listener = _webAppListeners.get(i);        try {          listener.contextInitialized(event);        } catch (Exception e) {          log.log(Level.WARNING, e.toString(), e);        }      }      try {        _filterManager.init();        _servletManager.init();      } catch (Exception e) {        setConfigException(e);      }      if (_parent instanceof Host) {        Host host = (Host) _parent;        host.setConfigETag(null);      }      _lifecycle.toActive();      clearCache();      isOkay = true;    } finally {      if (! isOkay)        _lifecycle.toError();      thread.setContextClassLoader(oldLoader);    }  }  /**   * Returns true if the webApp has been modified.   */  public boolean isModified()  {    // server/13l8    // _configException test is needed so compilation failures will force    // restart    if (_lifecycle.isAfterActive())      return true;    else if (_classLoader.isModified())      return true;    else      return false;  }  /**   * Returns true if the webApp has been modified.   */  public boolean isModifiedNow()  {    // force check    _classLoader.isModifiedNow();    _invocationDependency.isModifiedNow();    return isModified();  }  /**   * Log the reason for modification.   */  public boolean logModified(Logger log)  {    if (_lifecycle.isAfterActive()) {      // closed web-apps don't modify (XXX: errors?)      return true;    }    else      return _classLoader.logModified(log);  }  /**   * Returns true if the webApp deployed with an error.   */  public boolean isDeployError()  {    return _configException != null;  }  /**   * Returns true if the deployment is idle.   */  public boolean isDeployIdle()  {    if (_idleTime < 0)      return false;    else      return _lastRequestTime + _idleTime < Alarm.getCurrentTime();  }  /**   * Returns the servlet context for the URI.   */  public ServletContext getContext(String uri)  {    if (uri == null)      throw new IllegalArgumentException(L.l("getContext URI must not be null."));    else if (uri.startsWith("/")) {    }    else if (uri.equals(""))      uri = "/";    else      throw new IllegalArgumentException(L.l("getContext URI '{0}' must be absolute.", uri));    try {      if (_isDisableCrossContext)        return uri.startsWith(getContextPath()) ? this : null;      else if (_parent != null)        return _parent.findSubWebAppByURI(uri);      else        return this;    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);      return null;    }  }  /**   * Returns the best matching servlet pattern.   */  public String getServletPattern(String uri)  {    return _servletMapper.getServletPattern(uri);  }  /**   * Returns the best matching servlet pattern.   */  public ArrayList<String> getServletMappingPatterns()  {    return _servletMapper.getURLPatterns();  }  /**   * Returns the best matching servlet pattern.   */  public ArrayList<String> getServletIgnoreMappingPatterns()  {    return _servletMapper.getIgnorePatterns();  }  /**   * Fills the servlet instance.  (Generalize?)   */  public Invocation buildInvocation(Invocation invocation)  {    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    thread.setContextClassLoader(getClassLoader());    try {      FilterChain chain = null;      if (_configException != null) {        chain = new ExceptionFilterChain(_configException);        invocation.setFilterChain(chain);        invocation.setDependency(AlwaysModified.create());	        return invocation;      }      else if (! _lifecycle.waitForActive(_activeWaitTime)) {	if (log.isLoggable(Level.FINE))	  log.fine(this + " returned 503 busy for '" + invocation.getRawURI() + "'");        int code = HttpServletResponse.SC_SERVICE_UNAVAILABLE;        chain = new ErrorFilterChain(code);        invocation.setFilterChain(chain);        invocation.setDependency(AlwaysModified.create());	        return invocation;      }      else {        FilterChainEntry entry = null;        // jsp/1910 - can't cache jsp_precompile        String query = invocation.getQueryString();        boolean isCache = true;        if (query != null && query.indexOf("jsp_precompile") >= 0)          isCache = false;	else if (_requestRewriteDispatch != null)	  isCache = false;        if (isCache)          entry = _filterChainCache.get(invocation.getContextURI());        if (entry != null && ! entry.isModified()) {          chain = entry.getFilterChain();        } else {          chain = _servletMapper.mapServlet(invocation);          if (_requestRewriteDispatch != null) {	    FilterChain newChain	      = _requestRewriteDispatch.map(invocation.getContextURI(),					    invocation.getQueryString(),					    chain);	    chain = newChain;          }	  // server/13s[o-r]	  _filterMapper.buildDispatchChain(invocation, chain);          chain = invocation.getFilterChain();          entry = new FilterChainEntry(chain, invocation);          chain = entry.getFilterChain();          if (isCache)            _filterChainCache.put(invocation.getContextURI(), entry);        }        // the cache must be outside of the WebAppFilterChain because        // the CacheListener in ServletInvocation needs the top to        // be a CacheListener.  Otherwise, the cache won't get lru.        // top-level filter elements        if (_cache != null)          chain = _cache.createFilterChain(chain, this);        if (_isStatisticsEnabled)          chain = new StatisticsFilterChain(chain, this);        WebAppFilterChain webAppChain = new WebAppFilterChain(chain, this);        webAppChain.setSecurityRoleMap(invocation.getSecurityRoleMap());        invocation.setFilterChain(webAppChain);        invocation.setPathInfo(entry.getPathInfo());        invocation.setServletPath(entry.getServletPath());      }      if (_oldWebApp != null	  && Alarm.getCurrentTime() < _oldWebAppExpireTime) {

⌨️ 快捷键说明

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