KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > mail > SimpleMailListener


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis2.transport.mail;
18
19 import org.apache.axis2.Constants;
20 import org.apache.axis2.addressing.AddressingConstants;
21 import org.apache.axis2.addressing.EndpointReference;
22 import org.apache.axis2.context.ConfigurationContext;
23 import org.apache.axis2.context.ConfigurationContextFactory;
24 import org.apache.axis2.description.TransportInDescription;
25 import org.apache.axis2.engine.AxisFault;
26 import org.apache.axis2.transport.EmailReceiver;
27 import org.apache.axis2.transport.TransportListener;
28 import org.apache.axis2.util.Utils;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import javax.mail.Flags JavaDoc;
33 import javax.mail.Message JavaDoc;
34 import javax.mail.internet.MimeMessage JavaDoc;
35 import javax.xml.namespace.QName JavaDoc;
36 import java.io.File JavaDoc;
37
38 /**
39  * This is a simple implementation of an SMTP/POP3 server for processing SOAP
40  * requests via Apache's xml-axis. This is not intended for production use. Its
41  * intended uses are for demos, debugging, and performance profiling.
42  *
43  * @author Davanum Srinivas <dims@yahoo.com>
44  * @author Rob Jellinghaus (robj@unrealities.com)
45  *
46  * @author Chamil Thanthrimudalige <chamilt@gmail.com>Changes done to make the
47  * Class work inside Axis 2.
48  */

49
50 /*
51  * TODO ISSUES -- 1. Message.getMessage -- All messages are hardcoded in the
52  * code till a replacement or a working verion of this is put into Axis 2. When
53  * internationalization work is done this can be fixed. CT 15-Feb-2005
54  *
55  */

