KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > mail > factory > JavaMailSessionFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JavaMailSessionFactory.java,v 1.4 2005/04/28 08:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.mail.factory;
27
28 //import java
29
import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.OptionalDataException JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 //import javax
37
import javax.mail.Session JavaDoc;
38 import javax.naming.Context JavaDoc;
39 import javax.naming.Name JavaDoc;
40 import javax.naming.RefAddr JavaDoc;
41 import javax.naming.Reference JavaDoc;
42 import javax.naming.spi.ObjectFactory JavaDoc;
43
44 //import objectweb.util
45
import org.objectweb.util.monolog.api.BasicLevel;
46 import org.objectweb.util.monolog.api.Logger;
47
48 //import jonas
49
import org.objectweb.jonas.common.Log;
50 import org.objectweb.jonas.common.PropDump;
51 import org.objectweb.jonas.common.JNDIUtils;
52 import org.objectweb.jonas.mail.lib.JAuthenticator;
53
54 /**
55  * This class provides an implementation of a mail session factory for
56  * sending mail.
57  * @author Florent Benoit
58  * @author Ludovic Bert
59  */

60 public class JavaMailSessionFactory implements ObjectFactory JavaDoc {
61
62     /**
63      * The Java type for which this factory knows how to create objects.
64      */

65     protected static final String JavaDoc FACTORY_TYPE = "javax.mail.Session";
66
67     /**
68      * The logger used in JOnAS
69      */

70     private static Logger logger = null;
71
72
73     /**
74      * Creates a javax.mail.Session object using the location or reference
75      * information specified.
76      * @param obj the possibly null object containing location or reference
77      * information that can be used in creating an object.
78      * @param name the name of this object relative to nameCtx, or null if no
79      * name is specified.
80      * @param nameCtx the context relative to which the name parameter is
81      * specified, or null if name is relative to the default initial context.
82      * @param environment the possibly null environment that is used in
83      * creating the object.
84      * @return a newly created javax.mail.Session object with the specific
85      * configuration; null if an object cannot be created.
86      * @throws Exception if this object factory encountered an exception
87      * while attempting to create an object, and no other object factories
88      * are to be tried.
89      */

90     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
91                                     Hashtable JavaDoc environment) throws Exception JavaDoc {
92
93         //Get the logger
94
if (logger == null) {
95             logger = Log.getLogger(Log.JONAS_MAIL_PREFIX);
96         }
97
98         //Get the reference
99
Reference JavaDoc ref = (Reference JavaDoc) obj;
100
101         //Get the class name
102
String JavaDoc clname = ref.getClassName();
103
104         //Check the class name
105
if (!ref.getClassName().equals(FACTORY_TYPE)) {
106             logger.log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'.");
107             return (null);
108         }
109
110         Properties JavaDoc props = new Properties JavaDoc();
111         Properties JavaDoc authenticationProps = new Properties JavaDoc();
112         RefAddr JavaDoc refAddr = null;
113
114         refAddr = ref.get("javaxmailSession.properties");
115         if (refAddr != null) {
116             props = (Properties JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
117             if (logger.isLoggable(BasicLevel.DEBUG)) {
118                 PropDump.print("These are the properties attached to the Reference object used to construct a Session",
119                            props, logger, BasicLevel.DEBUG);
120             }
121         }
122
123         refAddr = ref.get("authentication.properties");
124         if (refAddr != null) {
125             authenticationProps = (Properties JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
126             if (logger.isLoggable(BasicLevel.DEBUG)) {
127                 PropDump.print("These are the authentication properties used to construct a Authenticator",
128                            authenticationProps, logger, BasicLevel.DEBUG);
129             }
130         }
131
132         //username (Authentication)
133
String JavaDoc mailAuthenticationUsername = authenticationProps.getProperty("mail.authentication.username");
134
135         //Password (Authentication)
136
String JavaDoc mailAuthenticationPassword = authenticationProps.getProperty("mail.authentication.password");
137
138         JAuthenticator jAuthenticator = null;
139         if ((mailAuthenticationUsername != null) && (mailAuthenticationPassword != null)) {
140             jAuthenticator = new JAuthenticator(mailAuthenticationUsername, mailAuthenticationPassword);
141         }
142
143         // Create and return a new Session object
144
Session JavaDoc session = Session.getInstance(props, jAuthenticator);
145
146         return (session);
147     }
148
149 }
150
Popular Tags