KickJava   Java API By Example, From Geeks To Geeks.

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


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.Topic;
31 import org.objectweb.joram.mom.dest.TopicImpl;
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>JavaMailTopicImpl</code> class implements the MOM topic behaviour,
46  * basically storing messages and delivering them upon clients requests.
47  */

48 public class JavaMailTopicImpl extends TopicImpl implements JavaMailTopicImplMBean {
49   public static Logger logger = Debug.getLogger(JavaMailTopicImpl.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>JavaMailTopicImpl</code> instance.
72    *
73    * @param destId Identifier of the agent hosting the topic.
74    * @param adminId Identifier of the administrator of the topic.
75    * @param prop Properties to configure the topic.
76    */

77   public JavaMailTopicImpl(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                  " JavaMailTopicImpl : " +
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 "JavaMailTopicImpl:" + 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 void updateSenderInfo(SenderInfo oldSi, SenderInfo newSi)
413     throws ArrayIndexOutOfBoundsException JavaDoc {
414     int index = senderInfos.indexOf(oldSi);
415     if (index > -1)
416       senderInfos.set(index,newSi);
417   }
418
419   public ClientMessages preProcess(AgentId from, ClientMessages not) {
420     for (Enumeration JavaDoc msgs = not.getMessages().elements();
421          msgs.hasMoreElements();) {
422       Message msg = (Message) msgs.nextElement();
423       SenderInfo si = match(msg);
424
425       if (logger.isLoggable(BasicLevel.DEBUG))
426         logger.log(BasicLevel.DEBUG,
427                    "--- " + this +
428                    " match=" + (si!=null));
429       if (si != null) {
430         try {
431           javaMailUtil.sendJavaMail(si, new MailMessage(msg));
432         } catch (Exception JavaDoc exc) {
433           ClientMessages deadM =
434             new ClientMessages(not.getClientContext(),
435                                not.getRequestId());
436           
437           deadM.addMessage(msg);
438           sendToDMQ(deadM,not.getDMQId());
439           
440           if (logger.isLoggable(BasicLevel.WARN))
441             logger.log(BasicLevel.WARN, "JavaMailTopicImpl.sendJavaMail", exc);
442         }
443       }
444     }
445     return not;
446   }
447   
448   protected SenderInfo match(Message msg) {
449     for (Enumeration JavaDoc e = senderInfos.elements(); e.hasMoreElements(); ) {
450       SenderInfo si = (SenderInfo) e.nextElement();
451       if (si.selector == null) return si;
452       if (Selector.matches(msg,si.selector))
453         return si;
454     }
455     return null;
456   }
457
458   public void doPop() {
459     long count = 0;
460     Vector JavaDoc toExpunge = new Vector JavaDoc();
461     javax.mail.Message JavaDoc[] msgs = javaMailUtil.popMail(popServer,
462                                                      popUser,
463                                                      popPassword,
464                                                      expunge);
465     if (msgs != null) {
466       for (int i = 0; i < msgs.length; i++) {
467         if (logger.isLoggable(BasicLevel.DEBUG))
468           logger.log(BasicLevel.DEBUG,
469                      "--- " + this +
470                      " doPop : msgs[" + i + "] = " + msgs[i]);
471         try {
472           count++;
473           Properties JavaDoc prop = javaMailUtil.getMOMProperties(msgs[i]);
474           MailMessage m =
475             javaMailUtil.createMessage(prop,
476                                        destId.toString()+"mail_"+count,
477                                        Topic.getDestinationType(),
478                                        destId.toString(),
479                                        Topic.getDestinationType());
480           publish(m.getSharedMessage());
481
482           if (logger.isLoggable(BasicLevel.DEBUG))
483             logger.log(BasicLevel.DEBUG,
484                        "--- " + this +
485                        " doPop : publish m = " + m);
486           if (expunge)
487             toExpunge.add(msgs[i]);
488         } catch (Exception JavaDoc exc) {
489           if (logger.isLoggable(BasicLevel.DEBUG))
490             logger.log(BasicLevel.DEBUG,
491                        "--- " + this +
492                        " doPop", exc);
493           continue;
494         }
495       }
496     }
497     
498     javaMailUtil.closeFolder(toExpunge,expunge);
499     toExpunge.clear();
500   }
501   
502   private void publish(Message msg) {
503     if (logger.isLoggable(BasicLevel.DEBUG))
504       logger.log(BasicLevel.DEBUG,
505                  "--- " + this +
506                  " publish msg=" + msg);
507
508     Vector JavaDoc messages = new Vector JavaDoc();
509     messages.add(msg);
510     ClientMessages cm = new ClientMessages(-1,-1,messages);
511     // not use channel.sendTo(...) because from=#0.0.0
512
//javaMailTopic.send(destId,cm);
513
forward(destId,cm);
514   }
515
516   private void readObject(java.io.ObjectInputStream JavaDoc in)
517     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
518
519     in.defaultReadObject();
520
521     if (logger.isLoggable(BasicLevel.DEBUG))
522       logger.log(BasicLevel.DEBUG,
523                  "--- " + this +
524                  " JavaMailTopicImpl.readObject : " +
525                  "\nsenderInfos=" + senderInfos +
526                  "\npopServer=" + popServer +
527                  "\npopUser=" + popUser +
528                  "\npopPeriod=" + popPeriod +
529                  "\nexpunge=" + expunge);
530     javaMailUtil = new JavaMailUtil();
531   }
532 }
533
Popular Tags