@Test public void environmentOperations() { DispatcherServlet servlet = new DispatcherServlet(); ConfigurableEnvironment defaultEnv = servlet.getEnvironment(); assertThat(defaultEnv, notNullValue()); ConfigurableEnvironment env1 = new StandardServletEnvironment(); servlet.setEnvironment(env1); // should succeed assertThat(servlet.getEnvironment(), sameInstance(env1)); try { servlet.setEnvironment(new DummyEnvironment()); fail("expected IllegalArgumentException for non-configurable Environment"); } catch (IllegalArgumentException ex) { } class CustomServletEnvironment extends StandardServletEnvironment { } @SuppressWarnings("serial") DispatcherServlet custom = new DispatcherServlet() { @Override protected ConfigurableWebEnvironment createEnvironment() { return new CustomServletEnvironment(); } }; assertThat(custom.getEnvironment(), instanceOf(CustomServletEnvironment.class)); }
public void init(String location){ applicationContext= new PluginXmlWebApplicationContext(pluginContext); applicationContext.setId(pluginContext.getName()); applicationContext.setParent(WebApplicationContextUtils.getWebApplicationContext(PluginFramework.getFrameworkServletContext())); applicationContext.setServletContext(pluginContext.getServletContext()); applicationContext.setServletConfig(pluginContext.getServletConfig()); applicationContext.setClassLoader(pluginContext.getClassLoader()); pluginContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext); applicationContext.setConfigLocation(location); ConfigurableEnvironment env = applicationContext.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(pluginContext.getServletContext(), null); } ((PluginContextSupport)pluginContext).setApplicationContext(applicationContext); try{ applicationContext.refresh(); }catch(Throwable e){ logger.error("Plugin ["+pluginContext.getName()+"] "+location+" parse failure",e); throw new PuzzleException(e); } registerPluginService2OSGiServiceBus(); logger.info(location+" parse success"); }
public void testEnvironmentOperations() { DispatcherServlet servlet = new DispatcherServlet(); ConfigurableEnvironment defaultEnv = servlet.getEnvironment(); assertThat(defaultEnv, notNullValue()); ConfigurableEnvironment env1 = new StandardServletEnvironment(); servlet.setEnvironment(env1); // should succeed assertThat(servlet.getEnvironment(), sameInstance(env1)); try { servlet.setEnvironment(new DummyEnvironment()); fail("expected IllegalArgumentException for non-configurable Environment"); } catch (IllegalArgumentException ex) { } class CustomServletEnvironment extends StandardServletEnvironment { } @SuppressWarnings("serial") DispatcherServlet custom = new DispatcherServlet() { @Override protected ConfigurableWebEnvironment createEnvironment() { return new CustomServletEnvironment(); } }; assertThat(custom.getEnvironment(), instanceOf(CustomServletEnvironment.class)); }
/** * {@inheritDoc} * <p>Replace {@code Servlet}-related property sources. */ @Override protected void initPropertySources() { ConfigurableEnvironment env = getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null); } }
/** * {@inheritDoc} * <p>Replace {@code Servlet}-related property sources. */ @Override protected void initPropertySources() { ConfigurableEnvironment env = getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig); } }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information if (this.contextId != null) { wac.setId(this.contextId); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName()); } } wac.setServletContext(getServletContext()); wac.setServletConfig(getServletConfig()); wac.setNamespace(getNamespace()); wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener())); // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig()); } postProcessWebApplicationContext(wac); applyInitializers(wac); wac.refresh(); }
@Before public void setUp() throws Exception { mockApplicationContext = mock(ConfigurableWebApplicationContext.class); mockEnviroment = mock(ConfigurableWebEnvironment.class); mockPropertySources = mock(MutablePropertySources.class); mockHomeDirectory = mock(Resource.class); when(mockApplicationContext.getEnvironment()).thenReturn(mockEnviroment); when(mockEnviroment.getPropertySources()).thenReturn(mockPropertySources); File mockFile = new File(MOCK_HMP_HOME_PATH); when(mockHomeDirectory.getFile()).thenReturn(mockFile); when(mockHomeDirectory.getFilename()).thenReturn(MOCK_HMP_HOME_PATH); }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information if (this.contextId != null) { wac.setId(this.contextId); } else { // Generate default id... ServletContext sc = getServletContext(); if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) { // Servlet <= 2.4: resort to name specified in web.xml, if any. String servletContextName = sc.getServletContextName(); if (servletContextName != null) { wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + servletContextName + "." + getServletName()); } else { wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + getServletName()); } } else { // Servlet 2.5's getContextPath available! wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath()) + "/" + getServletName()); } } } wac.setServletContext(getServletContext()); wac.setServletConfig(getServletConfig()); wac.setNamespace(getNamespace()); wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener())); // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig()); } postProcessWebApplicationContext(wac); applyInitializers(wac); wac.refresh(); }