1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: RmiEmailService.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.switchrmi.transport.smtp;
27   
28   import com.mjh.switchrmi.*;
29   
30   import java.util.*;
31   
32   import javax.mail.*;
33   import javax.mail.internet.*;
34   
35   import javax.naming.*;
36   
37   import org.apache.log4j.*;
38   
39   public class RmiEmailService
40   {
41       private static final String CONFIG_RESOURCE_PROPERTY_NAME = 
42               "switchrmi.configuration";
43       private static final String POP3 = "pop3";
44       private static final String IMAP = "imap";
45       private static final Logger log = 
46               Logger.getLogger(RmiEmailService.class.getName());
47       private String provider = "pop3";
48       private String user;
49       private String password;
50       private String host;
51       private int pollingInterval = 1000 * 60 * 2;
52       private Session session;
53       private Store store;
54       private Folder defaultFolder;
55       private Folder folder;
56       private SmtpServiceHandler handler;
57   
58       RmiEmailService(Properties props)
59                throws Exception
60       {
61           provider = props.getProperty("mail.store.protocol", POP3);
62           log.debug("provider = " + provider);
63           host = props.getProperty("mail." + provider + ".host");
64           log.debug("host = " + host);
65           user = props.getProperty("mail." + provider + ".user");
66           log.debug("user = " + user);
67           password = props.getProperty("mail." + provider + ".password");
68           log.debug("password = " + password);
69   
70           String intervalString = props.getProperty("interval");
71   
72           if (intervalString != null)
73           {
74               pollingInterval = (new Integer(intervalString)).intValue() * 1000 * 60;
75           }
76   
77           String resourceName = props.getProperty(Context.PROVIDER_URL);
78           String factoryName = props.getProperty(Context.INITIAL_CONTEXT_FACTORY);
79           Hashtable env = new Hashtable();
80   
81           env.put(Context.PROVIDER_URL, resourceName);
82           env.put(Context.INITIAL_CONTEXT_FACTORY, factoryName);
83   
84           Context jndiContext = new InitialContext(env);
85   
86           handler = new SmtpServiceHandler(jndiContext);
87           session = Session.getInstance(props, null);
88   
89           if (log.isDebugEnabled())
90           {
91               session.setDebug(true);
92           }
93   
94           store = session.getStore(provider);
95           store.connect(host, user, password);
96           defaultFolder = store.getDefaultFolder();
97           folder = defaultFolder.getFolder("INBOX");
98       }
99   
100      /**
101       * DOCUMENT ME!
102       * 
103       * @param args DOCUMENT ME!
104       */
105      public static void main(String[] args)
106      {
107          try
108          {
109              RmiEmailService service = 
110                      new RmiEmailService(System.getProperties());
111  
112              service.run();
113          }
114          catch (Exception ex)
115          {
116              System.err.println("Unable to start Pop3MailService: " + ex);
117          }
118  
119          System.exit(-1);
120      }
121  
122      /**
123       * DOCUMENT ME!
124       * 
125       * @throws Exception DOCUMENT ME!
126       */
127      public void run() throws Exception
128      {
129          while (true)
130          {
131              runOnce();
132              Thread.sleep(pollingInterval);
133          }
134      }
135  
136      /**
137       * DOCUMENT ME!
138       * 
139       * @throws Exception DOCUMENT ME!
140       */
141      public void runOnce() throws Exception
142      {
143          folder.open(Folder.READ_WRITE);
144  
145          Message[] message = folder.getMessages();
146  
147          log.debug("message.length = " + message.length);
148  
149          for (int i = 0; i < message.length; i++)
150          {
151              Message msg = message[i];
152              String subject = msg.getSubject();
153  
154              System.out.println("subject = " + subject);
155  
156              if (subject.toLowerCase().indexOf("switchrmi") != -1)
157              {
158                  handler.setMessage(msg);
159                  handler.invoke();
160                  System.out.println("Method was invoked");
161                  msg.setFlag(Flags.Flag.DELETED, true);
162              }
163          }
164  
165          if (!provider.equals(POP3))
166          {
167              folder.expunge();
168          }
169  
170          folder.close(true);
171      }
172  }