webappcontainer.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,204 行 · 第 1/2 页
JAVA
1,204 行
_hasWarGenerator = true; addWebAppDeploy(_warGenerator); } } /** * Gets the war-dir. */ public Path getWarDir() { return _warGenerator.getPath(); } /** * Sets the war-expand-dir. */ public void setWarExpandDir(Path warDir) { _warGenerator.setExpandDirectory(warDir); } /** * Gets the war-expand-dir. */ public Path getWarExpandDir() { return _warGenerator.getExpandDirectory(); } /** * Init the container. */ @PostConstruct public void init() throws Exception { if (! _lifecycle.toInitializing()) return; log.fine(this + " initializing"); _lifecycle.toInit(); } /** * Starts the container. */ protected void start() { if (! _lifecycle.toActive()) return; /* try { _earDeploy.start(); } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); } */ try { _appDeploy.start(); } catch (ConfigException e) { log.warning(e.toString()); if (log.isLoggable(Level.FINE)) log.log(Level.FINE, e.toString(), e); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } /** * Clears the cache */ public void clearCache() { if (_dispatchServer != null) _dispatchServer.clearCache(); _uriToAppCache.clear(); } /** * Creates the invocation. */ public Invocation buildInvocation(Invocation invocation) throws Exception { if (_configException != null) { FilterChain chain = new ExceptionFilterChain(_configException); invocation.setFilterChain(chain); invocation.setDependency(AlwaysModified.create()); return invocation; } else if (! _lifecycle.waitForActive(_startWaitTime)) { int code = HttpServletResponse.SC_SERVICE_UNAVAILABLE; FilterChain chain = new ErrorFilterChain(code); invocation.setFilterChain(chain); if (_dispatchServer instanceof Server) { Server server = (Server) _dispatchServer; invocation.setWebApp(getErrorWebApp()); } invocation.setDependency(AlwaysModified.create()); return invocation ; } FilterChain chain; WebApp app = getWebApp(invocation, true); boolean isAlwaysModified; if (app != null) { invocation = app.buildInvocation(invocation); chain = invocation.getFilterChain(); isAlwaysModified = false; } else { int code = HttpServletResponse.SC_NOT_FOUND; chain = new ErrorFilterChain(code); ContextFilterChain contextChain = new ContextFilterChain(chain); contextChain.setErrorPageManager(_errorPageManager); chain = contextChain; invocation.setFilterChain(contextChain); isAlwaysModified = true; } if (_rewriteDispatch != null) { String uri = invocation.getURI(); String queryString = invocation.getQueryString(); FilterChain rewriteChain = _rewriteDispatch.map(uri, queryString, chain); if (rewriteChain != chain) { Server server = (Server) _dispatchServer; // server/13sf, server/1kq1 WebApp webApp = findWebAppByURI("/"); if (webApp != null) invocation.setWebApp(webApp); else invocation.setWebApp(getErrorWebApp()); invocation.setFilterChain(rewriteChain); isAlwaysModified = false; } } if (isAlwaysModified) invocation.setDependency(AlwaysModified.create()); return invocation; } /** * Returns a dispatcher for the named servlet. */ public RequestDispatcher getRequestDispatcher(String url) { // Currently no caching since this is only used for the error-page directive at the host level if (url == null) throw new IllegalArgumentException(L.l("request dispatcher url can't be null.")); else if (! url.startsWith("/")) throw new IllegalArgumentException(L.l("request dispatcher url `{0}' must be absolute", url)); Invocation includeInvocation = new Invocation(); Invocation forwardInvocation = new Invocation(); Invocation errorInvocation = new Invocation(); Invocation dispatchInvocation = new Invocation(); InvocationDecoder decoder = new InvocationDecoder(); String rawURI = url; try { decoder.splitQuery(includeInvocation, rawURI); decoder.splitQuery(forwardInvocation, rawURI); decoder.splitQuery(errorInvocation, rawURI); buildIncludeInvocation(includeInvocation); buildForwardInvocation(forwardInvocation); buildErrorInvocation(errorInvocation); buildDispatchInvocation(dispatchInvocation); RequestDispatcher disp = new RequestDispatcherImpl(includeInvocation, forwardInvocation, errorInvocation, dispatchInvocation, getWebApp(includeInvocation, false)); return disp; } catch (Exception e) { log.log(Level.FINE, e.toString(), e); return null; } } /** * Creates the invocation. */ public void buildIncludeInvocation(Invocation invocation) throws ServletException { WebApp app = buildSubInvocation(invocation); if (app != null) app.buildIncludeInvocation(invocation); } /** * Creates the invocation. */ public void buildForwardInvocation(Invocation invocation) throws ServletException { WebApp app = buildSubInvocation(invocation); if (app != null) app.buildForwardInvocation(invocation); } /** * Creates the error invocation. */ public void buildErrorInvocation(Invocation invocation) throws ServletException { WebApp app = buildSubInvocation(invocation); if (app != null) app.buildErrorInvocation(invocation); } /** * Creates the invocation. */ public void buildLoginInvocation(Invocation invocation) throws ServletException { WebApp app = buildSubInvocation(invocation); if (app != null) app.buildErrorInvocation(invocation); } /** * Creates the invocation for a rewrite-dispatch/dispatch. */ public void buildDispatchInvocation(Invocation invocation) throws ServletException { WebApp app = buildSubInvocation(invocation); if (app != null) app.buildDispatchInvocation(invocation); } /** * Creates a sub invocation, handing unmapped URLs and stopped webApps. */ private WebApp buildSubInvocation(Invocation invocation) { if (! _lifecycle.waitForActive(_startWaitTime)) { UnavailableException e; e = new UnavailableException(invocation.getURI()); FilterChain chain = new ExceptionFilterChain(e); invocation.setFilterChain(chain); invocation.setDependency(AlwaysModified.create()); return null; } WebAppController appController = getWebAppController(invocation); if (appController == null) { String url = invocation.getURI(); FileNotFoundException e = new FileNotFoundException(url); FilterChain chain = new ExceptionFilterChain(e); invocation.setFilterChain(chain); invocation.setDependency(AlwaysModified.create()); return null; } WebApp app = appController.subrequest(); if (app == null) { UnavailableException e; e = new UnavailableException(invocation.getURI()); FilterChain chain = new ExceptionFilterChain(e); invocation.setFilterChain(chain); invocation.setDependency(AlwaysModified.create()); return null; } return app; } /** * Returns the webApp for the current request. */ private WebApp getWebApp(Invocation invocation, boolean enableRedeploy) throws ServletException { try { WebAppController controller = getWebAppController(invocation); if (controller != null) { WebApp app; if (enableRedeploy) app = controller.request(); else app = controller.subrequest(); if (app == null) { return null; } invocation.setWebApp(app); return app; } else { return null; } } catch (Exception e) { throw new ServletException(e); } } /** * Returns the webApp controller for the current request. Side effect * of filling in the invocation's context path and context uri. * * @param invocation the request's invocation * * @return the controller or null if none match the url. */ private WebAppController getWebAppController(Invocation invocation) { WebAppController controller = findByURI(invocation.getURI()); if (controller == null) return null; String invocationURI = invocation.getURI(); String contextPath = controller.getContextPath(invocationURI); invocation.setContextPath(invocationURI.substring(0, contextPath.length())); String uri = invocationURI.substring(contextPath.length()); invocation.setContextURI(uri); return controller; } /** * Creates the invocation. */ public WebApp findWebAppByURI(String uri) throws Exception { WebAppController controller = findByURI(uri); if (controller != null) return controller.request(); else return null; } /** * Creates the invocation. */ public WebApp findSubWebAppByURI(String uri) throws Exception { WebAppController controller = findByURI(uri); if (controller != null) return controller.subrequest(); else return null; } /** * Finds the web-app matching the current entry. */ public WebAppController findByURI(String uri) { if (_appDeploy.isModified()) _uriToAppCache.clear(); WebAppController controller = _uriToAppCache.get(uri); if (controller != null) return controller; String cleanUri = uri; if (CauchoSystem.isCaseInsensitive()) cleanUri = cleanUri.toLowerCase(); // server/105w try { cleanUri = InvocationDecoder.normalizeUri(cleanUri); } catch (java.io.IOException e) { log.log(Level.FINER, e.toString(), e); } controller = findByURIImpl(cleanUri); _uriToAppCache.put(uri, controller); return controller; } /** * Finds the web-app for the entry. */ private WebAppController findByURIImpl(String subURI) { WebAppController controller = _uriToAppCache.get(subURI); if (controller != null) { return controller; } int length = subURI.length(); int p = subURI.lastIndexOf('/'); if (p < 0 || p < length - 1) { // server/26cf controller = _appDeploy.findController(subURI); if (controller != null) { _uriToAppCache.put(subURI, controller); return controller; } } if (p >= 0) { controller = findByURIImpl(subURI.substring(0, p)); if (controller != null) _uriToAppCache.put(subURI, controller); } return controller; } /** * Finds the web-app for the entry, not checking for sub-apps. * (used by LocalDeployServlet) */ public WebAppController findController(String subURI) { return _appDeploy.findController(subURI); } /** * Returns a list of the webApps. */ public ArrayList<WebAppController> getWebAppList() { return _appDeploy.getControllers(); } /** * Returns a list of the webApps. */ public ArrayList<EarDeployController> getEntAppList() { return _earDeploy.getControllers(); } /** * Returns true if the webApp container has been closed. */ public final boolean isDestroyed() { return _lifecycle.isDestroyed(); } /** * Returns true if the webApp container is active */ public final boolean isActive() { return _lifecycle.isActive(); } /** * Closes the container. */ public boolean stop() { if (! _lifecycle.toStop()) return false; _earDeploy.stop(); _appDeploy.stop(); return true; } /** * Closes the container. */ public void destroy() { stop(); if (! _lifecycle.toDestroy()) return; _earDeploy.destroy(); _appDeploy.destroy(); } /** * Returns the error webApp during startup. */ public WebApp getErrorWebApp() { if (_errorWebApp == null && _classLoader != null && ! _classLoader.isModified()) { Thread thread = Thread.currentThread(); ClassLoader loader = thread.getContextClassLoader(); try { thread.setContextClassLoader(_classLoader); _errorWebApp = new WebApp(getRootDirectory().lookup("caucho-web-app-error")); _errorWebApp.setParent(this); //_errorWebApp.init(); //_errorWebApp.start(); } catch (Throwable e) { log.log(Level.WARNING, e.toString(), e); } finally { thread.setContextClassLoader(loader); } } return _errorWebApp; } /** * Handles the case where a class loader has completed initialization */ public void classLoaderInit(DynamicClassLoader loader) { } /** * Handles the case where a class loader is dropped. */ public void classLoaderDestroy(DynamicClassLoader loader) { destroy(); } /** * Handles the environment config phase */ public void environmentConfigure(EnvironmentClassLoader loader) { } /** * Handles the environment bind phase */ public void environmentBind(EnvironmentClassLoader loader) { } /** * Handles the case where the environment is starting (after init). */ public void environmentStart(EnvironmentClassLoader loader) { } /** * Handles the case where the environment is stopping */ public void environmentStop(EnvironmentClassLoader loader) { stop(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?