1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
127 public void run() throws Exception
128 {
129 while (true)
130 {
131 runOnce();
132 Thread.sleep(pollingInterval);
133 }
134 }
135
136
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 }