KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > client > ServiceMixClientFacade


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.client;
18
19 import org.apache.servicemix.components.util.PojoSupport;
20 import org.apache.servicemix.jbi.FaultException;
21 import org.apache.servicemix.jbi.NoOutMessageAvailableException;
22 import org.apache.servicemix.jbi.container.ActivationSpec;
23 import org.apache.servicemix.jbi.container.JBIContainer;
24 import org.apache.servicemix.jbi.messaging.DefaultMarshaler;
25 import org.apache.servicemix.jbi.messaging.PojoMarshaler;
26 import org.apache.servicemix.jbi.resolver.EndpointFilter;
27 import org.apache.servicemix.jbi.resolver.EndpointResolver;
28 import org.apache.servicemix.jbi.resolver.ExternalInterfaceNameEndpointResolver;
29 import org.apache.servicemix.jbi.resolver.ExternalServiceNameEndpointResolver;
30 import org.apache.servicemix.jbi.resolver.InterfaceNameEndpointResolver;
31 import org.apache.servicemix.jbi.resolver.NullEndpointFilter;
32 import org.apache.servicemix.jbi.resolver.ServiceAndEndpointNameResolver;
33 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver;
34
35 import javax.jbi.JBIException;
36 import javax.jbi.component.ComponentContext;
37 import javax.jbi.messaging.DeliveryChannel;
38 import javax.jbi.messaging.ExchangeStatus;
39 import javax.jbi.messaging.Fault;
40 import javax.jbi.messaging.InOnly;
41 import javax.jbi.messaging.InOptionalOut;
42 import javax.jbi.messaging.InOut;
43 import javax.jbi.messaging.MessageExchange;
44 import javax.jbi.messaging.MessageExchangeFactory;
45 import javax.jbi.messaging.MessagingException;
46 import javax.jbi.messaging.NormalizedMessage;
47 import javax.jbi.messaging.RobustInOnly;
48 import javax.xml.namespace.QName JavaDoc;
49 import javax.xml.transform.Source JavaDoc;
50
51 import java.util.Iterator JavaDoc;
52 import java.util.Map JavaDoc;
53
54 /**
55  * A Facade around the {@link ComponentContext} to provide the {@link ServiceMixClient} API which is useful for
56  * working with JBI from inside a POJO based JBI Component which doesn't derive from {@link PojoSupport}
57  *
58  * @version $Revision: 429658 $
59  */

