KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jbi > components > MuleReceiver


1 /*
2  * $Id: MuleReceiver.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.jbi.components;
12
13 import java.io.OutputStream JavaDoc;
14 import java.util.Arrays JavaDoc;
15
16 import javax.jbi.JBIException;
17 import javax.jbi.messaging.MessageExchange;
18 import javax.jbi.messaging.MessagingException;
19 import javax.jbi.messaging.NormalizedMessage;
20 import javax.jbi.servicedesc.ServiceEndpoint;
21 import javax.xml.namespace.QName JavaDoc;
22
23 import org.apache.commons.lang.SystemUtils;
24 import org.mule.MuleManager;
25 import org.mule.config.converters.QNameConverter;
26 import org.mule.config.i18n.Message;
27 import org.mule.impl.MuleDescriptor;
28 import org.mule.impl.MuleMessage;
29 import org.mule.providers.AbstractMessageReceiver;
30 import org.mule.providers.InternalMessageListener;
31 import org.mule.providers.jbi.JbiMessageAdapter;
32 import org.mule.providers.jbi.JbiUtils;
33 import org.mule.umo.UMOComponent;
34 import org.mule.umo.UMODescriptor;
35 import org.mule.umo.UMOEvent;
36 import org.mule.umo.UMOException;
37 import org.mule.umo.UMOMessage;
38 import org.mule.umo.UMOTransaction;
39 import org.mule.umo.lifecycle.InitialisationException;
40 import org.mule.umo.lifecycle.RecoverableException;
41 import org.mule.umo.provider.UMOMessageReceiver;
42
43 /**
44  * Can receive events over Mule transports. Given an muleEndpoint (or endpoint string
45  * i.e. jms://my.queue) This component will set up the necessary bindings with Mule
46  *
47  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
48  * @version $Revision: 4259 $
49  */

