7676 * @since 0.1
7777 */
7878public class GroovyPagesTemplateEngine extends ResourceAwareTemplateEngine implements ApplicationContextAware , ServletContextAware , InitializingBean {
79-
79+ public static final String CONFIG_PROPERTY_DISABLE_CACHING_RESOURCES ="grails.gsp.disable.caching.resources" ;
80+ public static final String CONFIG_PROPERTY_GSP_ENABLE_RELOAD ="grails.gsp.enable.reload" ;
8081 private static final String GENERATED_GSP_NAME_PREFIX = "gsp_script_" ;
8182 private static final Log LOG = LogFactory .getLog (GroovyPagesTemplateEngine .class );
8283 private Map <String , GroovyPageMetaInfo > pageCache = new ConcurrentHashMap <String , GroovyPageMetaInfo >();
@@ -92,6 +93,8 @@ public class GroovyPagesTemplateEngine extends ResourceAwareTemplateEngine impl
9293 private TagLibraryResolver jspTagLibraryResolver ;
9394 private Map <String , String > precompiledGspMap ;
9495 private Map <String , GroovyPageMetaInfo > precompiledCache = new ConcurrentHashMap <String , GroovyPageMetaInfo >();
96+ private boolean cacheResources =true ;
97+ private boolean resourceLoaderDefined =false ;
9598
9699 private static File dumpLineNumbersTo ;
97100
@@ -149,6 +152,7 @@ public void setClassLoader(ClassLoader classLoader) {
149152 * @param resourceLoader The ResourceLoader instance
150153 */
151154 public void setResourceLoader (ResourceLoader resourceLoader ) {
155+ this .resourceLoaderDefined =(resourceLoader != null );
152156 this .resourceLoader = resourceLoader ;
153157 }
154158
@@ -184,7 +188,7 @@ public int[] calculateLineNumbersForPage(@SuppressWarnings("unused") ServletCont
184188 */
185189 @ Override
186190 public Template createTemplate (Resource resource ) {
187- return createTemplate (resource , false );
191+ return createTemplate (resource , cacheResources );
188192 }
189193
190194 /**
@@ -225,7 +229,7 @@ else if (LOG.isInfoEnabled()) {
225229 }
226230 }
227231
228- if (pageCache .containsKey (name )&& cacheable ) {
232+ if (cacheable && pageCache .containsKey (name )) {
229233 GroovyPageMetaInfo meta = pageCache .get (name );
230234
231235 if (isGroovyPageReloadable (resource , meta )) {
@@ -255,15 +259,11 @@ else if (LOG.isInfoEnabled()) {
255259 */
256260 @ Override
257261 public Template createTemplate (String uri ) {
258- Template t = createTemplateFromPrecompiled (uri );
259- if (t == null ) {
260- t = createTemplate (getResourceForUri (uri ));
261- }
262- return t ;
262+ return createTemplateForUri (uri );
263263 }
264264
265265 private boolean isPrecompiledAvailable () {
266- return precompiledGspMap != null && precompiledGspMap .size () > 0 ;
266+ return precompiledGspMap != null && precompiledGspMap .size () > 0 && ! GrailsUtil . isDevelopmentEnv () ;
267267 }
268268
269269 private GroovyPageTemplate createTemplateFromPrecompiled (String uri ) {
@@ -370,7 +370,7 @@ public Template createTemplateForUri(String[] uri) {
370370 LOG .warn ("Precompiled GSP not found for uri: " + Arrays .asList (uri ) + ". Using resource " + resource );
371371 }
372372 }
373- return createTemplate (resource );
373+ return createTemplate (resource , true );
374374 }
375375 return null ;
376376 }
@@ -549,13 +549,15 @@ private Resource getResourceForUri(String uri) {
549549
550550 private Resource getResourceWithinContext (String uri ) {
551551 Assert .state (resourceLoader != null , "TemplateEngine not initialised correctly, no [resourceLoader] specified!" );
552- if (Environment . getCurrent (). isReloadEnabled () && Metadata . getCurrent (). isWarDeployed () ) {
552+ if (resourceLoaderDefined ) {
553553 return resourceLoader .getResource (uri );
554554 }
555-
556555 Resource r = servletContextLoader .getResource (uri );
557- if (r .exists ()) return r ;
558- return resourceLoader .getResource (uri );
556+ if (r .exists ()) {
557+ return r ;
558+ } else {
559+ return resourceLoader .getResource (uri );
560+ }
559561 }
560562
561563
@@ -635,7 +637,7 @@ private Class<?> compileGroovyPage(InputStream in, String name, String pageName,
635637 // Compile the script into an object
636638 Class <?> scriptClass ;
637639 try {
638- scriptClass = groovyClassLoader .parseClass (DefaultGroovyMethods .getText (in ), name );
640+ scriptClass = groovyClassLoader .parseClass (DefaultGroovyMethods .getText (in , GroovyPageParser . GROOVY_SOURCE_CHAR_ENCODING ), name );
639641 }
640642 catch (CompilationFailedException e ) {
641643 LOG .error ("Compilation error compiling GSP [" +name +"]:" + e .getMessage (), e );
@@ -680,7 +682,7 @@ private GroovyPageMetaInfo createPageMetaInfo(GroovyPageParser parse, InputStrea
680682 pageMeta .setCodecName (parse .getDefaultCodecDirectiveValue ());
681683 pageMeta .initCodec ();
682684 // just return groovy and don't compile if asked
683- if (isReloadEnabled () || GrailsUtil .isDevelopmentEnv ()) {
685+ if (GrailsUtil .isDevelopmentEnv ()) {
684686 pageMeta .setGroovySource (in );
685687 }
686688
@@ -818,4 +820,12 @@ public Map<String, String> getPrecompiledGspMap() {
818820 public void setPrecompiledGspMap (Map <String , String > precompiledGspMap ) {
819821 this .precompiledGspMap = precompiledGspMap ;
820822 }
823+
824+ public boolean isCacheResources () {
825+ return cacheResources ;
826+ }
827+
828+ public void setCacheResources (boolean cacheResources ) {
829+ this .cacheResources = cacheResources ;
830+ }
821831}
0 commit comments