Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » web » context » support » [javadoc | source]
    1   /*
    2    * Copyright 2002-2008 the original author or authors.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   
   17   package org.springframework.web.context.support;
   18   
   19   import javax.servlet.ServletConfig;
   20   import javax.servlet.ServletContext;
   21   
   22   import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
   23   import org.springframework.context.support.StaticApplicationContext;
   24   import org.springframework.core.io.Resource;
   25   import org.springframework.core.io.support.ResourcePatternResolver;
   26   import org.springframework.ui.context.Theme;
   27   import org.springframework.ui.context.ThemeSource;
   28   import org.springframework.ui.context.support.UiApplicationContextUtils;
   29   import org.springframework.web.context.ConfigurableWebApplicationContext;
   30   import org.springframework.web.context.ServletConfigAware;
   31   import org.springframework.web.context.ServletContextAware;
   32   import org.springframework.web.context.request.RequestScope;
   33   import org.springframework.web.context.request.SessionScope;
   34   
   35   /**
   36    * Static {@link org.springframework.web.context.WebApplicationContext}
   37    * implementation for testing. Not intended for use in production applications.
   38    *
   39    * <p>Implements the {@link org.springframework.web.context.ConfigurableWebApplicationContext}
   40    * interface to allow for direct replacement of an {@link XmlWebApplicationContext},
   41    * despite not actually supporting external configuration files.
   42    *
   43    * <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
   44    * the web application root. Absolute paths, e.g. for files outside the web app root,
   45    * can be accessed via "file:" URLs, as implemented by
   46    * {@link org.springframework.core.io.DefaultResourceLoader}.
   47    *
   48    * <p>In addition to the special beans detected by
   49    * {@link org.springframework.context.support.AbstractApplicationContext},
   50    * this class detects a bean of type {@link org.springframework.ui.context.ThemeSource}
   51    * in the context, under the special bean name "themeSource".
   52    *
   53    * @author Rod Johnson
   54    * @author Juergen Hoeller
   55    * @see org.springframework.ui.context.ThemeSource
   56    */
   57   public class StaticWebApplicationContext extends StaticApplicationContext
   58   		implements ConfigurableWebApplicationContext, ThemeSource {
   59   
   60   	private ServletContext servletContext;
   61   
   62   	private ServletConfig servletConfig;
   63   
   64   	private String namespace;
   65   
   66   	private ThemeSource themeSource;
   67   
   68   
   69   	public StaticWebApplicationContext() {
   70   		setDisplayName("Root WebApplicationContext");
   71   	}
   72   
   73   
   74   	/**
   75   	 * Set the ServletContext that this WebApplicationContext runs in.
   76   	 */
   77   	public void setServletContext(ServletContext servletContext) {
   78   		this.servletContext = servletContext;
   79   	}
   80   
   81   	public ServletContext getServletContext() {
   82   		return this.servletContext;
   83   	}
   84   
   85   	public void setServletConfig(ServletConfig servletConfig) {
   86   		this.servletConfig = servletConfig;
   87   		if (servletConfig != null && this.servletContext == null) {
   88   			this.servletContext = servletConfig.getServletContext();
   89   		}
   90   	}
   91   
   92   	public ServletConfig getServletConfig() {
   93   		return this.servletConfig;
   94   	}
   95   
   96   	public void setNamespace(String namespace) {
   97   		this.namespace = namespace;
   98   		if (namespace != null) {
   99   			setDisplayName("WebApplicationContext for namespace '" + namespace + "'");
  100   		}
  101   	}
  102   
  103   	public String getNamespace() {
  104   		return this.namespace;
  105   	}
  106   
  107   	/**
  108   	 * The {@link StaticWebApplicationContext} class does not support this method.
  109   	 * @throws UnsupportedOperationException <b>always</b>
  110   	 */
  111   	public void setConfigLocation(String configLocation) {
  112   		if (configLocation != null) {
  113   			throw new UnsupportedOperationException("StaticWebApplicationContext does not support config locations");
  114   		}
  115   	}
  116   
  117   	/**
  118   	 * The {@link StaticWebApplicationContext} class does not support this method.
  119   	 * @throws UnsupportedOperationException <b>always</b>
  120   	 */
  121   	public void setConfigLocations(String[] configLocations) {
  122   		if (configLocations != null) {
  123   			throw new UnsupportedOperationException("StaticWebApplicationContext does not support config locations");
  124   		}
  125   	}
  126   
  127   	public String[] getConfigLocations() {
  128   		return null;
  129   	}
  130   
  131   
  132   	/**
  133   	 * Register request/session scopes, a {@link ServletContextAwareProcessor}, etc.
  134   	 */
  135   	protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  136   		beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
  137   		beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false));
  138   		beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true));
  139   
  140   		beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
  141   		beanFactory.ignoreDependencyInterface(ServletContextAware.class);
  142   		beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
  143   	}
  144   
  145   	/**
  146   	 * This implementation supports file paths beneath the root of the ServletContext.
  147   	 * @see ServletContextResource
  148   	 */
  149   	protected Resource getResourceByPath(String path) {
  150   		return new ServletContextResource(this.servletContext, path);
  151   	}
  152   
  153   	/**
  154   	 * This implementation supports pattern matching in unexpanded WARs too.
  155   	 * @see ServletContextResourcePatternResolver
  156   	 */
  157   	protected ResourcePatternResolver getResourcePatternResolver() {
  158   		return new ServletContextResourcePatternResolver(this);
  159   	}
  160   
  161   	/**
  162   	 * Initialize the theme capability.
  163   	 */
  164   	protected void onRefresh() {
  165   		this.themeSource = UiApplicationContextUtils.initThemeSource(this);
  166   	}
  167   
  168   	public Theme getTheme(String themeName) {
  169   		return this.themeSource.getTheme(themeName);
  170   	}
  171   
  172   }

Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » web » context » support » [javadoc | source]