1 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 ; 28 import javax.mail.internet.MimeMessage ; 29 import java.io.BufferedReader ; 30 import java.io.ByteArrayInputStream ; 31 import java.io.Reader ; 32 import java.net.MalformedURLException ; 33 import java.util.Properties ; 34 35 44 45 public class MailServer implements Runnable { 46 protected static Log log = 47 LogFactory.getLog(MailServer.class.getName()); 48 49 private String host; 50 private int port; 51 private String userid; 52 private String password; 53 54 public MailServer(String host, int port, String userid, String password) { 55 this.host = host; 56 this.port = port; 57 this.userid = userid; 58 this.password = password; 59 } 60 61 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 getHost() { 73 return host; 74 } 75 76 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 private boolean stopped = false; 89 90 94 public void run() { 95 log.info(Messages.getMessage("start00", "MailServer", host + ":" + port)); 96 97 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 reader = pop3.retrieveMessage(messages[i].number); 107 if (reader == null) { 108 continue; 109 } 110 111 StringBuffer buffer = new StringBuffer (); 112 BufferedReader bufferedReader = 113 new BufferedReader (reader); 114 int ch; 115 while ((ch = bufferedReader.read()) != -1) { 116 buffer.append((char) ch); 117 } 118 bufferedReader.close(); 119 ByteArrayInputStream bais = new ByteArrayInputStream (buffer.toString().getBytes()); 120 Properties prop = new Properties (); 121 Session session = Session.getDefaultInstance(prop, null); 122 123 MimeMessage mimeMsg = new MimeMessage (session, bais); 124 pop3.deleteMessage(messages[i].number); 125 if (mimeMsg != null) { 126 MailWorker worker = new MailWorker(this, mimeMsg); 127 if (doThreads) { 128 Thread thread = new Thread (worker); 129 thread.setDaemon(true); 130 thread.start(); 131 } else { 132 worker.run(); 133 } 134 } 135 } 136 } 137 } catch (java.io.InterruptedIOException iie) { 138 } catch (Exception 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 e) { 147 log.error(Messages.getMessage("exception00"), e); 148 } 149 } 150 } 151 log.info(Messages.getMessage("quit00", "MailServer")); 152 } 153 154 157 private POP3Client pop3; 158 159 162 public POP3Client getPOP3() { 163 return pop3; 164 } 165 166 171 public void setPOP3(POP3Client pop3) { 172 this.pop3 = pop3; 173 } 174 175 182 public void start(boolean daemon) throws Exception { 183 if (doThreads) { 184 Thread thread = new Thread (this); 185 thread.setDaemon(daemon); 186 thread.start(); 187 } else { 188 run(); 189 } 190 } 191 192 195 public void start() throws Exception { 196 start(false); 197 } 198 199 204 public void stop() throws Exception { 205 209 stopped = true; 210 log.info(Messages.getMessage("quit00", "MailServer")); 211 212 System.exit(0); 214 } 215 216 219 public static void main(String args[]) { 220 Options opts = null; 221 try { 222 opts = new Options(args); 223 } catch (MalformedURLException e) { 224 log.error(Messages.getMessage("malformedURLException00"), e); 225 return; 226 } 227 228 try { 229 doThreads = (opts.isFlagSet('t') > 0); 230 String 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 e) { 238 log.error(Messages.getMessage("exception00"), e); 239 return; 240 } 241 242 } 243 } 244 | Popular Tags |