KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mail > MailService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mail;
23
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27
28 import java.util.Properties JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MalformedObjectNameException JavaDoc;
33
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.Reference JavaDoc;
36 import javax.naming.StringRefAddr JavaDoc;
37 import javax.naming.Name JavaDoc;
38 import javax.naming.Context JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import javax.naming.NameNotFoundException JavaDoc;
41
42 import javax.mail.Session JavaDoc;
43 import javax.mail.PasswordAuthentication JavaDoc;
44 import javax.mail.Authenticator JavaDoc;
45
46 import org.jboss.system.ServiceMBeanSupport;
47 import org.jboss.naming.NonSerializableFactory;
48
49 /**
50  * MBean that gives support for JavaMail. Object of class javax.mail.Session will be bound
51  * in JNDI with the name provided with method {@link #setJNDIName}.
52  *
53  * @jmx:mbean name="jboss:type=Service,service=Mail"
54  * extends="org.jboss.system.ServiceMBean"
55  *
56  * @version <tt>$Revision: 37459 $</tt>
57  * @author <a HREF="mailto:simone.bordet@compaq.com">Simone Bordet</a>
58  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
59  * @author Scott.Stark@jboss.org
60  */

61 public class MailService
62    extends ServiceMBeanSupport
63    implements MailServiceMBean
64 {
65    public static final String JavaDoc JNDI_NAME = "java:/Mail";
66
67    private String JavaDoc user;
68    private String JavaDoc password;
69    private String JavaDoc jndiName = JNDI_NAME;
70    private Element JavaDoc config;
71
72    /** Object Name of the JSR-77 representant of this service */
73    ObjectName JavaDoc mMail;
74
75    /** save properties here */
76    Properties JavaDoc ourProps = null;
77
78    /**
79     * User id used to connect to a mail server
80     *
81     * @see #setPassword
82     *
83     * @jmx:managed-attribute
84     */

85    public void setUser(final String JavaDoc user)
86    {
87       this.user = user;
88    }
89
90    /**
91     * @jmx:managed-attribute
92     */

93    public String JavaDoc getUser()
94    {
95       return user;
96    }
97
98    /**
99     * Password used to connect to a mail server
100     *
101     * @see #setUser
102     *
103     * @jmx:managed-attribute
104     */

105    public void setPassword(final String JavaDoc password)
106    {
107       this.password = password;
108    }
109
110    /**
111     * Password is write only.
112     */

113    protected String JavaDoc getPassword()
114    {
115       return password;
116    }
117
118    /**
119     * Configuration for the mail service.
120     *
121     * @jmx:managed-attribute
122     */

123    public Element JavaDoc getConfiguration()
124    {
125       return config;
126    }
127
128    /**
129     * Configuration for the mail service.
130     *
131     * @jmx:managed-attribute
132     */

133    public void setConfiguration(final Element JavaDoc element)
134    {
135       config = element;
136    }
137
138    /** The JNDI name under which javax.mail.Session objects are bound.
139     *
140     * @jmx:managed-attribute
141     */

142    public void setJNDIName(final String JavaDoc name)
143    {
144       jndiName = name;
145    }
146
147    /**
148     * @jmx:managed-attribute
149     */

150    public String JavaDoc getJNDIName()
151    {
152       return jndiName;
153    }
154
155    /**
156     * @jmx:managed-attribute
157     */

158    public String JavaDoc getStoreProtocol()
159    {
160       if (ourProps != null)
161          return ourProps.getProperty("mail.store.protocol");
162       else
163          return null;
164    }
165
166    /**
167     * @jmx:managed-attribute
168     */

169    public String JavaDoc getTransportProtocol()
170    {
171       if (ourProps != null)
172          return ourProps.getProperty("mail.transport.protocol");
173       else
174          return null;
175    }
176
177    /**
178     * @jmx:managed-attribute
179     */

180    public String JavaDoc getDefaultSender()
181    {
182       if (ourProps != null)
183          return ourProps.getProperty("mail.from");
184       else
185          return null;
186    }
187
188
189    /**
190     * @jmx:managed-attribute
191     */

192    public String JavaDoc getSMTPServerHost()
193    {
194       if (ourProps != null)
195          return ourProps.getProperty("mail.smtp.host");
196       else
197          return null;
198    }
199
200    /**
201     * @jmx:managed-attribute
202     */

203    public String JavaDoc getPOP3ServerHost()
204    {
205       if (ourProps != null)
206          return ourProps.getProperty("mail.pop3.host");
207       else
208          return null;
209    }
210
211    protected ObjectName JavaDoc getObjectName(MBeanServer JavaDoc server, ObjectName JavaDoc name)
212       throws MalformedObjectNameException JavaDoc
213    {
214       return name == null ? OBJECT_NAME : name;
215    }
216
217    protected void startService() throws Exception JavaDoc
218    {
219       // Setup password authentication
220
final PasswordAuthentication JavaDoc pa = new PasswordAuthentication JavaDoc(getUser(), getPassword());
221       Authenticator JavaDoc a = new Authenticator JavaDoc()
222       {
223          protected PasswordAuthentication JavaDoc getPasswordAuthentication()
224          {
225             return pa;
226          }
227       };
228
229       Properties JavaDoc props = getProperties();
230
231       // Finally create a mail session
232
Session JavaDoc session = Session.getInstance(props, a);
233       bind(session);
234       
235       // now make the properties available
236
ourProps = props;
237    }
238
239    protected Properties JavaDoc getProperties() throws Exception JavaDoc
240    {
241       Properties JavaDoc props = new Properties JavaDoc();
242       if (config == null)
243       {
244          log.warn("No configuration specified; using empty properties map");
245          return props;
246       }
247
248       NodeList JavaDoc list = config.getElementsByTagName("property");
249       int len = list.getLength();
250
251       for (int i = 0; i < len; i++)
252       {
253          Node JavaDoc node = list.item(i);
254
255          switch (node.getNodeType())
256          {
257             case Node.ELEMENT_NODE:
258                Element JavaDoc child = (Element JavaDoc) node;
259                String JavaDoc name, value;
260
261                // get the name
262
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                // get the value
273
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                // ignore
292
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 JavaDoc
306    {
307       unbind();
308    }
309
310    private void bind(Session JavaDoc session) throws NamingException JavaDoc
311    {
312       String JavaDoc bindName = getJNDIName();
313
314       // Ah ! Session isn't serializable, so we use a helper class
315
NonSerializableFactory.bind(bindName, session);
316
317       Context JavaDoc ctx = new InitialContext JavaDoc();
318       try
319       {
320          Name JavaDoc n = ctx.getNameParser("").parse(bindName);
321          while (n.size() > 1)
322          {
323             String JavaDoc ctxName = n.get(0);
324             try
325             {
326                ctx = (Context JavaDoc) ctx.lookup(ctxName);
327             }
328             catch (NameNotFoundException JavaDoc e)
329             {
330                ctx = ctx.createSubcontext(ctxName);
331             }
332             n = n.getSuffix(1);
333          }
334
335
336          // The helper class NonSerializableFactory uses address type nns, we go on to
337
// use the helper class to bind the javax.mail.Session object in JNDI
338

339          StringRefAddr JavaDoc addr = new StringRefAddr JavaDoc("nns", bindName);
340          Reference JavaDoc ref = new Reference JavaDoc(Session JavaDoc.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 JavaDoc
355    {
356       String JavaDoc bindName = getJNDIName();
357
358       if (bindName != null)
359       {
360          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
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