KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > jms > outgoing > JBIListener


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 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: JBIExchangeProcessor.java 12:18:02 alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.jms.outgoing;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.jbi.messaging.ExchangeStatus;
32 import javax.jbi.messaging.MessageExchange;
33 import javax.jbi.messaging.MessagingException;
34 import javax.jbi.messaging.NormalizedMessage;
35 import javax.jms.JMSException JavaDoc;
36 import javax.jms.JMSSecurityException JavaDoc;
37 import javax.jms.TextMessage JavaDoc;
38 import javax.xml.namespace.QName JavaDoc;
39 import javax.xml.transform.Transformer JavaDoc;
40 import javax.xml.transform.TransformerException JavaDoc;
41 import javax.xml.transform.TransformerFactory JavaDoc;
42 import javax.xml.transform.stream.StreamResult JavaDoc;
43
44 import org.objectweb.petals.binding.jms.JMSConnection;
45 import org.objectweb.petals.component.common.HandlingException;
46 import org.objectweb.petals.component.common.MEPConstants;
47 import org.objectweb.petals.component.common.util.MessageExchangeWrapper;
48 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions;
49
50 /**
51  * This class is the JBI message processor for the JmsBC component. It receives
52  * messages from JBI and send them to a JMS destination.
53  *
54  * @version $Revision: $ $Date: $
55  * @since Petals 1.0
56  * @author alouis
57  */

