@Override @Nullable public String getProperty(String name) { return this.source.getInitParameter(name); }
private static String getParameter(ServletContext servletContext, String key, String defaultValue) { String value = servletContext.getInitParameter(key); return value == null ? defaultValue : value; }
@Override public void setServletContext(ServletContext servletContext) { if (this.initParamName == null) { throw new IllegalArgumentException("initParamName is required"); } this.paramValue = servletContext.getInitParameter(this.initParamName); if (this.paramValue == null) { throw new IllegalStateException("No ServletContext init parameter '" + this.initParamName + "' found"); } }
/** * Remove the system property that points to the web app root directory. * To be called on shutdown of the web application. * @param servletContext the servlet context of the web application * @see #setWebAppRootSystemProperty */ public static void removeWebAppRootSystemProperty(ServletContext servletContext) { Assert.notNull(servletContext, "ServletContext must not be null"); String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); System.getProperties().remove(key); }
@Override public void setServletContext(ServletContext servletContext) { if (servletContext.getInitParameter("rails.env").equals("development")) { this.modificationCheckInterval = 1; } }
private static String doTryGetValueFromContext(String key) { if (StringUtils.isNotBlank(key) && Listener.theContext != null) { try { String value = Listener.theContext.getInitParameter(key); if (value != null) { return value; } } catch (SecurityException ex) { // Log exception and go on LOGGER.log(Level.CONFIG, "Access to the property {0} is prohibited", key); } } return null; }
@Override @Nullable public String getProperty(String name) { return this.source.getInitParameter(name); }
/** * Return whether default HTML escaping is enabled for the web application, * i.e. the value of the "defaultHtmlEscape" context-param in {@code web.xml} * (if any). * <p>This method differentiates between no param specified at all and * an actual boolean value specified, allowing to have a context-specific * default in case of no setting at the global level. * @param servletContext the servlet context of the web application * @return whether default HTML escaping is enabled for the given application * ({@code null} = no explicit default) */ @Nullable public static Boolean getDefaultHtmlEscape(@Nullable ServletContext servletContext) { if (servletContext == null) { return null; } String param = servletContext.getInitParameter(HTML_ESCAPE_CONTEXT_PARAM); return (StringUtils.hasText(param) ? Boolean.valueOf(param) : null); }
@Override @Nullable public String resolvePlaceholder(String placeholderName) { try { String propVal = this.servletContext.getInitParameter(placeholderName); if (propVal == null) { // Fall back to system properties. propVal = System.getProperty(placeholderName); if (propVal == null) { // Fall back to searching the system environment. propVal = System.getenv(placeholderName); } } return propVal; } catch (Throwable ex) { System.err.println("Could not resolve placeholder '" + placeholderName + "' in [" + this.text + "] as ServletContext init-parameter or system property: " + ex); return null; } } }
/** * Return whether response encoding should be used when HTML escaping characters, * thus only escaping XML markup significant characters with UTF-* encodings. * This option is enabled for the web application with a ServletContext param, * i.e. the value of the "responseEncodedHtmlEscape" context-param in {@code web.xml} * (if any). * <p>This method differentiates between no param specified at all and * an actual boolean value specified, allowing to have a context-specific * default in case of no setting at the global level. * @param servletContext the servlet context of the web application * @return whether response encoding is to be used for HTML escaping * ({@code null} = no explicit default) * @since 4.1.2 */ @Nullable public static Boolean getResponseEncodedHtmlEscape(@Nullable ServletContext servletContext) { if (servletContext == null) { return null; } String param = servletContext.getInitParameter(RESPONSE_ENCODED_HTML_ESCAPE_CONTEXT_PARAM); return (StringUtils.hasText(param) ? Boolean.valueOf(param) : null); }
@Override public void setServletContext(ServletContext servletContext) { if (this.initParamName == null) { throw new IllegalArgumentException("initParamName is required"); } this.paramValue = servletContext.getInitParameter(this.initParamName); if (this.paramValue == null) { throw new IllegalStateException("No ServletContext init parameter '" + this.initParamName + "' found"); } }
/** * Remove the system property that points to the web app root directory. * To be called on shutdown of the web application. * @param servletContext the servlet context of the web application * @see #setWebAppRootSystemProperty */ public static void removeWebAppRootSystemProperty(ServletContext servletContext) { Assert.notNull(servletContext, "ServletContext must not be null"); String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); System.getProperties().remove(key); }
public String getInitParameter(String name) { String param = initParams.get(name); if (param == null) { param = sc.getInitParameter(name); if (param == null && useServletContextParameters) { param = sc.getServletContext().getInitParameter(name); } } return param; }
/** * Return the {@link ApplicationContextInitializer} implementation classes to use * if any have been specified by {@link #CONTEXT_INITIALIZER_CLASSES_PARAM}. * @param servletContext current servlet context * @see #CONTEXT_INITIALIZER_CLASSES_PARAM */ protected List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> determineContextInitializerClasses(ServletContext servletContext) { List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> classes = new ArrayList<>(); String globalClassNames = servletContext.getInitParameter(GLOBAL_INITIALIZER_CLASSES_PARAM); if (globalClassNames != null) { for (String className : StringUtils.tokenizeToStringArray(globalClassNames, INIT_PARAM_DELIMITERS)) { classes.add(loadInitializerClass(className)); } } String localClassNames = servletContext.getInitParameter(CONTEXT_INITIALIZER_CLASSES_PARAM); if (localClassNames != null) { for (String className : StringUtils.tokenizeToStringArray(localClassNames, INIT_PARAM_DELIMITERS)) { classes.add(loadInitializerClass(className)); } } return classes; }
"Cannot set web app root system property when WAR file is not expanded"); String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM); String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY); String oldValue = System.getProperty(key);
public void contextInitialized(ServletContextEvent evt) { ServletContext ctx = evt.getServletContext(); ctx.setAttribute(DELEGATE_SERVLET, createServlet(ctx.getInitParameter(DELEGATE_SERVLET))); }
/** * Return the WebApplicationContext implementation class to use, either the * default XmlWebApplicationContext or a custom context class if specified. * @param servletContext current servlet context * @return the WebApplicationContext implementation class to use * @see #CONTEXT_CLASS_PARAM * @see org.springframework.web.context.support.XmlWebApplicationContext */ protected Class<?> determineContextClass(ServletContext servletContext) { String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM); if (contextClassName != null) { try { return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader()); } catch (ClassNotFoundException ex) { throw new ApplicationContextException( "Failed to load custom context class [" + contextClassName + "]", ex); } } else { contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName()); try { return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader()); } catch (ClassNotFoundException ex) { throw new ApplicationContextException( "Failed to load default context class [" + contextClassName + "]", ex); } } }
/** * Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext" * context-param and creates a corresponding child message source, * with the provided Spring-defined MessageSource as parent. * @param servletContext the ServletContext we're running in * (to check JSTL-related context-params in {@code web.xml}) * @param messageSource the MessageSource to expose, typically * the ApplicationContext of the current DispatcherServlet * @return the MessageSource to expose to JSTL; first checking the * JSTL-defined bundle, then the Spring-defined MessageSource * @see org.springframework.context.ApplicationContext */ public static MessageSource getJstlAwareMessageSource( @Nullable ServletContext servletContext, MessageSource messageSource) { if (servletContext != null) { String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT); if (jstlInitParam != null) { // Create a ResourceBundleMessageSource for the specified resource bundle // basename in the JSTL context-param in web.xml, wiring it with the given // Spring-defined MessageSource as parent. ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource(); jstlBundleWrapper.setBasename(jstlInitParam); jstlBundleWrapper.setParentMessageSource(messageSource); return jstlBundleWrapper; } } return messageSource; }
/** * Delegate the WebApplicationContext before it is refreshed to any * {@link ApplicationContextInitializer} instances specified by the * "contextInitializerClasses" servlet init-param. * <p>See also {@link #postProcessWebApplicationContext}, which is designed to allow * subclasses (as opposed to end-users) to modify the application context, and is * called immediately before this method. * @param wac the configured WebApplicationContext (not refreshed yet) * @see #createWebApplicationContext * @see #postProcessWebApplicationContext * @see ConfigurableApplicationContext#refresh() */ protected void applyInitializers(ConfigurableApplicationContext wac) { String globalClassNames = getServletContext().getInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM); if (globalClassNames != null) { for (String className : StringUtils.tokenizeToStringArray(globalClassNames, INIT_PARAM_DELIMITERS)) { this.contextInitializers.add(loadInitializer(className, wac)); } } if (this.contextInitializerClasses != null) { for (String className : StringUtils.tokenizeToStringArray(this.contextInitializerClasses, INIT_PARAM_DELIMITERS)) { this.contextInitializers.add(loadInitializer(className, wac)); } } AnnotationAwareOrderComparator.sort(this.contextInitializers); for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) { initializer.initialize(wac); } }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { 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 String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // 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(sc, null); } customizeContext(sc, wac); wac.refresh(); }