User: user57  
  Date: 02/02/15 00:11:02

  Added:       varia/src/main/org/jboss/mail MailService.java
                        MailServiceMBean.java package.html
  Log:
   o Moved MailService to varia, made is use the xml snippent stuff
     to get its properties (this is *ucking cool) and made it deployable
     with mail-service.xml
  
  Revision  Changes    Path
  1.1                  contrib/varia/src/main/org/jboss/mail/MailService.java
  
  Index: MailService.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.mail;
  
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  import java.util.Properties;
  
  import javax.management.ObjectName;
  import javax.management.MBeanServer;
  import javax.management.MalformedObjectNameException;
  
  import javax.naming.InitialContext;
  import javax.naming.Reference;
  import javax.naming.StringRefAddr;
  import javax.naming.Name;
  import javax.naming.Context;
  import javax.naming.NamingException;
  import javax.naming.NameNotFoundException;
  
  import javax.mail.Session;
  import javax.mail.PasswordAuthentication;
  import javax.mail.Authenticator;
  
  import org.jboss.system.ServiceMBeanSupport;
  import org.jboss.naming.NonSerializableFactory;
  
  /**
   * MBean that gives support for JavaMail. Object of class javax.mail.Session will be 
bound
   * in JNDI under java:/ namespace with the name provided with method {@link 
#setJNDIName}.
   *
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Simone Bordet</a>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public class MailService
     extends ServiceMBeanSupport
     implements MailServiceMBean
  {       
     private String user;
     private String password;
     private String jndiName = JNDI_NAME;
     private Element config;
     
     public void setUser(final String user) {
        this.user = user;
     }
     
     protected String getUser() {
        return user;
     }
     
     public void setPassword(final String password) {
        this.password = password;
     }
     
     protected String getPassword() {
        return password;
     }
  
     public void setConfiguration(final Element element) {
        config = element;
     }
     
     public void setJNDIName(final String name) {
        if (!name.startsWith("java:/")) {
           jndiName = "java:/" + name;
        }
        else {
           jndiName = name;
        }
     }
     
     protected String getJNDIName() {
        return jndiName;
     }
  
     protected ObjectName getObjectName(MBeanServer server, ObjectName name)
        throws MalformedObjectNameException
     {
        return name == null ? OBJECT_NAME : name;
     }
     
     protected void startService() throws Exception
     {
        // Setup password authentication
        final PasswordAuthentication pa = new PasswordAuthentication(getUser(), 
getPassword());
        Authenticator a = new Authenticator()
           {
              protected PasswordAuthentication getPasswordAuthentication()
              {
                 return pa;
              }
           };
  
        Properties props = getProperties();
        
        // Finally create a mail session
        Session session = Session.getInstance(props, a);
        bind(session);
     }
  
     protected Properties getProperties() throws Exception {
        boolean debug = log.isDebugEnabled();
  
        Properties props = new Properties();
        if (config == null) {
           log.warn("No configuration specified; using empty properties map");
           return props;
        }
  
        NodeList list = config.getElementsByTagName("property");
        int len = list.getLength();
        
        for (int i=0; i< len; i++) {
           Node node = list.item(i);
  
           switch (node.getNodeType()) {
            case Node.ELEMENT_NODE:
               Element child = (Element)node;
               String name, value;
  
               // get the name
               if (child.hasAttribute("name")) {
                  name = child.getAttribute("name");
               }
               else {
                  log.warn("Ignoring invalid element; missing 'name' attribute: " + 
child);
                  break;
               }
  
               // get the value
               if (child.hasAttribute("value")) {
                  value = child.getAttribute("value");
               }
               else {
                  log.warn("Ignoring invalid element; missing 'value' attribute: " + 
child);
                  break;
               }
  
               if (debug) {
                  log.debug("setting property " + name + "=" + value);
               }
               props.setProperty(name, value);
               break;
  
            case Node.COMMENT_NODE:
               // ignore
               break;
               
            default:
               log.debug("ignoring unsupported node type: " + node);
               break;
           }
        }
  
        if (debug) {
           log.debug("Using properties: " + props);
        }
        
        return props;
     }
     
     protected void stopService() throws Exception
     {
        unbind();
     }
  
  
     private void bind(Session session) throws NamingException
     {
        String bindName = getJNDIName();
  
        // Ah ! Session isn't serializable, so we use a helper class
        NonSerializableFactory.bind(bindName, session);
  
        Context ctx = new InitialContext();
        try {
           Name n = ctx.getNameParser("").parse(bindName);
           while (n.size() > 1)
           {
              String ctxName = n.get(0);
              try
              {
                 ctx = (Context)ctx.lookup(ctxName);
              }
              catch (NameNotFoundException e)
              {
                 ctx = ctx.createSubcontext(ctxName);
              }
              n = n.getSuffix(1);
           }
        
  
           // The helper class NonSerializableFactory uses address type nns, we go on 
to
           // use the helper class to bind the javax.mail.Session object in JNDI
        
           StringRefAddr addr = new StringRefAddr("nns", bindName);
           Reference ref = new Reference(Session.class.getName(),
                                         addr,
                                         NonSerializableFactory.class.getName(),
                                         null);
           ctx.bind(n.get(0), ref);
        }
        finally {
           ctx.close();
        }
        
        log.info("Mail Service bound to " + bindName);
     }
  
     private void unbind() throws NamingException
     {
        String bindName = getJNDIName();
        
        if (bindName != null)
        {
           InitialContext ctx = new InitialContext();
           try {
              ctx.unbind(bindName);
           }
           finally {
              ctx.close();
           }
           
           NonSerializableFactory.unbind(bindName);
           log.info("Mail service '" + getJNDIName() + "' removed from JNDI");
        }
     }
  }
  
  
  
  1.1                  contrib/varia/src/main/org/jboss/mail/MailServiceMBean.java
  
  Index: MailServiceMBean.java
  ===================================================================
  /***************************************
   *                                     *
   *  JBoss: The OpenSource J2EE WebOS   *
   *                                     *
   *  Distributable under LGPL license.  *
   *  See terms of license at gnu.org.   *
   *                                     *
   ***************************************/
  
  package org.jboss.mail;
  
  import org.w3c.dom.Element;
  
  import javax.management.ObjectName;
  
  import org.jboss.util.jmx.ObjectNameFactory;
  
  /**
   * MBean interface for the mail service.
   * 
   * @version <tt>$Revision: 1.1 $</tt>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Simone Bordet</a>
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
   */
  public interface MailServiceMBean
     extends org.jboss.system.ServiceMBean
  {
     /** The default object name to use. */
     ObjectName OBJECT_NAME = ObjectNameFactory.create(":service=Mail");
  
     /** The default JNDI name to bind to. */
     String JNDI_NAME = "java:/Mail";
     
     /**
      * User id used to connect to a mail server
      * 
      * @see #setPassword
      */
     void setUser(String user);
     
     /**
      * Password used to connect to a mail server
      * 
      * @see #setUser
      */
     void setPassword(String password);
     
     /**
      * Configuration for the mail service.
      */
     void setConfiguration(Element element);
     
     /**
      * The JNDI name under the java:/ namespace to which javax.mail.Session objects 
are
      * bound.
      */
     void setJNDIName(String name);
  }
  
  
  
  1.1                  contrib/varia/src/main/org/jboss/mail/package.html
  
  Index: package.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
    <head>
      <!-- $Id: package.html,v 1.1 2002/02/15 08:11:02 user57 Exp $ -->
      <!--
  
      JBoss: The OpenSource J2EE WebOS 
  
      Distributable under LGPL license.
      See terms of license at gnu.org.
  
      -->
    </head>
  
    <body bgcolor="white">
      <p>E-Mail service components.
  
      <h2>Package Specification</h2>
      <ul>
        <li><a href="javascript: alert('not available')">Not Available</a>
      </ul>
        
      <h2>Related Documentation</h2>
      <ul>
        <li><a href="javascript: alert('not available')">Not Available</a>
      </ul>
  
      <h2>Package Status</h2>
      <ul>
        <li><font color="green"><b>STABLE</b></font>
      </ul>
  
      <h2>Todo</h2>
      <ul>
        <li>???
      </ul>
  
      <!-- Put @see and @since tags down here. -->
  
    </body>
  </html>
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to