Skip to content

Commit 3f121de

Browse files
committed
fix for GRAILS-9490 "Cannot use Java String with '${}' in Config.groovy"
1 parent 354200c commit 3f121de

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

grails-core/src/main/groovy/grails/util/Holders.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import javax.servlet.ServletContext;
2323

24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
2426
import org.codehaus.groovy.grails.commons.GrailsApplication;
2527
import org.codehaus.groovy.grails.compiler.support.GrailsResourceLoader;
2628
import org.codehaus.groovy.grails.plugins.GrailsPluginManager;
@@ -34,6 +36,7 @@
3436
*/
3537
public class Holders {
3638

39+
private static final Log LOG = LogFactory.getLog(Holders.class);
3740
private static final String APPLICATION_BEAN_NAME = "grailsApplication";
3841

3942
private static Holder<GrailsResourceLoader> resourceLoaders = new Holder<GrailsResourceLoader>("ResourceLoader");
@@ -175,15 +178,15 @@ private static void createServletContextsHolder() {
175178
}
176179
catch (ClassNotFoundException e) {
177180
// shouldn't happen
178-
throw new RuntimeException(e);
181+
LOG.error("Error initializing servlet context holder", e);
179182
}
180183
catch (InstantiationException e) {
181184
// shouldn't happen
182-
throw new RuntimeException(e);
185+
LOG.error("Error initializing servlet context holder", e);
183186
}
184187
catch (IllegalAccessException e) {
185188
// shouldn't happen
186-
throw new RuntimeException(e);
189+
LOG.error("Error initializing servlet context holder", e);
187190
}
188191
}
189192
}

grails-core/src/main/groovy/org/codehaus/groovy/grails/commons/cfg/GrailsConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*/
3434
public class GrailsConfig {
3535

36+
public static final String SPRING_PLACEHOLDER_PREFIX = "grails.spring.placeholder.prefix";
3637
private static final Log LOG = LogFactory.getLog(GrailsConfig.class);
3738

3839
private GrailsApplication grailsApplication;

grails-core/src/main/groovy/org/codehaus/groovy/grails/commons/cfg/GrailsPlaceholderConfigurer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@
2121

2222
import org.codehaus.groovy.grails.commons.GrailsApplication;
2323
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
24+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
2425

2526
/**
2627
* Uses Grails' ConfigObject for place holder values.
2728
*
2829
* @author Graeme Rocher
2930
* @since 1.0
3031
*/
31-
public class GrailsPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
32+
public class GrailsPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {
3233

3334
private GrailsApplication grailsApplication;
3435

3536
public GrailsPlaceholderConfigurer(GrailsApplication grailsApplication) {
3637
this.grailsApplication = grailsApplication;
38+
GrailsConfig config = new GrailsConfig(grailsApplication);
39+
setPlaceholderPrefix(config.get(GrailsConfig.SPRING_PLACEHOLDER_PREFIX, "${"));
40+
setIgnoreUnresolvablePlaceholders(true);
3741
}
3842

3943
@Override
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.codehaus.groovy.grails.commons.cfg
2+
3+
import grails.spring.BeanBuilder
4+
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
5+
import spock.lang.Issue
6+
import spock.lang.Specification
7+
8+
/**
9+
* @author Graeme Rocher
10+
*/
11+
class GrailsPlaceholderConfigurerSpec extends Specification {
12+
13+
void "Test that property placeholder configuration works for simple properties"() {
14+
when:"A bean is defined with a placeholder"
15+
def application = new DefaultGrailsApplication()
16+
application.config.foo.bar="test"
17+
def bb = new BeanBuilder()
18+
bb.beans {
19+
addBeanFactoryPostProcessor(new GrailsPlaceholderConfigurer(application))
20+
testBean(TestBean) {
21+
name = '${foo.bar}'
22+
}
23+
}
24+
def applicationContext = bb.createApplicationContext()
25+
def bean = applicationContext.getBean(TestBean)
26+
then:"The placeholder is replaced"
27+
bean.name == "test"
28+
29+
}
30+
31+
@Issue('GRAILS-9490')
32+
void "Test that property placeholder configuration doesn't throw an error if invalid placeholders are configured"() {
33+
when:"A bean is defined with a placeholder"
34+
def application = new DefaultGrailsApplication()
35+
application.config.bar.foo="test"
36+
application.config.more.stuff='another ${place.holder}'
37+
def bb = new BeanBuilder()
38+
bb.beans {
39+
addBeanFactoryPostProcessor(new GrailsPlaceholderConfigurer(application))
40+
testBean(TestBean) {
41+
name = '${foo.bar}'
42+
}
43+
}
44+
def applicationContext = bb.createApplicationContext()
45+
def bean = applicationContext.getBean(TestBean)
46+
then:"The placeholder is replaced"
47+
bean.name == '${foo.bar}'
48+
49+
}
50+
51+
void "Test that property placeholder configuration works for simple properties with a custom placeholder prefix"() {
52+
when:"A bean is defined with a placeholder"
53+
def application = new DefaultGrailsApplication()
54+
application.config.foo.bar="test"
55+
application.config.grails.spring.placeholder.prefix='£{'
56+
application.setConfig(application.config)
57+
def bb = new BeanBuilder()
58+
bb.beans {
59+
addBeanFactoryPostProcessor(new GrailsPlaceholderConfigurer(application))
60+
testBean(TestBean) {
61+
name = '£{foo.bar}'
62+
}
63+
}
64+
def applicationContext = bb.createApplicationContext()
65+
def bean = applicationContext.getBean(TestBean)
66+
then:"The placeholder is replaced"
67+
bean.name == "test"
68+
69+
}
70+
}
71+
class TestBean {
72+
String name
73+
}

0 commit comments

Comments
 (0)