forked from padcom/grails-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoutingGrailsPlugin.groovy
171 lines (148 loc) · 6 KB
/
RoutingGrailsPlugin.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
import org.apache.camel.groovy.extend.CamelGroovyMethods
import org.apache.camel.model.ChoiceDefinition
import org.apache.camel.model.ProcessorDefinition
import org.grails.plugins.routing.RouteArtefactHandler
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
class RoutingGrailsPlugin {
def version = '1.2.9'
def grailsVersion = '2.0.0 > *'
def dependsOn = [:]
def loadAfter = [ 'mail','controllers', 'services' ]
def artefacts = [ new RouteArtefactHandler() ]
def author = 'Matthias Hryniszak, Chris Navta'
def authorEmail = '[email protected], [email protected]'
def documentation = 'http://grails.org/plugin/routing'
def title = 'Apache Camel Plugin'
def description = 'Provides message routing capabilities using Apache Camel'
def license = "APACHE"
def developers = [
[name: "Matthias Hryniszak", email: "[email protected]"],
[name: "Chris Navta", email: "[email protected]"],
[name: "Arsen A. Gutsal", email: "[email protected]"]
]
def issueManagement = [system: "GITHUB", url: "https://github.com/padcom/grails-routing/issues"]
def scm = [url: "https://github.com/padcom/grails-routing"]
def doWithSpring = {
def config = application.config.grails.routing
def camelContextId = config?.camelContextId ?: 'camelContext'
def useMDCLogging = config?.useMDCLogging ?: false
def streamCache = config?.streamCache ?: false
def trace = config?.trace ?: false
def routeClasses = application.routeClasses
initializeRouteBuilderHelpers()
routeClasses.each { routeClass ->
def fullName = routeClass.fullName
"${fullName}Class"(MethodInvokingFactoryBean) {
targetObject = ref("grailsApplication", true)
targetMethod = "getArtefact"
arguments = [RouteArtefactHandler.ROUTE, fullName]
}
"${fullName}"(ref("${fullName}Class")) { bean ->
bean.factoryMethod = "newInstance"
bean.autowire = "byName"
}
}
xmlns camel:'http://camel.apache.org/schema/spring'
// we don't allow camel autostart regardless to autoStartup value
// this may cause problems if autostarted camel start invoking routes which calls service/controller
// methods, which use dynamically injected methods
// because doWithDynamicMethods is called after doWithSpring
camel.camelContext(id: camelContextId,
useMDCLogging: useMDCLogging,
autoStartup: false,
streamCache: streamCache,
trace: trace) {
def threadPoolProfileConfig = config?.defaultThreadPoolProfile
camel.threadPoolProfile(
id: "defaultThreadPoolProfile",
defaultProfile: "true",
poolSize: threadPoolProfileConfig?.poolSize ?: "10",
maxPoolSize: threadPoolProfileConfig?.maxPoolSize ?: "20",
maxQueueSize: threadPoolProfileConfig?.maxQueueSize ?: "1000",
rejectedPolicy: threadPoolProfileConfig?.rejectedPolicy ?: "CallerRuns")
routeClasses.each { routeClass ->
camel.routeBuilder(ref: "${routeClass.fullName}")
}
camel.template(id: 'producerTemplate')
}
}
def doWithDynamicMethods = { ctx ->
def template = ctx.getBean('producerTemplate')
addDynamicMethods(application.controllerClasses, template);
addDynamicMethods(application.serviceClasses, template);
addDynamicMethods(application.routeClasses, template);
if (isQuartzPluginInstalled(application))
addDynamicMethods(application.jobClasses, template);
// otherwise we autostart camelContext here
if (application.config?.grails.routing.autoStartup ?: true) {
def camelContextId = application.config?.camelContextId ?: 'camelContext'
application.mainContext.getBean(camelContextId).start()
}
}
def watchedResources = [
"file:./grails-app/controllers/**/*Controller.groovy",
"file:./grails-app/services/**/*Service.groovy",
"file:./grails-app/processors/**/*Processor.groovy"
]
def onChange = { event ->
def artifactName = "${event.source.name}"
if (artifactName.endsWith('Controller') || artifactName.endsWith('Service') || artifactName.endsWith('Processor')) {
def parts = artifactName.split("(?<!^)(?=[A-Z])")
//def artifactType = (artifactName.endsWith('Controller')) ? 'controller' : 'service'
def artifactType = parts[1]
def grailsClass = application."${artifactType}Classes".find { it.fullName == artifactName }
addDynamicMethods([grailsClass], event.ctx.getBean('producerTemplate'))
}
}
// ------------------------------------------------------------------------
// Private methods
// ------------------------------------------------------------------------
private initializeRouteBuilderHelpers() {
ProcessorDefinition.metaClass.filter = { filter ->
if (filter instanceof Closure) {
CamelGroovyMethods.filter(delegate, filter)
} else {
delegate.filter(filter)
}
}
ChoiceDefinition.metaClass.when = { filter ->
if (filter instanceof Closure) {
CamelGroovyMethods.when(delegate, filter)
} else {
delegate.when(filter)
}
}
ProcessorDefinition.metaClass.process = { filter ->
if (filter instanceof Closure) {
CamelGroovyMethods.process(delegate, filter)
} else {
delegate.process(filter)
}
}
}
private addDynamicMethods(artifacts, template) {
artifacts?.each { artifact ->
artifact.metaClass.sendMessage = { endpoint,message ->
template.sendBody(endpoint,message)
}
artifact.metaClass.sendMessageAndHeaders = { endpoint, message, headers ->
template.sendBodyAndHeaders(endpoint,message,headers)
}
artifact.metaClass.requestMessage = { endpoint,message ->
template.requestBody(endpoint,message)
}
artifact.metaClass.requestMessageAndHeaders = { endpoint, message, headers ->
template.requestBodyAndHeaders(endpoint, message, headers)
}
}
}
private isQuartzPluginInstalled(application) {
// this is a nasty implementation... maybe there's something better?
try {
def tasks = application.jobClasses
return true
} catch (e) {
return false
}
}
}