KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > mail > listeners > MailBCJBIListener


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: MailBCJBIListener.java 154 27 sept. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.binding.mail.listeners;
24
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.mail.Session JavaDoc;
28 import javax.mail.internet.InternetAddress JavaDoc;
29 import javax.mail.internet.MimeMessage JavaDoc;
30
31 import org.objectweb.petals.component.common.HandlingException;
32 import org.objectweb.petals.component.common.bc.JBIListener;
33 import org.objectweb.petals.component.common.util.MessageExchangeWrapper;
34 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions;
35
36 /**
37  * Mail BC listener for incoming JBI messages
38  *
39  * @author ofabre - EBM Websourcing
40  *
41  */

42 public class MailBCJBIListener implements JBIListener {
43
44     protected Logger JavaDoc log;
45
46     protected MailSessionManager mailSessionManager;
47
48     protected MimeMessageManager mimeMessageManager;
49
50     protected SessionDescriptorBuilder sessionDescriptorBuilder;
51
52     /**
53      * Creates a new instance of {@link MailBCJBIListener}
54      *
55      * @param mimeMessageManager
56      * @param mailSessionManager
57      * @param sessionDescriptorBuilder
58      * @param log
59      */

60     public MailBCJBIListener(MimeMessageManager mimeMessageManager,
61             MailSessionManager mailSessionManager,
62             SessionDescriptorBuilder sessionDescriptorBuilder, Logger JavaDoc log) {
63         super();
64         this.log = log;
65         this.mimeMessageManager = mimeMessageManager;
66         this.mailSessionManager = mailSessionManager;
67         this.sessionDescriptorBuilder = sessionDescriptorBuilder;
68     }
69
70     /*
71      * (non-Javadoc)
72      * @see org.objectweb.petals.component.common.bc.JBIListener#onJBIMessage(java.lang.String, org.objectweb.petals.component.common.util.MessageExchangeWrapper, org.objectweb.petals.tools.jbicommon.descriptor.Extensions)
73      */

74     public boolean onJBIMessage(String JavaDoc address, MessageExchangeWrapper exchange,
75             Extensions extensions) throws HandlingException {
76
77         if (!exchange.isInOnlyPattern()) {
78             throw new HandlingException(
79                     "Mail BC can only handle InOnly message exchanges");
80         }
81
82         try {
83             // Build a mail session descriptor from the given address (URI)
84
SessionDescriptor sessionDescriptor = sessionDescriptorBuilder
85                     .build(address, exchange);
86
87             // Build the destination address
88
InternetAddress JavaDoc internetAddress = new InternetAddress JavaDoc(
89                     sessionDescriptor.getToAddress());
90
91             // Build the mail session
92
Session JavaDoc session = mailSessionManager
93                     .createSessionPropertiesFromDescriptor(sessionDescriptor);
94
95             // Create a mail message with the incoming jbi message
96
MimeMessage JavaDoc mimeMessage = mimeMessageManager
97                     .mapNormalizedMessageToMimeMessage(session,
98                             internetAddress, exchange.getInMessage());
99
100             // Send this mail
101
mailSessionManager
102                     .sendMail(mimeMessage, sessionDescriptor, session);
103
104         } catch (Throwable JavaDoc e) {
105             throw new HandlingException(e);
106         }
107         return false;
108     }
109
110 }
111
Popular Tags