001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JavaMailSessionFactory.java 7668 2005-11-16 09:22:33Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.mail.factory;
025:
026: //import java
027: import java.util.Hashtable;
028: import java.util.Properties;
029:
030: import javax.mail.Session;
031: import javax.naming.Context;
032: import javax.naming.Name;
033: import javax.naming.RefAddr;
034: import javax.naming.Reference;
035: import javax.naming.spi.ObjectFactory;
036:
037: import org.objectweb.jonas.common.JNDIUtils;
038: import org.objectweb.jonas.common.Log;
039: import org.objectweb.jonas.common.PropDump;
040: import org.objectweb.jonas.mail.lib.JAuthenticator;
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043:
044: /**
045: * This class provides an implementation of a mail session factory for
046: * sending mail.
047: * @author Florent Benoit
048: * @author Ludovic Bert
049: */
050: public class JavaMailSessionFactory implements ObjectFactory {
051:
052: /**
053: * The Java type for which this factory knows how to create objects.
054: */
055: protected static final String FACTORY_TYPE = "javax.mail.Session";
056:
057: /**
058: * The logger used in JOnAS
059: */
060: private static Logger logger = null;
061:
062: /**
063: * Creates a javax.mail.Session object using the location or reference
064: * information specified.
065: * @param obj the possibly null object containing location or reference
066: * information that can be used in creating an object.
067: * @param name the name of this object relative to nameCtx, or null if no
068: * name is specified.
069: * @param nameCtx the context relative to which the name parameter is
070: * specified, or null if name is relative to the default initial context.
071: * @param environment the possibly null environment that is used in
072: * creating the object.
073: * @return a newly created javax.mail.Session object with the specific
074: * configuration; null if an object cannot be created.
075: * @throws Exception if this object factory encountered an exception
076: * while attempting to create an object, and no other object factories
077: * are to be tried.
078: */
079: public Object getObjectInstance(Object obj, Name name,
080: Context nameCtx, Hashtable environment) throws Exception {
081:
082: //Get the logger
083: if (logger == null) {
084: logger = Log.getLogger(Log.JONAS_MAIL_PREFIX);
085: }
086:
087: //Get the reference
088: Reference ref = (Reference) obj;
089:
090: //Get the class name
091: String clname = ref.getClassName();
092:
093: //Check the class name
094: if (!ref.getClassName().equals(FACTORY_TYPE)) {
095: logger.log(BasicLevel.ERROR,
096: "Cannot create object : required type is '"
097: + FACTORY_TYPE + "', but found type is '"
098: + clname + "'.");
099: return (null);
100: }
101:
102: Properties props = new Properties();
103: Properties authenticationProps = new Properties();
104: RefAddr refAddr = null;
105:
106: refAddr = ref.get("javaxmailSession.properties");
107: if (refAddr != null) {
108: props = (Properties) JNDIUtils
109: .getObjectFromBytes((byte[]) refAddr.getContent());
110: if (logger.isLoggable(BasicLevel.DEBUG)) {
111: PropDump
112: .print(
113: "These are the properties attached to the Reference object used to construct a Session",
114: props, logger, BasicLevel.DEBUG);
115: }
116: }
117:
118: refAddr = ref.get("authentication.properties");
119: if (refAddr != null) {
120: authenticationProps = (Properties) JNDIUtils
121: .getObjectFromBytes((byte[]) refAddr.getContent());
122: if (logger.isLoggable(BasicLevel.DEBUG)) {
123: PropDump
124: .print(
125: "These are the authentication properties used to construct a Authenticator",
126: authenticationProps, logger,
127: BasicLevel.DEBUG);
128: }
129: }
130:
131: //username (Authentication)
132: String mailAuthenticationUsername = authenticationProps
133: .getProperty("mail.authentication.username");
134:
135: //Password (Authentication)
136: String mailAuthenticationPassword = authenticationProps
137: .getProperty("mail.authentication.password");
138:
139: JAuthenticator jAuthenticator = null;
140: if ((mailAuthenticationUsername != null)
141: && (mailAuthenticationPassword != null)) {
142: jAuthenticator = new JAuthenticator(
143: mailAuthenticationUsername,
144: mailAuthenticationPassword);
145: }
146:
147: // Create and return a new Session object
148: Session session = Session.getInstance(props, jAuthenticator);
149:
150: return (session);
151: }
152:
153: }
|