1 25 26 package org.objectweb.jonas.mail.factory; 27 28 import java.io.ByteArrayInputStream ; 30 import java.io.IOException ; 31 import java.io.ObjectInputStream ; 32 import java.io.OptionalDataException ; 33 import java.util.Hashtable ; 34 import java.util.Properties ; 35 36 import javax.mail.Session ; 38 import javax.naming.Context ; 39 import javax.naming.Name ; 40 import javax.naming.RefAddr ; 41 import javax.naming.Reference ; 42 import javax.naming.spi.ObjectFactory ; 43 44 import org.objectweb.util.monolog.api.BasicLevel; 46 import org.objectweb.util.monolog.api.Logger; 47 48 import org.objectweb.jonas.common.Log; 50 import org.objectweb.jonas.common.PropDump; 51 import org.objectweb.jonas.common.JNDIUtils; 52 import org.objectweb.jonas.mail.lib.JAuthenticator; 53 54 60 public class JavaMailSessionFactory implements ObjectFactory { 61 62 65 protected static final String FACTORY_TYPE = "javax.mail.Session"; 66 67 70 private static Logger logger = null; 71 72 73 90 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 91 Hashtable environment) throws Exception { 92 93 if (logger == null) { 95 logger = Log.getLogger(Log.JONAS_MAIL_PREFIX); 96 } 97 98 Reference ref = (Reference ) obj; 100 101 String clname = ref.getClassName(); 103 104 if (!ref.getClassName().equals(FACTORY_TYPE)) { 106 logger.log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'."); 107 return (null); 108 } 109 110 Properties props = new Properties (); 111 Properties authenticationProps = new Properties (); 112 RefAddr refAddr = null; 113 114 refAddr = ref.get("javaxmailSession.properties"); 115 if (refAddr != null) { 116 props = (Properties ) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent()); 117 if (logger.isLoggable(BasicLevel.DEBUG)) { 118 PropDump.print("These are the properties attached to the Reference object used to construct a Session", 119 props, logger, BasicLevel.DEBUG); 120 } 121 } 122 123 refAddr = ref.get("authentication.properties"); 124 if (refAddr != null) { 125 authenticationProps = (Properties ) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent()); 126 if (logger.isLoggable(BasicLevel.DEBUG)) { 127 PropDump.print("These are the authentication properties used to construct a Authenticator", 128 authenticationProps, logger, BasicLevel.DEBUG); 129 } 130 } 131 132 String mailAuthenticationUsername = authenticationProps.getProperty("mail.authentication.username"); 134 135 String mailAuthenticationPassword = authenticationProps.getProperty("mail.authentication.password"); 137 138 JAuthenticator jAuthenticator = null; 139 if ((mailAuthenticationUsername != null) && (mailAuthenticationPassword != null)) { 140 jAuthenticator = new JAuthenticator(mailAuthenticationUsername, mailAuthenticationPassword); 141 } 142 143 Session session = Session.getInstance(props, jAuthenticator); 145 146 return (session); 147 } 148 149 } 150 | Popular Tags |