1 26 27 package org.objectweb.jonas.mail; 28 29 30 import java.io.FileNotFoundException ; 31 import java.util.Enumeration ; 32 import java.util.Hashtable ; 33 import java.util.List ; 34 import java.util.Properties ; 35 import java.util.StringTokenizer ; 36 import java.util.Vector ; 37 38 import javax.management.JMException ; 39 import javax.management.MBeanServer ; 40 import javax.management.ObjectName ; 41 import javax.management.modelmbean.ModelMBean ; 42 import javax.naming.Context ; 43 import javax.naming.InitialContext ; 44 import javax.naming.NamingException ; 45 46 import org.apache.commons.modeler.ManagedBean; 47 import org.apache.commons.modeler.Registry; 48 import org.objectweb.jonas.common.JModule; 49 import org.objectweb.jonas.common.JProp; 50 import org.objectweb.jonas.common.Log; 51 import org.objectweb.jonas.common.PropDump; 52 import org.objectweb.jonas.jmx.J2eeObjectName; 53 import org.objectweb.jonas.jmx.JmxService; 54 import org.objectweb.jonas.jmx.JonasObjectName; 55 import org.objectweb.jonas.mail.factory.JavaMail; 56 import org.objectweb.jonas.mail.factory.JavaMailMimePartDS; 57 import org.objectweb.jonas.mail.factory.JavaMailMimePartDSResource; 58 import org.objectweb.jonas.mail.factory.JavaMailSession; 59 import org.objectweb.jonas.mail.factory.JavaMailSessionResource; 60 import org.objectweb.jonas.management.JonasMBeanTools; 61 import org.objectweb.jonas.service.AbsServiceImpl; 62 import org.objectweb.jonas.service.ServiceException; 63 import org.objectweb.jonas.service.ServiceManager; 64 import org.objectweb.util.monolog.api.BasicLevel; 65 import org.objectweb.util.monolog.api.Logger; 66 75 public class MailServiceImpl extends AbsServiceImpl 76 implements MailService, MailServiceImplMBean { 77 78 81 private Vector mailSessionList = null; 82 83 86 private Vector mailMimePartDSList = null; 87 88 91 private MBeanServer mbeanServer = null; 92 93 96 private Context ictx = null; 97 98 protected String serverName = null; 101 protected String domainName = null; 102 103 106 private static Logger logger = null; 107 108 111 private Vector factoryNames = new Vector (); 112 113 116 private Hashtable jMailSessionFactories = new Hashtable (); 117 118 121 private Hashtable jMailMimePartDSFactories = new Hashtable (); 122 123 128 private Hashtable bindedFactories = new Hashtable (); 129 130 133 private static final int JAVAX_MAIL_SESSION_FACTORY = 1; 134 135 138 private static final int JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE = 2; 139 140 143 public static final String PROPERTY_NAME = "mail.factory.name"; 144 145 148 public static final String PROPERTY_TYPE = "mail.factory.type"; 149 150 public static final String SESSION_PROPERTY_TYPE = "javax.mail.Session"; 151 public static final String MIMEPART_PROPERTY_TYPE = "javax.mail.internet.MimePartDataSource"; 152 153 156 public static final String FACTORIES = "jonas.service.mail.factories"; 157 158 161 public static final String CLASS = "jonas.service.mail.class"; 162 163 168 protected void doInit(Context ctx) throws ServiceException { 169 170 logger = Log.getLogger(Log.JONAS_MAIL_PREFIX); 172 super.initLogger(Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX)); 174 175 try { 177 ictx = new InitialContext (); 178 } catch (NamingException e) { 179 logger.log(BasicLevel.ERROR, "Cannot create initial context during the mail service initializing"); 180 throw new ServiceException("Cannot create initial context during the mail service initializing", e); 181 } 182 183 try { 185 serverName = (String ) ctx.lookup(JProp.JONAS_NAME); 186 domainName = (String ) ctx.lookup(JProp.DOMAIN_NAME); 187 } catch (NamingException ne) { 188 logger.log(BasicLevel.DEBUG, "Cannot initialize the Mail service " + ne); 189 throw new ServiceException("Cannot initialize the Mail service", ne); 190 } 191 192 try { 194 mbeanServer = 195 ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer(); 196 } catch (Exception e) { 197 mbeanServer = null; 199 } 200 201 String factories = null; 203 try { 204 factories = (String ) ctx.lookup(FACTORIES); 205 } catch (NamingException e) { 206 ; } 208 if (factories != null) { 209 StringTokenizer st = new StringTokenizer (factories, ","); 210 while (st.hasMoreTokens()) { 211 factoryNames.add(st.nextToken().trim()); 212 } 213 } 214 if (logger.isLoggable(BasicLevel.DEBUG)) { 215 logger.log(BasicLevel.DEBUG, "Mail service initialized"); 216 } 217 218 } 219 220 224 protected void doStart() throws ServiceException { 225 226 String factoryName = null; 228 for (int i = 0; i < factoryNames.size(); i++) { 229 factoryName = (String ) factoryNames.elementAt(i); 230 try { 231 JProp prop = JProp.getInstance(factoryName); 232 if (logger.isLoggable(BasicLevel.DEBUG)) { 233 logger.log(BasicLevel.DEBUG, "Creating mail factory " + factoryName); 234 } 235 createMailFactory(factoryName, prop.getConfigFileEnv()); 236 } catch (Exception e) { 237 if (logger.isLoggable(BasicLevel.ERROR)) { 238 logger.log(BasicLevel.ERROR, "JOnAS: Cannot create mail factory " + factoryName + " : " + e); 239 logger.log(BasicLevel.ERROR, "Please check the " + factoryName + ".properties file"); 240 } 241 } 242 } 243 244 try { 245 if (mbeanServer != null) { 247 mbeanServer.registerMBean(this, JonasObjectName.mailService()); 248 } 249 } catch (JMException e) { 250 throw new ServiceException("Cannot start the MailService: " , e); 251 } 252 253 } 254 255 259 protected void doStop() throws ServiceException { 260 261 try { 262 unbindMailFactories(); 263 } catch (MailServiceException e) { 264 logger.log(BasicLevel.ERROR, "Cannot unbind mail factories " + e); 265 throw new ServiceException("Cannot unbind mail factories ", e); 266 } 267 268 if (mbeanServer != null) { 269 try { 270 mbeanServer.unregisterMBean(JonasObjectName.mailService()); 271 } catch (Exception e) { 273 logger.log(BasicLevel.ERROR, "Cannot stop the Mail Service (JMX): " + e.getMessage()); 274 throw new ServiceException("Cannot stop the mail service (JMX).", e); 275 } 276 } 277 if (logger.isLoggable(BasicLevel.DEBUG)) { 278 logger.log(BasicLevel.DEBUG, "mail service stopped"); 279 } 280 } 281 282 290 public void recreateJavaMailFactory(JavaMail factory) throws MailServiceException { 291 String jndiName = factory.getName(); 292 try { 294 ictx.rebind(jndiName, factory); 295 } catch (NamingException e) { 296 logger.log(BasicLevel.ERROR, "Cannot bind mail factory '" + jndiName + "' :" + e.getMessage()); 297 throw new MailServiceException("Cannot bind mail factory " + jndiName + ".", e); 298 } 299 } 300 301 312 public void renameJavaMailFactory(String oldName, JavaMail factory) throws MailServiceException { 313 314 if (logger.isLoggable(BasicLevel.DEBUG)) { 315 logger.log(BasicLevel.DEBUG, "In renameMailFactory, old name = " + oldName); 316 } 317 try { 318 ictx.unbind(oldName); 319 if (logger.isLoggable(BasicLevel.DEBUG)) { 320 logger.log(BasicLevel.DEBUG, oldName + " unbound"); 321 } 322 } catch (Exception e) { 323 if (logger.isLoggable(BasicLevel.DEBUG)) { 324 logger.log(BasicLevel.DEBUG, "Warning: cannot unbind mail factory object named " + oldName); 325 } 326 } 327 String jndiName = factory.getName(); 328 try { 330 ictx.rebind(jndiName, factory); 331 if (logger.isLoggable(BasicLevel.DEBUG)) { 332 logger.log(BasicLevel.DEBUG, "factory rebound under the name " + jndiName); 333 } 334 } catch (NamingException e) { 335 logger.log(BasicLevel.ERROR, "Cannot bind mail factory '" + jndiName + "' :" + e.getMessage()); 336 throw new MailServiceException("Cannot bind mail factory " + jndiName + ".", e); 337 } 338 bindedFactories.put(jndiName, factory.getFactoryName()); 339 bindedFactories.remove(oldName); 340 } 341 342 350 public void createMailFactory(String factoryName, Properties props) 351 throws MailServiceException { 352 353 if (logger.isLoggable(BasicLevel.DEBUG)) { 354 PropDump.print("These are the properties from which the MailService picks to construct Mail Factories", props, logger, BasicLevel.DEBUG); 355 } 356 357 Object factory = null; 358 359 String factoryType = props.getProperty(PROPERTY_TYPE); 361 String jndiName = props.getProperty(PROPERTY_NAME); 362 363 if (jndiName == null) { 364 logger.log(BasicLevel.ERROR, "The property 'mail.factory.name' is a required property."); 365 throw new MailServiceException("The property 'mail.factory.name' is a required property for this factory."); 366 } 367 368 if (factoryType == null) { 369 logger.log(BasicLevel.ERROR, "The property 'mail.factory.type' is a required property."); 370 throw new MailServiceException("The property 'mail.factory.type' is a required property for this factory."); 371 } 372 373 if (bindedFactories.containsKey(jndiName)) { 375 logger.log(BasicLevel.ERROR, "There is already a factory bound with the name " + jndiName); 376 throw new MailServiceException("There is already a factory bound with the name '" + jndiName + "', please correct the provided configuration properties"); 377 } 378 379 int typeOfFactory; 381 if (factoryType.equalsIgnoreCase("javax.mail.Session")) { 382 typeOfFactory = JAVAX_MAIL_SESSION_FACTORY; 383 } else if (factoryType.equalsIgnoreCase("javax.mail.internet.MimePartDataSource")) { 384 typeOfFactory = JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE; 385 } else { 386 typeOfFactory = 0; 387 } 388 389 switch (typeOfFactory) { 391 392 case JAVAX_MAIL_SESSION_FACTORY : 393 JavaMailSession sessionFactory = new JavaMailSession(factoryName, jndiName, props); 394 jMailSessionFactories.put(factoryName, sessionFactory); 395 factory = sessionFactory; 396 break; 397 398 case JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE : 399 JavaMailMimePartDS mimeFactory = new JavaMailMimePartDS(factoryName, jndiName, props); 400 jMailMimePartDSFactories.put(factoryName, mimeFactory); 401 factory = mimeFactory; 402 break; 403 404 default : 405 throw new MailServiceException("Can not create a factory of the type '" + factoryType + "'. This type is incorrect."); 406 } 407 408 try { 410 ictx.rebind(jndiName, factory); 411 bindedFactories.put(jndiName, factoryName); 412 } catch (NamingException e) { 413 logger.log(BasicLevel.ERROR, "Cannot bind mail factory '" + jndiName + "' :" + e.getMessage()); 414 throw new MailServiceException("Cannot bind mail factory " + jndiName + ".", e); 415 } 416 417 logger.log(BasicLevel.INFO, "Mapping Mail Factory " + factoryType + " on " + jndiName); 418 419 Registry oRegistry = JonasMBeanTools.getRegistry(); 422 423 try { 425 if (mbeanServer != null) { 426 ObjectName on = null; 427 switch (typeOfFactory) { 428 case JAVAX_MAIL_SESSION_FACTORY : 429 431 on = J2eeObjectName.JavaMailResource(domainName, factoryName, serverName, SESSION_PROPERTY_TYPE); 432 JavaMailSessionResource javaMailSessionResource = new JavaMailSessionResource(on.toString(), false, false, false, (JavaMailSession) factory); 433 ManagedBean oManaged = oRegistry.findManagedBean("JavaMailSessionResource"); 435 ModelMBean oMBean = oManaged.createMBean(javaMailSessionResource); 436 mbeanServer.registerMBean(oMBean, on); 437 if (logger.isLoggable(BasicLevel.DEBUG)) { 439 logger.log(BasicLevel.DEBUG, "Register session mail factory with name " + factoryName); 440 } 441 break; 442 case JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE : 443 on = J2eeObjectName.JavaMailResource(domainName, factoryName, serverName, MIMEPART_PROPERTY_TYPE); 445 JavaMailMimePartDSResource javaMailMimePartDSResource = new JavaMailMimePartDSResource(on.toString(), false, false, false, (JavaMailMimePartDS) factory); 446 oManaged = oRegistry.findManagedBean("JavaMailMimePartDSResource"); 448 oMBean = oManaged.createMBean(javaMailMimePartDSResource); 449 mbeanServer.registerMBean(oMBean, on); 450 if (logger.isLoggable(BasicLevel.DEBUG)) { 451 logger.log(BasicLevel.DEBUG, "Register mime mail factory with name " + factoryName); 452 } 453 } 454 } 455 } catch (JMException me) { 456 throw new MailServiceException("Cannot register mail factory '" + factoryName + "' in JMX server", me); 457 } catch (Exception e) { 458 logger.log(BasicLevel.WARN, "Could not register JavaMailResource MBean"); 459 } 460 461 462 } 463 464 474 public void createMailFactoryMBean(String name, Properties props, Boolean loadFromFile) 475 throws MailServiceException { 476 477 boolean fromFile = loadFromFile.booleanValue(); 478 if (!fromFile) { 479 try { 480 if (logger.isLoggable(BasicLevel.DEBUG)) { 481 logger.log(BasicLevel.DEBUG, "Call getInstance on JProp in order to create the properties file"); 482 } 483 JProp.getInstance(name, props); 484 } catch (Exception e) { 485 logger.log(BasicLevel.ERROR, "Cannot create mail factory " + name + " as cannot create properties file : " + e.toString()); 486 throw new ServiceException("MailService: Cannot create mail factory " + name + ",\n" + e.toString()); 487 } 488 } 489 try { 491 createMailFactory(name, props); 492 } catch (Exception e) { 493 logger.log(BasicLevel.ERROR, "Cannot create mail factory: " + name); 494 throw new ServiceException("MailService: Cannot create mail factory: " + name + ",\n" + e.toString()); 495 } 496 } 497 498 503 public void unbindMailFactories() throws MailServiceException { 504 for (Enumeration e = jMailSessionFactories.keys(); e.hasMoreElements();) { 506 unbindMailFactoryMBean((String ) e.nextElement()); 507 } 508 for (Enumeration e = jMailMimePartDSFactories.keys(); e.hasMoreElements();) { 509 unbindMailFactoryMBean((String ) e.nextElement()); 510 } 511 } 512 513 519 public void unbindMailFactoryMBean(String factoryName) 520 throws MailServiceException { 521 522 String name = null; 524 int typeOfFactory; 525 JavaMailSession jmailSession = (JavaMailSession) jMailSessionFactories.get(factoryName); 526 JavaMailMimePartDS jMailMimePartDS = (JavaMailMimePartDS) jMailMimePartDSFactories.get(factoryName); 527 if (jmailSession != null) { 528 name = jmailSession.getName(); 529 typeOfFactory = JAVAX_MAIL_SESSION_FACTORY; 530 } else if (jMailMimePartDS != null) { 531 name = jMailMimePartDS.getName(); 532 typeOfFactory = JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE; 533 } else { 534 throw new MailServiceException("Can not unload the mail factory '" + factoryName + "' (this is not a known factory name"); 535 } 536 537 try { 539 ictx.unbind(name); 540 bindedFactories.remove(name); 541 } catch (NamingException e) { 542 throw new MailServiceException("Can not unbind the factory '" + name + "'.", e); 543 } 544 545 try { 547 if (mbeanServer != null) { 548 ObjectName on = null; 549 switch (typeOfFactory) { 550 case JAVAX_MAIL_SESSION_FACTORY : 551 on = J2eeObjectName.JavaMailResource(domainName, factoryName, serverName, SESSION_PROPERTY_TYPE); 553 mbeanServer.unregisterMBean(on); 554 if (logger.isLoggable(BasicLevel.DEBUG)) { 555 logger.log(BasicLevel.DEBUG, "Unregister session mail factory with name " + factoryName); 556 } 557 jMailSessionFactories.remove(factoryName); 558 break; 559 case JAVAX_MAIL_INTERNET_MIMEPARTDATASOURCE : 560 on = J2eeObjectName.JavaMailResource(domainName, factoryName, serverName, MIMEPART_PROPERTY_TYPE); 562 mbeanServer.unregisterMBean(on); 563 if (logger.isLoggable(BasicLevel.DEBUG)) { 564 logger.log(BasicLevel.DEBUG, "Unregister mime mail factory with name " + factoryName); 565 } 566 jMailMimePartDSFactories.remove(factoryName); 567 } 568 } 569 } catch (JMException me) { 570 throw new MailServiceException("Cannot unregister mail factory '" + factoryName + "' from the JMX server", me); 571 } 572 573 574 } 575 576 581 public String getFactoryName(String jndiName) { 582 return (String ) bindedFactories.get(jndiName); 583 } 584 585 589 public Integer getCurrentNumberOfMailFactories() { 590 return new Integer (jMailSessionFactories.size() + jMailMimePartDSFactories.size()); 591 } 592 593 597 public Integer getCurrentNumberOfSessionMailFactories() { 598 return new Integer (jMailSessionFactories.size()); 599 } 600 601 605 public Integer getCurrentNumberOfMimeMailFactories() { 606 return new Integer (jMailMimePartDSFactories.size()); 607 } 608 609 615 public Properties getMailFactoryPropertiesFile(String configFile) throws Exception { 616 try { 617 return JProp.getInstance(configFile).getConfigFileEnv(); 618 } catch (Exception e) { 619 if (e instanceof FileNotFoundException ) { 620 logger.log(BasicLevel.ERROR, "Please check if " + configFile + ".properties is available in JONAS_ROOT/config, HOME, or ."); 621 } else { 622 logger.log(BasicLevel.ERROR, "Error occured when reading file " + configFile); 623 } 624 throw e; 625 } 626 } 627 628 632 public List getMailFactoryPropertiesFiles() throws Exception { 633 return JModule.getMailFactoryPropsInDir(); 634 } 635 639 public List getMimePartMailFactoryPropertiesFiles() throws Exception { 640 return JModule.getMailFactoryPropsInDir(MIMEPART_PROPERTY_TYPE); 641 } 642 646 public List getSessionMailFactoryPropertiesFiles() throws Exception { 647 return JModule.getMailFactoryPropsInDir(SESSION_PROPERTY_TYPE); 648 } 649 } 650 | Popular Tags |