-
Notifications
You must be signed in to change notification settings - Fork 8
/
ScopedProxyGrailsPlugin.groovy
176 lines (151 loc) · 5.82 KB
/
ScopedProxyGrailsPlugin.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
/*
* Copyright 2010 Luke Daley
*
* 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.scopedproxy.ScopedProxyUtils as SPU
import grails.plugin.scopedproxy.TypeSpecifyableTransactionProxyFactoryBean
import grails.plugin.scopedproxy.reload.session.ReloadedScopedBeanSessionPurger
import grails.plugin.scopedproxy.reload.filters.ReloadedScopedBeanFiltersReloader
import grails.plugin.scopedproxy.reload.request.RequestScopedBeanReloadSessionPurger
import org.slf4j.LoggerFactory
import org.codehaus.groovy.grails.orm.support.GroovyAwareNamedTransactionAttributeSource
import org.codehaus.groovy.grails.commons.GrailsClassUtils
class ScopedProxyGrailsPlugin {
def version = "0.3-SNAPSHOT"
def grailsVersion = "1.2.0 > *"
def dependsOn = [:]
def observe = ["services"]
def loadAfter = ['services']
def pluginExcludes = [
"grails-app/**/*"
]
def author = "Luke Daley"
def authorEmail = "ld@ldaley.com"
def title = "Scoped Proxy Plugin"
def description = 'Adds support for scoped bean proxies (including hot reloading)'
def documentation = "http://github.com/alkemist/grails-scoped-proxy"
def doWithSpring = {
for (serviceClass in application.serviceClasses) {
buildServiceProxyIfNecessary(delegate, application.classLoader, serviceClass.clazz)
}
if (SPU.isEnvironmentClassReloadable()) {
if (log.infoEnabled) {
log.info("Registering session purger in application context")
}
reloadedScopedBeanSessionPurger(ReloadedScopedBeanSessionPurger) {
it.autowire = true
}
if (log.infoEnabled) {
log.info("Registering filters reloader in application context")
}
reloadedScopedBeanFiltersReloader(ReloadedScopedBeanFiltersReloader) {
it.autowire = true
}
if (log.infoEnabled) {
log.info("Registering request scoped bean reload listener in application context")
}
requestScopedBeanReloadSessionPurger(RequestScopedBeanReloadSessionPurger) {
it.autowire = true
}
} else {
if (log.debugEnabled) {
log.debug("NOT registering session purger in application context (env not reloadable)")
}
}
}
def doWithWebDescriptor = { webXml ->
if (SPU.isEnvironmentClassReloadable()) {
if (log.debugEnabled) {
log.debug("Registering session listener in web descriptor for ReloadedScopedBeanSessionPurger")
}
def listeners = webXml.'listener'
listeners + {
'listener' {
'listener-class'("grails.plugin.scopedproxy.reload.session.SessionLifecycleListener")
}
}
}
}
def onChange = { event ->
if (application.isServiceClass(event.source)) {
def classLoader = application.classLoader
def serviceClass = application.getServiceClass(event.source.name)
def newClass = classLoader.loadClass(event.source.name, false)
def serviceBeanName = GrailsClassUtils.getPropertyName(newClass)
def proxyName = SPU.getProxyBeanName(serviceBeanName)
if (log.infoEnabled) {
log.info("handling change of service class '$newClass.name'")
}
SPU.fireWillReloadIfNecessary(application, serviceBeanName, proxyName)
def didBuildProxy = false
def beanDefinitions = beans {
didBuildProxy = buildServiceProxyIfNecessary(delegate, classLoader, newClass)
}
if (didBuildProxy) {
beanDefinitions.registerBeans(event.ctx)
}
SPU.fireWasReloadedIfNecessary(application, serviceBeanName, proxyName)
}
}
static private buildServiceProxyIfNecessary(beanBuilder, classLoader, serviceClass) {
def propertyFetcher = SPU.createPropertyFetcher(serviceClass)
def wantsProxy = SPU.wantsProxy(propertyFetcher)
def scope = SPU.getScope(propertyFetcher)
if (wantsProxy) {
if (log.infoEnabled) {
log.info("service class '$serviceClass.name' DOES want a proxy")
}
if (SPU.isTransactional(propertyFetcher)) {
buildTransactionalServiceProxy(beanBuilder, classLoader, serviceClass, scope)
} else {
buildServiceProxy(beanBuilder, classLoader, serviceClass)
}
} else {
if (log.debugEnabled) {
log.debug("service class '$serviceClass.name' DOES NOT want a proxy")
}
}
wantsProxy
}
static private buildServiceProxy(beanBuilder, classLoader, serviceClass) {
def targetBeanName = GrailsClassUtils.getPropertyName(serviceClass)
SPU.buildProxy(beanBuilder, classLoader, targetBeanName, serviceClass, SPU.getProxyBeanName(targetBeanName))
}
static private buildTransactionalServiceProxy(beanBuilder, classLoader, serviceClass, scope) {
def targetBeanName = GrailsClassUtils.getPropertyName(serviceClass)
if (log.debugEnabled) {
log.debug("redefining transactional proxy for '$targetBeanName'")
}
beanBuilder.with {
def props = new Properties()
props."*" = "PROPAGATION_REQUIRED"
"${targetBeanName}"(TypeSpecifyableTransactionProxyFactoryBean, serviceClass) { bean ->
bean.scope = scope
bean.lazyInit = true
target = { innerBean ->
innerBean.lazyInit = true
innerBean.factoryBean = "${serviceClass.name}ServiceClass"
innerBean.factoryMethod = "newInstance"
innerBean.autowire = "byName"
innerBean.scope = scope
}
proxyTargetClass = true
transactionAttributeSource = new GroovyAwareNamedTransactionAttributeSource(transactionalAttributes: props)
transactionManager = ref("transactionManager")
}
}
buildServiceProxy(beanBuilder, classLoader, serviceClass)
}
private static final log = LoggerFactory.getLogger('grails.plugin.scopedproxy.ScopedProxyGrailsPlugin')
}