|
| 1 | +/* Copyright 2004-2005 the original author or authors. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package org.codehaus.groovy.grails.web.context; |
| 16 | + |
| 17 | +import org.apache.commons.logging.Log; |
| 18 | +import org.apache.commons.logging.LogFactory; |
| 19 | +import org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator; |
| 20 | +import org.codehaus.groovy.grails.commons.*; |
| 21 | +import org.codehaus.groovy.grails.plugins.GrailsPluginManager; |
| 22 | +import org.codehaus.groovy.grails.support.PersistenceContextInterceptor; |
| 23 | +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; |
| 24 | +import org.springframework.web.context.WebApplicationContext; |
| 25 | + |
| 26 | +import javax.servlet.ServletContext; |
| 27 | + |
| 28 | +/** |
| 29 | + * A common class where shared configurational methods can reside |
| 30 | + * |
| 31 | + * @author Graeme Rocher |
| 32 | + * @since 22-Feb-2006 |
| 33 | + */ |
| 34 | +public class GrailsConfigUtils { |
| 35 | + |
| 36 | + private static final Log LOG = LogFactory.getLog(GrailsConfigUtils.class); |
| 37 | + |
| 38 | + /** |
| 39 | + * Executes Grails bootstrap classes |
| 40 | + * |
| 41 | + * @param application The Grails ApplicationContext instance |
| 42 | + * @param webContext The WebApplicationContext instance |
| 43 | + * @param servletContext The ServletContext instance |
| 44 | + */ |
| 45 | + public static void executeGrailsBootstraps(GrailsApplication application, WebApplicationContext webContext, |
| 46 | + ServletContext servletContext) { |
| 47 | + |
| 48 | + PersistenceContextInterceptor interceptor = null; |
| 49 | + String[] beanNames = webContext.getBeanNamesForType(PersistenceContextInterceptor.class); |
| 50 | + if(beanNames.length > 0) { |
| 51 | + interceptor = (PersistenceContextInterceptor)webContext.getBean(beanNames[0]); |
| 52 | + } |
| 53 | + |
| 54 | + if(interceptor != null) { |
| 55 | + interceptor.init(); |
| 56 | + // init the Grails application |
| 57 | + try { |
| 58 | + GrailsClass[] bootstraps = application.getArtefacts(BootstrapArtefactHandler.TYPE); |
| 59 | + for (int i = 0; i < bootstraps.length; i++) { |
| 60 | + final GrailsBootstrapClass bootstrapClass = (GrailsBootstrapClass) bootstraps[i]; |
| 61 | + final Object instance = bootstrapClass.getReference().getWrappedInstance(); |
| 62 | + webContext.getAutowireCapableBeanFactory() |
| 63 | + .autowireBeanProperties(instance, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); |
| 64 | + bootstrapClass.callInit( servletContext ); |
| 65 | + } |
| 66 | + interceptor.flush(); |
| 67 | + } |
| 68 | + finally { |
| 69 | + interceptor.destroy(); |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static WebApplicationContext configureWebApplicationContext(ServletContext servletContext, WebApplicationContext parent) { |
| 76 | + GrailsApplication application = (GrailsApplication)parent.getBean(GrailsApplication.APPLICATION_ID); |
| 77 | + |
| 78 | + if(LOG.isDebugEnabled()) { |
| 79 | + LOG.debug("[GrailsContextLoader] Configurating Grails Application"); |
| 80 | + } |
| 81 | + |
| 82 | + if(application.getParentContext() == null) { |
| 83 | + application.setApplicationContext(parent); |
| 84 | + } |
| 85 | + |
| 86 | + GrailsRuntimeConfigurator configurator; |
| 87 | + if(parent.containsBean(GrailsRuntimeConfigurator.BEAN_ID)) { |
| 88 | + configurator = (GrailsRuntimeConfigurator)parent.getBean(GrailsRuntimeConfigurator.BEAN_ID); |
| 89 | + } |
| 90 | + else { |
| 91 | + configurator = new GrailsRuntimeConfigurator(application,parent); |
| 92 | + if(parent.containsBean(GrailsPluginManager.BEAN_NAME)) { |
| 93 | + GrailsPluginManager pluginManager = (GrailsPluginManager)parent.getBean(GrailsPluginManager.BEAN_NAME); |
| 94 | + configurator.setPluginManager(pluginManager); |
| 95 | + } |
| 96 | + } |
| 97 | + servletContext.setAttribute(ApplicationAttributes.PLUGIN_MANAGER, configurator.getPluginManager()); |
| 98 | + // use config file locations if available |
| 99 | + servletContext.setAttribute(ApplicationAttributes.PARENT_APPLICATION_CONTEXT,parent); |
| 100 | + servletContext.setAttribute(GrailsApplication.APPLICATION_ID,application); |
| 101 | + |
| 102 | + ServletContextHolder.setServletContext(servletContext); |
| 103 | + |
| 104 | + |
| 105 | + // return a context that obeys grails' settings |
| 106 | + WebApplicationContext webContext = configurator.configure( servletContext ); |
| 107 | + configurator.getPluginManager().setApplicationContext(webContext); |
| 108 | + servletContext.setAttribute(ApplicationAttributes.APPLICATION_CONTEXT,webContext ); |
| 109 | + servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext); |
| 110 | + LOG.info("[GrailsContextLoader] Grails application loaded."); |
| 111 | + return webContext; |
| 112 | + } |
| 113 | +} |
0 commit comments