50 public class MuleReceiver extends AbstractEndpointComponent implements InternalMessageListener
51 {
52
53     private AbstractMessageReceiver receiver;
54
55     protected QName JavaDoc targetService;
56
57     protected String JavaDoc targetServiceName;
58
59     public QName JavaDoc getTargetService()
60     {
61         return targetService;
62     }
63
64     public void setTargetService(QName JavaDoc targetService)
65     {
66         this.targetService = targetService;
67     }
68
69     public AbstractMessageReceiver getReceiver()
70     {
71         return receiver;
72     }
73
74     public void setReceiver(AbstractMessageReceiver receiver)
75     {
76         this.receiver = receiver;
77     }
78
79     public String JavaDoc getTargetServiceName()
80     {
81         return targetServiceName;
82     }
83
84     public void setTargetServiceName(String JavaDoc targetServiceName)
85     {
86         this.targetServiceName = targetServiceName;
87     }
88
89     protected void doInit() throws JBIException
90     {
91         super.doInit();
92         try
93         {
94             if (targetService == null)
95             {
96                 if (targetServiceName != null)
97                 {
98                     targetService = (QName JavaDoc)new QNameConverter().convert(QName JavaDoc.class, targetServiceName);
99                 }
100             }
101
102             UMOMessageReceiver receiver = muleEndpoint.getConnector().registerListener(
103                 new NullUMOComponent(getName()), muleEndpoint);
104
105             if (receiver == null)
106             {
107                 throw new NullPointerException JavaDoc(new Message("jbi", 1, getName()).toString());
108             }
109             else if (receiver instanceof AbstractMessageReceiver)
110             {
111                 this.receiver = (AbstractMessageReceiver)receiver;
112             }
113             else
114             {
115                 throw new IllegalArgumentException JavaDoc(new Message("jbi", 2, getName(),
116                     AbstractMessageReceiver.class.getName()).toString());
117             }
118
119             this.receiver.setListener(this);
120         }
121         catch (Exception JavaDoc e)
122         {
123             throw new JBIException(e);
124         }
125     }
126
127     public UMOMessage onMessage(UMOMessage message,
128                                 UMOTransaction trans,
129                                 boolean synchronous,
130                                 OutputStream JavaDoc outputStream) throws UMOException
131     {
132         MessageExchange me = null;
133         try
134         {
135             if (synchronous)
136             {
137                 me = exchangeFactory.createInOutExchange();
138             }
139             else
140             {
141                 me = exchangeFactory.createInOnlyExchange();
142             }
143             if (targetService != null)
144             {
145                 me.setService(targetService);
146                 ServiceEndpoint endpoint = null;
147                 ServiceEndpoint[] eps = context.getEndpointsForService(targetService);
148                 if (eps.length == 0)
149                 {
150                     // container should handle this
151
throw new MessagingException("There are no endpoints registered for targetService: "
152                                                  + targetService);
153                 }
154                 else
155                 {
156                     endpoint = eps[0];
157                 }
158
159                 if (logger.isDebugEnabled())
160                 {
161                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Found the following endpoints for: ");
162                     buf.append(targetService).append(SystemUtils.LINE_SEPARATOR);
163                     for (int i = 0; i < eps.length; i++)
164                     {
165                         ServiceEndpoint ep = eps[i];
166                         buf.append(ep.getEndpointName())
167                             .append(";")
168                             .append(ep.getServiceName())
169                             .append(";")
170                             .append(Arrays.asList(ep.getInterfaces()))
171                             .append(SystemUtils.LINE_SEPARATOR);
172                     }
173                     logger.debug(buf.toString());
174                 }
175
176                 logger.debug("Using Jbi Endpoint for targetService: " + targetService + " is: " + endpoint);
177                 if (endpoint != null)
178                 {
179                     me.setEndpoint(endpoint);
180                 }
181             }
182             else
183             {
184                 logger.debug("Jbi target service is not set Container will need to resolve target");
185             }
186
187             NormalizedMessage nmessage = me.createMessage();
188             JbiUtils.populateNormalizedMessage(message, nmessage);
189
190             me.setMessage(nmessage, IN);
191             if (synchronous)
192             {
193                 deliveryChannel.sendSync(me, MuleManager.getConfiguration().getSynchronousEventTimeout());
194                 NormalizedMessage result = null;
195
196                 result = me.getMessage(OUT);
197                 done(me);
198                 if (result != null)
199                 {
200                     return new MuleMessage(new JbiMessageAdapter(result));
201                 }
202                 else
203                 {
204                     return null;
205                 }
206             }
207             else
208             {
209                 deliveryChannel.send(me);
210                 return null;
211             }
212         }
213         catch (MessagingException e)
214         {
215             try
216             {
217                 error(me, e);
218                 return null;
219             }
220             catch (MessagingException e1)
221             {
222                 handleException(e);
223                 return null;
224             }
225         }
226     }
227
228     /**
229      * A null component is used when interfacing with JBI components, the Null
230      * component is a placeholder of the JBI component that isn't managed by mule
231      */

232     class NullUMOComponent implements UMOComponent
233     {
234         /**
235          * Serial version
236          */

237         private static final long serialVersionUID = 6446394166371870045L;
238
239         private UMODescriptor descriptor;
240
241         public NullUMOComponent(String JavaDoc name)
242         {
243             this.descriptor = new MuleDescriptor(name);
244         }
245
246         public UMODescriptor getDescriptor()
247         {
248             return descriptor;
249         }
250
251         public void dispatchEvent(UMOEvent event) throws UMOException
252         {
253             throw new UnsupportedOperationException JavaDoc("NullComponent:dispatchEvent");
254         }
255
256         public UMOMessage sendEvent(UMOEvent event) throws UMOException
257         {
258             throw new UnsupportedOperationException JavaDoc("NullComponent:sendEvent");
259         }
260
261         public void pause() throws UMOException
262         {
263             // nothing to do
264
}
265
266         public void resume() throws UMOException
267         {
268             // nothing to do
269
}
270
271         public boolean isPaused()
272         {
273             return false;
274         }
275
276         public void start() throws UMOException
277         {
278             // nothing to do
279
}
280
281         public void stop() throws UMOException
282         {
283             // nothing to do
284
}
285
286         public void dispose()
287         {
288             // nothing to do
289
}
290
291         public void initialise() throws InitialisationException, RecoverableException
292         {
293             // nothing to do
294
}
295
296         public boolean isStarted()
297         {
298             return true;
299         }
300
301         public Object JavaDoc getInstance() throws UMOException
302         {
303             return null;
304         }
305     }
306
307 }
308
Popular Tags