-
Notifications
You must be signed in to change notification settings - Fork 6
/
CamelGrailsPlugin.groovy
executable file
·154 lines (126 loc) · 4.47 KB
/
CamelGrailsPlugin.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
import org.apache.camel.spring.CamelContextFactoryBean
import org.ix.grails.plugins.camel.*
import org.ix.test.*
import grails.util.GrailsNameUtils
import org.apache.camel.model.ProcessorType
import org.apache.camel.model.ChoiceType
import org.apache.camel.language.groovy.CamelGroovyMethods
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
import org.apache.log4j.Logger
class CamelGrailsPlugin {
private static final Logger log = Logger.getLogger('org.ix.grails.plugins.camel.CamelGrailsPlugin')
// the plugin version
def version = "0.2"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.1 > *"
// the other plugins this plugin depends on
def dependsOn = [:]
// resources that are excluded from plugin packaging
def loadAfter = ['controllers','services']
def pluginExcludes = [
"grails-app/views/error.gsp"
]
def watchedResources = [
"file:./grails-app/routes/**/*Route.groovy",
"file:./plugins/*/grails-app/routes/**/*Route.groovy",
"file:./grails-app/controllers/**/*Controller.groovy",
"file:./grails-app/services/**/*Service.groovy"
]
def artefacts = [new RouteArtefactHandler()]
def author = "Chris Navta"
def authorEmail = "[email protected]"
def title = "Integration with Apache Camel"
def description = '''\\
This plugin provides an integration with Apache Camel (http://camel.apache.org), giving Controllers and Services a 'sendMessage' method that will send
a message to a given endpoint.
It also adds a 'Route' artifact that allows configuration of Camel routing using the Java DSL. New Routes can be
added with the 'grails create-route MyRouteName' command.
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/Camel+Plugin"
def doWithSpring = {
init()
xmlns camel:'http://activemq.apache.org/camel/schema/spring'
def routeClasses = application.routeClasses
log.debug "Configuring Routes"
routeClasses.each { routeClass ->
configureRouteBeans.delegate = delegate
configureRouteBeans(routeClass)
}
camel {
camelContext(id:'camelContext')
{
routeClasses.each { routeClass ->
routeBuilderRef(ref:"${routeClass.fullName}")
}
template(id:'producerTemplate')
}
}
}
def doWithApplicationContext = { applicationContext ->
}
def doWithWebDescriptor = { xml ->
}
def doWithDynamicMethods = { ctx ->
this.addMethods(application.controllerClasses,ctx);
this.addMethods(application.serviceClasses,ctx);
}
def onChange = { event ->
def artifactName = "${event.source.name}"
log.debug "Detected a change in ${artifactName}"
if (artifactName.endsWith('Controller') || artifactName.endsWith('Service'))
{
def artifactType = (artifactName.endsWith('Controller')) ? 'controller' : 'service'
log.debug "It's a ${artifactType}"
def grailsClass = application."${artifactType}Classes".find { it.fullName == artifactName }
this.addMethods([grailsClass],event.ctx)
}
}
def onConfigChange = { event ->
}
private init() {
log.debug "Adding Groovy-ish methods to RouteBuilder Helpers"
ProcessorType.metaClass.filter = {filter ->
if (filter instanceof Closure) {
filter = CamelGroovyMethods.toExpression(filter)
}
delegate.filter(filter);
}
ChoiceType.metaClass.when = {filter ->
if (filter instanceof Closure) {
filter = CamelGroovyMethods.toExpression(filter)
}
delegate.when(filter);
}
ProcessorType.metaClass.process = {filter ->
if (filter instanceof Closure) {
filter = new ClosureProcessor(filter)
}
delegate.process(filter);
}
}
private configureRouteBeans = { routeClazz ->
def fullName = routeClazz.fullName
"${fullName}RouteClass"(MethodInvokingFactoryBean) {
targetObject = ref("grailsApplication",true)
targetMethod = "getArtefact"
arguments = [RouteArtefactHandler.TYPE, fullName]
}
"${fullName}"(GrailsRouteBuilderFactoryBean)
{
routeClass = ref("${fullName}RouteClass")
}
}
private addMethods(artifacts,ctx) {
log.debug "Adding dynamic methods to ${artifacts}"
def template = ctx.getBean('producerTemplate')
artifacts.each { artifact ->
artifact.metaClass.sendMessage = { endpoint,message ->
template.sendBody(endpoint,message)
}
artifact.metaClass.requestMessage = { endpoint,message ->
template.requestBody(endpoint,message)
}
}
}
}