List of usage examples for org.springframework.web.context ConfigurableWebApplicationContext start
void start();
From source file:com.enonic.cms.server.service.servlet.CmsDispatcherServlet.java
private static void startContextIfNeeded(ConfigurableWebApplicationContext context) { if (!context.isRunning()) { context.start(); }//from w w w .j a v a 2 s . c om }
From source file:org.red5.server.undertow.UndertowLoader.java
/** * Initialization.//from w w w . j ava 2 s . c om */ public void start() { log.info("Loading undertow context"); //get a reference to the current threads classloader final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); // root location for servlet container String serverRoot = System.getProperty("red5.root"); log.info("Server root: {}", serverRoot); String confRoot = System.getProperty("red5.config_root"); log.info("Config root: {}", confRoot); if (webappFolder == null) { // Use default webapps directory webappFolder = FileUtil.formatPath(System.getProperty("red5.root"), "/webapps"); } System.setProperty("red5.webapp.root", webappFolder); log.info("Application root: {}", webappFolder); // scan the sub directories to determine our context paths buildContextPathList(webappFolder); try { // create our servlet container container = ServletContainer.Factory.newInstance(); // create a root path handler final PathHandler rootHandler = new PathHandler(); // create one server and use it everywhere Undertow.Builder builder = Undertow.builder(); // loop through connectors adding listeners to the builder for (UndertowConnector undertowConnector : connectors) { InetSocketAddress addr = undertowConnector.getSocketAddress(); builder.addListener(addr.getPort(), addr.getHostName(), (undertowConnector.isSecure() ? ListenerType.HTTPS : ListenerType.HTTP)); } log.trace("Classloader for undertow: {} TCL: {}", Undertow.class.getClassLoader(), originalClassLoader); // create references for later lookup LoaderBase.setApplicationLoader(new UndertowApplicationLoader(container, applicationContext)); // loop the other contexts for (String contextPath : contextPaths) { // create a class loader for the context ClassLoader classLoader = buildWebClassLoader(originalClassLoader, webappFolder, contextPath); // create deployment info DeploymentInfo info = deployment().setClassLoader(classLoader) .setContextPath(contextPath.equalsIgnoreCase("/ROOT") ? "/" : contextPath) .setDefaultEncoding("UTF-8").setDeploymentName(contextPath.substring(1) + ".war") .setUrlEncoding("UTF-8"); // parse the web.xml and configure the context parseWebXml(webappFolder, contextPath, info); if (log.isDebugEnabled()) { log.debug("Deployment info - name: {} servlets: {} filters: {}", new Object[] { info.getDisplayName(), info.getServlets().size(), info.getFilters().size() }); } // add the new deployment to the servlet container DeploymentManager manager = container.addDeployment(info); // set a reference to the manager and deploy the context LoaderBase.setRed5ApplicationContext(contextPath, new UndertowApplicationContext(manager)); // deploy manager.deploy(); // add path rootHandler.addPrefixPath(info.getContextPath(), manager.start()); // get deployment Deployment deployment = manager.getDeployment(); // get servlet context final ServletContext servletContext = deployment.getServletContext(); log.debug("Context initialized: {}", servletContext.getContextPath()); try { log.debug("Context: {}", servletContext); final ClassLoader webClassLoader = info.getClassLoader(); log.debug("Webapp classloader: {}", webClassLoader); // get the (spring) config file path final String contextConfigLocation = servletContext.getInitParameter( org.springframework.web.context.ContextLoader.CONFIG_LOCATION_PARAM) == null ? defaultSpringConfigLocation : servletContext.getInitParameter( org.springframework.web.context.ContextLoader.CONFIG_LOCATION_PARAM); log.debug("Spring context config location: {}", contextConfigLocation); // get the (spring) parent context key final String parentContextKey = servletContext.getInitParameter( org.springframework.web.context.ContextLoader.LOCATOR_FACTORY_KEY_PARAM) == null ? defaultParentContextKey : servletContext.getInitParameter( org.springframework.web.context.ContextLoader.LOCATOR_FACTORY_KEY_PARAM); log.debug("Spring parent context key: {}", parentContextKey); // set current threads classloader to the webapp classloader Thread.currentThread().setContextClassLoader(webClassLoader); // create a thread to speed-up application loading Thread thread = new Thread("Launcher:" + servletContext.getContextPath()) { public void run() { //set thread context classloader to web classloader Thread.currentThread().setContextClassLoader(webClassLoader); //get the web app's parent context ApplicationContext parentContext = null; if (applicationContext.containsBean(parentContextKey)) { parentContext = (ApplicationContext) applicationContext.getBean(parentContextKey); } else { log.warn("Parent context was not found: {}", parentContextKey); } // create a spring web application context final String contextClass = servletContext.getInitParameter( org.springframework.web.context.ContextLoader.CONTEXT_CLASS_PARAM) == null ? XmlWebApplicationContext.class.getName() : servletContext.getInitParameter( org.springframework.web.context.ContextLoader.CONTEXT_CLASS_PARAM); // web app context (spring) ConfigurableWebApplicationContext appctx = null; try { Class<?> clazz = Class.forName(contextClass, true, webClassLoader); appctx = (ConfigurableWebApplicationContext) clazz.newInstance(); // set the root webapp ctx attr on the each servlet context so spring can find it later servletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appctx); appctx.setConfigLocations(new String[] { contextConfigLocation }); appctx.setServletContext(servletContext); // set parent context or use current app context if (parentContext != null) { appctx.setParent(parentContext); } else { appctx.setParent(applicationContext); } // refresh the factory log.trace("Classloader prior to refresh: {}", appctx.getClassLoader()); appctx.refresh(); if (log.isDebugEnabled()) { log.debug("Red5 app is active: {} running: {}", appctx.isActive(), appctx.isRunning()); } appctx.start(); } catch (Throwable e) { throw new RuntimeException("Failed to load webapplication context class", e); } } }; thread.setDaemon(true); thread.start(); } catch (Throwable t) { log.error("Error setting up context: {} due to: {}", servletContext.getContextPath(), t.getMessage()); t.printStackTrace(); } finally { //reset the classloader Thread.currentThread().setContextClassLoader(originalClassLoader); } } // Dump deployments list if (log.isDebugEnabled()) { for (String deployment : container.listDeployments()) { log.debug("Container deployment: {}", deployment); } } // if everything is ok at this point then call the rtmpt and rtmps beans so they will init // if (applicationContext.containsBean("rtmpt.server")) { // log.debug("Initializing RTMPT"); // applicationContext.getBean("rtmpt.server"); // log.debug("Finished initializing RTMPT"); // } else { // log.info("Dedicated RTMPT server configuration was not specified"); // } // if (applicationContext.containsBean("rtmps.server")) { // log.debug("Initializing RTMPS"); // applicationContext.getBean("rtmps.server"); // log.debug("Finished initializing RTMPS"); // } else { // log.debug("Dedicated RTMPS server configuration was not specified"); // } // add a root handler builder.setHandler(rootHandler); // build the server instance server = builder.build(); log.info("Starting Undertow"); server.start(); } catch (Exception e) { if (e instanceof BindException || e.getMessage().indexOf("BindException") != -1) { log.error( "Error loading undertow, unable to bind connector. You may not have permission to use the selected port", e); } else { log.error("Error loading undertow", e); } } finally { registerJMX(); } log.debug("Undertow init exit"); }