60 public class ServiceMixClientFacade implements ServiceMixClient {
61
62     private ComponentContext context;
63     private EndpointFilter filter = NullEndpointFilter.getInstance();
64     private PojoMarshaler marshaler = new DefaultMarshaler();
65     private MessageExchangeFactory exchangeFactory;
66
67     public ServiceMixClientFacade(ComponentContext context) {
68         this.context = context;
69     }
70
71     /**
72      * Provides the JBI container used for message dispatch.
73      */

74     public ServiceMixClientFacade(JBIContainer container) throws JBIException {
75         this(container, new ActivationSpec());
76     }
77
78     /**
79      * Provides the JBI container and the activation specification, which can be used to register this
80      * client at a specific endpoint so that default container routing rules can be configured via dependency injection
81      * and the client endpoint metadata can be configured to allow services to talk to this client.
82      */

83     public ServiceMixClientFacade(JBIContainer container, ActivationSpec activationSpec) throws JBIException {
84         activationSpec.setComponent(this);
85         container.activateComponent(activationSpec);
86     }
87
88     public InOnly createInOnlyExchange() throws MessagingException {
89         InOnly exchange = getExchangeFactory().createInOnlyExchange();
90         NormalizedMessage in = exchange.createMessage();
91         exchange.setInMessage(in);
92         return exchange;
93     }
94
95     public InOnly createInOnlyExchange(EndpointResolver resolver) throws JBIException {
96         InOnly exchange = createInOnlyExchange();
97         configureEndpoint(exchange, resolver);
98         return exchange;
99     }
100
101     public InOut createInOutExchange() throws MessagingException {
102         InOut exchange = getExchangeFactory().createInOutExchange();
103         NormalizedMessage in = exchange.createMessage();
104         exchange.setInMessage(in);
105         return exchange;
106     }
107
108     public InOut createInOutExchange(EndpointResolver resolver) throws JBIException {
109         InOut exchange = createInOutExchange();
110         configureEndpoint(exchange, resolver);
111         return exchange;
112     }
113
114     public InOptionalOut createInOptionalOutExchange() throws MessagingException {
115         InOptionalOut exchange = getExchangeFactory().createInOptionalOutExchange();
116         NormalizedMessage in = exchange.createMessage();
117         exchange.setInMessage(in);
118         return exchange;
119     }
120
121     public InOptionalOut createInOptionalOutExchange(EndpointResolver resolver) throws JBIException {
122         InOptionalOut exchange = createInOptionalOutExchange();
123         configureEndpoint(exchange, resolver);
124         return exchange;
125     }
126
127     public RobustInOnly createRobustInOnlyExchange() throws MessagingException {
128         RobustInOnly exchange = getExchangeFactory().createRobustInOnlyExchange();
129         NormalizedMessage in = exchange.createMessage();
130         exchange.setInMessage(in);
131         return exchange;
132     }
133
134     public RobustInOnly createRobustInOnlyExchange(EndpointResolver resolver) throws JBIException {
135         RobustInOnly exchange = getExchangeFactory().createRobustInOnlyExchange();
136         configureEndpoint(exchange, resolver);
137         return exchange;
138     }
139
140     public Destination createDestination(String JavaDoc uri) throws MessagingException {
141         return new DefaultDestination(this, uri);
142     }
143
144     public void send(MessageExchange exchange) throws MessagingException {
145         getDeliveryChannel().send(exchange);
146         done(exchange);
147     }
148         
149     public void send(Message message) throws MessagingException {
150         send(message.getExchange());
151     }
152
153     public boolean sendSync(MessageExchange exchange) throws MessagingException {
154         return getDeliveryChannel().sendSync(exchange);
155     }
156
157     public boolean sendSync(MessageExchange exchange, long timeout) throws MessagingException {
158         return getDeliveryChannel().sendSync(exchange, timeout);
159     }
160
161     public MessageExchange receive() throws MessagingException {
162         return getDeliveryChannel().accept();
163     }
164
165     public MessageExchange receive(long timeout) throws MessagingException {
166         return getDeliveryChannel().accept(timeout);
167     }
168
169     public ComponentContext getContext() {
170         return context;
171     }
172
173     public DeliveryChannel getDeliveryChannel() throws MessagingException {
174         return getContext().getDeliveryChannel();
175     }
176
177     /**
178      * Provide access to the default message exchange exchangeFactory, lazily creating one.
179      */

180     public MessageExchangeFactory getExchangeFactory() throws MessagingException {
181         if (exchangeFactory == null) {
182             if (context != null) {
183                 exchangeFactory = getDeliveryChannel().createExchangeFactory();
184             }
185         }
186         return exchangeFactory;
187     }
188
189     /**
190      * A helper method to indicate that the message exchange is complete
191      * which will set the status to {@link ExchangeStatus#DONE} and send the message
192      * on the delivery channel.
193      *
194      * @param exchange
195      * @throws MessagingException
196      */

197     public void done(MessageExchange exchange) throws MessagingException {
198         exchange.setStatus(ExchangeStatus.DONE);
199         getDeliveryChannel().send(exchange);
200     }
201
202     /**
203      * A helper method which fails and completes the given exchange with the specified fault
204      */

205     public void fail(MessageExchange exchange, Fault fault) throws MessagingException {
206         exchange.setFault(fault);
207         exchange.setStatus(ExchangeStatus.ERROR);
208         getDeliveryChannel().send(exchange);
209     }
210
211     /**
212      * A helper method which fails and completes the given exchange with the specified error
213      */

214     public void fail(MessageExchange exchange, Exception JavaDoc error) throws MessagingException {
215         exchange.setError(error);
216         if (error instanceof FaultException) {
217             FaultException faultException = (FaultException) error;
218             exchange.setFault(faultException.getFault());
219         }
220         getDeliveryChannel().send(exchange);
221     }
222
223     // Helper methods to make JBI a little more concise to use from a client
224
//-------------------------------------------------------------------------
225

226     public Object JavaDoc request(Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
227         return request(null, null, inMessageProperties, content);
228     }
229
230     public void send(Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
231         send(null, null, inMessageProperties, content);
232     }
233
234     public boolean sendSync(Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
235         return sendSync(null, null, inMessageProperties, content);
236     }
237
238     public void send(EndpointResolver resolver, Map JavaDoc exchangeProperties, Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
239         InOnly exchange = createInOnlyExchange(resolver);
240         populateMessage(exchange, exchangeProperties, inMessageProperties, content);
241         send(exchange);
242     }
243
244     public boolean sendSync(EndpointResolver resolver, Map JavaDoc exchangeProperties, Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
245         InOnly exchange = createInOnlyExchange(resolver);
246         populateMessage(exchange, exchangeProperties, inMessageProperties, content);
247         return sendSync(exchange);
248     }
249
250     public Object JavaDoc request(EndpointResolver resolver, Map JavaDoc exchangeProperties, Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
251         InOut exchange = createInOutExchange(resolver);
252         populateMessage(exchange, exchangeProperties, inMessageProperties, content);
253         boolean answer = sendSync(exchange);
254         if (!answer) {
255             throw new JBIException("Exchange aborted");
256         }
257         Exception JavaDoc error = exchange.getError();
258         if (error != null) {
259             throw new JBIException(error);
260         }
261         if (exchange.getFault() != null) {
262             throw FaultException.newInstance(exchange);
263         }
264
265
266         NormalizedMessage outMessage = exchange.getOutMessage();
267         if (outMessage == null) {
268             throw new NoOutMessageAvailableException(exchange);
269         }
270         return getMarshaler().unmarshal(exchange, outMessage);
271     }
272
273     public EndpointResolver createResolverForService(QName JavaDoc service) {
274         return new ServiceNameEndpointResolver(service);
275     }
276
277     public EndpointResolver createResolverInterface(QName JavaDoc interfaceName) {
278         return new InterfaceNameEndpointResolver(interfaceName);
279     }
280
281     public EndpointResolver createResolverForExternalService(QName JavaDoc service) {
282         return new ExternalServiceNameEndpointResolver(service);
283     }
284
285     public EndpointResolver createResolverForExternalInterface(QName JavaDoc interfaceName) {
286         return new ExternalInterfaceNameEndpointResolver(interfaceName);
287     }
288
289     public EndpointResolver createResolverForExternalInterface(QName JavaDoc service, String JavaDoc endpoint) {
290         return new ServiceAndEndpointNameResolver(service, endpoint);
291     }
292     
293     public void close() throws JBIException {
294     }
295
296
297     // Properties
298
//-------------------------------------------------------------------------
299
public EndpointFilter getFilter() {
300         return filter;
301     }
302
303     /**
304      * Sets the filter used to exclude possible endpoints based on their capabilities
305      *
306      * @param filter
307      */

308     public void setFilter(EndpointFilter filter) {
309         this.filter = filter;
310     }
311
312     public PojoMarshaler getMarshaler() {
313         return marshaler;
314     }
315
316     /**
317      * Sets the marshaler used to convert objects which are not already JAXP {@link Source} instances
318      * into the normalized message content.
319      *
320      * @param marshaler
321      */

322     public void setMarshaler(PojoMarshaler marshaler) {
323         this.marshaler = marshaler;
324     }
325
326     // Implementation methods
327
//-------------------------------------------------------------------------
328

329     protected void configureEndpoint(MessageExchange exchange, EndpointResolver resolver) throws JBIException {
330         if (resolver != null) {
331             exchange.setEndpoint(resolver.resolveEndpoint(getContext(), exchange, filter));
332         }
333     }
334
335     protected void populateMessage(MessageExchange exchange, Map JavaDoc exchangeProperties, Map JavaDoc inMessageProperties, Object JavaDoc content) throws MessagingException {
336         NormalizedMessage in = exchange.getMessage("in");
337         populateExchangeProperties(exchange, exchangeProperties);
338         populateMessageProperties(in, inMessageProperties);
339         getMarshaler().marshal(exchange, in, content);
340     }
341
342     protected void populateExchangeProperties(MessageExchange exchange, Map JavaDoc properties) {
343         if (properties != null) {
344             for (Iterator JavaDoc iter = properties.entrySet().iterator(); iter.hasNext();) {
345                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
346                 exchange.setProperty((String JavaDoc) entry.getKey(), entry.getValue());
347             }
348         }
349     }
350
351     protected void populateMessageProperties(NormalizedMessage message, Map JavaDoc properties) {
352         if (properties != null) {
353             for (Iterator JavaDoc iter = properties.entrySet().iterator(); iter.hasNext();) {
354                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
355                 message.setProperty((String JavaDoc) entry.getKey(), entry.getValue());
356             }
357         }
358     }
359 }
360
Popular Tags