forked from Grailsrocks/grails-platform-core
-
Notifications
You must be signed in to change notification settings - Fork 26
/
PlatformCoreGrailsPlugin.groovy
304 lines (245 loc) · 11.5 KB
/
PlatformCoreGrailsPlugin.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
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
/* Copyright 2011-2012 the original author or authors:
*
* Marc Palmer (marc@grailsrocks.com)
* Stéphane Maldini (smaldini@vmware.com)
*
* 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.util.Holders
import org.grails.plugin.platform.config.PluginConfigurationFactory
import org.grails.plugin.platform.conventions.ConventionsImpl
import org.grails.plugin.platform.events.EventsImpl
import org.grails.plugin.platform.events.dispatcher.GormTopicSupport1X
import org.grails.plugin.platform.events.dispatcher.GormTopicSupport2X
import org.grails.plugin.platform.events.publisher.DefaultEventsPublisher
import org.grails.plugin.platform.events.publisher.GormBridgePublisher
import org.grails.plugin.platform.events.registry.DefaultEventsRegistry
import org.grails.plugin.platform.injection.InjectionImpl
import org.grails.plugin.platform.navigation.NavigationImpl
import org.grails.plugin.platform.security.SecurityImpl
import org.grails.plugin.platform.ui.UiExtensions
class PlatformCoreGrailsPlugin {
def version = "1.0.1-SNAPSHOT"
def grailsVersion = "1.3 > *"
def pluginExcludes = [
"grails-app/conf/Test*.groovy",
"grails-app/i18n/test.properties",
"grails-app/domain/org/grails/plugin/platform/test/**/*.groovy",
"grails-app/controllers/org/grails/plugin/platform/test/**/*.groovy",
"grails-app/services/org/grails/plugin/platform/test/**/*.groovy",
"grails-app/src/groovy/org/grails/plugin/platform/test/**/*.groovy",
"grails-app/src/java/org/grails/plugin/platform/test/**/*.java",
"grails-app/views/test/**/*.gsp"
]
def observe = ['*'] // We observe everything so we can re-apply dynamic methods, conventions etc
def watchedResources = [
"file:./grails-app/conf/*Navigation.groovy",
"file:./grails-app/conf/*Events.groovy",
"file:./plugins/*/grails-app/conf/*Navigation.groovy",
"file:./plugins/*/grails-app/conf/*Events.groovy"
]
def artefacts = [getNavigationArtefactHandler(), getEventsArtefactHandler()]
def title = "Plugin Platform Core"
def author = "Grails Plugin Collective"
def authorEmail = "grails.plugin.collective@gmail.com"
def organization = [name: 'Grails Plugin Collective', url: 'http://github.com/gpc']
def description = 'Grails Plugin Platform Core APIs'
def loadBefore = ['core'] // Before rest of beans are initialized
def loadAfter = ['logging'] // After logging though, we need that
def documentation = "http://grails-plugins.github.io/grails-platform-core/"
def license = "APACHE"
def developers = [
[name: "Marc Palmer", email: "marc@grailsrocks.com"],
[name: "Graeme Rocher", email: "grocher@gopivotal.com"],
[name: "Stéphane Maldini", email: "smaldini@gopivotal.com"]
]
def issueManagement = [system: "JIRA", url: "http://jira.grails.org/browse/GPPLATFORMCORE"]
def scm = [url: "https://github.com/grails-plugins/grails-platform-core"]
boolean platformInitialized
/*
* Initialize the Configuration API instance
* We have to do this before anything else runs, e.g. to make sure correct
* config is available when web.xml is generated, which normally happens before
* the spring config is initialized
* If we don't do this, we can't use the Configuration API to affect the behaviour of
* code that generates stuff in web.xml - for example Spring Security cannot be configured
* via the Config API without this.
*/
void initPlatform(application, pluginManager = Holders.pluginManager) {
// The terrible things we have to do...
def hackyInstance = org.grails.plugin.platform.config.PluginConfigurationFactory.instance
hackyInstance.grailsApplication = application
hackyInstance.pluginManager = pluginManager
hackyInstance.applyConfig()
// Trigger doPlatformBuildInit(pluginManager)
application.config.grails.plugin.platform.initialized = true
platformInitialized = true
}
/**
* This happens only when building app, or in dev
*/
def doWithWebDescriptor = { xml ->
initPlatform(application, manager)
}
/**
* This happens all the time, but dWWD may not have run if we're in a WAR
*/
def doWithSpring = {
xmlns task: "http://www.springframework.org/schema/task"
initPlatform(application, manager)
def config = application.config.plugin.platformCore
// Config API
grailsPluginConfiguration(PluginConfigurationFactory) { bean ->
bean.factoryMethod = 'getInstance'
grailsApplication = ref('grailsApplication')
}
def deployed = application.warDeployed
def grailsVersion = application.metadata['app.grails.version']
// Security API
if (!config.security.disabled) {
grailsSecurity(SecurityImpl)
}
// Injection API
grailsInjection(InjectionImpl) {
grailsApplication = ref('grailsApplication')
}
// Navigation API
if (!config.navigation.disabled) {
grailsNavigation(NavigationImpl) {
grailsApplication = ref('grailsApplication')
grailsConventions = ref('grailsConventions')
}
}
// Convention API
grailsConventions(ConventionsImpl) {
grailsApplication = ref('grailsApplication')
}
// UI Helper API
if (!config.ui.disabled) {
grailsUiExtensions(UiExtensions)
}
// Events API
if (!config.events.disabled) {
task.executor(id: "grailsTopicExecutor", 'pool-size': config.events.poolSize)
//init api bean
grailsEventsRegistry(DefaultEventsRegistry)
grailsEventsPublisher(DefaultEventsPublisher) {
grailsEventsRegistry = ref('grailsEventsRegistry')
persistenceInterceptor = ref("persistenceInterceptor")
catchFlushExceptions = config.events.catchFlushExceptions
}
grailsEvents(EventsImpl) {
grailsApplication = ref('grailsApplication')
grailsEventsRegistry = ref('grailsEventsRegistry')
grailsEventsPublisher = ref('grailsEventsPublisher')
}
if (!config.events.gorm.disabled) {
if (grailsVersion.startsWith('1')) {
gormTopicSupport(GormTopicSupport1X)
} else {
gormTopicSupport(GormTopicSupport2X) {
translateTable = [
'PreInsertEvent': 'beforeInsert', 'PreUpdateEvent': 'beforeUpdate', /*'PreLoadEvent': 'beforeLoad',*/
'PreDeleteEvent': 'beforeDelete', 'ValidationEvent': 'beforeValidate', 'PostInsertEvent': 'afterInsert',
'PostUpdateEvent': 'afterUpdate', 'PostDeleteEvent': 'afterDelete', /*'PostLoadEvent': 'afterLoad',*/
'SaveOrUpdateEvent': 'onSaveOrUpdate'
]
}
}
grailsEventsGormBridge(GormBridgePublisher) {
gormTopicSupport = ref("gormTopicSupport")
grailsEvents = ref("grailsEvents")
}
}
}
}
def doWithDynamicMethods = { ctx ->
def config = ctx.grailsApplication.config.plugin.platformCore
ctx.grailsInjection.initInjections()
if (!config.events.disabled)
ctx.grailsEvents.reloadListeners()
}
def doWithConfigOptions = {
legacyPrefix = 'grails.plugin.platform'
'organization.name'(type: String, defaultValue: 'My Corp (set plugin.platformCore.organization.name)')
'site.name'(type: String, defaultValue: 'Our App (set plugin.platformCore.site.name)')
'site.url'(type: String, defaultValue: null)
'show.startup.info'(type: Boolean, defaultValue: true)
'navigation.disabled'(type: Boolean, defaultValue: false)
'events.disabled'(type: Boolean, defaultValue: false)
'events.poolSize'(type: Integer, defaultValue: 10)
'events.catchFlushExceptions'(type: Boolean, defaultValue: true)
'events.gorm.disabled'(type: Boolean, defaultValue: false)
'security.disabled'(type: Boolean, defaultValue: false)
'ui.disabled'(type: Boolean, defaultValue: false)
}
def doWithConfig = {
}
def doWithInjection = { ctx ->
def config = ctx.grailsApplication.config.plugin.platformCore
if (!config.security.disabled) {
register ctx.grailsSecurity.injectedMethods
}
if (!config.config.disabled) {
register ctx.grailsPluginConfiguration.injectedMethods
}
if (!config.events.disabled) {
register ctx.grailsEvents.injectedMethods
}
if (!config.ui.disabled) {
register ctx.grailsUiExtensions.injectedMethods
}
}
def doWithApplicationContext = { applicationContext ->
}
def onConfigChange = { event ->
event.application.mainContext.grailsPluginConfiguration.reload()
}
def onChange = { event ->
def ctx = event.application.mainContext
def config = event.application.config.plugin.platformCore
def navArtefactType = getNavigationArtefactHandler().TYPE
def eventArtefactType = getEventsArtefactHandler().TYPE
if (event.source instanceof Class) {
if (!config.navigation.disabled && application.isArtefactOfType(navArtefactType, event.source)) {
// Update the app with the new class
event.application.addArtefact(navArtefactType, event.source)
ctx.grailsNavigation.reload(event.source)
} else if (!config.events.disabled && application.isArtefactOfType(eventArtefactType, event.source)) {
event.application.addArtefact(eventArtefactType, event.source)
ctx.grailsEvents.reloadListeners()
}
else if (!config.navigation.disabled && application.isArtefactOfType('Controller', event.source)) {
ctx.grailsNavigation.reload() // conventions on controller may have changed
} else if (!config.events.disabled && application.isServiceClass(event.source)) {
ctx.grailsEvents.reloadListener(event.source)
}
// Always do this stuff
ctx.grailsInjection.applyTo(event.source)
}
}
static getNavigationArtefactHandler() {
softLoadClass('org.grails.plugin.platform.navigation.NavigationArtefactHandler')
}
static getEventsArtefactHandler() {
softLoadClass('org.grails.plugin.platform.events.EventsArtefactHandler')
}
static softLoadClass(String className) {
try {
this.getClassLoader().loadClass(className)
} catch (ClassNotFoundException e) {
println "ERROR: Could not load $className"
null
}
}
}