forked from grails/grails-gsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroovyPagesTemplateEngine.java
805 lines (712 loc) · 30.7 KB
/
GroovyPagesTemplateEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.gsp;
import grails.config.Config;
import grails.config.Settings;
import grails.core.GrailsApplication;
import grails.core.GrailsClass;
import grails.io.IOUtils;
import grails.util.CacheEntry;
import grails.util.Environment;
import grails.util.GrailsUtil;
import groovy.lang.GroovyClassLoader;
import groovy.text.Template;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.runtime.IOGroovyMethods;
import org.grails.core.artefact.DomainClassArtefactHandler;
import org.grails.core.exceptions.DefaultErrorsPrinter;
import org.grails.exceptions.ExceptionUtils;
import org.grails.gsp.compiler.GroovyPageParser;
import org.grails.gsp.io.*;
import org.grails.gsp.jsp.TagLibraryResolver;
import org.grails.taglib.TagLibraryLookup;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.*;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.util.Assert;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.PrivilegedAction;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Based on (but not extending) the existing TemplateEngine implementations
* within Groovy. It allows GSP pages to be re-used in different context using code like the below:
*
* <code>
* Template t = new GroovyPagesTemplateEngine()
* .createTemplate(context,request,response);
* t.make()
* .writeTo(out);
* </code>
*
* @author Graeme Rocher
* @author Lari Hotari
*
* @since 0.1
*/
public class GroovyPagesTemplateEngine extends ResourceAwareTemplateEngine implements ResourceLoaderAware, ApplicationContextAware, InitializingBean, BeanClassLoaderAware {
public static final String CONFIG_PROPERTY_DISABLE_CACHING_RESOURCES = Settings.GSP_DISABLE_CACHING_RESOURCES;
public static final String CONFIG_PROPERTY_GSP_ENABLE_RELOAD = Settings.GSP_ENABLE_RELOAD;
public static final String BEAN_ID = ResourceAwareTemplateEngine.BEAN_ID;
private static final String GENERATED_GSP_NAME_PREFIX = "gsp_script_";
private static final Log LOG = LogFactory.getLog(GroovyPagesTemplateEngine.class);
private static File dumpLineNumbersTo;
private ConcurrentMap<String, CacheEntry<GroovyPageMetaInfo>> pageCache = new ConcurrentHashMap<String, CacheEntry<GroovyPageMetaInfo>>();
private ClassLoader classLoader;
private AtomicInteger scriptNameCount=new AtomicInteger(0);
private GroovyPageLocator groovyPageLocator = new DefaultGroovyPageLocator();
private boolean reloadEnabled;
private TagLibraryLookup tagLibraryLookup;
private TagLibraryResolver jspTagLibraryResolver;
private boolean cacheResources = true;
private String gspEncoding = System.getProperty("file.encoding", GroovyPageParser.DEFAULT_ENCODING);
private GrailsApplication grailsApplication;
private Map<String, Class<?>> cachedDomainsWithoutPackage;
private List<GroovyPageSourceDecorator> groovyPageSourceDecorators = new ArrayList();
static {
String dirPath = System.getProperty("grails.dump.gsp.line.numbers.to.dir");
if (dirPath != null) {
File dir = new File(dirPath);
if (dir.exists() || dir.mkdirs()) {
dumpLineNumbersTo = dir;
}
}
}
private class GroovyPagesTemplateEngineCacheEntry extends CacheEntry<GroovyPageMetaInfo>{
private final String pageName;
public GroovyPagesTemplateEngineCacheEntry(String pageName){
this.pageName = pageName;
}
@Override
protected boolean hasExpired(long timeout, Object cacheRequestObject) {
GroovyPageMetaInfo meta = getValue();
Resource resource = (Resource)cacheRequestObject;
return meta == null || isGroovyPageReloadable(resource, meta);
}
@Override
protected GroovyPageMetaInfo updateValue(GroovyPageMetaInfo oldValue, Callable<GroovyPageMetaInfo> updater, Object cacheRequestObject)
throws Exception {
if(oldValue != null) {
oldValue.removePageMetaClass();
}
Resource resource = (Resource)cacheRequestObject;
return buildPageMetaInfo(resource, pageName);
}
}
private static class GroovyPagesTemplateEngineCallable implements Callable<CacheEntry<GroovyPageMetaInfo>> {
private final CacheEntry<GroovyPageMetaInfo> cacheEntry;
public GroovyPagesTemplateEngineCallable(CacheEntry<GroovyPageMetaInfo> cacheEntry){
this.cacheEntry = cacheEntry;
}
@Override
public CacheEntry<GroovyPageMetaInfo> call() throws Exception {
return cacheEntry;
}
}
public GroovyPagesTemplateEngine() {
// default
}
public void setGroovyPageSourceDecorators(List<GroovyPageSourceDecorator> groovyPageSourceDecorators){
this.groovyPageSourceDecorators = groovyPageSourceDecorators;
}
public List<GroovyPageSourceDecorator> getGroovyPageSourceDecorators(){
return groovyPageSourceDecorators;
}
public void setGroovyPageLocator(GroovyPageLocator groovyPageLocator) {
this.groovyPageLocator = groovyPageLocator;
}
public GroovyPageLocator getGroovyPageLocator() {
return groovyPageLocator;
}
public void setResourceLoader(ResourceLoader resourceLoader) {
groovyPageLocator.addResourceLoader(resourceLoader);
}
public void afterPropertiesSet() {
if (classLoader == null) {
classLoader = initGroovyClassLoader(Thread.currentThread().getContextClassLoader());
}else if (!classLoader.getClass().equals(GroovyPageClassLoader.class)) {
classLoader = initGroovyClassLoader(classLoader);
}
if (!Environment.isDevelopmentMode()) {
cachedDomainsWithoutPackage = createDomainClassMap();
}
}
private GroovyClassLoader initGroovyClassLoader(ClassLoader parent) {
CompilerConfiguration compConfig = new CompilerConfiguration();
compConfig.setSourceEncoding(GroovyPageParser.GROOVY_SOURCE_CHAR_ENCODING);
return new GroovyPageClassLoader(parent, compConfig);
}
public void setTagLibraryLookup(TagLibraryLookup tagLibraryLookup) {
this.tagLibraryLookup = tagLibraryLookup;
}
public void setJspTagLibraryResolver(TagLibraryResolver jspTagLibraryResolver) {
this.jspTagLibraryResolver = jspTagLibraryResolver;
}
/**
* Retrieves a line number matrix for the specified page that can be used
* to retrieve the actual line number within the GSP page if the line number within the
* compiled GSP is known
*
* @param url The URL of the page
* @return An array where the index is the line number witin the compiled GSP and the value is the line number within the source
*/
public int[] calculateLineNumbersForPage(String url) {
try {
Template t = createTemplate(url);
if (t instanceof GroovyPageTemplate) {
return ((GroovyPageTemplate)t).getMetaInfo().getLineNumbers();
}
}
catch (Exception e) {
// ignore, non critical method used for retrieving debug info
LOG.warn("Exception retrieving line numbers from GSP: " + url + ", message: " + e.getMessage());
LOG.debug("Full stack trace of error", e);
}
return new int[0];
}
public int mapStackLineNumber(String url, int lineNumber) {
int[] lineNumbers = calculateLineNumbersForPage(url);
if (lineNumber < lineNumbers.length) {
lineNumber = lineNumbers[lineNumber - 1];
}
return lineNumber;
}
/**
* Creates a Template for the given Spring Resource instance
*
* @param resource The Resource to create the Template for
* @return The Template instance
*/
@Override
public Template createTemplate(Resource resource) {
return createTemplate(resource, cacheResources);
}
/**
* Creates a Template for the given Spring Resource instance
*
* @param resource The Resource to create the Template for
* @param cacheable The resource can be cached or not
* @return The Template instance
*/
@Override
public Template createTemplate(Resource resource, final boolean cacheable) {
if (resource == null) {
throw new GroovyPagesException("Resource is null. No Groovy page found.");
}
//Yags: Because, "pageName" was sent as null originally, it is never go in pageCache, but will force to compile the String again and till the time this request
// is getting executed, it will occupy space in PermGen space. So if there are 1000 request for the same resource at a particular instance, there will be 1000 instance
// class in PermGen instead of ideally being 1 as they as essentially same resource.
//we will cache metaInfo only is Developer wants-to. Developer will make sure that he creates unique key for every unique pages s/he wants to put in cache
final String pageName = establishPageName(resource, cacheable);
try {
return createTemplate(resource, pageName, cacheable);
}
catch (IOException e) {
throw new GroovyPagesException("Error loading template", e);
}
}
protected Template createTemplate(Resource resource, final String pageName, final boolean cacheable) throws IOException {
GroovyPageMetaInfo meta;
if(cacheable) {
meta = CacheEntry.getValue(pageCache, pageName, -1, null,
new GroovyPagesTemplateEngineCallable(new GroovyPagesTemplateEngineCacheEntry(pageName))
, true, resource);
} else {
meta = buildPageMetaInfo(resource, pageName);
}
return new GroovyPageTemplate(meta);
}
protected String establishPageName(Resource resource, final boolean cacheable) {
String name;
if (cacheable) {
name = establishPageName(resource, getPathForResource(resource));
} else {
name = establishPageName(resource, null);
}
return name;
}
/**
* Creates a Template using the given URI.
*
* @param uri The URI of the page to create the template for
* @return The Template instance
* @throws CompilationFailedException
*/
@Override
public Template createTemplate(String uri) {
return createTemplateForUri(uri);
}
private Template createTemplateFromPrecompiled(GroovyPageCompiledScriptSource compiledScriptSource) {
GroovyPageMetaInfo meta = initializeCompiledMetaInfo(compiledScriptSource.getGroovyPageMetaInfo());
if (isReloadEnabled()) {
GroovyPageResourceScriptSource changedResourceScriptSource = compiledScriptSource.getReloadableScriptSource();
if (changedResourceScriptSource != null) {
groovyPageLocator.removePrecompiledPage(compiledScriptSource);
return createTemplate(changedResourceScriptSource);
}
}
return new GroovyPageTemplate(meta);
}
private GroovyPageMetaInfo initializeCompiledMetaInfo(GroovyPageMetaInfo meta) {
meta.initializeOnDemand(new GroovyPageMetaInfo.GroovyPageMetaInfoInitializer() {
public void initialize(GroovyPageMetaInfo metaInfo) {
metaInfo.setGrailsApplication(grailsApplication);
metaInfo.setJspTagLibraryResolver(jspTagLibraryResolver);
metaInfo.setTagLibraryLookup(tagLibraryLookup);
metaInfo.initialize();
GroovyPagesMetaUtils.registerMethodMissingForGSP(metaInfo.getPageClass(), tagLibraryLookup);
}
});
return meta;
}
public Template createTemplateForUri(String uri) {
return createTemplateForUri(new String[]{uri});
}
public Template createTemplateForUri(String[] uris) {
GroovyPageScriptSource scriptSource = findScriptSource(uris);
if (scriptSource != null) {
return createTemplate(scriptSource);
}
return null;
}
public GroovyPageScriptSource findScriptSource(String uri) {
return findScriptSource(new String[]{uri});
}
public GroovyPageScriptSource findScriptSource(String[] uris) {
GroovyPageScriptSource scriptSource = null;
for (String uri : uris) {
scriptSource = groovyPageLocator.findPage(uri);
if (scriptSource != null) break;
}
return scriptSource;
}
public Template createTemplate(ScriptSource scriptSource) {
if (scriptSource instanceof GroovyPageCompiledScriptSource) {
// handle pre-compiled
return createTemplateFromPrecompiled((GroovyPageCompiledScriptSource) scriptSource);
}
if (scriptSource instanceof ResourceScriptSource) {
ResourceScriptSource resourceSource = (ResourceScriptSource) scriptSource;
Resource resource = resourceSource.getResource();
return createTemplate(resource, true);
}
try {
return createTemplate(scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
} catch (IOException e) {
throw new RuntimeException("IOException in createTemplate", e);
}
}
/**
* Creates a Template using the given text for the Template and the given name. The name
* of the template is required
*
* @param txt The URI of the page to create the template for
* @param pageName The name of the page being parsed
*
* @return The Template instance
* @throws CompilationFailedException
* @throws IOException Thrown if an IO exception occurs creating the Template
*/
public Template createTemplate(String txt, String pageName) throws IOException {
Assert.hasLength(txt, "Argument [txt] cannot be null or blank");
Assert.hasLength(pageName, "Argument [pageName] cannot be null or blank");
return createTemplate(new ByteArrayResource(txt.getBytes("UTF-8"), pageName), pageName, pageName != null);
}
/**
* Creates a Template for the given file
*
* @param file The File to use to construct the template with
* @return A Groovy Template instance
*
* @throws CompilationFailedException When an error occured compiling the Template
* @throws ClassNotFoundException When a Class cannot be found within the given Template
* @throws IOException When a I/O Exception occurs reading the Template
*/
@Override
public Template createTemplate(File file) throws CompilationFailedException, ClassNotFoundException, IOException {
return createTemplate(new FileSystemResource(file));
}
/**
* Creates a Template for the given URL
*
* @param url The URL to use to construct the template with
* @return A Groovy Template instance
*
* @throws CompilationFailedException When an error occured compiling the Template
* @throws ClassNotFoundException When a Class cannot be found within the given Template
* @throws IOException When a I/O Exception occurs reading the Template
*/
@Override
public Template createTemplate(URL url) throws CompilationFailedException, ClassNotFoundException, IOException {
return createTemplate(new UrlResource(url));
}
/**
* Create a Template for the given InputStream
* @param inputStream The InputStream to create the Template for
* @return The Template instance
*/
@Override
public Template createTemplate(InputStream inputStream) {
GroovyPageMetaInfo metaInfo = buildPageMetaInfo(inputStream, null, null);
return new GroovyPageTemplate(metaInfo);
}
protected GroovyPageMetaInfo buildPageMetaInfo(Resource resource, String pageName) throws IOException {
InputStream inputStream = resource.getInputStream();
try {
return buildPageMetaInfo(inputStream, resource, pageName);
}
finally {
inputStream.close();
}
}
private StringBuilder decorateGroovyPageSource(StringBuilder source) throws IOException {
for(GroovyPageSourceDecorator groovyPageSourceDecorator : groovyPageSourceDecorators){
source = groovyPageSourceDecorator.decorate(source);
}
return source;
}
/**
* Establishes whether a Groovy page is reloadable. A GSP is only reloadable in the development environment.
*
* @param resource The Resource to check.
* @param meta The current GroovyPageMetaInfo instance
* @return true if it is reloadable
*/
private boolean isGroovyPageReloadable(final Resource resource, GroovyPageMetaInfo meta) {
return isReloadEnabled() && meta.shouldReload(new PrivilegedAction<Resource>() {
public Resource run() {
return resource;
}
});
}
/**
* Return whether reload is enabled for the GroovyPagesTemplateEngine
*
* @return true if it is
*/
public boolean isReloadEnabled() {
return reloadEnabled;
}
/**
* Sets whether reloading is enabled
*
* @param b True if it is enabled
*/
public void setReloadEnabled(boolean b) {
reloadEnabled = b;
}
/**
* Attempts to retrieve a reference to a GSP as a Spring Resource instance for the given URI.
*
* @param uri The URI to check
* @return A Resource instance
*/
public Resource getResourceForUri(String uri) {
GroovyPageScriptSource scriptSource = getResourceWithinContext(uri);
if (scriptSource != null && (scriptSource instanceof GroovyPageResourceScriptSource)) {
return ((GroovyPageResourceScriptSource)scriptSource).getResource();
}
return null;
}
private GroovyPageScriptSource getResourceWithinContext(String uri) {
Assert.state(groovyPageLocator != null, "TemplateEngine not initialised correctly, no [groovyPageLocator] specified!");
GroovyPageScriptSource scriptSource = groovyPageLocator.findPage(uri);
if (scriptSource != null) {
return scriptSource;
}
return null;
}
/**
* Constructs a GroovyPageMetaInfo instance which holds the script class, modified date and so on
*
* @param inputStream The InputStream to construct the GroovyPageMetaInfo instance from
* @param res The Spring Resource to construct the MetaInfo from
* @param pageName The name of the page (can be null, in which case method responsible for calculating appropriate alternative)
* @return The GroovyPageMetaInfo instance
*/
protected GroovyPageMetaInfo buildPageMetaInfo(InputStream inputStream, Resource res, String pageName) {
String name = establishPageName(res, pageName);
GroovyPageParser parser;
String path = getPathForResource(res);
try {
String gspSource = IOUtils.toString(inputStream, getGspEncoding());
parser = new GroovyPageParser(name, path, path, decorateGroovyPageSource(new StringBuilder(gspSource)).toString());
if (grailsApplication != null) {
Config config = grailsApplication.getConfig();
parser.configure(config);
}
}
catch (IOException e) {
throw new GroovyPagesException("I/O parsing Groovy page ["+(res != null ? res.getDescription() : name)+"]: " + e.getMessage(),e);
}
InputStream in = parser.parse();
// Make a new metaInfo
GroovyPageMetaInfo metaInfo = createPageMetaInfo(parser, in);
metaInfo.applyLastModifiedFromResource(res);
try {
metaInfo.setPageClass(compileGroovyPage(in, name, path, metaInfo));
metaInfo.setHtmlParts(parser.getHtmlPartsArray());
}
catch (GroovyPagesException e) {
metaInfo.setCompilationException(e);
}
return metaInfo;
}
private String getPathForResource(Resource res) {
if (res == null) return "";
String path = null;
try {
File file = res.getFile();
if (file != null) {
path = file.getAbsolutePath();
}
}
catch (IOException e) {
// ignore
}
if (path != null) {
return path;
}
if (res.getDescription() != null) {
return res.getDescription();
}
return "";
}
/**
* Attempts to compile the given InputStream into a Groovy script using the given name
* @param in The InputStream to read the Groovy code from
* @param name The name of the class to use
* @param pageName The page name
* @param metaInfo
* @return The compiled java.lang.Class, which is an instance of groovy.lang.Script
*/
private Class<?> compileGroovyPage(InputStream in, String name, String pageName, GroovyPageMetaInfo metaInfo) {
GroovyClassLoader groovyClassLoader = findOrInitGroovyClassLoader();
// Compile the script into an object
Class<?> scriptClass;
try {
String groovySource = IOGroovyMethods.getText(in, GroovyPageParser.GROOVY_SOURCE_CHAR_ENCODING);
//System.out.println(groovySource);
scriptClass = groovyClassLoader.parseClass(groovySource, name);
}
catch (CompilationFailedException e) {
LOG.error("Compilation error compiling GSP ["+name+"]:" + e.getMessage(), e);
int lineNumber = ExceptionUtils.extractLineNumber(e);
final int[] lineMappings = metaInfo.getLineNumbers();
if (lineNumber>0 && lineNumber < lineMappings.length) {
lineNumber = lineMappings[lineNumber-1];
}
String relativePageName = DefaultErrorsPrinter.makeRelativeIfPossible(pageName);
throw new GroovyPagesException("Could not parse script [" + relativePageName + "]: " + e.getMessage(),e, lineNumber, pageName);
}
catch (IOException e) {
String relativePageName = DefaultErrorsPrinter.makeRelativeIfPossible(pageName);
throw new GroovyPagesException("IO exception parsing script ["+ relativePageName + "]: " + e.getMessage(), e);
}
GroovyPagesMetaUtils.registerMethodMissingForGSP(scriptClass, tagLibraryLookup);
return scriptClass;
}
private synchronized GroovyClassLoader findOrInitGroovyClassLoader() {
if (!(classLoader instanceof GroovyPageClassLoader)) {
classLoader = initGroovyClassLoader(classLoader);
}
return (GroovyClassLoader)classLoader;
}
/**
* Creates a GroovyPageMetaInfo instance from the given Parse object, and initialises it with the the specified
* last modifed date and InputStream
*
* @param parse The Parse object
* @param in The InputStream instance
* @return A GroovyPageMetaInfo instance
*/
private GroovyPageMetaInfo createPageMetaInfo(GroovyPageParser parse, InputStream in) {
GroovyPageMetaInfo pageMeta = new GroovyPageMetaInfo();
pageMeta.setGrailsApplication(grailsApplication);
pageMeta.setJspTagLibraryResolver(jspTagLibraryResolver);
pageMeta.setTagLibraryLookup(tagLibraryLookup);
pageMeta.setContentType(parse.getContentType());
pageMeta.setLineNumbers(parse.getLineNumberMatrix());
pageMeta.setJspTags(parse.getJspTags());
pageMeta.setStaticCodecName(parse.getStaticCodecDirectiveValue());
pageMeta.setExpressionCodecName(parse.getExpressionCodecDirectiveValue());
pageMeta.setOutCodecName(parse.getOutCodecDirectiveValue());
pageMeta.setTaglibCodecName(parse.getTaglibCodecDirectiveValue());
pageMeta.setCompileStaticMode(parse.isCompileStaticMode());
pageMeta.setModelFieldsMode(parse.isModelFieldsMode());
pageMeta.initialize();
// just return groovy and don't compile if asked
if (GrailsUtil.isDevelopmentEnv()) {
pageMeta.setGroovySource(in);
}
if (dumpLineNumbersTo != null) {
String fileName = parse.getClassName() + GroovyPageMetaInfo.LINENUMBERS_DATA_POSTFIX;
File file = new File(dumpLineNumbersTo, fileName);
try {
parse.writeLineNumbers(file);
}
catch (IOException ignored) {
if (file.exists()) {
file.delete();
}
}
}
return pageMeta;
}
/**
* Establishes the name to use for the given resource
*
* @param res The Resource to calculate the name for
* @param pageName The name of the page, can be null, in which case method responsible for calculation
*
* @return The name as a String
*/
protected String establishPageName(Resource res, String pageName) {
if (res == null) {
return generateTemplateName();
}
try {
String name = pageName != null ? pageName : res.getURL().getPath();
// As the name take the first / off and then replace all characters that aren't
// a word character or a digit with an underscore
if (name.startsWith("/")) name = name.substring(1);
return name.replaceAll("[^\\w\\d]", "_");
}
catch (IllegalStateException e) {
return generateTemplateName();
}
catch (IOException ioex) {
return generateTemplateName();
}
}
/**
* Generates the template name to use if it cannot be established from the Resource
*
* @return The template name
*/
private String generateTemplateName() {
return GENERATED_GSP_NAME_PREFIX + scriptNameCount.incrementAndGet();
}
/**
* Sets the ResourceLoader from the ApplicationContext
*
* @param applicationContext The ApplicationContext
* @throws BeansException Thrown when an error occurs with the ApplicationContext
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (applicationContext.containsBean(GrailsApplication.APPLICATION_ID)) {
this.grailsApplication = applicationContext.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class);
Config config = grailsApplication.getConfig();
this.gspEncoding = config.getProperty(GroovyPageParser.CONFIG_PROPERTY_GSP_ENCODING, System.getProperty("file.encoding", GroovyPageParser.DEFAULT_ENCODING));
}
}
/**
* Returns the path to the view of the relative URI within the Grails views directory
*
* @param relativeUri The relative URI
* @return The path of the URI within the Grails view directory
*/
protected String getUriWithinGrailsViews(String relativeUri) {
StringBuilder buf = new StringBuilder();
String[] tokens;
if (relativeUri.startsWith("/")) {
relativeUri = relativeUri.substring(1);
}
if (relativeUri.indexOf('/')>-1) {
tokens = relativeUri.split("/");
}
else {
tokens = new String[]{relativeUri};
}
buf.append(DefaultGroovyPageLocator.PATH_TO_WEB_INF_VIEWS);
for (String token : tokens) {
buf.append('/').append(token);
}
if (!relativeUri.endsWith(GroovyPage.EXTENSION)) {
buf.append(GroovyPage.EXTENSION);
}
return buf.toString();
}
/**
* Clears the page cache. Views will be re-compiled.
*/
public void clearPageCache() {
for(Iterator<Map.Entry<String, CacheEntry<GroovyPageMetaInfo>>> it = pageCache.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, CacheEntry<GroovyPageMetaInfo>> entry = it.next();
GroovyPageMetaInfo metaInfo = entry.getValue().getValue();
if(metaInfo != null) {
metaInfo.removePageMetaClass();
}
it.remove();
}
}
public boolean isCacheResources() {
return cacheResources;
}
public void setCacheResources(boolean cacheResources) {
this.cacheResources = cacheResources;
}
public Map<String, Class<?>> getDomainClassMap() {
if (cachedDomainsWithoutPackage != null) {
return cachedDomainsWithoutPackage;
}
return createDomainClassMap();
}
/**
* The domainClassMap is used in GSP binding to "auto-import" domain classes in packages without package prefix.
* real imports aren't used, instead each class is added to the binding
*
* This feature has existed earlier, the code has just been refactored and moved to GroovyPagesTemplateEngine
* to prevent using the static cache that was used previously.
*/
private Map<String, Class<?>> createDomainClassMap() {
Map<String, Class<?>> domainsWithoutPackage = new HashMap<String, Class<?>>();
if (grailsApplication != null) {
GrailsClass[] domainClasses = grailsApplication.getArtefacts(DomainClassArtefactHandler.TYPE);
for (GrailsClass domainClass : domainClasses) {
final Class<?> theClass = domainClass.getClazz();
domainsWithoutPackage.put(theClass.getName(), theClass);
}
}
return domainsWithoutPackage;
}
@Override
public void setBeanClassLoader(ClassLoader beanClassLoader) {
// support passing BeanClassLoader as parent classloader for templates
// don't set the classLoader field if it already has an explicit value
if(beanClassLoader != null && this.classLoader == null) {
this.classLoader = beanClassLoader;
}
}
public String getGspEncoding() {
return this.gspEncoding;
}
}