KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > eip > EIPEndpoint


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.eip;
18
19 import java.net.URL JavaDoc;
20
21 import javax.jbi.JBIException;
22 import javax.jbi.component.ComponentContext;
23 import javax.jbi.management.DeploymentException;
24 import javax.jbi.messaging.DeliveryChannel;
25 import javax.jbi.messaging.ExchangeStatus;
26 import javax.jbi.messaging.MessageExchange;
27 import javax.jbi.messaging.MessageExchangeFactory;
28 import javax.jbi.messaging.MessagingException;
29 import javax.jbi.messaging.MessageExchange.Role;
30 import javax.jbi.servicedesc.ServiceEndpoint;
31 import javax.wsdl.Definition;
32 import javax.wsdl.WSDLException;
33 import javax.wsdl.factory.WSDLFactory;
34 import javax.wsdl.xml.WSDLReader;
35
36 import org.apache.servicemix.JbiConstants;
37 import org.apache.servicemix.common.BaseLifeCycle;
38 import org.apache.servicemix.common.Endpoint;
39 import org.apache.servicemix.common.ExchangeProcessor;
40 import org.apache.servicemix.eip.support.ExchangeTarget;
41 import org.apache.servicemix.locks.LockManager;
42 import org.apache.servicemix.locks.impl.SimpleLockManager;
43 import org.apache.servicemix.store.Store;
44 import org.apache.servicemix.store.StoreFactory;
45 import org.apache.servicemix.store.memory.MemoryStoreFactory;
46 import org.apache.servicemix.timers.TimerManager;
47 import org.apache.servicemix.timers.impl.TimerManagerImpl;
48 import org.springframework.core.io.Resource;
49 import org.w3c.dom.Document JavaDoc;
50
51 import com.ibm.wsdl.Constants;
52
53 /**
54  * @author gnodet
55  * @version $Revision: 376451 $
56  */

