forked from gpc/grails-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailGrailsPlugin.groovy
159 lines (131 loc) · 5.16 KB
/
MailGrailsPlugin.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
/*
* Copyright 2008 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 org.springframework.jndi.JndiObjectFactoryBean
import org.springframework.mail.javamail.JavaMailSenderImpl
import grails.plugin.mail.*
class MailGrailsPlugin {
def version = "1.0-SNAP-ITDS-0.1"
def grailsVersion = "1.3 > *"
def author = "Grails Plugin Collective"
def authorEmail = "grails.plugin.collective@gmail.com"
def title = "Provides Mail support to a running Grails application"
def description = '''\
This plug-in provides a MailService class as well as configuring the necessary beans within
the Spring ApplicationContext.
It also adds a "sendMail" method to all controller classes. A typical example usage is:
sendMail {
to "fred@g2one.com","ginger@g2one.com"
from "john@g2one.com"
cc "marge@g2one.com", "ed@g2one.com"
bcc "joe@g2one.com"
subject "Hello John"
text "this is some text"
}
'''
def documentation = "http://plugin.grails.org/mail"
def observe = ['controllers','services']
def mailConfigHash = null
def mailConfig = null
def createdSession = false
def doWithSpring = {
mailConfig = application.config.grails.mail
mailConfigHash = mailConfig.hashCode()
configureMailSender(delegate, mailConfig)
mailMessageBuilderFactory(MailMessageBuilderFactory) {
it.autowire = true
}
mailMessageContentRenderer(MailMessageContentRenderer) {
it.autowire = true
}
}
def doWithApplicationContext = { applicationContext ->
configureSendMail(application, applicationContext)
}
def onChange = { event ->
configureSendMail(event.application, event.ctx)
}
def onConfigChange = { event ->
def newMailConfig = event.source.grails.mail
def newMailConfigHash = newMailConfig.hashCode()
if (newMailConfigHash != mailConfigHash) {
if (createdSession) {
event.ctx.removeBeanDefinition("mailSession")
}
event.ctx.removeBeanDefinition("mailSender")
mailConfig = newMailConfig
mailConfigHash = newMailConfigHash
def newBeans = beans {
configureMailSender(delegate, mailConfig)
}
newBeans.beanDefinitions.each { name, definition ->
event.ctx.registerBeanDefinition(name, definition)
}
}
}
def configureMailSender(builder, config) {
builder.with {
if (config.jndiName && !springConfig.containsBean("mailSession")) {
mailSession(JndiObjectFactoryBean) {
jndiName = config.jndiName
}
createdSession = true
} else {
createdSession = false
}
mailSender(JavaMailSenderImpl) {
if (config.host) {
host = config.host
} else if (!config.jndiName) {
host = "localhost"
}
if (config.encoding) {
defaultEncoding = config.encoding
} else if (!config.jndiName) {
defaultEncoding = "utf-8"
}
if (config.jndiName)
session = ref('mailSession')
if (config.port)
port = config.port
if (config.username)
username = config.username
if (config.password)
password = config.password
if (config.protocol)
protocol = config.protocol
if (config.props instanceof Map && config.props)
javaMailProperties = config.props
}
}
}
def configureSendMail(application, applicationContext) {
//adding sendMail to controllers
for (controllerClass in application.controllerClasses) {
controllerClass.metaClass.sendMail = { Closure dsl ->
applicationContext.mailService.sendMail(dsl)
}
}
def mailServiceClassName = applicationContext.mailService.class.name
//adding sendMail to all services, besides the mailService of the plugin
/* for (serviceClass in application.serviceClasses) {
if (serviceClass.clazz.name != mailServiceClassName) {
serviceClass.metaClass.sendMail = { Closure dsl ->
applicationContext.mailService.sendMail(dsl)
}
}
} */
}
}