List of usage examples for org.springframework.web.context.support XmlWebApplicationContext setParent
@Override public void setParent(@Nullable ApplicationContext parent)
From source file:pl.exsio.frameset.vaadin.bootstrap.servlet.FramesetServlet.java
@Override public void init(ServletConfig config) throws ServletException { this.applicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext()); if (config.getInitParameter(CONTEXT_CONFIG_LOCATION_PARAMETER) != null) { XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setParent(this.applicationContext); context.setConfigLocation(config.getInitParameter(CONTEXT_CONFIG_LOCATION_PARAMETER)); context.setServletConfig(config); context.setServletContext(config.getServletContext()); context.refresh();//w w w .jav a2 s. c o m this.applicationContext = context; } if (config.getInitParameter(SYSTEM_MESSAGES_BEAN_NAME_PARAMETER) != null) { this.systemMessagesBeanName = config.getInitParameter(SYSTEM_MESSAGES_BEAN_NAME_PARAMETER); } if (FramesetApplicationContext.getApplicationContext() == null) { FramesetApplicationContext.setApplicationContext(applicationContext); } super.init(config); }
From source file:eu.enhan.timelord.web.init.TimelordWebInit.java
/** * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext) *//*from w w w . java2s . co m*/ @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext(); rootCtx.register(AppConfig.class); servletContext.addListener(new ContextLoaderListener(rootCtx)); AnnotationConfigWebApplicationContext webAppCtx = new AnnotationConfigWebApplicationContext(); webAppCtx.setParent(rootCtx); webAppCtx.register(WebConfig.class); XmlWebApplicationContext securityCtx = new XmlWebApplicationContext(); securityCtx.setServletContext(servletContext); securityCtx.setParent(rootCtx); securityCtx.setConfigLocation("classpath:/spring/security.xml"); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(webAppCtx)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain", securityCtx)); // securityFilter.addMappingForUrlPatterns(null, false, "/**"); securityFilter.addMappingForServletNames(null, false, "dispatcher"); }
From source file:com.clican.pluto.cms.ui.listener.StartupListener.java
public void contextInitialized(ServletContextEvent event) { super.contextInitialized(event); try {/*from w ww. j a va 2 s . com*/ Velocity.init( Thread.currentThread().getContextClassLoader().getResource("velocity.properties").getPath()); } catch (Exception e) { throw new RuntimeException(e); } ApplicationContext ctx = null; JndiUtils.setJndiFactory(MockContextFactory.class.getName()); Hashtable<String, String> ht = new Hashtable<String, String>(); ht.put(Context.INITIAL_CONTEXT_FACTORY, MockContextFactory.class.getName()); try { ctx = (ApplicationContext) JndiUtils.lookupObject(ApplicationContext.class.getName()); if (ctx == null) { log.warn("Cannot get ApplicationContext from JNDI"); } } catch (Exception e) { log.warn("Cannot get ApplicationContext from JNDI"); } if (ctx == null) { ctx = (new ClassPathXmlApplicationContext(new String[] { "classpath*:META-INF/ui-*.xml", })); } XmlWebApplicationContext wac = (XmlWebApplicationContext) WebApplicationContextUtils .getRequiredWebApplicationContext(event.getServletContext()); wac.setParent(ctx); wac.refresh(); event.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); Constants.ctx = wac; }
From source file:org.red5.server.tomcat.TomcatVHostLoader.java
/** * Starts a web application and its red5 (spring) component. This is basically a stripped down * version of init()./*from w ww. j av a 2 s .c om*/ * * @return true on success */ @SuppressWarnings("cast") public boolean startWebApplication(String applicationName) { boolean result = false; log.info("Starting Tomcat virtual host - Web application"); log.info("Virtual host root: {}", webappRoot); log.info("Virtual host context id: {}", defaultApplicationContextId); // application directory String contextName = '/' + applicationName; Container cont = null; //check if the context already exists for the host if ((cont = host.findChild(contextName)) == null) { log.debug("Context did not exist in host"); String webappContextDir = FileUtil.formatPath(webappRoot, applicationName); //prepend slash Context ctx = addContext(contextName, webappContextDir); //set the newly created context as the current container cont = ctx; } else { log.debug("Context already exists in host"); } try { ServletContext servletContext = ((Context) cont).getServletContext(); log.debug("Context initialized: {}", servletContext.getContextPath()); String prefix = servletContext.getRealPath("/"); log.debug("Path: {}", prefix); Loader cldr = cont.getLoader(); log.debug("Loader type: {}", cldr.getClass().getName()); ClassLoader webClassLoader = cldr.getClassLoader(); log.debug("Webapp classloader: {}", webClassLoader); //create a spring web application context XmlWebApplicationContext appctx = new XmlWebApplicationContext(); appctx.setClassLoader(webClassLoader); appctx.setConfigLocations(new String[] { "/WEB-INF/red5-*.xml" }); //check for red5 context bean if (applicationContext.containsBean(defaultApplicationContextId)) { appctx.setParent((ApplicationContext) applicationContext.getBean(defaultApplicationContextId)); } else { log.warn("{} bean was not found in context: {}", defaultApplicationContextId, applicationContext.getDisplayName()); //lookup context loader and attempt to get what we need from it if (applicationContext.containsBean("context.loader")) { ContextLoader contextLoader = (ContextLoader) applicationContext.getBean("context.loader"); appctx.setParent(contextLoader.getContext(defaultApplicationContextId)); } else { log.debug("Context loader was not found, trying JMX"); MBeanServer mbs = JMXFactory.getMBeanServer(); //get the ContextLoader from jmx ObjectName oName = JMXFactory.createObjectName("type", "ContextLoader"); ContextLoaderMBean proxy = null; if (mbs.isRegistered(oName)) { proxy = (ContextLoaderMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, oName, ContextLoaderMBean.class, true); log.debug("Context loader was found"); appctx.setParent(proxy.getContext(defaultApplicationContextId)); } else { log.warn("Context loader was not found"); } } } if (log.isDebugEnabled()) { if (appctx.getParent() != null) { log.debug("Parent application context: {}", appctx.getParent().getDisplayName()); } } // appctx.setServletContext(servletContext); //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.refresh(); result = true; } catch (Throwable t) { log.error("Error setting up context: {}", applicationName, t); if (log.isDebugEnabled()) { t.printStackTrace(); } } return result; }
From source file:org.red5.server.tomcat.TomcatVHostLoader.java
/** * Initialization.// ww w . ja v a 2s.com */ @SuppressWarnings("cast") public void init() { log.info("Loading tomcat virtual host"); if (webappFolder != null) { //check for match with base webapp root if (webappFolder.equals(webappRoot)) { log.error("Web application root cannot be the same as base"); return; } } ClassLoader classloader = Thread.currentThread().getContextClassLoader(); //ensure we have a host if (host == null) { host = createHost(); } host.setParentClassLoader(classloader); String propertyPrefix = name; if (domain != null) { propertyPrefix += '_' + domain.replace('.', '_'); } log.debug("Generating name (for props) {}", propertyPrefix); System.setProperty(propertyPrefix + ".webapp.root", webappRoot); log.info("Virtual host root: {}", webappRoot); log.info("Virtual host context id: {}", defaultApplicationContextId); // Root applications directory File appDirBase = new File(webappRoot); // Subdirs of root apps dir File[] dirs = appDirBase.listFiles(new TomcatLoader.DirectoryFilter()); // Search for additional context files for (File dir : dirs) { String dirName = '/' + dir.getName(); // check to see if the directory is already mapped if (null == host.findChild(dirName)) { String webappContextDir = FileUtil.formatPath(appDirBase.getAbsolutePath(), dirName); Context ctx = null; if ("/root".equals(dirName) || "/root".equalsIgnoreCase(dirName)) { log.debug("Adding ROOT context"); ctx = addContext("/", webappContextDir); } else { log.debug("Adding context from directory scan: {}", dirName); ctx = addContext(dirName, webappContextDir); } log.debug("Context: {}", ctx); webappContextDir = null; } } appDirBase = null; dirs = null; // Dump context list if (log.isDebugEnabled()) { for (Container cont : host.findChildren()) { log.debug("Context child name: {}", cont.getName()); } } engine.addChild(host); // Start server try { log.info("Starting Tomcat virtual host"); //may not have to do this step for every host LoaderBase.setApplicationLoader(new TomcatApplicationLoader(embedded, host, applicationContext)); for (Container cont : host.findChildren()) { if (cont instanceof StandardContext) { StandardContext ctx = (StandardContext) cont; ServletContext servletContext = ctx.getServletContext(); log.debug("Context initialized: {}", servletContext.getContextPath()); //set the hosts id servletContext.setAttribute("red5.host.id", getHostId()); String prefix = servletContext.getRealPath("/"); log.debug("Path: {}", prefix); try { Loader cldr = ctx.getLoader(); log.debug("Loader type: {}", cldr.getClass().getName()); ClassLoader webClassLoader = cldr.getClassLoader(); log.debug("Webapp classloader: {}", webClassLoader); //create a spring web application context XmlWebApplicationContext appctx = new XmlWebApplicationContext(); appctx.setClassLoader(webClassLoader); appctx.setConfigLocations(new String[] { "/WEB-INF/red5-*.xml" }); //check for red5 context bean if (applicationContext.containsBean(defaultApplicationContextId)) { appctx.setParent( (ApplicationContext) applicationContext.getBean(defaultApplicationContextId)); } else { log.warn("{} bean was not found in context: {}", defaultApplicationContextId, applicationContext.getDisplayName()); //lookup context loader and attempt to get what we need from it if (applicationContext.containsBean("context.loader")) { ContextLoader contextLoader = (ContextLoader) applicationContext .getBean("context.loader"); appctx.setParent(contextLoader.getContext(defaultApplicationContextId)); } else { log.debug("Context loader was not found, trying JMX"); MBeanServer mbs = JMXFactory.getMBeanServer(); //get the ContextLoader from jmx ObjectName oName = JMXFactory.createObjectName("type", "ContextLoader"); ContextLoaderMBean proxy = null; if (mbs.isRegistered(oName)) { proxy = (ContextLoaderMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, oName, ContextLoaderMBean.class, true); log.debug("Context loader was found"); appctx.setParent(proxy.getContext(defaultApplicationContextId)); } else { log.warn("Context loader was not found"); } } } if (log.isDebugEnabled()) { if (appctx.getParent() != null) { log.debug("Parent application context: {}", appctx.getParent().getDisplayName()); } } // appctx.setServletContext(servletContext); //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.refresh(); } catch (Throwable t) { log.error("Error setting up context: {}", servletContext.getContextPath(), t); if (log.isDebugEnabled()) { t.printStackTrace(); } } } } } catch (Exception e) { log.error("Error loading Tomcat virtual host", e); } }
From source file:org.red5.server.tomcat.TomcatLoader.java
/** * Starts a web application and its red5 (spring) component. This is * basically a stripped down version of init(). * //from w w w . ja v a 2 s.c om * @return true on success */ public boolean startWebApplication(String applicationName) { log.info("Starting Tomcat - Web application"); boolean result = false; //get a reference to the current threads classloader final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); log.debug("Webapp root: {}", webappFolder); // application directory String contextName = '/' + applicationName; Container ctx = null; if (webappFolder == null) { // Use default webapps directory webappFolder = System.getProperty("red5.root") + "/webapps"; } System.setProperty("red5.webapp.root", webappFolder); log.info("Application root: {}", webappFolder); // scan for additional webapp contexts // Root applications directory File appDirBase = new File(webappFolder); // check if the context already exists for the host if ((ctx = host.findChild(contextName)) == null) { log.debug("Context did not exist in host"); String webappContextDir = FileUtil.formatPath(appDirBase.getAbsolutePath(), applicationName); log.debug("Webapp context directory (full path): {}", webappContextDir); // set the newly created context as the current container ctx = addContext(contextName, webappContextDir); } else { log.debug("Context already exists in host"); } final ServletContext servletContext = ((Context) ctx).getServletContext(); log.debug("Context initialized: {}", servletContext.getContextPath()); String prefix = servletContext.getRealPath("/"); log.debug("Path: {}", prefix); try { Loader cldr = ctx.getLoader(); log.debug("Loader delegate: {} type: {}", cldr.getDelegate(), cldr.getClass().getName()); if (cldr instanceof WebappLoader) { log.debug("WebappLoader class path: {}", ((WebappLoader) cldr).getClasspath()); } final ClassLoader webClassLoader = cldr.getClassLoader(); log.debug("Webapp classloader: {}", webClassLoader); // get the (spring) config file path final String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation") == null ? defaultSpringConfigLocation : servletContext.getInitParameter("contextConfigLocation"); log.debug("Spring context config location: {}", contextConfigLocation); // get the (spring) parent context key final String parentContextKey = servletContext.getInitParameter("parentContextKey") == null ? defaultParentContextKey : servletContext.getInitParameter("parentContextKey"); 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()) { @SuppressWarnings("cast") public void run() { //set current threads classloader to the webapp classloader Thread.currentThread().setContextClassLoader(webClassLoader); // create a spring web application context XmlWebApplicationContext appctx = new XmlWebApplicationContext(); appctx.setClassLoader(webClassLoader); appctx.setConfigLocations(new String[] { contextConfigLocation }); // check for red5 context bean ApplicationContext parentAppCtx = null; if (applicationContext.containsBean(defaultParentContextKey)) { parentAppCtx = (ApplicationContext) applicationContext.getBean(defaultParentContextKey); } else { log.warn("{} bean was not found in context: {}", defaultParentContextKey, applicationContext.getDisplayName()); // lookup context loader and attempt to get what we need from it if (applicationContext.containsBean("context.loader")) { ContextLoader contextLoader = (ContextLoader) applicationContext .getBean("context.loader"); parentAppCtx = contextLoader.getContext(defaultParentContextKey); } else { log.debug("Context loader was not found, trying JMX"); MBeanServer mbs = JMXFactory.getMBeanServer(); // get the ContextLoader from jmx ObjectName oName = JMXFactory.createObjectName("type", "ContextLoader"); ContextLoaderMBean proxy = null; if (mbs.isRegistered(oName)) { proxy = (ContextLoaderMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, oName, ContextLoaderMBean.class, true); log.debug("Context loader was found"); parentAppCtx = proxy.getContext(defaultParentContextKey); } else { log.warn("Context loader was not found"); } } } if (log.isDebugEnabled()) { if (appctx.getParent() != null) { log.debug("Parent application context: {}", appctx.getParent().getDisplayName()); } } appctx.setParent(parentAppCtx); appctx.setServletContext(servletContext); // 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.refresh(); } }; thread.setDaemon(true); thread.start(); result = true; } 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); } return result; }
From source file:org.kuali.rice.core.framework.resourceloader.SpringResourceLoader.java
@Override public void start() throws Exception { if (!isStarted()) { LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ",")); if (parentSpringResourceLoader != null && parentContext != null) { throw new ConfigurationException( "Both a parentSpringResourceLoader and parentContext were defined. Only one can be defined!"); }// w ww . java2 s. c om if (parentSpringResourceLoader != null) { parentContext = parentSpringResourceLoader.getContext(); } if (servletContextcontext != null) { XmlWebApplicationContext lContext = new XmlWebApplicationContext(); lContext.setServletContext(servletContextcontext); lContext.setParent(parentContext); lContext.setConfigLocations(this.fileLocs.toArray(new String[] {})); lContext.refresh(); context = lContext; } else { this.context = new ClassPathXmlApplicationContext(this.fileLocs.toArray(new String[] {}), parentContext); } super.start(); } }
From source file:org.opentestsystem.shared.test.jetty.InheritingSpringContextLoaderListener.java
@Override public WebApplicationContext createWebApplicationContext(ServletContext servletContext) { _logger.debug("Adding root Spring application context to Jetty container"); XmlWebApplicationContext uiSpringContext = new XmlWebApplicationContext(); uiSpringContext.setServletContext(servletContext); uiSpringContext.setParent(_externalContext); String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation"); if (!StringUtils.isEmpty(contextConfigLocation)) { uiSpringContext.setConfigLocation(contextConfigLocation); _logger.debug("root Spring application context to Jetty container configured from {}", contextConfigLocation);/*from ww w . j a v a 2s . com*/ } else { _logger.debug( "root Spring application context to Jetty container using default configuration location"); } uiSpringContext.refresh(); return uiSpringContext; }
From source file:org.springbyexample.web.service.EmbeddedJetty.java
@PostConstruct public void init() throws Exception { ctx = new ClassPathXmlApplicationContext(); ((AbstractApplicationContext) ctx).getEnvironment().setActiveProfiles(activeProfiles); ((AbstractXmlApplicationContext) ctx).setConfigLocations(configLocations); if (logger.isInfoEnabled()) { logger.info("Creating embedded jetty context. activeProfiles='{}' configLocations='{}'", new Object[] { ArrayUtils.toString(activeProfiles), ArrayUtils.toString(configLocations) }); }// www. java 2s.co m ((AbstractXmlApplicationContext) ctx).refresh(); ((AbstractApplicationContext) ctx).registerShutdownHook(); Server server = (Server) ctx.getBean("jettyServer"); if (port > 0) { Connector connector = server.getConnectors()[0]; connector.setPort(port); } ServletContext servletContext = null; for (Handler handler : server.getHandlers()) { if (handler instanceof Context) { Context context = (Context) handler; if (StringUtils.hasText(contextPath)) { context.setContextPath("/" + contextPath); } servletContext = context.getServletContext(); // setup Spring Security Filter FilterHolder filterHolder = new FilterHolder(); filterHolder.setName(SECURITY_FILTER_NAME); filterHolder.setClassName(DelegatingFilterProxy.class.getName()); context.getServletHandler().addFilterWithMapping(filterHolder, "/*", 0); break; } } XmlWebApplicationContext wctx = new XmlWebApplicationContext(); wctx.setParent(ctx); wctx.setConfigLocation(""); wctx.setServletContext(servletContext); wctx.refresh(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wctx); server.start(); logger.info("Server started."); }