-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDelayedDatasourcesGrailsPlugin.groovy
90 lines (72 loc) · 3.19 KB
/
DelayedDatasourcesGrailsPlugin.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
import com.simplicityitself.grails.plugin.delayeddatasource.DataSourcePostProcessor
import com.simplicityitself.grails.plugin.delayeddatasource.proxies.LazyDataSource
import org.codehaus.groovy.grails.commons.ConfigurationHolder
import org.springframework.context.ApplicationContext
import org.springframework.jdbc.support.lob.DefaultLobHandler
import org.springframework.jdbc.support.lob.OracleLobHandler
import org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor
import org.springframework.jmx.export.MBeanExporter
import org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler
import org.springframework.jmx.support.MBeanServerFactoryBean
class DelayedDatasourcesGrailsPlugin {
def version = "0.3"
def grailsVersion = "1.3.7 > *"
def title = "Delayed Datasources Plugin"
def author = "David Dawson"
def authorEmail = "[email protected]"
def description = '''\
Reconfigures and replaces datasource handling with versions that do not require a database on startup.
Requires some changes to datasource config, which this plugin will attempt to apply automatically and fail fast if this doesn't work
'''
def documentation = "https://github.com/simplicityitself/grails-delayed-datasources"
def license = "APACHE"
def organization = [name: "Simplicity Itself", url: "http://www.simplicityitself.com/"]
def issueManagement = [system: "GitHub", url: "https://github.com/simplicityitself/grails-delayed-datasources/issues"]
def scm = [url: "https://github.com/simplicityitself/grails-delayed-datasources"]
def doWithSpring = {
if (pluginEnabled) {
log.info "Delayed Datasources Activated"
datasourcePostProcessor(DataSourcePostProcessor)
lobHandlerOverrideOracle(OracleLobHandler)
lobHandlerOverrideOraclePooled(OracleLobHandler) {
nativeJdbcExtractor = new CommonsDbcpNativeJdbcExtractor()
}
lobHandlerOverride(DefaultLobHandler)
mbeanServer(MBeanServerFactoryBean) {
locateExistingServerIfPossible = true
}
delayedDataSourceJmxAssembler(MethodNameBasedMBeanInfoAssembler) {
managedMethods = [
"testConnection",
"isCreated",
"isCurrentlyAvailable",
"getLastError"
]
}
delayedDataSourceExporter(MBeanExporter) {
server = mbeanServer
assembler = ref("delayedDataSourceJmxAssembler")
beans = [:]
}
} else {
log.info "Delayed Datasources Disabled"
}
}
def doWithApplicationContext = { applicationContext ->
if (pluginEnabled) {
exportDatasourcesAsMBeans(applicationContext)
}
}
def exportDatasourcesAsMBeans(ApplicationContext applicationContext) {
def exporter = applicationContext.getBean("delayedDataSourceExporter")
def datasources = applicationContext.getBeansOfType(LazyDataSource)
datasources.each { name, ds ->
def mbeanName = "Data Sources:type=datasource,datasource=${name}"
println "Exporting : $mbeanName"
exporter.beans."${mbeanName}" = ds
}
}
boolean isPluginEnabled() {
ConfigurationHolder.config.delayed_datasource.enabled == "true" || ConfigurationHolder.config.delayed_datasource.enabled == true
}
}