1616package grails.plugins.mail
1717
1818import grails.config.Config
19- import grails.core.support.GrailsConfigurationAware
2019import groovy.transform.CompileStatic
2120import groovy.util.logging.Slf4j
2221import org.springframework.beans.factory.DisposableBean
2322import org.springframework.beans.factory.InitializingBean
23+ import org.springframework.boot.context.properties.ConfigurationProperties
24+ import org.springframework.boot.context.properties.bind.Bindable
25+ import org.springframework.boot.context.properties.bind.Binder
26+ import org.springframework.boot.context.properties.source.ConfigurationPropertySource
27+ import org.springframework.boot.context.properties.source.ConfigurationPropertySources
28+ import org.springframework.core.env.PropertiesPropertySource
29+ import org.springframework.core.env.PropertySource
2430import org.springframework.mail.MailMessage
2531
2632import java.util.concurrent.LinkedBlockingQueue
2733import java.util.concurrent.ThreadPoolExecutor
2834import java.util.concurrent.TimeUnit
35+
2936/**
3037 * Provides the entry point to the mail sending API.
3138 */
3239@Slf4j
3340@CompileStatic
34- class MailService implements InitializingBean , DisposableBean , GrailsConfigurationAware {
41+ class MailService implements InitializingBean , DisposableBean {
3542
3643 static transactional = false
3744
38- MailConfig configuration
45+ MailConfigurationProperties mailConfigurationProperties
3946 MailMessageBuilderFactory mailMessageBuilderFactory
4047 ThreadPoolExecutor mailExecutorService
4148
4249 private static final Integer DEFAULT_POOL_SIZE = 5
4350
44- MailMessage sendMail (MailConfig config , @DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = MailMessageBuilder ) Closure callable ) {
51+ private static final Bindable<MailConfigurationProperties > CONFIG_BINDABLE = Bindable . of(MailConfigurationProperties )
52+
53+ MailMessage sendMail (MailConfigurationProperties properties , @DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = MailMessageBuilder ) Closure callable ) {
4554 if (isDisabled()) {
4655 log. warn(" Sending emails disabled by configuration option" )
4756 return
4857 }
4958
50- MailMessageBuilder messageBuilder = mailMessageBuilderFactory. createBuilder(config )
59+ MailMessageBuilder messageBuilder = mailMessageBuilderFactory. createBuilder(properties )
5160 callable. delegate = messageBuilder
5261 callable. resolveStrategy = Closure . DELEGATE_FIRST
5362 callable. call(messageBuilder)
@@ -56,28 +65,29 @@ class MailService implements InitializingBean, DisposableBean, GrailsConfigurati
5665 }
5766
5867 MailMessage sendMail (Config config , @DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = MailMessageBuilder ) Closure callable ) {
59- return sendMail(new MailConfig (config), callable)
68+ return sendMail(toMailProperties (config), callable)
6069 }
6170
6271 MailMessage sendMail (@DelegatesTo (strategy = Closure .DELEGATE_FIRST , value = MailMessageBuilder ) Closure callable ) {
63- return sendMail(configuration , callable)
72+ return sendMail(mailConfigurationProperties , callable)
6473 }
6574
75+ private static MailConfigurationProperties toMailProperties (Config config ) {
76+ PropertySource propertySource = new PropertiesPropertySource (' mailProperties' , config. toProperties())
77+ Iterable<ConfigurationPropertySource > configurationPropertySources = ConfigurationPropertySources . from(propertySource)
78+ Binder binder = new Binder (configurationPropertySources)
79+ return binder. bind(MailConfigurationProperties . PREFIX , CONFIG_BINDABLE ). get()
80+ }
6681
6782 boolean isDisabled () {
68- configuration . disabled
83+ mailConfigurationProperties . disabled
6984 }
7085
7186 void setPoolSize (Integer poolSize ){
7287 mailExecutorService. setMaximumPoolSize(poolSize ?: DEFAULT_POOL_SIZE )
7388 mailExecutorService. setCorePoolSize(poolSize ?: DEFAULT_POOL_SIZE )
7489 }
7590
76- @Override
77- void setConfiguration (Config config ) {
78- configuration = new MailConfig (config)
79- }
80-
8191 @Override
8292 public void destroy () throws Exception {
8393 mailExecutorService. shutdown()
@@ -88,7 +98,7 @@ class MailService implements InitializingBean, DisposableBean, GrailsConfigurati
8898 public void afterPropertiesSet () throws Exception {
8999 mailExecutorService = new ThreadPoolExecutor (1 , 1 , 60 , TimeUnit . SECONDS , new LinkedBlockingQueue<Runnable > ())
90100
91- Integer poolSize = configuration . poolSize
101+ Integer poolSize = mailConfigurationProperties . poolSize
92102 try {
93103 ((ThreadPoolExecutor )mailExecutorService). allowCoreThreadTimeOut(true )
94104 }catch (MissingMethodException e){
0 commit comments