1 17 package org.apache.geronimo.mail; 18 19 import javax.mail.Authenticator ; 20 import javax.mail.Session ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.Properties ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.apache.geronimo.gbean.GBeanInfo; 29 import org.apache.geronimo.gbean.GBeanInfoBuilder; 30 import org.apache.geronimo.gbean.GBeanLifecycle; 31 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 32 import org.apache.geronimo.management.JavaMailResource; 33 34 35 48 public class MailGBean implements GBeanLifecycle, JavaMailResource { 49 50 private final Log log = LogFactory.getLog(MailGBean.class); 51 52 private final String objectName; 53 private final Collection protocols; 54 private Boolean useDefault; 55 private Properties properties; 56 private Authenticator authenticator; 57 private String storeProtocol; 58 private String transportProtocol; 59 private String host; 60 private String user; 61 private Boolean debug; 62 63 64 80 public MailGBean(String objectName, Collection protocols, Boolean useDefault, Properties properties, Authenticator authenticator, 81 String storeProtocol, String transportProtocol, String host, String user, Boolean debug) { 82 this.objectName = objectName; 83 this.protocols = protocols; 84 setUseDefault(useDefault); 85 this.properties = (properties == null ? new Properties () : properties); 86 setAuthenticator(authenticator); 87 setStoreProtocol(storeProtocol); 88 setTransportProtocol(transportProtocol); 89 setHost(host); 90 setUser(user); 91 setDebug(debug); 92 93 } 94 95 98 public Collection getProtocols() { 99 return protocols; 100 } 101 102 105 public Boolean getUseDefault() { 106 return useDefault; 107 } 108 109 114 public void setUseDefault(Boolean useDefault) { 115 this.useDefault = useDefault; 116 } 117 118 124 public Properties getProperties() { 125 return properties; 126 } 127 128 136 public void setProperties(Properties properties) { 137 this.properties = properties; 138 } 139 140 146 public Authenticator getAuthenticator() { 147 return authenticator; 148 } 149 150 158 public void setAuthenticator(Authenticator authenticator) { 159 this.authenticator = authenticator; 160 } 161 162 170 public String getStoreProtocol() { 171 return storeProtocol; 172 } 173 174 187 public void setStoreProtocol(String storeProtocol) { 188 this.storeProtocol = storeProtocol; 189 } 190 191 199 public String getTransportProtocol() { 200 return transportProtocol; 201 } 202 203 216 public void setTransportProtocol(String transportProtocol) { 217 this.transportProtocol = transportProtocol; 218 } 219 220 227 public String getHost() { 228 return host; 229 } 230 231 243 public void setHost(String host) { 244 this.host = host; 245 } 246 247 254 public String getUser() { 255 return user; 256 } 257 258 270 public void setUser(String user) { 271 this.user = user; 272 } 273 274 277 public Boolean getDebug() { 278 return debug; 279 } 280 281 289 public void setDebug(Boolean debug) { 290 this.debug = debug; 291 } 292 293 public Object $getResource() { 294 Properties props = new Properties (properties); 295 296 if (protocols != null) { 297 for (Iterator iter = protocols.iterator(); iter.hasNext();) { 298 ProtocolGBean protocol = (ProtocolGBean) iter.next(); 299 protocol.addOverrides(props); 300 } 301 } 302 303 props.putAll(properties); 304 305 if (storeProtocol != null) props.put("mail.store.protocol", storeProtocol); 306 if (transportProtocol != null) props.put("mail.transport.protocol", transportProtocol); 307 if (host != null) props.put("mail.host", host); 308 if (user != null) props.put("mail.user", user); 309 if (debug != null) props.put("mail.debug", debug.toString()); 311 312 if (Boolean.TRUE.equals(useDefault)) { 313 if (authenticator == null) { 314 return Session.getDefaultInstance(props); 315 } else { 316 return Session.getDefaultInstance(props, authenticator); 317 } 318 } else { 319 if (authenticator == null) { 320 return Session.getInstance(props); 321 } else { 322 return Session.getInstance(props, authenticator); 323 } 324 } 325 } 326 327 public void doStart() throws Exception { 328 log.debug("Started " + objectName + " - will return " 329 + (Boolean.TRUE.equals(useDefault) ? "default" : "new") 330 + " JavaMail Session " 331 + (authenticator == null ? "without" : "with") 332 + " authenticator"); 333 } 334 335 public void doStop() throws Exception { 336 log.debug("Stopped " + objectName); 337 } 338 339 public void doFail() { 340 log.warn("Failed " + objectName); 341 } 342 343 346 public String getObjectName() { 347 return objectName; 348 } 349 350 public boolean isStateManageable() { 351 return false; 352 } 353 354 public boolean isStatisticsProvider() { 355 return false; 356 } 357 358 public boolean isEventProvider() { 359 return false; 360 } 361 362 public static final GBeanInfo GBEAN_INFO; 363 364 static { 365 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(MailGBean.class, NameFactory.JAVA_MAIL_RESOURCE); 366 367 infoFactory.addAttribute("objectName", String .class, false); 368 infoFactory.addReference("Protocols", ProtocolGBean.class, NameFactory.GERONIMO_SERVICE); 369 infoFactory.addAttribute("useDefault", Boolean .class, true); 370 infoFactory.addAttribute("properties", Properties .class, true); 371 infoFactory.addReference("Authenticator", Authenticator .class, NameFactory.GERONIMO_SERVICE); 372 infoFactory.addAttribute("storeProtocol", String .class, true); 373 infoFactory.addAttribute("transportProtocol", String .class, true); 374 infoFactory.addAttribute("host", String .class, true); 375 infoFactory.addAttribute("user", String .class, true); 376 infoFactory.addAttribute("debug", Boolean .class, true); 377 infoFactory.addOperation("$getResource"); 378 infoFactory.addOperation("getProtocols"); 379 infoFactory.addInterface(JavaMailResource.class); 380 381 infoFactory.setConstructor(new String []{"objectName", 382 "Protocols", 383 "useDefault", 384 "properties", 385 "Authenticator", 386 "storeProtocol", 387 "transportProtocol", 388 "host", 389 "user", 390 "debug"}); 391 392 GBEAN_INFO = infoFactory.getBeanInfo(); 393 } 394 395 public static GBeanInfo getGBeanInfo() { 396 return GBEAN_INFO; 397 } 398 } 399 | Popular Tags |