57 public abstract class EIPEndpoint extends Endpoint implements ExchangeProcessor {
58
59     private ServiceEndpoint activated;
60     private DeliveryChannel channel;
61     private Resource wsdlResource;
62     
63     /**
64      * The store to keep pending exchanges
65      */

66     protected Store store;
67     /**
68      * The store factory.
69      */

70     protected StoreFactory storeFactory;
71     /**
72      * The lock manager.
73      */

74     protected LockManager lockManager;
75     /**
76      * The timer manager.
77      */

78     protected TimerManager timerManager;
79     /**
80      * The exchange factory
81      */

82     protected MessageExchangeFactory exchangeFactory;
83     
84     /**
85      * The ExchangeTarget to use to get the WSDL
86      */

87     protected ExchangeTarget wsdlExchangeTarget;
88     
89     /**
90      * @return Returns the exchangeFactory.
91      */

92     public MessageExchangeFactory getExchangeFactory() {
93         return exchangeFactory;
94     }
95     /**
96      * @param exchangeFactory The exchangeFactory to set.
97      */

98     public void setExchangeFactory(MessageExchangeFactory exchangeFactory) {
99         this.exchangeFactory = exchangeFactory;
100     }
101     /**
102      * @return Returns the store.
103      */

104     public Store getStore() {
105         return store;
106     }
107     /**
108      * @param store The store to set.
109      */

110     public void setStore(Store store) {
111         this.store = store;
112     }
113     /**
114      * @return Returns the storeFactory.
115      */

116     public StoreFactory getStoreFactory() {
117         return storeFactory;
118     }
119     /**
120      * @param storeFactory The storeFactory to set.
121      */

122     public void setStoreFactory(StoreFactory storeFactory) {
123         this.storeFactory = storeFactory;
124     }
125     /**
126      * @return the lockManager
127      */

128     public LockManager getLockManager() {
129         return lockManager;
130     }
131     /**
132      * @param lockManager the lockManager to set
133      */

134     public void setLockManager(LockManager lockManager) {
135         this.lockManager = lockManager;
136     }
137     /**
138      * @return the timerManager
139      */

140     public TimerManager getTimerManager() {
141         return timerManager;
142     }
143     /**
144      * @param timerManager the timerManager to set
145      */

146     public void setTimerManager(TimerManager timerManager) {
147         this.timerManager = timerManager;
148     }
149
150     /* (non-Javadoc)
151      * @see org.apache.servicemix.common.Endpoint#getRole()
152      */

153     public Role getRole() {
154         return Role.PROVIDER;
155     }
156
157     public void activate() throws Exception JavaDoc {
158         logger = this.serviceUnit.getComponent().getLogger();
159         ComponentContext ctx = getContext();
160         channel = ctx.getDeliveryChannel();
161         exchangeFactory = channel.createExchangeFactory();
162         activated = ctx.activateEndpoint(service, endpoint);
163         if (store == null) {
164             if (storeFactory == null) {
165                 storeFactory = new MemoryStoreFactory();
166             }
167             store = storeFactory.open(getService().toString() + getEndpoint());
168         }
169         if (lockManager == null) {
170             lockManager = new SimpleLockManager();
171         }
172         if (timerManager == null) {
173             timerManager = new TimerManagerImpl();
174         }
175         timerManager.start();
176         start();
177     }
178
179     public void deactivate() throws Exception JavaDoc {
180         if (timerManager != null) {
181             timerManager.stop();
182         }
183         stop();
184         ServiceEndpoint ep = activated;
185         activated = null;
186         ComponentContext ctx = getServiceUnit().getComponent().getComponentContext();
187         ctx.deactivateEndpoint(ep);
188     }
189
190     public ExchangeProcessor getProcessor() {
191         return this;
192     }
193     
194     public void validate() throws DeploymentException {
195     }
196     
197     protected ComponentContext getContext() {
198         return getServiceUnit().getComponent().getComponentContext();
199     }
200     
201     protected void send(MessageExchange me) throws MessagingException {
202         if (me.getRole() == MessageExchange.Role.CONSUMER &&
203             me.getStatus() == ExchangeStatus.ACTIVE) {
204             BaseLifeCycle lf = (BaseLifeCycle) getServiceUnit().getComponent().getLifeCycle();
205             lf.sendConsumerExchange(me, (Endpoint) this);
206         } else {
207             channel.send(me);
208         }
209     }
210     
211     protected void sendSync(MessageExchange me) throws MessagingException {
212         if (!channel.sendSync(me)) {
213             throw new MessagingException("SendSync failed");
214         }
215     }
216     
217     protected void done(MessageExchange me) throws MessagingException {
218         me.setStatus(ExchangeStatus.DONE);
219         send(me);
220     }
221     
222     protected void fail(MessageExchange me, Exception JavaDoc error) throws MessagingException {
223         me.setError(error);
224         send(me);
225     }
226     
227     public void start() throws Exception JavaDoc {
228     }
229     
230     public void stop() {
231     }
232
233     /* (non-Javadoc)
234      * @see org.apache.servicemix.common.ExchangeProcessor#process(javax.jbi.messaging.MessageExchange)
235      */

236     public void process(MessageExchange exchange) throws Exception JavaDoc {
237         boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC));
238         if (txSync && exchange.getRole() == Role.PROVIDER && exchange.getStatus() == ExchangeStatus.ACTIVE) {
239             processSync(exchange);
240         } else {
241             processAsync(exchange);
242         }
243     }
244     
245     /**
246      * @return Returns the description.
247      */

248     public Document JavaDoc getDescription() {
249         if( description == null ) {
250             definition = getDefinition();
251             if( definition!=null ) {
252                 try {
253                     description = WSDLFactory.newInstance().newWSDLWriter().getDocument(definition);
254                 } catch (WSDLException e) {
255                 }
256             }
257         }
258         return description;
259     }
260     
261     /**
262      * If the definition is not currently set, it tries to set it using
263      * the following sources in the order:
264      * description, wsdlResource, wsdlExchangeTarget
265      *
266      * @return Returns the definition.
267      */

