forked from noamt/elasticsearch-grails-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElasticsearchGrailsPlugin.groovy
executable file
·178 lines (155 loc) · 7.48 KB
/
ElasticsearchGrailsPlugin.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
/*
* Copyright 2002-2011 the original author or authors.
*
* 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.Environment
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.grails.plugins.elasticsearch.AuditEventListener
import org.grails.plugins.elasticsearch.ClientNodeFactoryBean
import org.grails.plugins.elasticsearch.ElasticSearchContextHolder
import org.grails.plugins.elasticsearch.ElasticSearchHelper
import org.grails.plugins.elasticsearch.conversion.CustomEditorRegistrar
import org.grails.plugins.elasticsearch.conversion.JSONDomainFactory
import org.grails.plugins.elasticsearch.conversion.unmarshall.DomainClassUnmarshaller
import org.grails.plugins.elasticsearch.index.IndexRequestQueue
import org.grails.plugins.elasticsearch.mapping.SearchableClassMappingConfigurator
import org.grails.plugins.elasticsearch.unwrap.DomainClassUnWrapperChain
import org.grails.plugins.elasticsearch.unwrap.HibernateProxyUnWrapper
import org.grails.plugins.elasticsearch.util.DomainDynamicMethodsUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
class ElasticsearchGrailsPlugin {
private static final Logger LOG = LoggerFactory.getLogger(this)
def version = '0.0.3.3'
def grailsVersion = '2.2.0 > *'
def loadAfter = ['services', 'mongodb']
def pluginExcludes = [
'grails-app/controllers/test/**',
'grails-app/services/test/**',
'grails-app/views/elasticSearch/index.gsp',
'grails-app/domain/test/**',
'grails-app/utils/test/**',
'test/**',
'src/docs/**',
'src/groovy/test/**',
]
def license = 'APACHE'
def organization = [name: '10ne.org', url: 'http://www.10ne.org/']
def developers = [
[name: 'Noam Y. Tenne', email: '[email protected]']
]
def issueManagement = [system: 'github', url: 'https://github.com/noamt/elasticsearch-grails-plugin/issues']
def scm = [url: 'https://github.com/noamt/elasticsearch-grails-plugin']
def author = 'Noam Y. Tenne'
def authorEmail = '[email protected]'
def title = 'ElasticSearch Grails Plugin'
def description = """The revived Elasticsearch plugin for Grails."""
def documentation = 'http://noamt.github.io/elasticsearch-grails-plugin'
def doWithSpring = {
def esConfig = getConfiguration(application)
elasticSearchContextHolder(ElasticSearchContextHolder) {
config = esConfig
}
elasticSearchHelper(ElasticSearchHelper) {
elasticSearchClient = ref('elasticSearchClient')
}
elasticSearchClient(ClientNodeFactoryBean) { bean ->
elasticSearchContextHolder = ref('elasticSearchContextHolder')
bean.destroyMethod = 'shutdown'
}
indexRequestQueue(IndexRequestQueue) {
elasticSearchContextHolder = ref('elasticSearchContextHolder')
elasticSearchClient = ref('elasticSearchClient')
jsonDomainFactory = ref('jsonDomainFactory')
}
searchableClassMappingConfigurator(SearchableClassMappingConfigurator) { bean ->
elasticSearchContext = ref('elasticSearchContextHolder')
grailsApplication = ref('grailsApplication')
elasticSearchClient = ref('elasticSearchClient')
config = esConfig
bean.initMethod = 'configureAndInstallMappings'
}
domainInstancesRebuilder(DomainClassUnmarshaller) {
elasticSearchContextHolder = ref('elasticSearchContextHolder')
elasticSearchClient = ref('elasticSearchClient')
grailsApplication = ref('grailsApplication')
}
customEditorRegistrar(CustomEditorRegistrar) {
grailsApplication = ref('grailsApplication')
}
if (manager?.hasGrailsPlugin('hibernate') || manager?.hasGrailsPlugin('hibernate4')) {
hibernateProxyUnWrapper(HibernateProxyUnWrapper)
}
domainClassUnWrapperChain(DomainClassUnWrapperChain)
jsonDomainFactory(JSONDomainFactory) {
elasticSearchContextHolder = ref('elasticSearchContextHolder')
grailsApplication = ref('grailsApplication')
domainClassUnWrapperChain = ref('domainClassUnWrapperChain')
}
if (!esConfig.disableAutoIndex) {
if (!esConfig.datastoreImpl) {
throw new Exception('No datastore implementation specified')
}
auditListener(AuditEventListener, ref(esConfig.datastoreImpl)) {
elasticSearchContextHolder = ref('elasticSearchContextHolder')
indexRequestQueue = ref('indexRequestQueue')
}
}
}
def doWithDynamicMethods = { ctx ->
// Define the custom ElasticSearch mapping for searchable domain classes
DomainDynamicMethodsUtils.injectDynamicMethods(application, ctx)
}
// Get a configuration instance
private getConfiguration(GrailsApplication application) {
def config = application.config
// try to load it from class file and merge into GrailsApplication#config
// Config.groovy properties override the default one
try {
Class dataSourceClass = application.getClassLoader().loadClass('DefaultElasticSearch')
ConfigSlurper configSlurper = new ConfigSlurper(Environment.current.name)
Map binding = [:]
binding.userHome = System.properties['user.home']
binding.grailsEnv = application.metadata['grails.env']
binding.appName = application.metadata['app.name']
binding.appVersion = application.metadata['app.version']
configSlurper.binding = binding
ConfigObject defaultConfig = configSlurper.parse(dataSourceClass)
ConfigObject newElasticSearchConfig = new ConfigObject()
newElasticSearchConfig.putAll(defaultConfig.elasticSearch.merge(config.elasticSearch))
config.elasticSearch = newElasticSearchConfig
application.configChanged()
return config.elasticSearch
} catch (ClassNotFoundException e) {
LOG.debug("ElasticSearch default configuration file not found: ${e.message}")
}
// Here the default configuration file was not found, so we
// try to get it from GrailsApplication#config and add some mandatory default values
if (config.containsKey('elasticSearch')) {
if (!config.elasticSearch.date?.formats) {
config.elasticSearch.date.formats = ['yyyy-MM-dd\'T\'HH:mm:ss\'Z\'']
}
if (config.elasticSearch.unmarshallComponents == [:]) {
config.elasticSearch.unmarshallComponents = true
}
application.configChanged()
return config.elasticSearch
}
// No config found, add some default and obligatory properties
config.elasticSearch.date.formats = ['yyyy-MM-dd\'T\'HH:mm:ss\'Z\'']
config.elasticSearch.unmarshallComponents = true
application.configChanged()
return config
}
}