Skip to content

Commit 94d8c8a

Browse files
author
graeme
committed
fix for GRAILS-2188
git-svn-id: https://svn.codehaus.org/grails/trunk@6485 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent f3b72cd commit 94d8c8a

File tree

5 files changed

+167
-5
lines changed

5 files changed

+167
-5
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

src/web/org/codehaus/groovy/grails/web/context/GrailsContextLoader.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import groovy.lang.ExpandoMetaClass;
1818
import org.apache.commons.logging.Log;
1919
import org.apache.commons.logging.LogFactory;
20-
import org.codehaus.groovy.grails.commons.GrailsConfigUtils;
2120
import org.codehaus.groovy.grails.commons.GrailsApplication;
2221
import org.springframework.beans.BeansException;
2322
import org.springframework.context.ApplicationContext;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright 2004-2005 Graeme Rocher
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 javax.servlet.ServletContext;
18+
19+
/**
20+
*
21+
* Holds a reference to the ServletContext
22+
*
23+
* @author Graeme Rocher
24+
* @since 1.0
25+
* <p/>
26+
* Created: Jan 16, 2008
27+
*/
28+
public class ServletContextHolder {
29+
30+
public static synchronized ServletContext getServletContext() {
31+
return servletContext;
32+
}
33+
34+
public static synchronized void setServletContext(ServletContext servletContext) {
35+
ServletContextHolder.servletContext = servletContext;
36+
}
37+
38+
private static ServletContext servletContext;
39+
}

src/web/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@
2020
import org.apache.commons.logging.LogFactory;
2121
import org.codehaus.groovy.grails.commons.GrailsControllerClass;
2222
import org.codehaus.groovy.grails.validation.ConstrainedProperty;
23+
import org.codehaus.groovy.grails.web.context.ServletContextHolder;
2324
import org.codehaus.groovy.grails.web.mapping.exceptions.UrlMappingException;
2425
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
2526
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException;
2627
import org.springframework.validation.Errors;
2728
import org.springframework.validation.MapBindingResult;
2829
import org.springframework.web.context.request.RequestContextHolder;
2930

31+
import javax.servlet.ServletContext;
32+
import java.io.UnsupportedEncodingException;
33+
import java.net.URLEncoder;
3034
import java.util.*;
3135
import java.util.regex.Matcher;
3236
import java.util.regex.Pattern;
3337
import java.util.regex.PatternSyntaxException;
34-
import java.net.URLEncoder;
35-
import java.io.UnsupportedEncodingException;
3638

3739
/**
3840
* <p>A UrlMapping implementation that takes a Grails URL pattern and turns it into a regex matcher so that
@@ -166,9 +168,17 @@ public UrlMappingInfo match(String uri) {
166168
* @see org.codehaus.groovy.grails.web.mapping.UrlMapping
167169
*/
168170
public String createURL(Map parameterValues, String encoding) {
169-
GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.currentRequestAttributes();
171+
ServletContext servletContext = ServletContextHolder.getServletContext();
172+
String contextPath;
173+
if(servletContext != null) {
174+
contextPath = servletContext.getContextPath();
175+
}
176+
else {
177+
GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.currentRequestAttributes();
178+
contextPath = webRequest.getAttributes().getApplicationUri(webRequest.getCurrentRequest());
179+
}
170180
if(parameterValues==null)parameterValues=Collections.EMPTY_MAP;
171-
StringBuffer uri = new StringBuffer(webRequest.getAttributes().getApplicationUri(webRequest.getCurrentRequest()));
181+
StringBuffer uri = new StringBuffer(contextPath);
172182
Set usedParams = new HashSet();
173183

174184
String[] tokens = urlData.getTokens();

src/web/org/codehaus/groovy/grails/web/servlet/GrailsDispatcherServlet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.codehaus.groovy.grails.commons.spring.GrailsApplicationContext;
2121
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
2222
import org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController;
23+
import org.codehaus.groovy.grails.web.context.GrailsConfigUtils;
2324
import org.springframework.beans.BeansException;
2425
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2526
import org.springframework.context.i18n.LocaleContext;

0 commit comments

Comments
 (0)