56
57 public class SimpleMailListener extends TransportListener implements Runnable JavaDoc {
58
59     protected static Log log = LogFactory.getLog(SimpleMailListener.class.getName());
60
61     private String JavaDoc host;
62
63     private String JavaDoc port;
64
65     private String JavaDoc user;
66
67     private String JavaDoc password;
68
69     private ConfigurationContext configurationContext = null;
70
71     private String JavaDoc replyTo;
72
73     public SimpleMailListener() {
74     }
75
76     public SimpleMailListener(
77         String JavaDoc host,
78         String JavaDoc port,
79         String JavaDoc userid,
80         String JavaDoc password,
81         String JavaDoc dir) {
82         this.host = host;
83         this.port = port;
84         this.user = userid;
85         this.password = password;
86         try {
87             ConfigurationContextFactory builder = new ConfigurationContextFactory();
88             configurationContext = builder.buildConfigurationContext(dir);
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91         }
92         try {
93             System.out.println("Sleeping for a bit to let the engine start up.");
94             Thread.sleep(2000);
95         } catch (InterruptedException JavaDoc e1) {
96             log.debug(e1.getMessage(), e1);
97         }
98     }
99
100     public SimpleMailListener(
101         String JavaDoc host,
102         String JavaDoc port,
103         String JavaDoc userid,
104         String JavaDoc password,
105         ConfigurationContext er) {
106         this.host = host;
107         this.port = port;
108         this.user = userid;
109         this.password = password;
110         this.configurationContext = er;
111     }
112
113     // Are we doing threads?
114
private static boolean doThreads = true;
115
116     public void setDoThreads(boolean value) {
117         doThreads = value;
118     }
119
120     public boolean getDoThreads() {
121         return doThreads;
122     }
123
124     // are we stopped?
125
// latch to true if stop() is called
126
private boolean stopped = false;
127
128     /**
129      * Accept requests from a given TCP port and send them through the Axis
130      * engine for processing.
131      */

132     public void run() {
133         // log.info(Message.getMessage("start00", "SimpleMailListner", host + ":" +
134
// port)); TODO Issue #1 CT 07-Feb-2005.
135
// Accept and process requests from the socket
136
if (!stopped) {
137             String JavaDoc logMessage =
138                 "Mail listner is being setup to listen to the address "
139                     + user
140                     + "@"
141                     + host
142                     + " On port "
143                     + port;
144             System.out.println(logMessage);
145             log.info(logMessage);
146         }
147         while (!stopped) {
148             try {
149
150                 EmailReceiver receiver = new EmailReceiver(user, host, port, password);
151                 receiver.connect();
152                 Message JavaDoc[] msgs = receiver.receive();
153
154                 if (msgs != null && msgs.length > 0) {
155                     System.out.println(msgs.length + " Message Found");
156                     for (int i = 0; i < msgs.length; i++) {
157                         MimeMessage JavaDoc msg = (MimeMessage JavaDoc) msgs[i];
158                         if (msg != null) {
159                             MailWorker worker = new MailWorker(msg, configurationContext);
160                             worker.doWork();
161                         }
162                         msg.setFlag(Flags.Flag.DELETED, true);
163                     }
164
165                 }
166
167                 receiver.disconnect();
168                 //Waiting for 3 seconds.
169
Thread.sleep(3000);
170
171             } catch (Exception JavaDoc e) {
172                 //log.debug(Messages.getMessage("exception00"), e); TODO Issue
173
// #1 CT 07-Feb-2005.
174
log.debug("An error occured when running the mail listner." + e.getMessage(), e);
175                 e.printStackTrace();
176                 break;
177             }
178         }
179
180         log.info("Mail listner has been stoped.");
181         System.out.println("Mail listner has been stoped.");
182         //log.info(Messages.getMessage("quit00", "SimpleMailListner")); TODO Issue #1
183
// CT 07-Feb-2005.
184

185     }
186
187     /**
188      * Start this server.
189      *
190      * Spawns a worker thread to listen for HTTP requests.
191      *
192      * @param daemon
193      * a boolean indicating if the thread should be a daemon.
194      */

195     public void start(boolean daemon) {
196         if (doThreads) {
197             Thread JavaDoc thread = new Thread JavaDoc(this);
198             thread.setDaemon(daemon);
199             thread.start();
200         } else {
201             run();
202         }
203     }
204
205     /**
206      * Start this server as a NON-daemon.
207      */

208     public void start() {
209         start(false);
210     }
211
212     /**
213      * Stop this server.
214      *
215      * This will interrupt any pending accept().
216      */

217     public void stop() {
218         /*
219          * Close the server socket cleanly, but avoid fresh accepts while the
220          * socket is closing.
221          */

222         stopped = true;
223         //log.info(Messages.getMessage("quit00", "SimpleMailListner")); TODO Issue #1
224
// CT 07-Feb-2005.
225
log.info("Quiting the mail listner");
226     }
227
228     /**
229      * Server process.
230      */

231     public static void main(String JavaDoc args[]) throws AxisFault {
232         if (args.length != 1) {
233             System.out.println("java SimpleMailListener <repository>");
234         } else {
235             ConfigurationContextFactory builder = new ConfigurationContextFactory();
236             ConfigurationContext configurationContext = builder.buildConfigurationContext(args[0]);
237             SimpleMailListener sas = new SimpleMailListener();
238             TransportInDescription transportIn =
239                 configurationContext.getAxisConfiguration().getTransportIn(
240                     new QName JavaDoc(Constants.TRANSPORT_MAIL));
241             if (transportIn != null) {
242                 sas.init(configurationContext, transportIn);
243                 System.out.println(
244                     "Starting the SimpleMailListener with repository "
245                         + new File JavaDoc(args[0]).getAbsolutePath());
246                 sas.start();
247             } else {
248                 System.out.println(
249                     "Startup failed, mail transport not configured, Configure the mail trnasport in the axis2.xml file");
250             }
251         }
252     }
253     /* (non-Javadoc)
254      * @see org.apache.axis2.transport.TransportListener#init(org.apache.axis2.context.ConfigurationContext, org.apache.axis2.description.TransportInDescription)
255      */

256     public void init(ConfigurationContext configurationContext, TransportInDescription transportIn)
257         throws AxisFault {
258         this.configurationContext = configurationContext;
259
260         user = Utils.getParameterValue(transportIn.getParameter(MailConstants.POP3_USER));
261         host = Utils.getParameterValue(transportIn.getParameter(MailConstants.POP3_HOST));
262         password = Utils.getParameterValue(transportIn.getParameter(MailConstants.POP3_PASSWORD));
263         port = Utils.getParameterValue(transportIn.getParameter(MailConstants.POP3_PORT));
264         replyTo = Utils.getParameterValue(transportIn.getParameter(MailConstants.RAPLY_TO));
265         if (user == null || host == null || password == null || port == null) {
266             throw new AxisFault(
267                 "user, port, host or password not set, "
268                     + " [user null = "
269                     + (user == null)
270                     + ", password null= "
271                     + (password == null)
272                     + ", host null "
273                     + (host == null)
274                     + ",port null "
275                     + (port == null));
276         }
277
278     }
279
280     /* (non-Javadoc)
281      * @see org.apache.axis2.transport.TransportListener#replyToEPR(java.lang.String)
282      */

283     public EndpointReference replyToEPR(String JavaDoc serviceName) throws AxisFault {
284         // TODO Auto-generated method stub
285
return new EndpointReference(AddressingConstants.WSA_REPLY_TO, replyTo+"/services/"+serviceName);
286     }
287
288 }
Popular Tags