1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: RmiMailet.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   //
27   package com.mjh.switchrmi.transport.smtp.james;
28   
29   import com.mjh.switchrmi.*;
30   import com.mjh.switchrmi.transport.smtp.SmtpServiceHandler;
31   
32   import java.util.*;
33   
34   import javax.mail.*;
35   import javax.mail.internet.*;
36   
37   import javax.naming.*;
38   
39   import org.apache.mailet.GenericMailet;
40   import org.apache.mailet.Mail;
41   import org.apache.mailet.MailetConfig;
42   
43   public class RmiMailet extends GenericMailet
44   {
45       Context jndiContext;
46   
47       public void init(MailetConfig config)
48                 throws MessagingException
49       {
50           try
51           {
52               String configurationName = 
53                       config.getInitParameter("rpcServiceConfiguration");
54               Hashtable env = new Hashtable();
55   
56               env.put(Context.PROVIDER_URL, configurationName);
57               jndiContext = new InitialContext(env);
58           }
59           catch (Exception ex)
60           {
61               throw new MessagingException(ex.toString());
62           }
63       }
64   
65       public void service(Mail mail)
66                    throws MessagingException
67       {
68           try
69           {
70               MimeMessage message = mail.getMessage();
71   
72               String subject = message.getSubject();
73   
74               if (subject.startsWith("RPC:"))
75               {
76                   Multipart multipart = (Multipart) message.getContent();
77   
78                   if (multipart.getCount() == 2)
79                   {
80                       SmtpServiceHandler handler = 
81                               new SmtpServiceHandler(jndiContext);
82   
83                       handler.setMessage(message);
84   
85                       handler.invoke();
86                   }
87               }
88           }
89           catch (Exception ex)
90           {
91               debug(ex);
92               throw new MessagingException(ex.toString());
93           }
94       }
95   
96       private void debug(String msg)
97       {
98           System.out.println(msg);
99       }
100  
101      private void debug(Exception ex)
102      {
103          debug(ex.toString());
104          ex.printStackTrace();
105      }
106  }