KickJava   Java API By Example, From Geeks To Geeks.

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


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.ComponentSupport;
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.Fault;
39 import javax.jbi.messaging.InOnly;
40 import javax.jbi.messaging.InOptionalOut;
41 import javax.jbi.messaging.InOut;
42 import javax.jbi.messaging.MessageExchange;
43 import javax.jbi.messaging.MessageExchangeFactory;
44 import javax.jbi.messaging.MessagingException;
45 import javax.jbi.messaging.NormalizedMessage;
46 import javax.jbi.messaging.RobustInOnly;
47 import javax.xml.namespace.QName JavaDoc;
48 import javax.xml.transform.Source JavaDoc;
49
50 import java.util.Iterator JavaDoc;
51 import java.util.Map JavaDoc;
52
53 /**
54  * The default implementation of the {@link ServiceMixClient} API.
55  *
56  * @version $Revision: 429658 $
57  */

58 public class DefaultServiceMixClient extends ComponentSupport implements ServiceMixClient {
59
60     private EndpointFilter filter = NullEndpointFilter.getInstance();
61     private PojoMarshaler marshaler = new DefaultMarshaler();
62     private JBIContainer container;
63     private ActivationSpec activationSpec;
64
65     protected DefaultServiceMixClient() {
66     }
67     
68     /**
69      * Provides the JBI container used for message dispatch.
70      */

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

80     public DefaultServiceMixClient(JBIContainer container, ActivationSpec activationSpec) throws JBIException {
81         this.container = container;
82         this.activationSpec = activationSpec;
83         activationSpec.setComponent(this);
84         container.activateComponent(activationSpec);
85     }
86
87     public InOnly createInOnlyExchange() throws MessagingException {
88         InOnly exchange = getExchangeFactory().createInOnlyExchange();
89         NormalizedMessage in = exchange.createMessage();
90         exchange.setInMessage(in);
91         return exchange;
92     }
93
94     public InOnly createInOnlyExchange(EndpointResolver resolver) throws JBIException {
95         InOnly exchange = createInOnlyExchange();
96         configureEndpoint(exchange, resolver);
97         return exchange;
98     }
99
100     public InOut createInOutExchange() throws MessagingException {
101         InOut exchange = getExchangeFactory().createInOutExchange();
102         NormalizedMessage in = exchange.createMessage();
103         exchange.setInMessage(in);
104         return exchange;
105     }
106
107     public InOut createInOutExchange(EndpointResolver resolver) throws JBIException {
108         InOut exchange = createInOutExchange();
109         configureEndpoint(exchange, resolver);
110         return exchange;
111     }
112
113     public InOptionalOut createInOptionalOutExchange() throws MessagingException {
114         InOptionalOut exchange = getExchangeFactory().createInOptionalOutExchange();
115         NormalizedMessage in = exchange.createMessage();
116         exchange.setInMessage(in);
117         return exchange;
118     }
119
120     public InOptionalOut createInOptionalOutExchange(EndpointResolver resolver) throws JBIException {
121         InOptionalOut exchange = createInOptionalOutExchange();
122         configureEndpoint(exchange, resolver);
123         return exchange;
124     }
125
126     public RobustInOnly createRobustInOnlyExchange() throws MessagingException {
127         RobustInOnly exchange = getExchangeFactory().createRobustInOnlyExchange();
128         NormalizedMessage in = exchange.createMessage();
129         exchange.setInMessage(in);
130         return exchange;
131     }
132
133     public RobustInOnly createRobustInOnlyExchange(EndpointResolver resolver) throws JBIException {
134         RobustInOnly exchange = getExchangeFactory().createRobustInOnlyExchange();
135         configureEndpoint(exchange, resolver);
136         return exchange;
137     }
138
139     public Destination createDestination(String JavaDoc uri) throws MessagingException {
140         return new DefaultDestination(this, uri);
141     }
142     
143     public void send(MessageExchange exchange) throws MessagingException {
144         getDeliveryChannel().send(exchange);
145     }
146     
147     public void send(Message message) throws MessagingException {
148         send(message.getExchange());
149     }
150
151     public boolean sendSync(MessageExchange exchange) throws MessagingException {
152         return getDeliveryChannel().sendSync(exchange);
153     }
154
155     public boolean sendSync(MessageExchange exchange, long timeout) throws MessagingException {
156         return getDeliveryChannel().sendSync(exchange, timeout);
157     }
158
159     public MessageExchange receive() throws MessagingException {
160         return getDeliveryChannel().accept();
161     }
162
163     public MessageExchange receive(long timeout) throws MessagingException {
164         return getDeliveryChannel().accept(timeout);
165     }
166
167     public ComponentContext getContext() {
168         return super.getContext();
169     }
170
171     public DeliveryChannel getDeliveryChannel() throws MessagingException {
172         return super.getDeliveryChannel();
173     }
174
175     public MessageExchangeFactory getExchangeFactory() throws MessagingException {
176         return super.getExchangeFactory();
177     }
178
179     public void done(MessageExchange exchange) throws MessagingException {
180         super.done(exchange);
181     }
182
183     public void fail(MessageExchange exchange, Fault fault) throws MessagingException {
184         super.fail(exchange, fault);
185     }
186
187     public void fail(MessageExchange exchange, Exception JavaDoc error) throws MessagingException {
188         super.fail(exchange, error);
189     }
190
191     // Helper methods to make JBI a little more concise to use from a client
192
//-------------------------------------------------------------------------
193
public Object JavaDoc request(Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
194         return request(null, null, inMessageProperties, content);
195     }
196
197     public void send(Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
198         send(null, null, inMessageProperties, content);
199     }
200
201     public boolean sendSync(Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
202         return sendSync(null, null, inMessageProperties, content);
203     }
204
205     public void send(EndpointResolver resolver, Map JavaDoc exchangeProperties, Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
206         InOnly exchange = createInOnlyExchange(resolver);
207         populateMessage(exchange, exchangeProperties, inMessageProperties, content);
208         send(exchange);
209     }
210
211     public boolean sendSync(EndpointResolver resolver, Map JavaDoc exchangeProperties, Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
212         InOnly exchange = createInOnlyExchange(resolver);
213         populateMessage(exchange, exchangeProperties, inMessageProperties, content);
214         return sendSync(exchange);
215     }
216
217     public Object JavaDoc request(EndpointResolver resolver, Map JavaDoc exchangeProperties, Map JavaDoc inMessageProperties, Object JavaDoc content) throws JBIException {
218         InOut exchange = createInOutExchange(resolver);
219         populateMessage(exchange, exchangeProperties, inMessageProperties, content);
220         boolean answer = sendSync(exchange);
221         if (!answer) {
222             throw new JBIException("Exchange aborted");
223         }
224         Exception JavaDoc error = exchange.getError();
225         if (error != null) {
226             throw new JBIException(error);
227         }
228         if (exchange.getFault() != null) {
229             throw FaultException.newInstance(exchange);
230         }
231
232
233         NormalizedMessage outMessage = exchange.getOutMessage();
234         if (outMessage == null) {
235             throw new NoOutMessageAvailableException(exchange);
236         }
237         return getMarshaler().unmarshal(exchange, outMessage);
238     }
239
240     public EndpointResolver createResolverForService(QName JavaDoc service) {
241         return new ServiceNameEndpointResolver(service);
242     }
243
244     public EndpointResolver createResolverInterface(QName JavaDoc interfaceName) {
245         return new InterfaceNameEndpointResolver(interfaceName);
246     }
247
248     public EndpointResolver createResolverForExternalService(QName JavaDoc service) {
249         return new ExternalServiceNameEndpointResolver(service);
250     }
251
252     public EndpointResolver createResolverForExternalInterface(QName JavaDoc interfaceName) {
253         return new ExternalInterfaceNameEndpointResolver(interfaceName);
254     }
255
256     public EndpointResolver createResolverForExternalInterface(QName JavaDoc service, String JavaDoc endpoint) {
257         return new ServiceAndEndpointNameResolver(service, endpoint);
258     }
259     
260     public void close() throws JBIException {
261         container.deactivateComponent(activationSpec.getComponentName());
262     }
263
264
265     // Properties
266
//-------------------------------------------------------------------------
267
public EndpointFilter getFilter() {
268         return filter;
269     }
270
271     /**
272      * Sets the filter used to exclude possible endpoints based on their capabilities
273      *
274      * @param filter
275      */

276     public void setFilter(EndpointFilter filter) {
277         this.filter = filter;
278     }
279
280     public PojoMarshaler getMarshaler() {
281         return marshaler;
282     }
283
284     /**
285      * Sets the marshaler used to convert objects which are not already JAXP {@link Source} instances
286      * into the normalized message content.
287      *
288      * @param marshaler
289      */

290     public void setMarshaler(PojoMarshaler marshaler) {
291         this.marshaler = marshaler;
292     }
293
294
295     // Implementation methods
296
//-------------------------------------------------------------------------
297

298     protected void configureEndpoint(MessageExchange exchange, EndpointResolver resolver) throws JBIException {
299         if (resolver != null) {
300             exchange.setEndpoint(resolver.resolveEndpoint(getContext(), exchange, filter));
301         }
302     }
303
304     protected void populateMessage(MessageExchange exchange, Map JavaDoc exchangeProperties, Map JavaDoc inMessageProperties, Object JavaDoc content) throws MessagingException {
305         NormalizedMessage in = exchange.getMessage("in");
306         populateExchangeProperties(exchange, exchangeProperties);
307         populateMessageProperties(in, inMessageProperties);
308         getMarshaler().marshal(exchange, in, content);
309     }
310
311     protected void populateExchangeProperties(MessageExchange exchange, Map JavaDoc properties) {
312         if (properties != null) {
313             for (Iterator JavaDoc iter = properties.entrySet().iterator(); iter.hasNext();) {
314                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
315                 exchange.setProperty((String JavaDoc) entry.getKey(), entry.getValue());
316             }
317         }
318     }
319
320     protected void populateMessageProperties(NormalizedMessage message, Map JavaDoc properties) {
321         if (properties != null) {
322             for (Iterator JavaDoc iter = properties.entrySet().iterator(); iter.hasNext();) {
323                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
324                 message.setProperty((String JavaDoc) entry.getKey(), entry.getValue());
325             }
326         }
327     }
328 }
329
Popular Tags