-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFalconeUtilGrailsPlugin.groovy
125 lines (111 loc) · 4.53 KB
/
FalconeUtilGrailsPlugin.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
import com.infusion.util.event.groovy.GroovyEventBroker
import com.infusion.util.domain.event.HibernateEventAdapter
import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass
import com.infusion.util.event.EventType
import org.hibernate.event.LoadEvent
import org.hibernate.event.PreDeleteEvent
import org.hibernate.event.PreUpdateEvent
import org.hibernate.event.PreInsertEvent
import com.infusion.util.event.groovy.EventUtils
import org.hibernate.event.PreInsertEvent
import com.infusion.util.event.groovy.GroovyEventBroker
import org.codehaus.groovy.grails.commons.spring.GrailsApplicationContext
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
import org.codehaus.groovy.grails.plugins.DefaultGrailsPluginManager
import org.codehaus.groovy.grails.plugins.GrailsPlugin
import com.infusion.util.event.EventBroker
import com.infusion.util.event.groovy.builder.EventBuilder
import com.infusion.util.event.groovy.builder.EventConsumerListBuilder
import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.codehaus.groovy.grails.commons.GrailsClass
import com.infusion.util.event.spring.InterceptableSessionFactoryPostProcessor
import org.hibernate.Criteria
import org.hibernate.criterion.Expression
import grails.spring.BeanBuilder
import org.springframework.util.ClassUtils
import grails.util.GrailsUtil
import com.infusion.util.event.EventBrokerHolder
class FalconeUtilGrailsPlugin {
def version = "1.1-DEV"
def dependsOn = [:]
def author = "Eric Martineau"
def authorEmail = "[email protected]"
def title = "Falcone Util Project"
def description = '''\\
The base classes used to support multi-tenancy and the falcone framework
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/FalconeUtil+Plugin"
def doWithSpring = {
//Register the event broker for the system
eventBroker(GroovyEventBroker)
eventBrokerHolder(EventBrokerHolder) {
eventBroker = eventBroker
}
//Register all hibernate events to be published to the event broker
hibernateEventAdapter(HibernateEventAdapter) {
eventBroker = eventBroker
sessionFactory = ref("sessionFactory")
}
//This post-processor wraps the session factory with a tenant-aware one
interceptableSessionFactoryPostProcessor(InterceptableSessionFactoryPostProcessor)
}
def doWithApplicationContext = {GrailsApplicationContext ctx ->
}
def doWithDynamicMethods = {
GrailsApplicationContext ctx ->
EventBroker eventBroker = ctx.eventBroker;
//Attach doWithEvents handler to all plugin classes
DefaultGrailsPluginManager grailsManager = (DefaultGrailsPluginManager) manager;
grailsManager.getAllPlugins().each {
GrailsPlugin plugin ->
if (plugin.getInstance().getProperties().containsKey("doWithEvents")) {
Closure doWithEventsClosure = plugin.getInstance().doWithEvents;
EventConsumerListBuilder builder = new EventConsumerListBuilder();
doWithEventsClosure.delegate = builder;
doWithEventsClosure.resolveStrategy = Closure.DELEGATE_ONLY;
doWithEventsClosure.call(ctx);
builder.getEventConsumers().each {
entry -> eventBroker.subscribe(entry.value, entry.key)
}
}
}
try {
Class groovyEventResourcesClass = null;
try {
groovyEventResourcesClass = ClassUtils.forName("events",
application.classLoader);
} catch (ClassNotFoundException e) {
// ignore
}
if (groovyEventResourcesClass != null) {
EventConsumerListBuilder eventConsumerListBuilder = new EventConsumerListBuilder();
Script script = (Script) groovyEventResourcesClass.newInstance();
script.run();
Closure beans = script.getProperty("consumers");
beans.delegate = eventConsumerListBuilder
beans.call()
eventConsumerListBuilder.getEventConsumers().each{entry ->
eventBroker.subscribe(entry.value, entry.key)
}
}
} catch (Exception ex) {
GrailsUtil.deepSanitize(ex);
}
//This is the publish closure to be assigned
def publishClosure = {
String name, Object event ->
eventBroker.publish(name, event)
}
//Attach publish method to domain classes
for (domainClass in application.domainClasses) {
def mc = domainClass.clazz.metaClass
mc.publishEvent = publishClosure
}
//Attach publish method to controllers
for (GrailsClass controller in application.controllerClasses) {
def mc = controller.clazz.metaClass
mc.publishEvent = publishClosure
}
}
}