View Javadoc

1   /*
2    * $Id: SimpleEmailListener.java,v 1.3 2004/06/03 16:37:31 johndavidtaylor Exp $
3    * Created on Feb 19, 2004 by John Taylor jdt@roe.ac.uk .
4    */
5   package org.abraracourcix.alipes.listeners.email;
6   import java.util.Date;
7   
8   import org.abraracourcix.alipes.common.Event;
9   import org.abraracourcix.alipes.common.messaging.EmailMessengerFactoryImpl;
10  import org.abraracourcix.alipes.common.messaging.Messenger;
11  import org.abraracourcix.alipes.common.messaging.MessengerException;
12  import org.abraracourcix.alipes.listeners.AbstractListener;
13  import org.abraracourcix.alipes.listeners.Listener;
14  import org.abraracourcix.alipes.listeners.ListenerException;
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  /***
18   * Simple email listener, it just waits for an event and immediately launches
19   * an email to its registered address.  These could be a comma separated list.
20   * 
21   * @author jdt
22   * @deprecated Better to get an email listener from the EmailListenerFactory
23   */
24  public final class SimpleEmailListener extends AbstractListener {
25      /***
26       * Logger
27       */
28      private static final Log log = LogFactory.getLog(SimpleEmailListener.class);
29      
30      /***
31       * messenger to use
32       */
33      private Messenger messenger;
34      /***
35       * Constructor
36       * 
37       * @param smtpServer The mail server e.g. 127.0.0.1 port 25 is assumed
38       * @param recipients e.g. jdt@roe.ac.uk
39       * @param user who's the email from?
40       * @param password user's password
41       * @param returnAddress who to complain to?
42       */
43      public SimpleEmailListener(
44          final String smtpServer,
45          final String user,
46          final String password,
47          final String recipients,
48          final String returnAddress) {
49          
50          final EmailMessengerFactoryImpl factory = new EmailMessengerFactoryImpl(smtpServer, user, password, returnAddress);
51          messenger = factory.getEmailMessenger(recipients);
52          log.debug(
53              "Constructor: smtpServer="
54                  + smtpServer
55                  + ", recipients="
56                  + recipients);
57      }
58      /***
59       * Something's happened - fire off emails
60       * 
61       * @param event what's happened
62       * @param resource to whom
63       * @throws EmailListenerException if unable to send email
64       * @see org.abraracourcix.alipes.listeners.Listener#eventOccurred(org.abraracourcix.alipes.common.Event,
65       *      java.lang.Object)
66       */
67      public void eventOccurred(final Event event, final Object resource) throws EmailListenerException {
68          log.debug("eventOccurred: event=" + event + ", resource=" + resource);
69          final String message =
70              new Date()
71                  + "\nGuess what?\n"
72                  + "The following event occurred\n"
73                  + "Resource "
74                  + resource
75                  + "\nEvent: "
76                  + event;
77       try {
78           messenger.sendMessage("RE: "+resource, message);
79      } catch (MessengerException me) {
80          log.error("Problem sending email", me);
81          throw new EmailListenerException("Problem sending email", me);
82      }   
83         
84          
85      }
86      /***
87       * Let's try it out.
88       * @param args smtpserver, user, password, recipient sender
89       * @throws ListenerException sometimes
90       */
91      public static void main(final String[] args) throws ListenerException {
92          if (args.length!=5) {
93              System.out.println("Usage: java EmailListener smtpserver user password recipient");
94              return;
95          }
96          final String server = args[0];
97          final String user = args[1];
98          final String pass = args[2];
99          final String recipient = args[3];
100         final String sender = args[4];
101         final Listener listener = new SimpleEmailListener(server, user, pass, recipient, sender);
102         listener.eventOccurred(new Event() { public String toString(){return "it worked!";}}, new String("What resource?"));
103         
104     }
105 }
106 /*
107  * $Log: SimpleEmailListener.java,v $
108  * Revision 1.3  2004/06/03 16:37:31  johndavidtaylor
109  * Now deprecated
110  *
111  * Revision 1.2  2004/06/02 20:11:07  johndavidtaylor
112  * Refactoring to allow email templates.
113  *
114  * Revision 1.1  2004/03/17 21:17:13  johndavidtaylor
115  * Copied across from incubation in beanpeeler
116  *
117  * Revision 1.4  2004/03/15 13:40:13  johndavidtaylor
118  * Now up the stage where it can be used.  Things still to do: 
119  * change the way email.txt is picked up so it can be modified mid-flight,
120  * remove the annoying logging.
121  *
122  * Revision 1.3  2004/03/14 16:26:49  johndavidtaylor
123  * refactored email
124  *
125  * Revision 1.2  2004/03/14 00:44:22  johndavidtaylor
126  * Added ListenerException
127  *
128  * Revision 1.1  2004/03/14 00:36:47  johndavidtaylor
129  * Added send email capability
130  * Revision 1.2 2004/03/08 20:16:45
131  * johndavidtaylor refactored to new package name Revision 1.1 2004/03/08
132  * 19:58:25 johndavidtaylor Initial commit -transfer from previous repository
133  * Revision 1.1 2004/03/02 23:04:37 jdt new
134  */