GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / commons / spring / GrailsWebApplicationContext.java
blob2046ecf96967016e1ad203b03e9a7edd35939ef1
1 /*
2 * Copyright 2004-2005 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 package org.codehaus.groovy.grails.commons.spring;
18 import groovy.lang.GroovyObject;
19 import org.codehaus.groovy.grails.commons.GrailsApplication;
20 import org.springframework.beans.BeansException;
21 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
22 import org.springframework.context.ApplicationContext;
23 import org.springframework.core.io.Resource;
24 import org.springframework.core.io.support.ResourcePatternResolver;
25 import org.springframework.ui.context.ThemeSource;
26 import org.springframework.web.context.ConfigurableWebApplicationContext;
27 import org.springframework.web.context.ServletContextAware;
28 import org.springframework.web.context.request.RequestScope;
29 import org.springframework.web.context.request.SessionScope;
30 import org.springframework.web.context.support.ServletContextAwareProcessor;
31 import org.springframework.web.context.support.ServletContextResource;
32 import org.springframework.web.context.support.ServletContextResourcePatternResolver;
33 import org.springframework.webflow.config.scope.ScopeRegistrar;
35 import javax.servlet.ServletConfig;
36 import javax.servlet.ServletContext;
38 /**
39 * A WebApplicationContext that extends StaticApplicationContext to allow for programmatic
40 * configuration at runtime. The code is adapted from StaticWebApplicationContext.
42 * @author Graeme
43 * @since 0.3
46 public class GrailsWebApplicationContext extends GrailsApplicationContext
47 implements ConfigurableWebApplicationContext, GroovyObject, ThemeSource {
49 private ServletContext servletContext;
50 private String namespace;
51 private ServletConfig servletConfig;
53 public GrailsWebApplicationContext() throws BeansException {
54 super();
57 public GrailsWebApplicationContext(ApplicationContext parent) throws BeansException {
58 super(parent);
61 public ClassLoader getClassLoader() {
62 ApplicationContext parent = getParent();
63 if(parent != null && parent.containsBean(GrailsApplication.APPLICATION_ID)) {
64 final GrailsApplication application = (GrailsApplication) parent.getBean(GrailsApplication.APPLICATION_ID);
65 return application.getClassLoader();
67 else
68 return super.getClassLoader();
72 /**
73 * Set the ServletContext that this WebApplicationContext runs in.
75 public void setServletContext(ServletContext servletContext) {
76 this.servletContext = servletContext;
79 public ServletContext getServletContext() {
80 return servletContext;
83 public void setNamespace(String namespace) {
84 this.namespace = namespace;
85 if (namespace != null) {
86 setDisplayName("WebApplicationContext for namespace '" + namespace + "'");
90 public String getNamespace() {
91 return namespace;
94 public void setConfigLocation(String s) {
97 public void setConfigLocations(String[] configLocations) {
100 public String[] getConfigLocations() {
101 return new String[0];
106 * Register ServletContextAwareProcessor.
107 * @see ServletContextAwareProcessor
109 protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
110 beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
111 beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false));
112 beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true));
113 new ScopeRegistrar().postProcessBeanFactory(beanFactory);
115 beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext));
116 beanFactory.ignoreDependencyInterface(ServletContextAware.class);
120 * This implementation supports file paths beneath the root of the ServletContext.
121 * @see ServletContextResource
123 protected Resource getResourceByPath(String path) {
124 return new ServletContextResource(this.servletContext, path);
128 * This implementation supports pattern matching in unexpanded WARs too.
129 * @see ServletContextResourcePatternResolver
131 protected ResourcePatternResolver getResourcePatternResolver() {
132 return new ServletContextResourcePatternResolver(this);
135 public void refresh() throws BeansException, IllegalStateException {
136 super.refresh();
139 public void setServletConfig(ServletConfig servletConfig) {
140 this.servletConfig = servletConfig;
143 public ServletConfig getServletConfig() {
144 return this.servletConfig;