001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.context.support;
018
019import groovy.lang.GroovyObject;
020import groovy.lang.GroovySystem;
021import groovy.lang.MetaClass;
022
023import org.springframework.beans.BeanWrapper;
024import org.springframework.beans.BeanWrapperImpl;
025import org.springframework.beans.factory.NoSuchBeanDefinitionException;
026import org.springframework.beans.factory.config.BeanDefinition;
027import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
028import org.springframework.core.env.ConfigurableEnvironment;
029import org.springframework.core.io.ClassPathResource;
030import org.springframework.core.io.Resource;
031import org.springframework.lang.Nullable;
032
033/**
034 * An {@link org.springframework.context.ApplicationContext} implementation that extends
035 * {@link GenericApplicationContext} and implements {@link GroovyObject} such that beans
036 * can be retrieved with the dot de-reference syntax instead of using {@link #getBean}.
037 *
038 * <p>Consider this as the equivalent of {@link GenericXmlApplicationContext} for
039 * Groovy bean definitions, or even an upgrade thereof since it seamlessly understands
040 * XML bean definition files as well. The main difference is that, within a Groovy
041 * script, the context can be used with an inline bean definition closure as follows:
042 *
043 * <pre class="code">
044 * import org.hibernate.SessionFactory
045 * import org.apache.commons.dbcp.BasicDataSource
046 *
047 * def context = new GenericGroovyApplicationContext()
048 * context.reader.beans {
049 *     dataSource(BasicDataSource) {                  // <--- invokeMethod
050 *         driverClassName = "org.hsqldb.jdbcDriver"
051 *         url = "jdbc:hsqldb:mem:grailsDB"
052 *         username = "sa"                            // <-- setProperty
053 *         password = ""
054 *         settings = [mynew:"setting"]
055 *     }
056 *     sessionFactory(SessionFactory) {
057 *         dataSource = dataSource                    // <-- getProperty for retrieving references
058 *     }
059 *     myService(MyService) {
060 *         nestedBean = { AnotherBean bean ->         // <-- setProperty with closure for nested bean
061 *             dataSource = dataSource
062 *         }
063 *     }
064 * }
065 * context.refresh()
066 * </pre>
067 *
068 * <p>Alternatively, load a Groovy bean definition script like the following
069 * from an external resource (e.g. an "applicationContext.groovy" file):
070 *
071 * <pre class="code">
072 * import org.hibernate.SessionFactory
073 * import org.apache.commons.dbcp.BasicDataSource
074 *
075 * beans {
076 *     dataSource(BasicDataSource) {
077 *         driverClassName = "org.hsqldb.jdbcDriver"
078 *         url = "jdbc:hsqldb:mem:grailsDB"
079 *         username = "sa"
080 *         password = ""
081 *         settings = [mynew:"setting"]
082 *     }
083 *     sessionFactory(SessionFactory) {
084 *         dataSource = dataSource
085 *     }
086 *     myService(MyService) {
087 *         nestedBean = { AnotherBean bean ->
088 *             dataSource = dataSource
089 *         }
090 *     }
091 * }
092 * </pre>
093 *
094 * <p>With the following Java code creating the {@code GenericGroovyApplicationContext}
095 * (potentially using Ant-style '*'/'**' location patterns):
096 *
097 * <pre class="code">
098 * GenericGroovyApplicationContext context = new GenericGroovyApplicationContext();
099 * context.load("org/myapp/applicationContext.groovy");
100 * context.refresh();
101 * </pre>
102 *
103 * <p>Or even more concise, provided that no extra configuration is needed:
104 *
105 * <pre class="code">
106 * ApplicationContext context = new GenericGroovyApplicationContext("org/myapp/applicationContext.groovy");
107 * </pre>
108 *
109 * <p><b>This application context also understands XML bean definition files,
110 * allowing for seamless mixing and matching with Groovy bean definition files.</b>
111 * ".xml" files will be parsed as XML content; all other kinds of resources will
112 * be parsed as Groovy scripts.
113 *
114 * @author Juergen Hoeller
115 * @author Jeff Brown
116 * @since 4.0
117 * @see org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
118 */
119public class GenericGroovyApplicationContext extends GenericApplicationContext implements GroovyObject {
120
121        private final GroovyBeanDefinitionReader reader = new GroovyBeanDefinitionReader(this);
122
123        private final BeanWrapper contextWrapper = new BeanWrapperImpl(this);
124
125        private MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(getClass());
126
127
128        /**
129         * Create a new GenericGroovyApplicationContext that needs to be
130         * {@link #load loaded} and then manually {@link #refresh refreshed}.
131         */
132        public GenericGroovyApplicationContext() {
133        }
134
135        /**
136         * Create a new GenericGroovyApplicationContext, loading bean definitions
137         * from the given resources and automatically refreshing the context.
138         * @param resources the resources to load from
139         */
140        public GenericGroovyApplicationContext(Resource... resources) {
141                load(resources);
142                refresh();
143        }
144
145        /**
146         * Create a new GenericGroovyApplicationContext, loading bean definitions
147         * from the given resource locations and automatically refreshing the context.
148         * @param resourceLocations the resources to load from
149         */
150        public GenericGroovyApplicationContext(String... resourceLocations) {
151                load(resourceLocations);
152                refresh();
153        }
154
155        /**
156         * Create a new GenericGroovyApplicationContext, loading bean definitions
157         * from the given resource locations and automatically refreshing the context.
158         * @param relativeClass class whose package will be used as a prefix when
159         * loading each specified resource name
160         * @param resourceNames relatively-qualified names of resources to load
161         */
162        public GenericGroovyApplicationContext(Class<?> relativeClass, String... resourceNames) {
163                load(relativeClass, resourceNames);
164                refresh();
165        }
166
167
168        /**
169         * Exposes the underlying {@link GroovyBeanDefinitionReader} for convenient access
170         * to the {@code loadBeanDefinition} methods on it as well as the ability
171         * to specify an inline Groovy bean definition closure.
172         * @see GroovyBeanDefinitionReader#loadBeanDefinitions(org.springframework.core.io.Resource...)
173         * @see GroovyBeanDefinitionReader#loadBeanDefinitions(String...)
174         */
175        public final GroovyBeanDefinitionReader getReader() {
176                return this.reader;
177        }
178
179        /**
180         * Delegates the given environment to underlying {@link GroovyBeanDefinitionReader}.
181         * Should be called before any call to {@code #load}.
182         */
183        @Override
184        public void setEnvironment(ConfigurableEnvironment environment) {
185                super.setEnvironment(environment);
186                this.reader.setEnvironment(getEnvironment());
187        }
188
189        /**
190         * Load bean definitions from the given Groovy scripts or XML files.
191         * <p>Note that ".xml" files will be parsed as XML content; all other kinds
192         * of resources will be parsed as Groovy scripts.
193         * @param resources one or more resources to load from
194         */
195        public void load(Resource... resources) {
196                this.reader.loadBeanDefinitions(resources);
197        }
198
199        /**
200         * Load bean definitions from the given Groovy scripts or XML files.
201         * <p>Note that ".xml" files will be parsed as XML content; all other kinds
202         * of resources will be parsed as Groovy scripts.
203         * @param resourceLocations one or more resource locations to load from
204         */
205        public void load(String... resourceLocations) {
206                this.reader.loadBeanDefinitions(resourceLocations);
207        }
208
209        /**
210         * Load bean definitions from the given Groovy scripts or XML files.
211         * <p>Note that ".xml" files will be parsed as XML content; all other kinds
212         * of resources will be parsed as Groovy scripts.
213         * @param relativeClass class whose package will be used as a prefix when
214         * loading each specified resource name
215         * @param resourceNames relatively-qualified names of resources to load
216         */
217        public void load(Class<?> relativeClass, String... resourceNames) {
218                Resource[] resources = new Resource[resourceNames.length];
219                for (int i = 0; i < resourceNames.length; i++) {
220                        resources[i] = new ClassPathResource(resourceNames[i], relativeClass);
221                }
222                load(resources);
223        }
224
225
226        // Implementation of the GroovyObject interface
227
228        @Override
229        public void setMetaClass(MetaClass metaClass) {
230                this.metaClass = metaClass;
231        }
232
233        @Override
234        public MetaClass getMetaClass() {
235                return this.metaClass;
236        }
237
238        @Override
239        public Object invokeMethod(String name, Object args) {
240                return this.metaClass.invokeMethod(this, name, args);
241        }
242
243        @Override
244        public void setProperty(String property, Object newValue) {
245                if (newValue instanceof BeanDefinition) {
246                        registerBeanDefinition(property, (BeanDefinition) newValue);
247                }
248                else {
249                        this.metaClass.setProperty(this, property, newValue);
250                }
251        }
252
253        @Override
254        @Nullable
255        public Object getProperty(String property) {
256                if (containsBean(property)) {
257                        return getBean(property);
258                }
259                else if (this.contextWrapper.isReadableProperty(property)) {
260                        return this.contextWrapper.getPropertyValue(property);
261                }
262                throw new NoSuchBeanDefinitionException(property);
263        }
264
265}