|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2002-2014 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 java.io.IOException; | 
|  | 20 | + | 
|  | 21 | +import groovy.lang.GroovyObject; | 
|  | 22 | +import groovy.lang.GroovySystem; | 
|  | 23 | +import groovy.lang.MetaClass; | 
|  | 24 | + | 
|  | 25 | +import org.springframework.beans.BeanWrapper; | 
|  | 26 | +import org.springframework.beans.BeanWrapperImpl; | 
|  | 27 | +import org.springframework.beans.BeansException; | 
|  | 28 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; | 
|  | 29 | +import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader; | 
|  | 30 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; | 
|  | 31 | + | 
|  | 32 | +/** | 
|  | 33 | + * {@link org.springframework.web.context.WebApplicationContext} implementation | 
|  | 34 | + * which takes its configuration from Groovy bean definition scripts, understood by | 
|  | 35 | + * an {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader}. | 
|  | 36 | + * This is essentially the equivalent of | 
|  | 37 | + * {@link org.springframework.context.support.GenericGroovyApplicationContext} | 
|  | 38 | + * for a web environment. | 
|  | 39 | + * | 
|  | 40 | + * <p>By default, the configuration will be taken from "/WEB-INF/applicationContext.groovy" | 
|  | 41 | + * for the root context, and "/WEB-INF/test-servlet.groovy" for a context with the namespace | 
|  | 42 | + * "test-servlet" (like for a DispatcherServlet instance with the servlet-name "test"). | 
|  | 43 | + * | 
|  | 44 | + * <p>The config location defaults can be overridden via the "contextConfigLocation" | 
|  | 45 | + * context-param of {@link org.springframework.web.context.ContextLoader} and servlet | 
|  | 46 | + * init-param of {@link org.springframework.web.servlet.FrameworkServlet}. Config locations | 
|  | 47 | + * can either denote concrete files like "/WEB-INF/context.groovy" or Ant-style patterns | 
|  | 48 | + * like "/WEB-INF/*-context.groovy" (see {@link org.springframework.util.PathMatcher} | 
|  | 49 | + * javadoc for pattern details). | 
|  | 50 | + * | 
|  | 51 | + * <p>Note: In case of multiple config locations, later bean definitions will | 
|  | 52 | + * override ones defined in earlier loaded files. This can be leveraged to | 
|  | 53 | + * deliberately override certain bean definitions via an extra Groovy script. | 
|  | 54 | + * | 
|  | 55 | + * <p><b>For a WebApplicationContext that reads in a different bean definition format, | 
|  | 56 | + * create an analogous subclass of {@link AbstractRefreshableWebApplicationContext}.</b> | 
|  | 57 | + * Such a context implementation can be specified as "contextClass" context-param | 
|  | 58 | + * for ContextLoader or "contextClass" init-param for FrameworkServlet. | 
|  | 59 | + * | 
|  | 60 | + * @author Juergen Hoeller | 
|  | 61 | + * @since 4.1 | 
|  | 62 | + * @see #setNamespace | 
|  | 63 | + * @see #setConfigLocations | 
|  | 64 | + * @see org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader | 
|  | 65 | + * @see org.springframework.web.context.ContextLoader#initWebApplicationContext | 
|  | 66 | + * @see org.springframework.web.servlet.FrameworkServlet#initWebApplicationContext | 
|  | 67 | + */ | 
|  | 68 | +public class GroovyWebApplicationContext extends AbstractRefreshableWebApplicationContext implements GroovyObject { | 
|  | 69 | + | 
|  | 70 | +	/** Default config location for the root context */ | 
|  | 71 | +	public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.groovy"; | 
|  | 72 | + | 
|  | 73 | +	/** Default prefix for building a config location for a namespace */ | 
|  | 74 | +	public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/"; | 
|  | 75 | + | 
|  | 76 | +	/** Default suffix for building a config location for a namespace */ | 
|  | 77 | +	public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".groovy"; | 
|  | 78 | + | 
|  | 79 | + | 
|  | 80 | +	private final BeanWrapper contextWrapper = new BeanWrapperImpl(this); | 
|  | 81 | + | 
|  | 82 | +	private MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(getClass()); | 
|  | 83 | + | 
|  | 84 | + | 
|  | 85 | +	/** | 
|  | 86 | +	 * Loads the bean definitions via an GroovyBeanDefinitionReader. | 
|  | 87 | +	 * @see org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader | 
|  | 88 | +	 * @see #initBeanDefinitionReader | 
|  | 89 | +	 * @see #loadBeanDefinitions | 
|  | 90 | +	 */ | 
|  | 91 | +	@Override | 
|  | 92 | +	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { | 
|  | 93 | +		// Create a new XmlBeanDefinitionReader for the given BeanFactory. | 
|  | 94 | +		GroovyBeanDefinitionReader beanDefinitionReader = new GroovyBeanDefinitionReader(beanFactory); | 
|  | 95 | + | 
|  | 96 | +		// Configure the bean definition reader with this context's | 
|  | 97 | +		// resource loading environment. | 
|  | 98 | +		beanDefinitionReader.setEnvironment(getEnvironment()); | 
|  | 99 | +		beanDefinitionReader.setResourceLoader(this); | 
|  | 100 | + | 
|  | 101 | +		// Allow a subclass to provide custom initialization of the reader, | 
|  | 102 | +		// then proceed with actually loading the bean definitions. | 
|  | 103 | +		initBeanDefinitionReader(beanDefinitionReader); | 
|  | 104 | +		loadBeanDefinitions(beanDefinitionReader); | 
|  | 105 | +	} | 
|  | 106 | + | 
|  | 107 | +	/** | 
|  | 108 | +	 * Initialize the bean definition reader used for loading the bean | 
|  | 109 | +	 * definitions of this context. Default implementation is empty. | 
|  | 110 | +	 * <p>Can be overridden in subclasses. | 
|  | 111 | +	 * @param beanDefinitionReader the bean definition reader used by this context | 
|  | 112 | +	 */ | 
|  | 113 | +	protected void initBeanDefinitionReader(GroovyBeanDefinitionReader beanDefinitionReader) { | 
|  | 114 | +	} | 
|  | 115 | + | 
|  | 116 | +	/** | 
|  | 117 | +	 * Load the bean definitions with the given GroovyBeanDefinitionReader. | 
|  | 118 | +	 * <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method; | 
|  | 119 | +	 * therefore this method is just supposed to load and/or register bean definitions. | 
|  | 120 | +	 * <p>Delegates to a ResourcePatternResolver for resolving location patterns | 
|  | 121 | +	 * into Resource instances. | 
|  | 122 | +	 * @throws IOException if the required Groovy script isn't found | 
|  | 123 | +	 * @see #refreshBeanFactory | 
|  | 124 | +	 * @see #getConfigLocations | 
|  | 125 | +	 * @see #getResources | 
|  | 126 | +	 * @see #getResourcePatternResolver | 
|  | 127 | +	 */ | 
|  | 128 | +	protected void loadBeanDefinitions(GroovyBeanDefinitionReader reader) throws IOException { | 
|  | 129 | +		String[] configLocations = getConfigLocations(); | 
|  | 130 | +		if (configLocations != null) { | 
|  | 131 | +			for (String configLocation : configLocations) { | 
|  | 132 | +				reader.loadBeanDefinitions(configLocation); | 
|  | 133 | +			} | 
|  | 134 | +		} | 
|  | 135 | +	} | 
|  | 136 | + | 
|  | 137 | +	/** | 
|  | 138 | +	 * The default location for the root context is "/WEB-INF/applicationContext.groovy", | 
|  | 139 | +	 * and "/WEB-INF/test-servlet.groovy" for a context with the namespace "test-servlet" | 
|  | 140 | +	 * (like for a DispatcherServlet instance with the servlet-name "test"). | 
|  | 141 | +	 */ | 
|  | 142 | +	@Override | 
|  | 143 | +	protected String[] getDefaultConfigLocations() { | 
|  | 144 | +		if (getNamespace() != null) { | 
|  | 145 | +			return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX}; | 
|  | 146 | +		} | 
|  | 147 | +		else { | 
|  | 148 | +			return new String[] {DEFAULT_CONFIG_LOCATION}; | 
|  | 149 | +		} | 
|  | 150 | +	} | 
|  | 151 | + | 
|  | 152 | + | 
|  | 153 | +	// Implementation of the GroovyObject interface | 
|  | 154 | + | 
|  | 155 | +	public void setMetaClass(MetaClass metaClass) { | 
|  | 156 | +		this.metaClass = metaClass; | 
|  | 157 | +	} | 
|  | 158 | + | 
|  | 159 | +	public MetaClass getMetaClass() { | 
|  | 160 | +		return this.metaClass; | 
|  | 161 | +	} | 
|  | 162 | + | 
|  | 163 | +	public Object invokeMethod(String name, Object args) { | 
|  | 164 | +		return this.metaClass.invokeMethod(this, name, args); | 
|  | 165 | +	} | 
|  | 166 | + | 
|  | 167 | +	public void setProperty(String property, Object newValue) { | 
|  | 168 | +		this.metaClass.setProperty(this, property, newValue); | 
|  | 169 | +	} | 
|  | 170 | + | 
|  | 171 | +	public Object getProperty(String property) { | 
|  | 172 | +		if (containsBean(property)) { | 
|  | 173 | +			return getBean(property); | 
|  | 174 | +		} | 
|  | 175 | +		else if (this.contextWrapper.isReadableProperty(property)) { | 
|  | 176 | +			return this.contextWrapper.getPropertyValue(property); | 
|  | 177 | +		} | 
|  | 178 | +		throw new NoSuchBeanDefinitionException(property); | 
|  | 179 | +	} | 
|  | 180 | + | 
|  | 181 | +} | 
0 commit comments