forked from kefirfromperm/grails-asynchronous-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsynchronousMailGrailsPlugin.groovy
124 lines (107 loc) · 4.79 KB
/
AsynchronousMailGrailsPlugin.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
import grails.plugin.asyncmail.AsynchronousMailJob
import grails.plugin.asyncmail.AsynchronousMailMessageBuilderFactory
import grails.plugin.asyncmail.ExpiredMessagesCollectorJob
import grails.plugin.mail.MailService
import grails.util.Environment
import org.codehaus.groovy.grails.commons.spring.GrailsApplicationContext
class AsynchronousMailGrailsPlugin {
def version = "1.0-RC6"
def grailsVersion = "2.0.0 > *"
def dependsOn = [mail:'1.0.1 > *', quartz:'1.0 > *', hibernate:'2.0.0 > *']
def pluginExcludes = [
"grails-app/conf/DataSource.groovy",
"grails-app/i18n/**",
"grails-app/views/test/**",
"web-app/WEB-INF/**",
"web-app/images/**",
"web-app/js/**",
"web-app/css/errors.css",
"web-app/css/main.css",
"web-app/css/mobile.css"
]
def author = "Vitalii Samolovskikh aka Kefir"
def authorEmail = "[email protected]"
def title = "Asynchronous Mail Plugin"
def description = 'The plugin realises asynchronous mail sending. ' +
'It stores messages in the DB and sends them asynchronously by the quartz job.'
def documentation = "http://www.grails.org/plugin/asynchronous-mail"
String license = 'APACHE'
def issueManagement = [system: 'JIRA', url: 'http://jira.grails.org/browse/GPASYNCHRONOUSMAIL']
def scm = [url: 'https://github.com/kefirfromperm/grails-asynchronous-mail']
def doWithSpring = {
loadAsyncMailConfig(application.config)
// The mail service from Mail plugin
nonAsynchronousMailService(MailService) {
mailMessageBuilderFactory = ref("mailMessageBuilderFactory")
grailsApplication = ref("grailsApplication")
}
asynchronousMailMessageBuilderFactory(AsynchronousMailMessageBuilderFactory) {
it.autowire = true
}
}
def doWithApplicationContext = { GrailsApplicationContext applicationContext ->
// Register alias for the asynchronousMailService
applicationContext.registerAlias('asynchronousMailService', 'asyncMailService')
// Configure sendMail methods
configureSendMail(application, applicationContext)
// Starts jobs
startJobs(application)
}
def onChange = { event ->
// Configure sendMail methods
configureSendMail(application, (GrailsApplicationContext) event.ctx)
}
/**
* Start send job and messages collector
*/
static startJobs(application) {
def asyncMailConfig = application.config.asynchronous.mail
if (!asyncMailConfig.disable) {
AsynchronousMailJob.schedule((Long) asyncMailConfig.send.repeat.interval)
ExpiredMessagesCollectorJob.schedule((Long) asyncMailConfig.expired.collector.repeat.interval)
}
}
/**
* Configure sendMail methods
*/
static configureSendMail(application, GrailsApplicationContext applicationContext){
def asyncMailConfig = application.config.asynchronous.mail
// Override the mailService
if (asyncMailConfig.override) {
applicationContext.mailService.metaClass*.sendMail = { Closure callable ->
applicationContext.asynchronousMailService?.sendAsynchronousMail(callable)
}
} else {
applicationContext.asynchronousMailService.metaClass*.sendMail = { Closure callable ->
applicationContext.asynchronousMailService?.sendAsynchronousMail(callable)
}
}
}
/**
* Loads the asynchronous mail configuration.
*
* 1. Loads the grails configuration.
* 2. Merges it with the default asynchronous mail configuration.
* 3. Merges it with the user asynchronous mail configuration.
*
* http://swestfall.blogspot.co.uk/2011/08/grails-plugins-and-default-configs.html
*/
private void loadAsyncMailConfig(def config) {
GroovyClassLoader classLoader = new GroovyClassLoader(getClass().classLoader)
// merging default config into main application config
ConfigObject currentAsyncConfig = config.asynchronous.mail
ConfigObject defaultAsyncConfig = new ConfigSlurper(Environment.current.name)
.parse(classLoader.loadClass('DefaultAsynchronousMailConfig'))
ConfigObject newAsyncConfig = new ConfigObject()
newAsyncConfig.putAll( defaultAsyncConfig.asynchronous.mail.merge(currentAsyncConfig))
config.asynchronous.mail = newAsyncConfig
// merging user-defined config into main application config if provided
try {
config.merge(new ConfigSlurper(Environment.current.name).parse(
classLoader.loadClass('AsynchronousMailConfig'))
)
} catch (Exception ignored) {
// ignore, just use the defaults
}
}
}