forked from burtbeckwith/grails-webxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebxmlGrailsPlugin.groovy
172 lines (153 loc) · 5.17 KB
/
WebxmlGrailsPlugin.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
import grails.util.Environment
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* Copyright 2008 Roger Cass ([email protected])
*
* 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.
*
* History
* 1.0 Roger Cass, Filter/BeanMapping
* 1.1 ericacm - gmail.com, Listener
* 1.2 Bob Schulze al.lias - web.de, Context Parameters
* 1.3 Burt Beckwith; added new feature to order filter-mapping elements
* 1.4 Stefano Gualdi; added patch from Daniel Bower to support session-timeout setting
*/
class WebxmlGrailsPlugin {
private static final String DEFAULT_CONFIG_FILE = 'DefaultWebXmlConfig'
private static final String APP_CONFIG_FILE = 'WebXmlConfig'
private Logger log = LoggerFactory.getLogger('grails.plugin.webxml.WebxmlGrailsPlugin')
def version = '1.5'
def grailsVersion = '1.2 > *'
def author = 'Roger Cass'
def authorEmail = '[email protected]'
def title = 'web.xml plugin'
def description = 'Add additional features to your web.xml, such as Filters, Config Listeners, Context Parameter definitions, or Resourse Refs'
def documentation = 'http://grails.org/plugin/webxml'
def license = 'APACHE'
def issueManagement = [system: 'JIRA', url: 'http://jira.grails.org/browse/GPWEBXML']
def scm = [url: 'http://plugins.grails.org/grails-webxml/']
def developers = [
[name: 'Eric Pederson', email: '[email protected]'],
[name: 'Bob Schulze', email: '[email protected]'],
[name: 'Burt Beckwith', email: '[email protected]'],
[name: 'Stefano Gualdi', email: '[email protected]'],
[name: 'Harvey McQueen', email: '[email protected]']
]
def doWithWebDescriptor = { xml ->
def config = getConfig()
if (!config) {
return
}
if (config.filterChainProxyDelegator.add) {
def contextParam = xml.'context-param'
contextParam[contextParam.size() - 1] + {
'filter' {
'filter-name'(config.filterChainProxyDelegator.filterName)
'filter-class'(config.filterChainProxyDelegator.className)
'init-param' {
'param-name'('targetBeanName')
'param-value'(config.filterChainProxyDelegator.targetBeanName)
}
}
}
def filter = xml.'filter'
filter[filter.size() - 1] + {
'filter-mapping' {
'filter-name'(config.filterChainProxyDelegator.filterName)
'url-pattern'(config.filterChainProxyDelegator.urlPattern)
}
}
}
if (config.listener.add) {
def listenerNode = xml.'listener'
for (String className in config.listener.classNames) {
listenerNode[listenerNode.size() - 1] + {
listener {
'listener-class'(className)
}
}
}
}
// add possibility for context params. As with the other features, the generated result
// is a bit thin, it could contain the descriptions field too, a context.xml also would
// be a good idea... (bs)
if (config.contextparams) {
config.contextparams.each { String name, String value ->
def contextParam = xml.'context-param'
contextParam[contextParam.size() - 1] + {
'context-param' {
'param-name'(name)
'param-value'(value)
}
}
}
}
//session Timeout
if (config.sessionConfig.sessionTimeout instanceof Integer) {
def contextParam = xml.'context-param'
contextParam[contextParam.size() - 1] + {
'session-config'{
'session-timeout'(config.sessionConfig.sessionTimeout)
}
}
}
// resource-refs
if (config.resourcerefs) {
def nodes = xml.'resource-ref'
if(nodes.isEmpty()) {
nodes = xml.'jsp-config'
}
config.resourcerefs.each {
def ref = it
nodes[nodes.size() - 1] + {
'resource-ref'{
ref.each { String name, String value ->
"$name"(value)
}
}
}
}
}
if (log.isTraceEnabled()) {
log.trace new StreamingMarkupBuilder().bind { out << xml }
}
}
private getConfig() {
GroovyClassLoader loader = new GroovyClassLoader(getClass().classLoader)
def config
try {
def defaultConfigFile = loader.loadClass(DEFAULT_CONFIG_FILE)
log.info "Loading default config file: $defaultConfigFile"
config = new ConfigSlurper(Environment.current.name).parse(defaultConfigFile)
try {
def appConfigFile = loader.loadClass(APP_CONFIG_FILE)
log.info "Found application config file: $appConfigFile"
def appConfig = new ConfigSlurper(Environment.current.name).parse(appConfigFile)
if (appConfig) {
log.info "Merging application config file: $appConfigFile"
config = config.merge(appConfig)
}
}
catch (ClassNotFoundException e) {
log.warn "Did not find application config file: $APP_CONFIG_FILE"
}
}
catch (ClassNotFoundException e) {
log.error "Did not find default config file: $DEFAULT_CONFIG_FILE"
}
config?.webxml
}
}