View Javadoc

1   /*
2    * $Id: SelectiveEmailListener.java,v 1.1 2004/03/14 16:26:49 johndavidtaylor
3    * Exp $ Created on Feb 19, 2004 by John Taylor jdt@roe.ac.uk .
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          //get latest list of interested parties
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                 //@TODO might want to consider carrying on regardless
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             //@TODO might want to consider carrying on regardless
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         //get latest list of interested parties
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  * $Log: SelectiveEmailListener.java,v $
166  * Revision 1.9  2004/07/23 17:37:41  johndavidtaylor
167  * Fixed too many files open bug
168  *
169  * Revision 1.8  2004/06/19 20:05:39  johndavidtaylor
170  * Bug fix.
171  *
172  * Revision 1.7  2004/06/03 16:38:04  johndavidtaylor
173  * Removed unnecessary methods.
174  *
175  * Revision 1.6  2004/06/03 16:31:46  johndavidtaylor
176  * Pipeline no longer set up in constructor
177  *
178  * Revision 1.5  2004/06/02 20:11:09  johndavidtaylor
179  * Refactoring to allow email templates.
180  *
181  * Revision 1.4  2004/05/21 23:29:59  johndavidtaylor
182  * quick hack to catch an exception.
183  *
184  * Revision 1.3  2004/05/21 23:22:01  johndavidtaylor
185  * Added a filter for resources and refactored out the commonality
186  *
187  * Revision 1.2  2004/05/04 11:36:13  johndavidtaylor
188  * moved list of emailers outside classpath
189  *
190  * Revision 1.1  2004/03/17 21:17:13  johndavidtaylor
191  * Copied across from incubation in beanpeeler
192  *
193  * Revision 1.3  2004/03/15 13:40:13  johndavidtaylor
194  * Now up the stage where it can be used.  Things still to do: 
195  * change the way email.txt is picked up so it can be modified mid-flight,
196  * remove the annoying logging.
197  *
198  * Revision 1.2  2004/03/14 18:46:21  johndavidtaylor
199  * refactored email
200  * Revision 1.1 2004/03/14 16:26:49
201  * johndavidtaylor refactored email Revision 1.2 2004/03/14 00:44:22
202  * johndavidtaylor Added ListenerException Revision 1.1 2004/03/14 00:36:47
203  * johndavidtaylor Added send email capability Revision 1.2 2004/03/08 20:16:45
204  * johndavidtaylor refactored to new package name Revision 1.1 2004/03/08
205  * 19:58:25 johndavidtaylor Initial commit -transfer from previous repository
206  * Revision 1.1 2004/03/02 23:04:37 jdt new
207  */