forked from grails-plugins/grails-freemarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreemarkerGrailsPlugin.groovy
194 lines (164 loc) · 7.33 KB
/
FreemarkerGrailsPlugin.groovy
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
/* Copyright 2009 Jeff Brown
*
* 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.
*/
import grails.plugin.freemarker.AbstractTagLibAwareConfigurer
import grails.plugin.freemarker.GrailsTemplateLoader
import grails.plugin.freemarker.TagLibAwareConfigurer
import grails.plugin.freemarker.TagLibPostProcessor
import grails.util.*;
import org.codehaus.groovy.grails.commons.GrailsClass
import org.codehaus.groovy.grails.commons.TagLibArtefactHandler
import org.codehaus.groovy.grails.web.metaclass.RenderDynamicMethod
import org.codehaus.groovy.grails.web.util.WebUtils
import org.springframework.context.ApplicationContext
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer
/**
* @author Jeff Brown
* @author Daniel Henrique Alves Lima
* @author Joshua Burnett
*/
class FreemarkerGrailsPlugin {
// the plugin version
def version = "1.0.0"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.2 > *"
// the other plugins this plugin depends on
//def dependsOn = [pluginConfig: '0.1.3 > *']
def observe = ["controllers", 'groovyPages']
def loadAfter = ['controllers', 'groovyPages']
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/**/*",
"grails-app/controllers/**/*",
"grails-app/services/grails/plugin/freemarker/test/**/*",
"src/groovy/grails/plugin/freemarker/test/**/*",
"grails-app/i18n/*",
'grails-app/taglib/**/test/**/*',
'scripts/**/Eclipse.groovy',
"test-plugins/**/*",
"web-app/**/*"
]
def author = "Jeff Brown"
def authorEmail = "jeff.brown@springsource.com"
def title = "FreeMarker Grails Plugin"
def description = '''\
The Grails FreeMarker plugin provides support for rendering FreeMarker templates
as views.
'''
def documentation = "http://grails.org/plugin/freemarker"
def doWithSpring = {
def freeconfig = application.mergedConfig.asMap(true).grails.plugin.freemarker
String ftlSuffix = '.ftl'
freemarkerGrailsTemplateLoader(grails.plugin.freemarker.GrailsTemplateLoader){ bean ->
bean.autowire = "byName"
}
def resolveLoaders = {List loaders ->
return loaders.collect{ (it instanceof CharSequence) ? ref(it.toString()) : it }
}
Class configClass = freeconfig.tags.enabled == true? TagLibAwareConfigurer : FreeMarkerConfigurer
freemarkerConfig(configClass) {
if(freeconfig.preTemplateLoaders){
preTemplateLoaders = resolveLoaders(freeconfig.preTemplateLoaders)
}
def confLoaderPaths = freeconfig.templateLoaderPaths?:[]
confLoaderPaths.add("classpath:freemarker/")
templateLoaderPaths = confLoaderPaths as String[]
if(freeconfig.postTemplateLoaders){
postTemplateLoaders = resolveLoaders(freeconfig.postTemplateLoaders)
postTemplateLoaders.add(0, ref('freemarkerGrailsTemplateLoader'))
} else{
postTemplateLoaders = [ref('freemarkerGrailsTemplateLoader')]
}
freemarkerSettings = application.mergedConfig.grails.plugin.freemarker.configSettings?.toProperties()
}
freemarkerViewResolver(grails.plugin.freemarker.GrailsFreeMarkerViewResolver) {
freemarkerConfig = ref('freemarkerConfig')
prefix = ''
suffix = ftlSuffix
requireViewSuffix = freeconfig.requireViewSuffix
hideException = freeconfig.viewResolver.hideException
order = 10
}
if (freeconfig.tags.enabled == true) {
// Now go through tag libraries and configure them in spring too. With AOP proxies and so on
for (taglib in application.tagLibClasses) {
"${taglib.fullName}_fm"(taglib.clazz) { bean ->
bean.autowire = true
bean.lazyInit = true
// Taglib scoping support could be easily added here. Scope could be based on a static field in the taglib class.
//bean.scope = 'request'
}
}
"${TagLibPostProcessor.class.name}"(TagLibPostProcessor) {
grailsApplication = ref('grailsApplication')
}
}
}
def doWithApplicationContext = { appCtx ->
}
def doWithDynamicMethods = { ctx ->
for (controller in application.controllerClasses) {
modRenderMethod(application, controller)
}
/** Fremarker configuration mods **/
def configLocalizedLookup = application.config.grails.plugin.freemarker.localizedLookup
//turn off the localizedLookup by default
if(!configLocalizedLookup) {
ctx.freemarkerConfig.configuration.localizedLookup = false
}
}
def onChange = { event ->
def freeconfig = application.mergedConfig.asMap(true).grails.plugin.freemarker
if (application.isControllerClass(event.source) ) {
modRenderMethod(application, event.source)
}
if (freeconfig.tags.enabled == true && application.isArtefactOfType(TagLibArtefactHandler.TYPE, event.source)) {
GrailsClass taglibClass = application.addArtefact(TagLibArtefactHandler.TYPE, event.source)
if (taglibClass) {
// replace tag library bean
def beanName = taglibClass.fullName
def beans = beans {
"${beanName}_fm"(taglibClass.clazz) { bean ->
bean.autowire = true
//bean.scope = 'request'
}
"${TagLibPostProcessor.class.name}"(TagLibPostProcessor) {
grailsApplication = ref('grailsApplication')
}
}
beans.registerBeans(event.ctx)
//event.manager?.getGrailsPlugin('groovyPages')?.doWithDynamicMethods(event.ctx)
def ApplicationContext springContext = application.mainContext
for (configurerBeanName in springContext.getBeanNamesForType(AbstractTagLibAwareConfigurer.class)) {
def configurer = springContext.getBean(configurerBeanName)
configurer.reconfigure()
}
}
}
}
def modRenderMethod(application, controller){
def original = controller.metaClass.getMetaMethod("render", [Map] as Class[])
controller.metaClass.render = {Map args ->
//if args has a pluginName then set it in a threadlocal so we can get to it from the freemarkerViewResolver
def request = WebUtils.retrieveGrailsWebRequest()?.getCurrentRequest()
if(args.plugin && request){
request.setAttribute(GrailsTemplateLoader.PLUGIN_NAME_ATTRIBUTE,args.plugin)
}
if(args.loaderAttribute && request){
request.setAttribute("loaderAttribute",args.loaderAttribute)
}
original.invoke(delegate, [args] as Object[])
}
}
}