1 23 24 package com.sun.enterprise.deployment; 25 26 import java.io.Serializable ; 27 import java.util.Properties ; 28 29 37 import java.util.Set ; 39 import java.util.Iterator ; 40 import java.util.Enumeration ; 41 import com.sun.enterprise.deployment.interfaces.MailResourceIntf; 42 import com.sun.enterprise.repository.ResourceProperty; 43 45 import java.util.logging.*; 46 import com.sun.enterprise.deployment.util.LogDomains; 47 48 49 53 public class MailConfiguration implements Serializable { 54 62 63 private static String PROTOCOL_TYPE_IMAP = "imap"; 65 private static String PROTOCOL_TYPE_POP3 = "pop3"; 66 private static String PROTOCOL_TYPE_SMTP = "smtp"; 67 68 private static String PROP_NAME_PREFIX = "mail-"; 69 private static String PROP_NAME_SUFFIX_HOST = "-host"; 70 private static String PROP_NAME_SUFFIX_USER = "-user"; 71 private static char PROP_NAME_DELIM = '-'; 72 73 private static String DEF_VAL_STORE_PROTOCOL = PROTOCOL_TYPE_IMAP; 74 private static String DEF_VAL_STORE_PROTOCOL_CLASS = 75 "com.sun.mail.imap.IMAPStore"; 76 private static String DEF_VAL_TRANSPORT_PROTOCOL = PROTOCOL_TYPE_SMTP; 77 private static String DEF_VAL_TRANSPORT_PROTOCOL_CLASS = 78 "com.sun.mail.smtp.SMTPTransport"; 79 private static String DEF_VAL_HOST = "localhost"; 80 private static String DEF_VAL_USER = "user.name"; 81 private static String DEF_VAL_FROM = "username@host"; 82 private static boolean DEF_VAL_DEBUG = false; 83 84 private static String MAIL_STORE_PROTOCOL = "mail.store.protocol"; 85 private static String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol"; 86 private static String MAIL_HOST = "mail.host"; 87 private static String MAIL_USER = "mail.user"; 88 private static String MAIL_FROM = "mail.from"; 89 private static String MAIL_DEBUG = "mail.debug"; 90 91 private static String MAIL_PREFIX = "mail."; 92 private static String MAIL_SUFFIX_CLASS = ".class"; 93 private static String MAIL_SUFFIX_HOST = ".host"; 94 private static String MAIL_SUFFIX_USER = ".user"; 95 private static char MAIL_DELIM = '.'; 96 97 100 private String description = ""; 101 private String jndiName = ""; 102 private boolean enabled = false; 103 private String storeProtocol = DEF_VAL_STORE_PROTOCOL; 104 private String storeProtocolClass = DEF_VAL_STORE_PROTOCOL_CLASS; 105 private String transportProtocol = DEF_VAL_TRANSPORT_PROTOCOL; 106 private String transportProtocolClass = DEF_VAL_TRANSPORT_PROTOCOL_CLASS; 107 private String mailHost = DEF_VAL_HOST; 108 private String username = DEF_VAL_USER; 109 private String mailFrom = DEF_VAL_FROM; 110 private boolean debug = DEF_VAL_DEBUG; 111 112 private Properties mailProperties = new Properties (); 113 115 118 static Logger _logger = LogDomains.getLogger(LogDomains.DPL_LOGGER); 119 120 127 public MailConfiguration(String username, String mailFrom) { 128 this.username = username; 129 this.mailFrom = mailFrom; 130 this.mailHost = ""; 131 132 mailProperties.put(MAIL_FROM, this.getMailFrom()); 133 mailProperties.put(MAIL_USER, this.getUsername()); 134 mailProperties.put(MAIL_HOST, this.getMailHost()); 135 } 136 137 144 public MailConfiguration(String username, 145 String mailFrom, 146 String mailHost) { 147 this.username = username; 148 this.mailFrom = mailFrom; 149 this.mailHost = mailHost; 150 151 mailProperties.put(MAIL_FROM, this.getMailFrom()); 152 mailProperties.put(MAIL_USER, this.getUsername()); 153 mailProperties.put(MAIL_HOST, this.getMailHost()); 154 } 155 156 161 public MailConfiguration(MailResourceIntf mailRes) { 162 try { 163 loadMailResources(mailRes); 164 } 165 catch (Exception ce) { 166 _logger.log(Level.INFO,"enterprise.deployment_mail_cfgexcp",ce); 168 169 } 170 } 171 172 176 private void loadMailResources(MailResourceIntf mailResource) throws Exception { 177 178 if (mailResource == null) { 179 _logger.log(Level.FINE, 180 "MailConfiguration: no MailResource object. mailResource=" + 181 mailResource); 182 return; 183 } 184 185 jndiName = mailResource.getName(); 186 description = mailResource.getDescription(); 187 enabled = mailResource.isEnabled(); 188 189 storeProtocol = mailResource.getStoreProtocol(); 190 storeProtocolClass = mailResource.getStoreProtocolClass(); 191 transportProtocol = mailResource.getTransportProtocol(); 192 transportProtocolClass = mailResource.getTransportProtocolClass(); 193 mailHost = mailResource.getMailHost(); 194 username = mailResource.getUsername(); 195 mailFrom = mailResource.getMailFrom(); 196 debug = mailResource.isDebug(); 197 198 String storeProtocolClassName = MAIL_PREFIX + storeProtocol + 200 MAIL_SUFFIX_CLASS; 201 String transportProtocolClassName = MAIL_PREFIX + transportProtocol + 202 MAIL_SUFFIX_CLASS; 203 204 mailProperties.put(MAIL_STORE_PROTOCOL, storeProtocol); 205 mailProperties.put(MAIL_TRANSPORT_PROTOCOL, transportProtocol); 206 mailProperties.put(storeProtocolClassName, storeProtocolClass); 207 mailProperties.put(transportProtocolClassName, transportProtocolClass); 208 mailProperties.put(MAIL_FROM, mailFrom); 209 mailProperties.put(MAIL_DEBUG, (debug ? "true" : "false")); 210 211 Set properties = mailResource.getProperties(); 213 ResourceProperty property = null; 214 String name = null; 215 String value = null; 216 217 String protRelatedHostPropName = PROP_NAME_PREFIX + storeProtocol + 218 PROP_NAME_SUFFIX_HOST; 219 String protRelatedUserPropName = PROP_NAME_PREFIX + storeProtocol + 220 PROP_NAME_SUFFIX_USER; 221 String protRelatedHostName = MAIL_PREFIX + storeProtocol + 222 MAIL_SUFFIX_HOST; 223 String protRelatedUserName = MAIL_PREFIX + storeProtocol + 224 MAIL_SUFFIX_USER; 225 226 for (Iterator it = properties.iterator(); it.hasNext();) { 227 property = (ResourceProperty)it.next(); 228 name = property.getName(); 229 value = (String )property.getValue(); 230 231 if (name.startsWith(PROP_NAME_PREFIX)) { 232 if (name.equals(protRelatedHostPropName)) { 233 mailHost = value; 234 mailProperties.put(protRelatedHostName, value); 235 } 236 else if (name.equals(protRelatedUserPropName)) { 237 username = value; 238 mailProperties.put(protRelatedUserName, value); 239 } 240 else { 241 name = name.replace(PROP_NAME_DELIM, MAIL_DELIM); 242 mailProperties.put(name, value); 243 } 244 } 245 } 246 247 mailProperties.put(MAIL_HOST, mailHost); 249 mailProperties.put(MAIL_USER, username); 250 } 251 254 258 public String getUsername() { 259 return this.username; 260 } 261 262 266 public String getMailFrom() { 267 return this.mailFrom; 268 } 269 270 274 public String getMailHost() { 275 return this.mailHost; 276 } 277 278 284 public String getMailStoreProtocol() { 285 return this.storeProtocol; 286 } 287 288 293 public String getMailTransportProtocol() { 294 return this.transportProtocol; 295 } 296 297 302 public String getMailStoreProtocolClass() { 303 return this.storeProtocolClass; 304 } 305 306 311 public String getMailTransportProtocolClass() { 312 return this.transportProtocolClass; 313 } 314 315 319 public boolean getMailDebug() { 320 return this.debug; 321 } 322 323 327 public String getDescription() { 328 return this.description; 329 } 330 331 335 public String getJndiName() { 336 return this.jndiName; 337 } 338 339 343 public boolean getEnabled() { 344 return this.enabled; 345 } 346 348 352 public Properties getMailProperties() { 353 359 360 return mailProperties; 361 } 362 363 366 public void print(StringBuffer toStringBuffer) { 367 371 372 toStringBuffer.append("MailConfiguration: ["); 374 toStringBuffer.append("description=").append(description); 375 toStringBuffer.append( ", jndiName=").append(jndiName); 376 toStringBuffer.append( ", enabled=").append(enabled); 377 378 toStringBuffer.append( ", storeProtocol=").append(storeProtocol); 379 toStringBuffer.append( ", transportProtocol=").append(transportProtocol); 380 toStringBuffer.append( ", storeProtocolClass=").append(storeProtocolClass); 381 toStringBuffer.append( ", transportProtocolClass=").append(transportProtocolClass); 382 toStringBuffer.append( ", mailHost=").append(mailHost); 383 toStringBuffer.append( ", username=").append(username); 384 toStringBuffer.append( ", mailFrom=").append(mailFrom); 385 toStringBuffer.append( ", debug=").append(debug); 386 toStringBuffer.append( ", mailProperties: ["); 387 388 Enumeration e = mailProperties.propertyNames(); 389 String name; 390 String value; 391 boolean isFirst = true; 392 393 while (e.hasMoreElements()) { 394 name = (String )e.nextElement(); 395 value = mailProperties.getProperty(name); 396 if (isFirst) { 397 toStringBuffer.append(name).append("=").append(value); 398 isFirst = false; 399 } 400 else { 401 toStringBuffer.append( ", ").append(name).append("=").append(value); 402 } 403 } 404 toStringBuffer.append( "]]"); 405 406 } 408 } 409 410 | Popular Tags |