1 17 18 package org.apache.naming.factory; 19 20 import java.security.AccessController ; 21 import java.security.PrivilegedAction ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Properties ; 25 import javax.mail.Authenticator ; 26 import javax.mail.PasswordAuthentication ; 27 import javax.mail.Session ; 28 import javax.naming.Name ; 29 import javax.naming.Context ; 30 import javax.naming.RefAddr ; 31 import javax.naming.Reference ; 32 import javax.naming.spi.ObjectFactory ; 33 34 67 68 public class MailSessionFactory implements ObjectFactory { 69 70 71 74 protected static final String factoryType = "javax.mail.Session"; 75 76 77 91 public Object getObjectInstance(Object refObj, Name name, Context context, 92 Hashtable env) throws Exception 93 { 94 95 final Reference ref = (Reference ) refObj; 97 if (!ref.getClassName().equals(factoryType)) 98 return (null); 99 100 return AccessController.doPrivileged( new PrivilegedAction () { 106 public Object run() { 107 108 Properties props = new Properties (); 110 props.put("mail.transport.protocol", "smtp"); 111 props.put("mail.smtp.host", "localhost"); 112 113 String password = null; 114 115 Enumeration attrs = ref.getAll(); 116 while (attrs.hasMoreElements()) { 117 RefAddr attr = (RefAddr ) attrs.nextElement(); 118 if ("factory".equals(attr.getType())) { 119 continue; 120 } 121 122 if ("password".equals(attr.getType())) { 123 password = (String ) attr.getContent(); 124 continue; 125 } 126 127 props.put(attr.getType(), (String ) attr.getContent()); 128 } 129 130 Authenticator auth = null; 131 if (password != null) { 132 String user = props.getProperty("mail.smtp.user"); 133 if(user == null) { 134 user = props.getProperty("mail.user"); 135 } 136 137 if(user != null) { 138 final PasswordAuthentication pa = new PasswordAuthentication (user, password); 139 auth = new Authenticator () { 140 protected PasswordAuthentication getPasswordAuthentication() { 141 return pa; 142 } 143 }; 144 } 145 } 146 147 Session session = Session.getInstance(props, auth); 149 return (session); 150 151 } 152 } ); 153 154 } 155 156 157 } 158 | Popular Tags |