1 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 ; 33 import javax.mail.Message ; 34 import javax.mail.internet.MimeMessage ; 35 import javax.xml.namespace.QName ; 36 import java.io.File ; 37 38 49 50 56 57 public class SimpleMailListener extends TransportListener implements Runnable { 58 59 protected static Log log = LogFactory.getLog(SimpleMailListener.class.getName()); 60 61 private String host; 62 63 private String port; 64 65 private String user; 66 67 private String password; 68 69 private ConfigurationContext configurationContext = null; 70 71 private String replyTo; 72 73 public SimpleMailListener() { 74 } 75 76 public SimpleMailListener( 77 String host, 78 String port, 79 String userid, 80 String password, 81 String 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 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 e1) { 96 log.debug(e1.getMessage(), e1); 97 } 98 } 99 100 public SimpleMailListener( 101 String host, 102 String port, 103 String userid, 104 String 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 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 private boolean stopped = false; 127 128 132 public void run() { 133 if (!stopped) { 137 String 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 [] 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 msg = (MimeMessage ) 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 Thread.sleep(3000); 170 171 } catch (Exception e) { 172 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 185 } 186 187 195 public void start(boolean daemon) { 196 if (doThreads) { 197 Thread thread = new Thread (this); 198 thread.setDaemon(daemon); 199 thread.start(); 200 } else { 201 run(); 202 } 203 } 204 205 208 public void start() { 209 start(false); 210 } 211 212 217 public void stop() { 218 222 stopped = true; 223 log.info("Quiting the mail listner"); 226 } 227 228 231 public static void main(String 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 (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 (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 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 283 public EndpointReference replyToEPR(String serviceName) throws AxisFault { 284 return new EndpointReference(AddressingConstants.WSA_REPLY_TO, replyTo+"/services/"+serviceName); 286 } 287 288 } | Popular Tags |