001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.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: AbsJavaMailFactory.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.mail.factory;
025:
026: import java.io.IOException;
027: import java.util.Properties;
028:
029: import javax.mail.Authenticator;
030: import javax.naming.NamingException;
031: import javax.naming.RefAddr;
032: import javax.naming.Reference;
033: import javax.naming.StringRefAddr;
034: import javax.naming.spi.ObjectFactory;
035:
036: import org.ow2.easybeans.util.marshalling.Serialization;
037:
038: /**
039: * Abstract JNDI factory for Mail factories.
040: * @author Florent BENOIT
041: */
042: public abstract class AbsJavaMailFactory implements ObjectFactory {
043:
044: /**
045: * User name for authentication.
046: */
047: public static final String AUTHENTICATION_USERNAME = "auth.name";
048:
049: /**
050: * Password for authentication.
051: */
052: public static final String AUTHENTICATION_PASSWORD = "auth.pass";
053:
054: /**
055: * Properties used for mail factory.
056: */
057: public static final String MAIL_PROPERTIES = "mail.properties";
058:
059: /**
060: * Gets an authenticator for the given reference.
061: * @param reference the given reference
062: * @return an authenticator or null if there is none.
063: */
064: protected Authenticator getAuthenticator(final Reference reference) {
065: // Get auth name
066: String authName = getString(reference, AUTHENTICATION_USERNAME);
067:
068: // Get auth password
069: String authPass = getString(reference, AUTHENTICATION_PASSWORD);
070:
071: // Build an authenticator if the properties have been set.
072: SimpleAuthenticator authenticator = null;
073: if (authName != null && authPass != null) {
074: authenticator = new SimpleAuthenticator(authName, authPass);
075: }
076: return authenticator;
077: }
078:
079: /**
080: * Gets the session properties.
081: * @param reference the given reference
082: * @return session properties
083: * @throws NamingException if object is not read.
084: */
085: public Properties getSessionProperties(final Reference reference)
086: throws NamingException {
087: return getObject(reference, MAIL_PROPERTIES);
088: }
089:
090: /**
091: * Get the given string from the reference.
092: * @param reference the given reference
093: * @param propertyName the given property
094: * @return the given string
095: */
096: public String getString(final Reference reference,
097: final String propertyName) {
098: RefAddr tmpRefAddr = reference.get(propertyName);
099: String tmpValue = null;
100: if (tmpRefAddr instanceof StringRefAddr) {
101: tmpValue = (String) ((StringRefAddr) tmpRefAddr)
102: .getContent();
103: }
104: return tmpValue;
105: }
106:
107: /**
108: * Load a given object for the given property name.
109: * @param reference the given reference
110: * @param propertyName the given property
111: * @return the loaded object
112: * @throws NamingException if object is not read.
113: * @param <T> the type of the object to load
114: */
115: @SuppressWarnings("unchecked")
116: public <T> T getObject(final Reference reference,
117: final String propertyName) throws NamingException {
118: RefAddr refAddr = reference.get(propertyName);
119: T obj = null;
120: if (refAddr != null) {
121: try {
122: obj = (T) Serialization.loadObject((byte[]) refAddr
123: .getContent());
124: } catch (IOException e) {
125: NamingException ne = new NamingException(
126: "Cannot load mail session properties object");
127: ne.initCause(e);
128: throw ne;
129: } catch (ClassNotFoundException e) {
130: NamingException ne = new NamingException(
131: "Cannot load mail session properties object");
132: ne.initCause(e);
133: throw ne;
134: }
135: }
136: return obj;
137: }
138:
139: }
|