KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > mail > MailServiceFactory


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util.mail;
25
26 import java.util.Properties JavaDoc;
27
28 import javax.mail.Session JavaDoc;
29
30 import org.infoglue.cms.util.CmsPropertyHandler;
31
32
33 /**
34  * Factory for creating MailService objects.
35  *
36  * @author Mattias Bogeblad
37  */

38
39 public class MailServiceFactory
40 {
41
42     // The singleton mail session; shared by all MailService objects.
43
private static Session JavaDoc session;
44
45
46     /**
47      * Returns a MailService object.
48      */

49     
50     public static synchronized MailService getService() throws Exception JavaDoc
51     {
52         if(session == null)
53         {
54             session = initializeSession();
55             //session.setDebug(true);
56
}
57     
58         return new MailService(session);
59     }
60
61
62     /**
63      * Static class; don't allow instantiation.
64      */

65     
66     private MailServiceFactory() {}
67
68
69     /**
70      * Initializes and constructs the shared mail session.
71      * Whenever a <code>mail.smtp.auth</code> property key has a <cdoe>true</code>
72      * value - which means that connection needs to be authenticated, keys
73      * <code>mail.smtp.user</code> and <code>mail.smtp.password</code> are
74      * used as principal information to be used to authenticate connection to
75      * specified SMTP server.
76      * @return SMTP session
77      */

78     private static Session JavaDoc initializeSession() throws Exception JavaDoc
79     {
80         Properties JavaDoc properties = CmsPropertyHandler.getProperties();
81         
82         Properties JavaDoc props = new Properties JavaDoc();
83
84         boolean needsAuthentication = false;
85         try
86         {
87             needsAuthentication = new Boolean JavaDoc((String JavaDoc)properties.get("mail.smtp.auth")).booleanValue();
88         }
89         catch (Exception JavaDoc ex)
90         {
91             needsAuthentication = false;
92         }
93         
94         if (needsAuthentication)
95         {
96             final String JavaDoc uName = (String JavaDoc)(String JavaDoc)properties.get("mail.smtp.user");
97             final String JavaDoc uPass = (String JavaDoc)(String JavaDoc)properties.get("mail.smtp.password");
98             
99             javax.mail.Authenticator JavaDoc authenticator = new javax.mail.Authenticator JavaDoc()
100             {
101                 protected javax.mail.PasswordAuthentication JavaDoc getPasswordAuthentication()
102                 {
103                     return new javax.mail.PasswordAuthentication JavaDoc(uName, uPass);
104                 }
105             };
106             
107             return Session.getInstance(properties, authenticator);
108         }
109         else
110         {
111             return Session.getInstance(properties);
112         }
113     }
114 }
Popular Tags