58 public class JBIListener implements
59     org.objectweb.petals.component.common.bc.JBIListener {
60
61     private static final String JavaDoc PROPERTY_OPERATION = "javax.jbi.messaging.operation";
62
63     private Logger JavaDoc logger;
64
65     private Map JavaDoc<String JavaDoc, JMSConnection> jmsConnections;
66
67     public JBIListener(Map JavaDoc<String JavaDoc, JMSConnection> jmsConnections, Logger JavaDoc log) {
68         this.jmsConnections = jmsConnections;
69         logger = log;
70     }
71
72     /**
73      * Process ACTIVE messages as a PROVIDER. Only InOnly and RobustInOnly
74      * messages are processed. Otherwise, set the Exchange to ERROR status and
75      * send it, and throw an Exception
76      *
77      * If the specified exchange endpoint is not registered, set the Exchange to
78      * ERROR status and send it, and throw an Exception * Else, send a
79      * <em>JMS message</em> to the corresponding <em>JMS Queue<em>,
80      *
81      * If the JMS msg has not been correctly sent
82      * set the Exchange to ERROR status and send it, and throw an Exception *
83      *
84      * @param exchange
85      * @throws Exception exhange not correct, destination not found, problem while sending the JMS message,
86      */

87     public boolean onJBIMessage(String JavaDoc address, MessageExchangeWrapper exchange,
88         Extensions extensions) throws HandlingException {
89         logger.log(Level.FINE, "onJBIMessage");
90
91         if (ExchangeStatus.ACTIVE.equals(exchange.getStatus())) {
92             if (MessageExchange.Role.PROVIDER.equals(exchange.getRole())) {
93                 if (MEPConstants.IN_ONLY_PATTERN.value().equals(exchange.getPattern())
94                         || MEPConstants.ROBUST_IN_ONLY_PATTERN.value().equals(exchange
95                                 .getPattern())
96                         || MEPConstants.IN_OPTIONAL_OUT_PATTERN.value().equals(exchange
97                         .getPattern())) {
98                     sendJMSMessage(address, exchange);
99                 } else {
100                     Exception JavaDoc e = new Exception JavaDoc(
101                         "MessageExchangePattern not allowed :"
102                             + exchange.getPattern());
103                     try {
104                         setErrorStatus(exchange, e, true);
105                     } catch (MessagingException e1) {
106                         throw new HandlingException(e1);
107                     }
108                 }
109             }
110         }
111         return false;
112     }
113
114     /**
115      * Set the exchange to "ERROR" status. An exception is created with the
116      * given reason as message. This exception is set in the MessageExchange
117      * object.
118      *
119      * @param msgExchange
120      * current MessageExchange used to send and receive messages
121      * @param ex
122      * The exception
123      * @param severe
124      * Log the error at warning or severe level
125      * @throws MessagingException
126      * If an error occured during calls on the JBI container
127      */

128     protected void setErrorStatus(MessageExchange msgExchange, Exception JavaDoc ex,
129         boolean severe) throws MessagingException {
130         msgExchange.setError(ex);
131         msgExchange.setStatus(ExchangeStatus.ERROR);
132         if (!severe) {
133             logger.log(Level.WARNING, ex.getMessage(), ex);
134         } else {
135             logger.log(Level.SEVERE, ex.getMessage(), ex);
136         }
137     }
138
139     /**
140      * Send a JMS message to the JMS queue corresponding to the specified
141      * exchange's endpoint.
142      *
143      * @param address
144      * JMS destination
145      * @param exchange
146      * @throws Exception
147      * no JMS destination found, or problem while sending the
148      * message to JMS
149      */

150     protected void sendJMSMessage(String JavaDoc address, MessageExchange exchange)
151         throws HandlingException {
152         logger.log(Level.FINE, "sendJMSMessage");
153         logger.log(Level.INFO, "A JBI message is received for '" + address
154             + "' JMS provider.");
155
156         // find the connection to use for this exchange
157
JMSConnection conn = jmsConnections.get(address);
158
159         if (conn == null) {
160             // No such endpoint registered
161
throw new HandlingException("'" + address
162                 + "' JMS provider not found.");
163         }
164
165         // transform the jbi msg into a jms msg
166
TextMessage JavaDoc msg = null;
167         try {
168             msg = conn.getSession().createTextMessage();
169             transform(exchange, msg);
170         } catch (JMSException JavaDoc e) {
171             logger.log(Level.SEVERE, "Can not create a JMS text message.", e);
172             throw new HandlingException(e);
173         } catch (Exception JavaDoc e) {
174             logger.log(Level.SEVERE,
175                 "Can not transform the JBI message to a JMS message.", e);
176             throw new HandlingException(e);
177         }
178
179         // send
180
try {
181             conn.getMessageProducer().send(msg);
182             if(conn.isTransacted()){
183                 conn.getSession().commit();
184             }
185         } catch (JMSSecurityException JavaDoc e) {
186             logger.log(Level.SEVERE, "Can not send the JMS message, WRITE right not granted.", e);
187             throw new HandlingException(e);
188         } catch (JMSException JavaDoc e) {
189             logger.log(Level.SEVERE, "Can not send the JMS message.", e);
190             throw new HandlingException(e);
191         }
192         logger.log(Level.INFO, "Message sent to '" + conn.getDestinationName()
193             + "'.");
194     }
195
196     /**
197      * Transform the NormalizedMessage of a JBI MsgExch into a TextMessage. The
198      * content , properties and attachments of the IN NormalizedMessage are set.
199      *
200      * @param exchange
201      * @param jms
202      * @throws JMSException
203      * writing properties to the TextMessage
204      * @throws TransformerException
205      * NormalizedMessage content access error
206      * @throws IOException
207      * NormalizedMessage content access error
208      */

209     protected void transform(MessageExchange exchange, TextMessage JavaDoc jms) throws JMSException JavaDoc, TransformerException JavaDoc, IOException JavaDoc {
210         logger.log(Level.FINE, "transform");
211
212         NormalizedMessage in = exchange.getMessage("in");
213         QName JavaDoc operation = exchange.getOperation();
214
215         // set normalized properties
216
for (Iterator JavaDoc it = in.getPropertyNames().iterator(); it.hasNext();) {
217             String JavaDoc pName = (String JavaDoc) it.next();
218             jms.setObjectProperty(pName, in.getProperty(pName));
219         }
220         // set operation
221
if (operation != null) {
222             jms.setStringProperty(PROPERTY_OPERATION, operation.toString());
223         }
224
225         // set content
226
StreamResult JavaDoc stream = new StreamResult JavaDoc(new ByteArrayOutputStream JavaDoc());
227         Transformer JavaDoc transformer = TransformerFactory.newInstance()
228             .newTransformer();
229         transformer.transform(in.getContent(), stream);
230         stream.getOutputStream().close();
231         jms.setText(stream.getOutputStream().toString());
232     }
233
234 }
235
Popular Tags