-
Notifications
You must be signed in to change notification settings - Fork 3
/
PortletsGrailsPlugin.groovy
211 lines (173 loc) · 7.12 KB
/
PortletsGrailsPlugin.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.lang.reflect.Modifier
import org.springframework.web.context.request.RequestContextHolder as RCH
import org.codehaus.groovy.grails.commons.GrailsClass
import org.codehaus.groovy.grails.commons.metaclass.MetaClassEnhancer
import org.codehaus.groovy.grails.plugins.PluginMetaManager
import org.codehaus.groovy.grails.plugins.web.api.ControllersApi
import org.codehaus.groovy.grails.web.metaclass.BindDynamicMethod
import org.codehaus.groovy.grails.web.plugins.support.WebMetaUtils
import org.springframework.aop.framework.ProxyFactoryBean
import org.springframework.aop.target.HotSwappableTargetSource
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
import org.springframework.context.ApplicationContext
import org.springframework.web.context.request.RequestAttributes
import org.codehaus.grails.portlets.*
class PortletsGrailsPlugin {
// the plugin version
def version = "0.9.2"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "2.0 > *"
// the other plugins this plugin depends on
def loadAfter = ['controllers']
def artefacts = [PortletArtefactHandler.class]
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp"
]
def author = "Kenji Nakamura, Philip Wu"
def authorEmail = "[email protected]"
def title = "Portlets Plugin"
def description = '''\\
Generate JSR-168 compliant portlet war.
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/plugins/portlets"
static WEB_APP_NAME = 'Grails Portlet Application'
def watchedResources = [
'file:./grails-app/portlets/**/*Portlet.groovy',
'file:./plugins/*/grails-app/portlets/**/*Portlet.groovy'
]
def doWithSpring = {
application.portletClasses.each {portlet ->
log.debug "Configuring portlet $portlet.fullName"
"${portlet.fullName}Class"(MethodInvokingFactoryBean) {
targetObject = ref("grailsApplication", true)
targetMethod = "getArtefact"
arguments = [PortletArtefactHandler.TYPE, portlet.fullName]
}
"${portlet.fullName}TargetSource"(HotSwappableTargetSource, ref("${portlet.fullName}Class"))
"${portlet.fullName}Proxy"(ProxyFactoryBean) {
targetSource = ref("${portlet.fullName}TargetSource")
proxyInterfaces = [GrailsPortletClass.class]
}
"${portlet.fullName}"("${portlet.fullName}Proxy": "newInstance") {bean ->
bean.singleton = false
bean.autowire = "byName"
}
}
portletHandlerMappings(GrailsPortletHandlerMapping) {
interceptors = [ref("portletHandlerInterceptor")]
}
portletHandlerAdapter(GrailsPortletHandlerAdapter)
portletReloadFilter(PortletReloadFilter)
portletHandlerInterceptor(GrailsPortletHandlerInterceptor) {
portletReloadFilter = ref(portletReloadFilter)
}
}
def doWithWebDescriptor = {webXml ->
def mappingElement = webXml.'servlet-mapping'
mappingElement = mappingElement[mappingElement.size() - 1]
mappingElement + {
'servlet-mapping'
{
'servlet-name'('view-servlet')
'url-pattern'('/WEB-INF/servlet/view')
}
}
def servletElement = webXml.'servlet'
servletElement = servletElement[servletElement.size() - 1]
servletElement + {
'servlet'
{
'servlet-name'('view-servlet')
'servlet-class'('org.springframework.web.servlet.ViewRendererServlet')
'load-on-startup'('1')
}
}
}
def doWithDynamicMethods = {ApplicationContext ctx ->
def registry = GroovySystem.getMetaClassRegistry()
def bind = new BindDynamicMethod()
// add commons objects and dynamic methods like render and redirect to portlets
for (GrailsClass portlet in application.portletClasses) {
MetaClass mc = portlet.metaClass
Class portletClass = portlet.clazz
WebMetaUtils.registerCommonWebProperties(mc, application)
//def controllersPlugin = new ControllersGrailsPlugin()
//controllersPlugin.registerControllerMethods(mc, ctx)
def enhancer = new MetaClassEnhancer()
enhancer.addApi(new ControllersApi(getManager()))
enhancer.enhance mc
Class superClass = portletClass.superclass
mc.getPluginContextPath = {->
PluginMetaManager metaManager = ctx.pluginMetaManager
String path = metaManager.getPluginPathForResource(delegate.class.name)
path ? path : ''
}
mc.getPortletRequest = {
getFromRequestAttributes('javax.portlet.request')
}
mc.getPortletResponse = {
getFromRequestAttributes('javax.portlet.response')
}
mc.getMode = {
def req = getFromRequestAttributes('javax.portlet.request')
req.portletMode
}
mc.getSession = {
def req = getFromRequestAttributes('javax.portlet.request')
req.portletSession
}
mc.getWindowState = {
def req = getFromRequestAttributes('javax.portlet.request')
req.windowState
}
mc.getPortalContext = {
def req = getFromRequestAttributes('javax.portlet.request')
req.portalContext
}
mc.getPreferences = {
def req = getFromRequestAttributes('javax.portlet.request')
req.preferences
}
// deal with abstract super classes
while (superClass != Object.class) {
if (Modifier.isAbstract(superClass.getModifiers())) {
WebMetaUtils.registerCommonWebProperties(superClass.metaClass, application)
//controllersPlugin.registerControllerMethods(superClass.metaClass, ctx)
enhancer = new MetaClassEnhancer()
enhancer.addApi(new ControllersApi(getManager()))
enhancer.enhance superClass.metaClass
}
}
}
}
private getFromRequestAttributes(key) {
def webRequest = RCH.currentRequestAttributes();
webRequest.getAttribute(key,
RequestAttributes.SCOPE_REQUEST)
}
def onChange = {event ->
def context = event.ctx
if (!context) {
if (log.isDebugEnabled())
log.debug("Application context not found. Can't reload")
return
}
boolean isNew = application.getPortletClass(event.source?.name) ? false : true
def portletClass = application.addArtefact(PortletArtefactHandler.TYPE, event.source)
if (isNew) {
log.info "Portlet ${event.source} found. You need to restart for the change to be applied"
}
else {
if (log.isDebugEnabled()) {
log.debug("Portlet ${event.source} changed. Reloading...")
}
def portletTargetSource = context.getBean("${portletClass.fullName}TargetSource")
portletTargetSource.swap(portletClass)
}
event.manager?.getGrailsPlugin("portlets")?.doWithDynamicMethods(event.ctx)
}
def generateTomcatContextFile() {
}
}