1
2
3
4
5 package org.abraracourcix.alipes.listeners.email;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.util.Date;
11 import java.util.Enumeration;
12 import java.util.Properties;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15
16 import org.abraracourcix.alipes.common.Event;
17 import org.abraracourcix.alipes.common.messaging.EmailMessenger;
18 import org.abraracourcix.alipes.common.messaging.EmailMessengerFactory;
19 import org.abraracourcix.alipes.common.messaging.EmailMessengerFactoryImpl;
20 import org.abraracourcix.alipes.common.messaging.MessengerException;
21 import org.abraracourcix.alipes.listeners.AbstractListener;
22 import org.abraracourcix.alipes.listeners.Listener;
23 import org.abraracourcix.alipes.listeners.ListenerException;
24 import org.abraracourcix.alipes.listeners.email.EmailListenerException;
25 import org.abraracourcix.alipes.listeners.filter.Filter;
26 import org.abraracourcix.alipes.listeners.filter.ResourceToStringWildCardFilter;
27 import org.abraracourcix.alipes.monitors.file.FileEvent;
28 import org.abraracourcix.alipes.pipes.Multiplexor;
29 import org.abraracourcix.alipes.pipes.MultiplexorException;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 /***
33 * A more sophisticated listener which combines message templates with a
34 * dynamic filtering system allowing certain recipients to register interest
35 * with only certain resources. The class requires a file (defaults to
36 * email.txt) containing a list of properties associating regexps with email
37 * addresses. The regexps are applied to any object which fires an event and a
38 * positive match results in an email to the email addresses. @TODO not quite
39 * happy abou this since it seems to break the job of the Filter classes and be
40 * a bit monolithic
41 *
42 * @author jdt
43 */
44 public final class SelectiveEmailListener extends AbstractListener {
45 /***
46 * Multiplexor to send the event to all the email listeners
47 */
48 private Multiplexor multiplexor = new Multiplexor();
49 /***
50 * Logger
51 */
52 private static final Log log =
53 LogFactory.getLog(SelectiveEmailListener.class);
54 /***
55 * messenger fctory to use
56 */
57 private EmailListenerFactory emailListenerFactory;
58 /***
59 * Default file with the properties
60 */
61 private String propertiesFile = "email.txt";
62 /***
63 * The properties into which the file is loaded.
64 */
65 private Properties properties = new Properties();
66 /***
67 * Constructor
68 *
69 * @param smtpServer The mail server e.g. 127.0.0.1 port 25 is assumed
70 * @param user who's the email from?
71 * @param password user's password
72 * @param returnAddress who to complain to?
73 */
74 public SelectiveEmailListener(
75 final String smtpServer,
76 final String user,
77 final String password,
78 final String returnAddress) {
79 EmailMessengerFactory messengerFactory = new EmailMessengerFactoryImpl( smtpServer,
80 user,
81 password,
82 returnAddress);
83 emailListenerFactory =
84 new EmailListenerFactory(messengerFactory);
85 log.debug("Constructor: smtpServer=" + smtpServer);
86 }
87 /***
88 * Sets up the chain of listeners that multiplex, filter, and notify
89 * @TODO - this is not adequate...if there is no event, then the email list will never
90 * be updated....it needs to be updated as part of a separate polling monitor.
91 */
92 private void setUpPipeLine() throws EmailListenerException {
93 log.debug("Resetting pipeline");
94 multiplexor.clear();
95
96 try {
97 final InputStream is = new FileInputStream(new File(propertiesFile));
98
99 if (is == null) {
100 throw new EmailListenerException(
101 "Couldn't find " + propertiesFile);
102
103 }
104 properties.clear();
105 properties.load(is);
106 is.close();
107 } catch (IOException ioe) {
108 throw new EmailListenerException(
109 "Couldn't load " + propertiesFile,
110 ioe);
111
112 }
113 final Enumeration en = properties.propertyNames();
114 while (en.hasMoreElements()) {
115 final String wildcard = (String) en.nextElement();
116 final String emails = (String) properties.getProperty(wildcard);
117 log.info(wildcard + "-> " + emails);
118 Listener emailListener = emailListenerFactory.getEmailListener(emails);
119 Filter fileNameFilter = new ResourceToStringWildCardFilter(emailListener, wildcard);
120 multiplexor.addListener(fileNameFilter);
121 }
122 }
123 /***
124 * Something's happened - fire off emails
125 *
126 * @param event what's happened
127 * @param resource to whom
128 * @throws EmailListenerException if unable to send email
129 * @see org.abraracourcix.alipes.listeners.Listener#eventOccurred(org.abraracourcix.alipes.common.Event,
130 * java.lang.Object) @TODO intercept emails, and wrap as multiplexed
131 */
132 public void eventOccurred(final Event event, final Object resource)
133 throws MultiplexorException, EmailListenerException {
134 log.info("eventOccurred: event=" + event + ", resource=" + resource);
135
136 setUpPipeLine();
137 multiplexor.eventOccurred(event, resource);
138 }
139 /***
140 * Let's try it out.
141 *
142 * @param args password,
143 * @throws ListenerException sometimes
144 */
145 public static void main(final String[] args) throws ListenerException {
146 final SelectiveEmailListener emailListener =
147 new SelectiveEmailListener(
148 "127.0.0.1",
149 "jdt",
150 args[0],
151 "jdt@roe.ac.uk");
152 emailListener.eventOccurred(FileEvent.CREATED, "www/datacenter.jar");
153 emailListener.eventOccurred(FileEvent.DELETED, "workflow");
154 }
155 /***
156 * Setter
157 *
158 * @param propertiesFile The propertiesFile to set.
159 */
160 public void setPropertiesFile(final String propertiesFile) {
161 this.propertiesFile = propertiesFile;
162 }
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207