James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
bdfdc9 1 /*
JM 2  * Copyright 2013 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.manager;
17
18 import java.util.Collection;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
22
23 import javax.mail.Message;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.gitblit.IStoredSettings;
29 import com.gitblit.Keys;
d7c5a6 30 import com.gitblit.models.Mailing;
7bf6e1 31 import com.gitblit.service.MailService;
f9980e 32 import com.google.inject.Inject;
JM 33 import com.google.inject.Singleton;
bdfdc9 34
JM 35 /**
36  * The notification manager dispatches notifications.  Currently, email is the
37  * only supported transport, however there is no reason why other transports
38  * could be supported (tweets, irc, sms, etc).
39  *
40  * @author James Moger
41  *
42  */
f9980e 43 @Singleton
bdfdc9 44 public class NotificationManager implements INotificationManager {
JM 45
46     private final Logger logger = LoggerFactory.getLogger(getClass());
47
48     private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
49
50     private final IStoredSettings settings;
51
d7c5a6 52     private final MailService mailService;
bdfdc9 53
1b34b0 54     @Inject
bdfdc9 55     public NotificationManager(IStoredSettings settings) {
JM 56         this.settings = settings;
d7c5a6 57         this.mailService = new MailService(settings);
bdfdc9 58     }
JM 59
60     @Override
269c50 61     public NotificationManager start() {
d7c5a6 62         if (mailService.isReady()) {
269c50 63             int period = 2;
JM 64             logger.info("Mail service will process the queue every {} minutes.", period);
d7c5a6 65             scheduledExecutor.scheduleAtFixedRate(mailService, 1, period, TimeUnit.MINUTES);
bdfdc9 66         } else {
269c50 67             logger.warn("Mail service disabled.");
bdfdc9 68         }
JM 69         return this;
70     }
71
72     @Override
269c50 73     public NotificationManager stop() {
bdfdc9 74         scheduledExecutor.shutdownNow();
JM 75         return this;
76     }
77
74221e 78     @Override
JM 79     public boolean isSendingMail() {
80         return mailService.isReady();
81     }
82
bdfdc9 83     /**
JM 84      * Notify the administrators by email.
85      *
86      * @param subject
87      * @param message
88      */
89     @Override
90     public void sendMailToAdministrators(String subject, String message) {
d7c5a6 91         Mailing mail = Mailing.newPlain();
JM 92         mail.subject = subject;
93         mail.content = message;
94         mail.setRecipients(settings.getStrings(Keys.mail.adminAddresses));
95         send(mail);
bdfdc9 96     }
JM 97
98     /**
99      * Notify users by email of something.
100      *
101      * @param subject
102      * @param message
103      * @param toAddresses
104      */
105     @Override
106     public void sendMail(String subject, String message, Collection<String> toAddresses) {
d7c5a6 107         Mailing mail = Mailing.newPlain();
JM 108         mail.subject = subject;
109         mail.content = message;
110         mail.setRecipients(toAddresses);
111         send(mail);
bdfdc9 112     }
JM 113
114     /**
115      * Notify users by email of something.
116      *
117      * @param subject
118      * @param message
119      * @param toAddresses
120      */
121     @Override
122     public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {
d7c5a6 123         Mailing mail = Mailing.newHtml();
22f90c 124         mail.subject = subject;
d7c5a6 125         mail.content = message;
JM 126         mail.setRecipients(toAddresses);
127         send(mail);
afaab5 128     }
JM 129
130     /**
131      * Notify users by email of something.
132      *
d7c5a6 133      * @param mailing
afaab5 134      */
JM 135     @Override
d7c5a6 136     public void send(Mailing mailing) {
JM 137         if (!mailing.hasRecipients()) {
138             logger.debug("Dropping message {} because there are no recipients", mailing.subject);
bdfdc9 139             return;
JM 140         }
d7c5a6 141         Message msg = mailService.createMessage(mailing);
JM 142         if (msg != null) {
143             mailService.queue(msg);
bdfdc9 144         }
JM 145     }
146
147 }