KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > joram > mom > dest > mail > JavaMailQueueImpl


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - 2007 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package com.scalagent.joram.mom.dest.mail;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.objectweb.joram.mom.dest.Queue;
31 import org.objectweb.joram.mom.dest.QueueImpl;
32 import org.objectweb.joram.mom.notifications.ClientMessages;
33 import org.objectweb.joram.mom.notifications.SpecialAdminRequest;
34 import org.objectweb.joram.shared.admin.SpecialAdmin;
35 import org.objectweb.joram.shared.excepts.RequestException;
36 import org.objectweb.joram.shared.messages.Message;
37 import org.objectweb.joram.shared.selectors.Selector;
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40
41 import fr.dyade.aaa.agent.AgentId;
42 import fr.dyade.aaa.util.Debug;
43
44 /**
45  * The <code>JavaMailQueueImpl</code> class implements the MOM queue behaviour,
46  * basically storing messages and delivering them upon clients requests.
47  */

48 public class JavaMailQueueImpl extends QueueImpl implements JavaMailQueueImplMBean {
49   public static Logger logger = Debug.getLogger(JavaMailQueueImpl.class.getName());
50
51   private String JavaDoc smtpServer = null;
52
53   private String JavaDoc to = null;
54   private String JavaDoc cc = null;
55   private String JavaDoc bcc = null;
56   private String JavaDoc from = null;
57   private String JavaDoc subject = null;
58   private String JavaDoc selector = null;
59
60   private long popPeriod = -1;
61   private String JavaDoc popServer = null;
62   private String JavaDoc popUser = null;
63   private String JavaDoc popPassword = null;
64   private boolean expunge = false;
65
66   private Vector JavaDoc senderInfos = null;
67
68   private transient JavaMailUtil javaMailUtil = null;
69
70   /**
71    * Constructs a <code>JavaMailQueueImpl</code> instance.
72    *
73    * @param destId Identifier of the agent hosting the queue.
74    * @param adminId Identifier of the administrator of the queue.
75    * @param prop Properties to configure the queue.
76    */

77   public JavaMailQueueImpl(AgentId destId,
78                            AgentId adminId,
79                            Properties JavaDoc prop) {
80     super(destId, adminId, prop);
81     setProperties(prop);
82
83     if (logger.isLoggable(BasicLevel.DEBUG))
84       logger.log(BasicLevel.DEBUG,
85                  "--- " + this +
86                  " JavaMailQueueImpl : " +
87                  "\nsenderInfos=" + senderInfos +
88                  "\npopServer=" + popServer +
89                  "\npopUser=" + popUser +
90                  "\npopPeriod=" + popPeriod +
91                  "\nexpunge=" + expunge);
92   }
93
94   public void setProperties(Properties JavaDoc prop) {
95     smtpServer = prop.getProperty("smtpServer", smtpServer);
96     to = prop.getProperty("to", to);
97     cc = prop.getProperty("cc", cc);
98     bcc = prop.getProperty("bcc", bcc);
99     from = prop.getProperty("from", from);
100     subject = prop.getProperty("subject", subject);
101     selector = prop.getProperty("selector", selector);
102
103     // to send mail
104
senderInfos = new Vector JavaDoc();
105     senderInfos.add(new SenderInfo(smtpServer,
106                                    to, cc, bcc, from, subject,
107                                    selector));
108
109     try {
110       popPeriod = Long.valueOf(prop.getProperty("popPeriod")).longValue();
111     } catch (NumberFormatException JavaDoc exc) {
112       popPeriod = -1;
113     }
114     popServer = prop.getProperty("popServer", popServer);
115     popUser = prop.getProperty("popUser", popUser);
116     popPassword = prop.getProperty("popPassword", popPassword);
117     expunge = Boolean.valueOf(prop.getProperty("expunge")).booleanValue();
118   }
119
120   // ==================================================
121
// MBean interface
122
// ==================================================
123
/**
124    * Returns the default SMTP server for outgoing mail.
125    *
126    * @return the default SMTP server.
127    */

128   public String JavaDoc getSMTPServer() {
129     return smtpServer;
130   }
131
132   /**
133    * Sets or unsets the default SMTP server for outgoing mail.
134    *
135    * @param period The default SMTP server or null for unsetting
136    * previous value.
137    */

138   public void setSMTPServer(String JavaDoc smtpServer) {
139     this.smtpServer = smtpServer;
140   }
141
142   /**
143    * Returns the default <code>to</code> field.
144    *
145    * @return the default <code>to</code> field.
146    */

147   public String JavaDoc getDefaultTo() {
148     return to;
149   }
150
151   /**
152    * Sets or unsets the default <code>to</code> field.
153    *
154    * @param to The default <code>to</code> field or null for unsetting
155    * previous value.
156    */

157   public void setDefaultTo(String JavaDoc to) {
158     this.to = to;
159   }
160
161   /**
162    * Returns the default <code>cc</code> field.
163    *
164    * @return the default <code>cc</code> field.
165    */

166   public String JavaDoc getDefaultCC() {
167     return cc;
168   }
169
170   /**
171    * Sets or unsets the default <code>cc</code> field.
172    *
173    * @param cc The default <code>cc</code> field or null for unsetting
174    * previous value.
175    */

176   public void setDefaultCC(String JavaDoc cc) {
177     this.cc = cc;
178   }
179
180   /**
181    * Returns the default <code>bcc</code> field.
182    *
183    * @return the default <code>bcc</code> field.
184    */

185   public String JavaDoc getDefaultBcc() {
186     return bcc;
187   }
188
189   /**
190    * Sets or unsets the default <code>bcc</code> field.
191    *
192    * @param bcc The default <code>bcc</code> field or null for unsetting
193    * previous value.
194    */

195   public void setDefaultBcc(String JavaDoc bcc) {
196     this.bcc = bcc;
197   }
198
199   /**
200    * Returns the default <code>from</code> field.
201    *
202    * @return the default <code>from</code> field.
203    */

204   public String JavaDoc getDefaultFrom() {
205     return from;
206   }
207
208   /**
209    * Sets or unsets the default <code>from</code> field.
210    *
211    * @param from The default <code>from</code> field or null for unsetting
212    * previous value.
213    */

214   public void setDefaultFrom(String JavaDoc from) {
215     this.from = from;
216   }
217
218   /**
219    * Returns the default <code>subject</code> field.
220    *
221    * @return the default <code>subject</code> field.
222    */

223   public String JavaDoc getDefaultSubject() {
224     return subject;
225   }
226
227   /**
228    * Sets or unsets the default <code>subject</code> field.
229    *
230    * @param subject The default <code>subject</code> field or null for
231    * unsetting previous value.
232    */

233   public void setDefaultSubject(String JavaDoc subject) {
234     this.subject = subject;
235   }
236
237   /**
238    * Returns the default <code>selector</code>.
239    *
240    * @return the default <code>selector</code>.
241    */

242   public String JavaDoc getDefaultSelector() {
243     return selector;
244   }
245
246   /**
247    * Sets or unsets the default <code>selector</code>.
248    *
249    * @param selector The default <code>selector</code> or null for
250    * unsetting previous value.
251    */

252   public void setDefaultSelector(String JavaDoc selector) {
253     this.selector = selector;
254   }
255
256   /**
257    * Returns the period value to collect mail, -1 if not set.
258    *
259    * @return the period value to collect mail; -1 if not set.
260    */

261   public long getPopPeriod() {
262     return popPeriod;
263   }
264
265   /**
266    * Sets or unsets the period value to collect mail.
267    *
268    * @param period The period value to collect mail or -1 for unsetting
269    * previous value.
270    */

271   public void setPopPeriod(long period) {
272     popPeriod = period;
273   }
274
275   /**
276    * Returns the pop server for incoming mail.
277    *
278    * @return the pop server for incoming mail.
279    */

280   public String JavaDoc getPopServer() {
281     return popServer;
282   }
283
284   /**
285    * Sets or unsets the pop server for incoming mail.
286    *
287    * @param server The pop server or null for unsetting previous value.
288    */

289   public void setPopServer(String JavaDoc server) {
290     this.popServer = popServer;
291   }
292
293   /**
294    * Returns the username for pop account.
295    *
296    * @return the username for pop account.
297    */

298   public String JavaDoc getPopUser() {
299     return popUser;
300   }
301
302   /**
303    * Sets or unsets the username for pop account.
304    *
305    * @param user The username for pop account or null for
306    * unsetting previous value.
307    */

308   public void setPopUser(String JavaDoc user) {
309     this.popUser = user;
310   }
311
312   /**
313    * Returns the password for pop account.
314    *
315    * @return the password for pop account.
316    */

317   public String JavaDoc getPopPassword() {
318     return popPassword;
319   }
320
321   /**
322    * Sets or unsets the password for pop account.
323    *
324    * @param pass The password for pop account or null for
325    * unsetting previous value.
326    */

327   public void setPopPassword(String JavaDoc pass) {
328     this.popPassword = pass;
329   }
330
331   /**
332    * Returns the default <code>expunge</code> field.
333    *
334    * @return the default <code>expunge</code> field.
335    */

336   public boolean getExpunge() {
337     return expunge;
338   }
339
340   /**
341    * Sets or unsets the default <code>expunge</code> field.
342    *
343    * @param expunge The default <code>expunge</code> field or null for
344    * unsetting previous value.
345    */

346   public void setExpunge(boolean expunge) {
347     this.expunge = expunge;
348   }
349
350   // ==================================================
351

352   public String JavaDoc toString() {
353     return "JavaMailQueueImpl:" + destId.toString();
354   }
355
356   protected Object JavaDoc specialAdminProcess(SpecialAdminRequest not)
357     throws RequestException {
358
359     try {
360       SpecialAdmin req = not.getRequest();
361       
362       if (logger.isLoggable(BasicLevel.DEBUG))
363         logger.log(BasicLevel.DEBUG,
364                    "--- " + this +
365                    " specialAdminProcess : " +
366                    req);
367       if (req instanceof AddSenderInfo)
368         addSenderInfo(((AddSenderInfo) req).si,
369                       ((AddSenderInfo) req).index);
370       else if (req instanceof RemoveSenderInfo) {
371         if (((RemoveSenderInfo) req).index > -1)
372           removeSenderInfo(((RemoveSenderInfo) req).index);
373         else
374           removeSenderInfo(((RemoveSenderInfo) req).si);
375       } else if (req instanceof UpdateSenderInfo)
376         updateSenderInfo(((UpdateSenderInfo) req).oldSi,
377                          ((UpdateSenderInfo) req).newSi);
378       
379       if (logger.isLoggable(BasicLevel.DEBUG))
380         logger.log(BasicLevel.DEBUG,
381                    "--- " + this +
382                    " specialAdminProcess senderInfos=" +
383                    senderInfos);
384     } catch (Exception JavaDoc exc) {
385       if (logger.isLoggable(BasicLevel.WARN))
386         logger.log(BasicLevel.WARN,
387                    "--- " + this +
388                    " specialAdminProcess",
389                    exc);
390       throw new RequestException(exc.getMessage());
391     }
392     return "done";
393   }
394     
395   protected void addSenderInfo(SenderInfo si, int index)
396     throws ArrayIndexOutOfBoundsException JavaDoc {
397     if (index > -1)
398       senderInfos.add(index,si);
399     else
400       senderInfos.add(si);
401   }
402
403   protected SenderInfo removeSenderInfo(int index)
404     throws ArrayIndexOutOfBoundsException JavaDoc {
405     return (SenderInfo) senderInfos.remove(index);
406   }
407
408   protected boolean removeSenderInfo(SenderInfo si) {
409     return senderInfos.remove(si);
410   }
411
412   protected boolean updateSenderInfo(SenderInfo oldSi, SenderInfo newSi)
413     throws ArrayIndexOutOfBoundsException JavaDoc {
414     int index = senderInfos.indexOf(oldSi);
415     if (index > -1) return false;
416     senderInfos.set(index,newSi);
417     return true;
418   }
419
420   public ClientMessages preProcess(AgentId from, ClientMessages not) {
421     for (Enumeration JavaDoc msgs = not.getMessages().elements();
422          msgs.hasMoreElements();) {
423       Message msg = (Message) msgs.nextElement();
424       SenderInfo si = match(msg);
425
426       if (logger.isLoggable(BasicLevel.DEBUG))
427         logger.log(BasicLevel.DEBUG,
428                    "--- " + this +
429                    " match=" + (si!=null));
430       if (si != null) {
431         try {
432           javaMailUtil.sendJavaMail(si, new MailMessage(msg));
433         } catch (Exception JavaDoc exc) {
434           ClientMessages deadM =
435             new ClientMessages(not.getClientContext(),
436                                not.getRequestId());
437           
438           deadM.addMessage(msg);
439           sendToDMQ(deadM,not.getDMQId());
440           
441           logger.log(BasicLevel.WARN,
442                      "JavaMailQueueImpl.sendJavaMail",
443                      exc);
444         }
445         not.getMessages().remove(msg);
446       }
447     }
448     if (not.getMessages().size() > 0) {
449       return not;
450     }
451     return null;
452   }
453   
454   protected SenderInfo match(Message msg) {
455     for (Enumeration JavaDoc e = senderInfos.elements(); e.hasMoreElements(); ) {
456       SenderInfo si = (SenderInfo) e.nextElement();
457       if (si.selector == null) return si;
458       if (Selector.matches(msg,si.selector))
459         return si;
460     }
461     return null;
462   }
463
464   public void doPop() {
465     long count = 0;
466     Vector JavaDoc toExpunge = new Vector JavaDoc();
467     javax.mail.Message JavaDoc[] msgs = javaMailUtil.popMail(popServer,
468                                                      popUser,
469                                                      popPassword,
470                                                      expunge);
471     if (msgs != null) {
472       for (int i = 0; i < msgs.length; i++) {
473         if (logger.isLoggable(BasicLevel.DEBUG))
474           logger.log(BasicLevel.DEBUG,
475                      "--- " + this +
476                      " doPop : msgs[" + i + "] = " + msgs[i]);
477         try {
478           count++;
479           Properties JavaDoc prop = javaMailUtil.getMOMProperties(msgs[i]);
480           MailMessage m =
481             javaMailUtil.createMessage(prop,
482                                        destId.toString()+"mail_"+count,
483                                        Queue.getDestinationType(),
484                                        destId.toString(),
485                                        Queue.getDestinationType());
486           storeMessage(new org.objectweb.joram.mom.messages.Message(m.getSharedMessage()));
487
488           if (logger.isLoggable(BasicLevel.DEBUG))
489             logger.log(BasicLevel.DEBUG,
490                        "--- " + this +
491                        " doPop : storeMessage m = " + m);
492           if (expunge)
493             toExpunge.add(msgs[i]);
494         } catch (Exception JavaDoc exc) {
495           if (logger.isLoggable(BasicLevel.DEBUG))
496             logger.log(BasicLevel.DEBUG,
497                        "--- " + this +
498                        " doPop", exc);
499           continue;
500         }
501       }
502       deliverMessages(0);
503     }
504
505     javaMailUtil.closeFolder(toExpunge,expunge);
506     toExpunge.clear();
507   }
508
509   private void readObject(java.io.ObjectInputStream JavaDoc in)
510     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
511
512     in.defaultReadObject();
513
514     if (logger.isLoggable(BasicLevel.DEBUG))
515       logger.log(BasicLevel.DEBUG,
516                  "--- " + this +
517                  " JavaMailQueueImpl.readObject : " +
518                  "\nsenderInfos=" + senderInfos +
519                  "\npopServer=" + popServer +
520                  "\npopUser=" + popUser +
521                  "\npopPeriod=" + popPeriod +
522                  "\nexpunge=" + expunge);
523     javaMailUtil = new JavaMailUtil();
524   }
525 }
526
Popular Tags