268     public Definition getDefinition() {
269         if( definition == null ) {
270             definition = getDefinitionFromDescription();
271             if( definition == null ) {
272                 definition = getDefinitionFromWsdlResource();
273                 if( definition == null ) {
274                     definition = getDefinitionFromWsdlExchangeTarget();
275                 }
276             }
277         }
278         return definition;
279     }
280     
281     protected Definition getDefinitionFromDescription() {
282         if( description!=null ) {
283             try {
284                 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
285                 reader.setFeature(Constants.FEATURE_VERBOSE, false);
286                 return reader.readWSDL(null, description);
287             } catch (WSDLException ignore) {
288             }
289         }
290         return null;
291     }
292
293     protected Definition getDefinitionFromWsdlResource() {
294         if( wsdlResource!=null ) {
295             try {
296                 URL JavaDoc resource = wsdlResource.getURL();
297                 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
298                 reader.setFeature(Constants.FEATURE_VERBOSE, false);
299                 return reader.readWSDL(null, resource.toString());
300             } catch (Throwable JavaDoc ignore) {
301             }
302         }
303         return null;
304     }
305         
306     protected Definition getDefinitionFromWsdlExchangeTarget() {
307         if( wsdlExchangeTarget != null ) {
308             try {
309                 Document JavaDoc description = getDescriptionForExchangeTarget(wsdlExchangeTarget);
310                 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
311                 reader.setFeature(Constants.FEATURE_VERBOSE, false);
312                 return reader.readWSDL(null, description);
313             } catch (Throwable JavaDoc ignore) {
314             }
315         }
316         return null;
317     }
318     
319     /**
320      * @return Returns the wsdl's Resource.
321      */

322     public Resource getWsdlResource() {
323         return wsdlResource;
324     }
325     public void setWsdlResource(Resource wsdlResource) {
326         this.wsdlResource = wsdlResource;
327     }
328     
329     protected Document JavaDoc getDescriptionForExchangeTarget(ExchangeTarget match) throws JBIException {
330         ServiceEndpoint[] endpoints = getEndpointsForExchangeTarget(match);
331         if (endpoints == null || endpoints.length == 0) {
332             return null;
333         }
334         ServiceEndpoint endpoint = chooseFirstEndpointWithDescriptor(endpoints);
335         if (endpoint == null) {
336             return null;
337         }
338         return getContext().getEndpointDescriptor(endpoint);
339     }
340     
341     /**
342      *
343      * @param match
344      * @return an ServiceEndpoint[] of all the endpoints that matched.
345      * @throws JBIException
346      */

347     protected ServiceEndpoint[] getEndpointsForExchangeTarget(ExchangeTarget match) throws JBIException {
348         ServiceEndpoint[] endpoints;
349         if (match.getEndpoint() != null && match.getService() != null) {
350             ServiceEndpoint endpoint = getContext().getEndpoint(match.getService(), match.getEndpoint());
351             if (endpoint == null) {
352                 endpoints = new ServiceEndpoint[0];
353             } else {
354                 endpoints = new ServiceEndpoint[] { endpoint };
355             }
356         } else if (match.getService() != null) {
357             endpoints = getContext().getEndpointsForService(match.getService());
358         } else if (interfaceName != null) {
359             endpoints = getContext().getEndpoints(interfaceName);
360         } else {
361             throw new IllegalStateException JavaDoc("One of interfaceName or serviceName should be provided");
362         }
363         return endpoints;
364     }
365     
366     protected ServiceEndpoint chooseFirstEndpointWithDescriptor(ServiceEndpoint[] endpoints) throws JBIException {
367         for (int i = 0; i < endpoints.length; i++) {
368             if (getContext().getEndpointDescriptor(endpoints[i]) != null) {
369                 return endpoints[i];
370             }
371         }
372         return null;
373     }
374
375
376     protected abstract void processAsync(MessageExchange exchange) throws Exception JavaDoc;
377
378     protected abstract void processSync(MessageExchange exchange) throws Exception JavaDoc;
379     
380     public ExchangeTarget getWsdlExchangeTarget() {
381         return wsdlExchangeTarget;
382     }
383     public void setWsdlExchangeTarget(ExchangeTarget wsdlExchangeTarget) {
384         this.wsdlExchangeTarget = wsdlExchangeTarget;
385     }
386     
387 }
388
Popular Tags