KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > mail > MailServer


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.axis.transport.mail;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.i18n.Messages;
21 import org.apache.axis.server.AxisServer;
22 import org.apache.axis.utils.Options;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.net.pop3.POP3Client;
25 import org.apache.commons.net.pop3.POP3MessageInfo;
26
27 import javax.mail.Session JavaDoc;
28 import javax.mail.internet.MimeMessage JavaDoc;
29 import java.io.BufferedReader JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 /**
36  * This is a simple implementation of an SMTP/POP3 server for processing
37  * SOAP requests via Apache's xml-axis. This is not intended for production
38  * use. Its intended uses are for demos, debugging, and performance
39  * profiling.
40  *
41  * @author Davanum Srinivas <dims@yahoo.com>
42  * @author Rob Jellinghaus (robj@unrealities.com)
43  */

44
45 public class MailServer implements Runnable JavaDoc {
46     protected static Log log =
47             LogFactory.getLog(MailServer.class.getName());
48
49     private String JavaDoc host;
50     private int port;
51     private String JavaDoc userid;
52     private String JavaDoc password;
53
54     public MailServer(String JavaDoc host, int port, String JavaDoc userid, String JavaDoc password) {
55         this.host = host;
56         this.port = port;
57         this.userid = userid;
58         this.password = password;
59     }
60
61     // Are we doing threads?
62
private static boolean doThreads = true;
63
64     public void setDoThreads(boolean value) {
65         doThreads = value;
66     }
67
68     public boolean getDoThreads() {
69         return doThreads;
70     }
71
72     public String JavaDoc getHost() {
73         return host;
74     }
75
76     // Axis server (shared between instances)
77
private static AxisServer myAxisServer = null;
78
79     protected static synchronized AxisServer getAxisServer() {
80         if (myAxisServer == null) {
81             myAxisServer = new AxisServer();
82         }
83         return myAxisServer;
84     }
85
86     // are we stopped?
87
// latch to true if stop() is called
88
private boolean stopped = false;
89
90     /**
91      * Accept requests from a given TCP port and send them through the
92      * Axis engine for processing.
93      */

94     public void run() {
95         log.info(Messages.getMessage("start00", "MailServer", host + ":" + port));
96
97         // Accept and process requests from the socket
98
while (!stopped) {
99             try {
100                 pop3.connect(host, port);
101                 pop3.login(userid, password);
102
103                 POP3MessageInfo[] messages = pop3.listMessages();
104                 if (messages != null && messages.length > 0) {
105                     for (int i = 0; i < messages.length; i++) {
106                         Reader JavaDoc reader = pop3.retrieveMessage(messages[i].number);
107                         if (reader == null) {
108                             continue;
109                         }
110
111                         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
112                         BufferedReader JavaDoc bufferedReader =
113                                 new BufferedReader JavaDoc(reader);
114                         int ch;
115                         while ((ch = bufferedReader.read()) != -1) {
116                             buffer.append((char) ch);
117                         }
118                         bufferedReader.close();
119                         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(buffer.toString().getBytes());
120                         Properties JavaDoc prop = new Properties JavaDoc();
121                         Session JavaDoc session = Session.getDefaultInstance(prop, null);
122
123                         MimeMessage JavaDoc mimeMsg = new MimeMessage JavaDoc(session, bais);
124                         pop3.deleteMessage(messages[i].number);
125                         if (mimeMsg != null) {
126                             MailWorker worker = new MailWorker(this, mimeMsg);
127                             if (doThreads) {
128                                 Thread JavaDoc thread = new Thread JavaDoc(worker);
129                                 thread.setDaemon(true);
130                                 thread.start();
131                             } else {
132                                 worker.run();
133                             }
134                         }
135                     }
136                 }
137             } catch (java.io.InterruptedIOException JavaDoc iie) {
138             } catch (Exception JavaDoc e) {
139                 log.debug(Messages.getMessage("exception00"), e);
140                 break;
141             } finally {
142                 try {
143                     pop3.logout();
144                     pop3.disconnect();
145                     Thread.sleep(3000);
146                 } catch (Exception JavaDoc e) {
147                     log.error(Messages.getMessage("exception00"), e);
148                 }
149             }
150         }
151         log.info(Messages.getMessage("quit00", "MailServer"));
152     }
153
154     /**
155      * POP3 connection
156      */

157     private POP3Client pop3;
158
159     /**
160      * Obtain the serverSocket that that MailServer is listening on.
161      */

162     public POP3Client getPOP3() {
163         return pop3;
164     }
165
166     /**
167      * Set the serverSocket this server should listen on.
168      * (note : changing this will not affect a running server, but if you
169      * stop() and then start() the server, the new socket will be used).
170      */

171     public void setPOP3(POP3Client pop3) {
172         this.pop3 = pop3;
173     }
174
175     /**
176      * Start this server.
177      *
178      * Spawns a worker thread to listen for HTTP requests.
179      *
180      * @param daemon a boolean indicating if the thread should be a daemon.
181      */

182     public void start(boolean daemon) throws Exception JavaDoc {
183         if (doThreads) {
184             Thread JavaDoc thread = new Thread JavaDoc(this);
185             thread.setDaemon(daemon);
186             thread.start();
187         } else {
188             run();
189         }
190     }
191
192     /**
193      * Start this server as a NON-daemon.
194      */

195     public void start() throws Exception JavaDoc {
196         start(false);
197     }
198
199     /**
200      * Stop this server.
201      *
202      * This will interrupt any pending accept().
203      */

204     public void stop() throws Exception JavaDoc {
205         /*
206          * Close the server socket cleanly, but avoid fresh accepts while
207          * the socket is closing.
208          */

209         stopped = true;
210         log.info(Messages.getMessage("quit00", "MailServer"));
211
212         // Kill the JVM, which will interrupt pending accepts even on linux.
213
System.exit(0);
214     }
215
216     /**
217      * Server process.
218      */

219     public static void main(String JavaDoc args[]) {
220         Options opts = null;
221         try {
222             opts = new Options(args);
223         } catch (MalformedURLException JavaDoc e) {
224             log.error(Messages.getMessage("malformedURLException00"), e);
225             return;
226         }
227
228         try {
229             doThreads = (opts.isFlagSet('t') > 0);
230             String JavaDoc host = opts.getHost();
231             int port = ((opts.isFlagSet('p') > 0) ? opts.getPort() : 110);
232             POP3Client pop3 = new POP3Client();
233             MailServer sas = new MailServer(host, port, opts.getUser(), opts.getPassword());
234
235             sas.setPOP3(pop3);
236             sas.start();
237         } catch (Exception JavaDoc e) {
238             log.error(Messages.getMessage("exception00"), e);
239             return;
240         }
241
242     }
243 }
244
Popular Tags