1 22 package org.jboss.mail; 23 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 28 import java.util.Properties ; 29 30 import javax.management.ObjectName ; 31 import javax.management.MBeanServer ; 32 import javax.management.MalformedObjectNameException ; 33 34 import javax.naming.InitialContext ; 35 import javax.naming.Reference ; 36 import javax.naming.StringRefAddr ; 37 import javax.naming.Name ; 38 import javax.naming.Context ; 39 import javax.naming.NamingException ; 40 import javax.naming.NameNotFoundException ; 41 42 import javax.mail.Session ; 43 import javax.mail.PasswordAuthentication ; 44 import javax.mail.Authenticator ; 45 46 import org.jboss.system.ServiceMBeanSupport; 47 import org.jboss.naming.NonSerializableFactory; 48 49 61 public class MailService 62 extends ServiceMBeanSupport 63 implements MailServiceMBean 64 { 65 public static final String JNDI_NAME = "java:/Mail"; 66 67 private String user; 68 private String password; 69 private String jndiName = JNDI_NAME; 70 private Element config; 71 72 73 ObjectName mMail; 74 75 76 Properties ourProps = null; 77 78 85 public void setUser(final String user) 86 { 87 this.user = user; 88 } 89 90 93 public String getUser() 94 { 95 return user; 96 } 97 98 105 public void setPassword(final String password) 106 { 107 this.password = password; 108 } 109 110 113 protected String getPassword() 114 { 115 return password; 116 } 117 118 123 public Element getConfiguration() 124 { 125 return config; 126 } 127 128 133 public void setConfiguration(final Element element) 134 { 135 config = element; 136 } 137 138 142 public void setJNDIName(final String name) 143 { 144 jndiName = name; 145 } 146 147 150 public String getJNDIName() 151 { 152 return jndiName; 153 } 154 155 158 public String getStoreProtocol() 159 { 160 if (ourProps != null) 161 return ourProps.getProperty("mail.store.protocol"); 162 else 163 return null; 164 } 165 166 169 public String getTransportProtocol() 170 { 171 if (ourProps != null) 172 return ourProps.getProperty("mail.transport.protocol"); 173 else 174 return null; 175 } 176 177 180 public String getDefaultSender() 181 { 182 if (ourProps != null) 183 return ourProps.getProperty("mail.from"); 184 else 185 return null; 186 } 187 188 189 192 public String getSMTPServerHost() 193 { 194 if (ourProps != null) 195 return ourProps.getProperty("mail.smtp.host"); 196 else 197 return null; 198 } 199 200 203 public String getPOP3ServerHost() 204 { 205 if (ourProps != null) 206 return ourProps.getProperty("mail.pop3.host"); 207 else 208 return null; 209 } 210 211 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 212 throws MalformedObjectNameException 213 { 214 return name == null ? OBJECT_NAME : name; 215 } 216 217 protected void startService() throws Exception 218 { 219 final PasswordAuthentication pa = new PasswordAuthentication (getUser(), getPassword()); 221 Authenticator a = new Authenticator () 222 { 223 protected PasswordAuthentication getPasswordAuthentication() 224 { 225 return pa; 226 } 227 }; 228 229 Properties props = getProperties(); 230 231 Session session = Session.getInstance(props, a); 233 bind(session); 234 235 ourProps = props; 237 } 238 239 protected Properties getProperties() throws Exception 240 { 241 Properties props = new Properties (); 242 if (config == null) 243 { 244 log.warn("No configuration specified; using empty properties map"); 245 return props; 246 } 247 248 NodeList list = config.getElementsByTagName("property"); 249 int len = list.getLength(); 250 251 for (int i = 0; i < len; i++) 252 { 253 Node node = list.item(i); 254 255 switch (node.getNodeType()) 256 { 257 case Node.ELEMENT_NODE: 258 Element child = (Element ) node; 259 String name, value; 260 261 if (child.hasAttribute("name")) 263 { 264 name = child.getAttribute("name"); 265 } 266 else 267 { 268 log.warn("Ignoring invalid element; missing 'name' attribute: " + child); 269 break; 270 } 271 272 if (child.hasAttribute("value")) 274 { 275 value = child.getAttribute("value"); 276 } 277 else 278 { 279 log.warn("Ignoring invalid element; missing 'value' attribute: " + child); 280 break; 281 } 282 283 if (log.isTraceEnabled()) 284 { 285 log.trace("setting property " + name + "=" + value); 286 } 287 props.setProperty(name, value); 288 break; 289 290 case Node.COMMENT_NODE: 291 break; 293 294 default: 295 log.debug("ignoring unsupported node type: " + node); 296 break; 297 } 298 } 299 300 log.debug("Using properties: " + props); 301 302 return props; 303 } 304 305 protected void stopService() throws Exception 306 { 307 unbind(); 308 } 309 310 private void bind(Session session) throws NamingException 311 { 312 String bindName = getJNDIName(); 313 314 NonSerializableFactory.bind(bindName, session); 316 317 Context ctx = new InitialContext (); 318 try 319 { 320 Name n = ctx.getNameParser("").parse(bindName); 321 while (n.size() > 1) 322 { 323 String ctxName = n.get(0); 324 try 325 { 326 ctx = (Context ) ctx.lookup(ctxName); 327 } 328 catch (NameNotFoundException e) 329 { 330 ctx = ctx.createSubcontext(ctxName); 331 } 332 n = n.getSuffix(1); 333 } 334 335 336 339 StringRefAddr addr = new StringRefAddr ("nns", bindName); 340 Reference ref = new Reference (Session .class.getName(), 341 addr, 342 NonSerializableFactory.class.getName(), 343 null); 344 ctx.bind(n.get(0), ref); 345 } 346 finally 347 { 348 ctx.close(); 349 } 350 351 log.info("Mail Service bound to " + bindName); 352 } 353 354 private void unbind() throws NamingException 355 { 356 String bindName = getJNDIName(); 357 358 if (bindName != null) 359 { 360 InitialContext ctx = new InitialContext (); 361 try 362 { 363 ctx.unbind(bindName); 364 } 365 finally 366 { 367 ctx.close(); 368 } 369 370 NonSerializableFactory.unbind(bindName); 371 log.info("Mail service '" + getJNDIName() + "' removed from JNDI"); 372 } 373 } 374 } 375 | Popular Tags |