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: MailComponent.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.mail;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import javax.mail.Address;
030: import javax.mail.internet.AddressException;
031: import javax.mail.internet.InternetAddress;
032: import javax.naming.InitialContext;
033: import javax.naming.NamingException;
034:
035: import org.ow2.easybeans.component.api.EZBComponentException;
036: import org.ow2.easybeans.component.itf.JavaMailComponent;
037: import org.ow2.easybeans.component.mail.factory.AbsJavaMailRef;
038: import org.ow2.easybeans.component.mail.factory.JavaMailMimePartDataSourceRef;
039: import org.ow2.easybeans.component.mail.factory.JavaMailSessionRef;
040: import org.ow2.util.log.Log;
041: import org.ow2.util.log.LogFactory;
042:
043: /**
044: * Component for providing mail access to EE components.
045: * @author Florent BENOIT
046: */
047: public class MailComponent implements JavaMailComponent {
048:
049: /**
050: * Logger.
051: */
052: private Log logger = LogFactory.getLog(MailComponent.class);
053:
054: /**
055: * Default auth for factories associated to this component.
056: */
057: private Auth defaultAuth = null;
058:
059: /**
060: * List of mail session factories.
061: */
062: private List<Session> sessionFactories = null;
063:
064: /**
065: * List of mail mime part ds factories.
066: */
067: private List<MimePart> mimePartFactories = null;
068:
069: /**
070: * Init method.<br/> This method is called before the start method.
071: * @throws EZBComponentException if the initialization has failed.
072: */
073: public void init() throws EZBComponentException {
074:
075: }
076:
077: /**
078: * Start method.<br/> This method is called after the init method.
079: * @throws EZBComponentException if the start has failed.
080: */
081: public void start() throws EZBComponentException {
082:
083: // Create and bind factories
084: for (MailItf factory : getAllFactories()) {
085:
086: AbsJavaMailRef javaMailRef = null;
087: String jndiName = factory.getJNDIName();
088:
089: // Create object and bind it
090: if (factory instanceof Session) {
091: JavaMailSessionRef javamailSession = new JavaMailSessionRef();
092: javaMailRef = javamailSession;
093: } else if (factory instanceof MimePart) {
094: JavaMailMimePartDataSourceRef mimePartDataSourceRef = new JavaMailMimePartDataSourceRef();
095: javaMailRef = mimePartDataSourceRef;
096:
097: // Set properties of this factory.
098: MimePart mimePart = (MimePart) factory;
099:
100: // Set subject
101: mimePartDataSourceRef.setSubject(mimePart.getSubject());
102:
103: // Define addresses
104: List<Address> toAddresses = new ArrayList<Address>();
105: List<Address> ccAddresses = new ArrayList<Address>();
106: List<Address> bccAddresses = new ArrayList<Address>();
107:
108: // Get value
109: for (MailAddress mail : mimePart.getMailAddresses()) {
110:
111: // Build address object from the string
112: Address address = null;
113: try {
114: address = new InternetAddress(mail.getName());
115: } catch (AddressException e) {
116: throw new EZBComponentException(
117: "Cannot build an internet address with given value '"
118: + mail.getName() + "'.", e);
119: }
120:
121: // Add address in the correct list
122: if ("CC".equalsIgnoreCase(mail.getType())) {
123: ccAddresses.add(address);
124: } else if ("BCC".equalsIgnoreCase(mail.getType())) {
125: bccAddresses.add(address);
126: } else {
127: // Default is TO
128: toAddresses.add(address);
129: }
130: }
131:
132: // Set recipients
133: mimePartDataSourceRef.setToRecipients(toAddresses
134: .toArray(new Address[toAddresses.size()]));
135: mimePartDataSourceRef.setCcRecipients(ccAddresses
136: .toArray(new Address[ccAddresses.size()]));
137: mimePartDataSourceRef.setBccRecipients(bccAddresses
138: .toArray(new Address[bccAddresses.size()]));
139:
140: } else {
141: throw new EZBComponentException("Unknown factory '"
142: + factory + "'.");
143: }
144:
145: // Set common values
146: javaMailRef.setName(factory.getName());
147: javaMailRef.setJNDIName(jndiName);
148: javaMailRef.setProperties(factory
149: .getMailSessionProperties());
150:
151: // Auth set for the factory ?
152: Auth auth = factory.getAuth();
153:
154: // No, try to see for default auth ?
155: if (auth == null) {
156: auth = getAuth();
157: }
158:
159: // There is authentication, set it
160: if (auth != null) {
161: javaMailRef.setAuthName(auth.getUsername());
162: javaMailRef.setAuthName(auth.getPassword());
163: }
164:
165: // Bind object
166: try {
167: new InitialContext().bind(jndiName, javaMailRef);
168: logger.info(
169: "Binding {0} Mail factory with JNDI name {1}",
170: javaMailRef.getType(), jndiName);
171: } catch (NamingException e) {
172: throw new EZBComponentException(
173: "Unable to bind the factory '" + factory
174: + "' with JNDI name '" + jndiName
175: + "'.", e);
176: }
177:
178: }
179:
180: }
181:
182: /**
183: * Stop method.<br/> This method is called when component needs to be
184: * stopped.
185: * @throws EZBComponentException if the stop is failing.
186: */
187: public void stop() throws EZBComponentException {
188: // Unbind factories
189: // Create and bind factories
190: for (MailItf factory : getAllFactories()) {
191: try {
192: new InitialContext().unbind(factory.getJNDIName());
193: } catch (NamingException e) {
194: logger
195: .error(
196: "Cannot unbind factory with name {0} and JNDI name {1}",
197: factory.getName(), factory
198: .getJNDIName());
199: }
200: }
201: }
202:
203: /**
204: * Set the auth.
205: * @param auth the given auth
206: */
207: public void setAuth(final Auth auth) {
208: this .defaultAuth = auth;
209: }
210:
211: /**
212: * @return the auth.
213: */
214: public Auth getAuth() {
215: return this .defaultAuth;
216: }
217:
218: /**
219: * @return list of factories.
220: */
221: public List<MailItf> getAllFactories() {
222: List<MailItf> factories = new ArrayList<MailItf>();
223: factories.addAll(sessionFactories);
224: factories.addAll(mimePartFactories);
225: return factories;
226: }
227:
228: /**
229: * Sets the list of mail session factories.
230: * @param sessionFactories the list of factories
231: */
232: public void setSessions(final List<Session> sessionFactories) {
233: this .sessionFactories = sessionFactories;
234: }
235:
236: /**
237: * Gets the list of mail session factories.
238: * @return the list of mail session factories.
239: */
240: public List<Session> getSessions() {
241: return this .sessionFactories;
242: }
243:
244: /**
245: * Sets the list of mail mimepart factories.
246: * @param mimePartFactories the list of factories
247: */
248: public void setMimeParts(final List<MimePart> mimePartFactories) {
249: this .mimePartFactories = mimePartFactories;
250: }
251:
252: /**
253: * Gets the list of mail mimepart factories.
254: * @return the list of mail mimepart factories.
255: */
256: public List<MimePart> getMimeParts() {
257: return this.mimePartFactories;
258: }
259: }
|