KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > jms > incoming > JMSMessageListener


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: JMSMessageListener.java 12:18:02 alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.jms.incoming;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import javax.jbi.messaging.InOnly;
29 import javax.jbi.messaging.MessageExchange;
30 import javax.jbi.messaging.MessageExchangeFactory;
31 import javax.jbi.messaging.MessagingException;
32 import javax.jbi.messaging.NormalizedMessage;
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.Message JavaDoc;
35 import javax.jms.MessageListener JavaDoc;
36 import javax.jms.TextMessage JavaDoc;
37 import javax.xml.namespace.QName JavaDoc;
38 import javax.xml.transform.Source JavaDoc;
39
40 import org.objectweb.petals.binding.jms.JmsBC;
41 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
42 import org.objectweb.petals.component.common.util.SourceHelper;
43
44 /**
45  * This class is the JMS message listener for the JmsBC component
46  *
47  * @version $Revision: $ $Date: $
48  * @since Petals 1.1
49  * @author alouis
50  */

51 public class JMSMessageListener implements MessageListener JavaDoc {
52
53     public static final String JavaDoc PROPERTY_OPERATION = "javax.jbi.messaging.operation";
54
55     protected JmsBC jmsBC;
56     
57     protected String JavaDoc address;
58     
59     protected Logger JavaDoc l;
60     
61     protected MessageExchangeFactory exchangeFactory;
62
63     public JMSMessageListener(JmsBC jmsBC, String JavaDoc address) {
64         this.jmsBC=jmsBC;
65         this.address = address;
66         l=jmsBC.getLogger();
67     }
68
69     public void onMessage(Message jmsExchange) {
70         l.log(Level.FINE, "onMessage");
71         l.log(Level.INFO, "A JMS message is received from '"
72             + address+ "'.");
73
74         try {
75             // 1. Only TextMessage are read.
76
TextMessage JavaDoc textMessage;
77             textMessage = (TextMessage JavaDoc) jmsExchange;
78
79             // 2. Create an exchange
80
InOnly exchange = createInOnly();
81
82             // 3. transform textMessage->normalizedMessage
83
transform(textMessage, exchange);
84
85             // 4. send the exchange, use the framework's send() method
86
jmsBC.sendMessage(address, exchange);
87             
88             l.log(Level.INFO, "the message has been sent.");
89
90         } catch (ClassCastException JavaDoc e) {
91             l
92             .log(
93                 Level.SEVERE,
94                 "Only TextMessage are processed by petals-binding-jms component.",
95                 e);
96         } catch (Exception JavaDoc e) {
97             l.log(Level.SEVERE, e.getClass() + " " + e.getMessage(), e);
98         }
99     }
100
101     /**
102      * Create an InOnly exchange,
103      * @return
104      * @throws MessagingException
105      */

106     protected InOnly createInOnly() throws MessagingException {
107         MessageExchangeFactory factory = getMessageExchangeFactory();
108         InOnly exchange = factory.createInOnlyExchange();
109         NormalizedMessage nm = exchange.createMessage();
110         exchange.setInMessage(nm);
111         return exchange;
112     }
113
114
115
116     /**
117      * Return an unique MessageExchangeFactory created by the
118      * DeliveryChannel. Lazyloading pattern.
119      * @return
120      */

121     protected MessageExchangeFactory getMessageExchangeFactory() {
122         if (exchangeFactory == null) {
123             exchangeFactory = jmsBC.getChannel().createExchangeFactory();
124         }
125         return exchangeFactory;
126     }
127
128     /**
129      * Transform the TextMessage
130      * into a NormalizedMessage of the JBI MsgExch .
131      * The content and properties are set.
132      * @param jms
133      * @param jbi
134      * @throws PEtALSComponentSDKException can not create a Source object from the jms text content
135      * @throws JMSException getting jms content or properties
136      * @throws MessagingException setting NormalizedMessage properties or content
137      */

138     protected void transform(TextMessage JavaDoc jms, MessageExchange jbi)
139     throws JMSException JavaDoc, MessagingException,PEtALSComponentSDKException {
140         l.log(Level.FINE, "transform");
141
142         //set content
143
String JavaDoc content = jms.getText();
144         Source JavaDoc source = SourceHelper.createSource(content);
145
146         jbi.getMessage("in").setContent(source);
147
148         // set operation
149
String JavaDoc operation = jms.getStringProperty(PROPERTY_OPERATION);
150         if (operation != null){
151             jbi.setOperation(new QName JavaDoc(operation));
152         }
153
154         // set properties
155
for (Enumeration JavaDoc it = jms.getPropertyNames(); it.hasMoreElements();) {
156             String JavaDoc pName = (String JavaDoc) it.nextElement();
157             jbi.setProperty(pName, jms.getObjectProperty(pName));
158         }
159     }
160 }
161
Popular Tags