JavaMailSessionFactory.java

00001 
00026 package org.objectweb.jonas.mail.factory;
00027 
00028 //import java
00029 import java.io.ByteArrayInputStream;
00030 import java.io.IOException;
00031 import java.io.ObjectInputStream;
00032 import java.io.OptionalDataException;
00033 import java.util.Hashtable;
00034 import java.util.Properties;
00035 
00036 //import javax
00037 import javax.mail.Session;
00038 import javax.naming.Context;
00039 import javax.naming.Name;
00040 import javax.naming.RefAddr;
00041 import javax.naming.Reference;
00042 import javax.naming.spi.ObjectFactory;
00043 
00044 //import objectweb.util
00045 import org.objectweb.util.monolog.api.BasicLevel;
00046 import org.objectweb.util.monolog.api.Logger;
00047 
00048 //import jonas
00049 import org.objectweb.jonas.common.Log;
00050 import org.objectweb.jonas.common.PropDump;
00051 import org.objectweb.jonas.common.JNDIUtils;
00052 import org.objectweb.jonas.mail.lib.JAuthenticator;
00053 
00060 public class JavaMailSessionFactory implements ObjectFactory {
00061 
00065     protected static final String FACTORY_TYPE = "javax.mail.Session";
00066 
00070     private static Logger logger = null;
00071 
00072 
00090     public Object getObjectInstance(Object obj, Name name, Context nameCtx,
00091                                     Hashtable environment) throws Exception {
00092 
00093         //Get the logger
00094         if (logger == null) {
00095             logger = Log.getLogger(Log.JONAS_MAIL_PREFIX);
00096         }
00097 
00098         //Get the reference
00099         Reference ref = (Reference) obj;
00100 
00101         //Get the class name
00102         String clname = ref.getClassName();
00103 
00104         //Check the class name
00105         if (!ref.getClassName().equals(FACTORY_TYPE)) {
00106             logger.log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'.");
00107             return (null);
00108         }
00109 
00110         Properties props = new Properties();
00111         Properties authenticationProps = new Properties();
00112         RefAddr refAddr = null;
00113 
00114         refAddr =  ref.get("javaxmailSession.properties");
00115         if (refAddr != null) {
00116             props = (Properties) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
00117             PropDump.print("These are the properties attached to the Reference object used to construct a Session",
00118                            props, logger, BasicLevel.DEBUG);
00119         }
00120 
00121         refAddr =  ref.get("authentication.properties");
00122         if (refAddr != null) {
00123             authenticationProps = (Properties) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
00124             PropDump.print("These are the authentication properties used to construct a Authenticator",
00125                            authenticationProps, logger, BasicLevel.DEBUG);
00126         }
00127 
00128         //username (Authentication)
00129         String mailAuthenticationUsername =  authenticationProps.getProperty("mail.authentication.username");
00130 
00131         //Password (Authentication)
00132         String mailAuthenticationPassword = authenticationProps.getProperty("mail.authentication.password");
00133 
00134         JAuthenticator jAuthenticator = null;
00135         if ((mailAuthenticationUsername != null) && (mailAuthenticationPassword != null)) {
00136             jAuthenticator = new JAuthenticator(mailAuthenticationUsername, mailAuthenticationPassword);
00137         }
00138 
00139         // Create and return a new Session object
00140         Session session = Session.getInstance(props, jAuthenticator);
00141 
00142         return (session);
00143     }
00144 
00145 }

Generated on Tue Feb 15 15:05:18 2005 for JOnAS by  doxygen 1.3.9.1