001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.eip;
018:
019: import java.net.URL;
020:
021: import javax.jbi.JBIException;
022: import javax.jbi.messaging.ExchangeStatus;
023: import javax.jbi.messaging.MessageExchange;
024: import javax.jbi.messaging.MessageExchange.Role;
025: import javax.jbi.servicedesc.ServiceEndpoint;
026: import javax.wsdl.Definition;
027: import javax.wsdl.WSDLException;
028: import javax.wsdl.factory.WSDLFactory;
029: import javax.wsdl.xml.WSDLReader;
030:
031: import org.w3c.dom.Document;
032:
033: import com.ibm.wsdl.Constants;
034:
035: import org.apache.servicemix.JbiConstants;
036: import org.apache.servicemix.common.endpoints.ProviderEndpoint;
037: import org.apache.servicemix.eip.support.ExchangeTarget;
038: import org.apache.servicemix.locks.LockManager;
039: import org.apache.servicemix.locks.impl.SimpleLockManager;
040: import org.apache.servicemix.store.Store;
041: import org.apache.servicemix.store.StoreFactory;
042: import org.apache.servicemix.store.memory.MemoryStoreFactory;
043: import org.apache.servicemix.timers.TimerManager;
044: import org.apache.servicemix.timers.impl.TimerManagerImpl;
045: import org.springframework.core.io.Resource;
046:
047: /**
048: * @author gnodet
049: * @version $Revision: 376451 $
050: */
051: public abstract class EIPEndpoint extends ProviderEndpoint {
052:
053: /**
054: * The resource pointing to the WSDL for this endpoint
055: */
056: protected Resource wsdlResource;
057:
058: /**
059: * The store to keep pending exchanges
060: */
061: protected Store store;
062: /**
063: * The store factory.
064: */
065: protected StoreFactory storeFactory;
066: /**
067: * The lock manager.
068: */
069: protected LockManager lockManager;
070: /**
071: * The timer manager.
072: */
073: protected TimerManager timerManager;
074:
075: /**
076: * The ExchangeTarget to use to get the WSDL
077: */
078: protected ExchangeTarget wsdlExchangeTarget;
079:
080: /**
081: * @return Returns the store.
082: */
083: public Store getStore() {
084: return store;
085: }
086:
087: /**
088: * @param store The store to set.
089: */
090: public void setStore(Store store) {
091: this .store = store;
092: }
093:
094: /**
095: * @return Returns the storeFactory.
096: */
097: public StoreFactory getStoreFactory() {
098: return storeFactory;
099: }
100:
101: /**
102: * @param storeFactory The storeFactory to set.
103: */
104: public void setStoreFactory(StoreFactory storeFactory) {
105: this .storeFactory = storeFactory;
106: }
107:
108: /**
109: * @return the lockManager
110: */
111: public LockManager getLockManager() {
112: return lockManager;
113: }
114:
115: /**
116: * @param lockManager the lockManager to set
117: */
118: public void setLockManager(LockManager lockManager) {
119: this .lockManager = lockManager;
120: }
121:
122: /**
123: * @return the timerManager
124: */
125: public TimerManager getTimerManager() {
126: return timerManager;
127: }
128:
129: /**
130: * @param timerManager the timerManager to set
131: */
132: public void setTimerManager(TimerManager timerManager) {
133: this .timerManager = timerManager;
134: }
135:
136: public void start() throws Exception {
137: super .start();
138: if (store == null) {
139: if (storeFactory == null) {
140: storeFactory = new MemoryStoreFactory();
141: }
142: store = storeFactory.open(getService().toString()
143: + getEndpoint());
144: }
145: if (lockManager == null) {
146: lockManager = new SimpleLockManager();
147: }
148: if (timerManager == null) {
149: timerManager = new TimerManagerImpl();
150: }
151: timerManager.start();
152: }
153:
154: public void stop() throws Exception {
155: if (timerManager != null) {
156: timerManager.stop();
157: }
158: super .stop();
159: }
160:
161: /* (non-Javadoc)
162: * @see org.apache.servicemix.common.ExchangeProcessor#process(javax.jbi.messaging.MessageExchange)
163: */
164: public void process(MessageExchange exchange) throws Exception {
165: boolean txSync = exchange.isTransacted()
166: && Boolean.TRUE.equals(exchange
167: .getProperty(JbiConstants.SEND_SYNC));
168: if (txSync && exchange.getRole() == Role.PROVIDER
169: && exchange.getStatus() == ExchangeStatus.ACTIVE) {
170: processSync(exchange);
171: } else {
172: processAsync(exchange);
173: }
174: }
175:
176: /**
177: * @return Returns the description.
178: */
179: public Document getDescription() {
180: if (description == null) {
181: definition = getDefinition();
182: if (definition != null) {
183: try {
184: description = WSDLFactory.newInstance()
185: .newWSDLWriter().getDocument(definition);
186: } catch (WSDLException e) {
187: // Ignore
188: }
189: }
190: }
191: return description;
192: }
193:
194: /**
195: * If the definition is not currently set, it tries to set it using
196: * the following sources in the order:
197: * description, wsdlResource, wsdlExchangeTarget
198: *
199: * @return Returns the definition.
200: */
201: public Definition getDefinition() {
202: if (definition == null) {
203: definition = getDefinitionFromDescription();
204: if (definition == null) {
205: definition = getDefinitionFromWsdlResource();
206: if (definition == null) {
207: definition = getDefinitionFromWsdlExchangeTarget();
208: }
209: }
210: }
211: return definition;
212: }
213:
214: protected Definition getDefinitionFromDescription() {
215: if (description != null) {
216: try {
217: WSDLReader reader = WSDLFactory.newInstance()
218: .newWSDLReader();
219: reader.setFeature(Constants.FEATURE_VERBOSE, false);
220: return reader.readWSDL(null, description);
221: } catch (WSDLException ignore) {
222: // Ignore
223: }
224: }
225: return null;
226: }
227:
228: protected Definition getDefinitionFromWsdlResource() {
229: if (wsdlResource != null) {
230: try {
231: URL resource = wsdlResource.getURL();
232: WSDLReader reader = WSDLFactory.newInstance()
233: .newWSDLReader();
234: reader.setFeature(Constants.FEATURE_VERBOSE, false);
235: return reader.readWSDL(null, resource.toString());
236: } catch (Throwable ignore) {
237: // Ignore
238: }
239: }
240: return null;
241: }
242:
243: protected Definition getDefinitionFromWsdlExchangeTarget() {
244: if (wsdlExchangeTarget != null) {
245: try {
246: Document description = getDescriptionForExchangeTarget(wsdlExchangeTarget);
247: WSDLReader reader = WSDLFactory.newInstance()
248: .newWSDLReader();
249: reader.setFeature(Constants.FEATURE_VERBOSE, false);
250: return reader.readWSDL(null, description);
251: } catch (Throwable ignore) {
252: // Ignore
253: }
254: }
255: return null;
256: }
257:
258: /**
259: * @return Returns the wsdl's Resource.
260: */
261: public Resource getWsdlResource() {
262: return wsdlResource;
263: }
264:
265: public void setWsdlResource(Resource wsdlResource) {
266: this .wsdlResource = wsdlResource;
267: }
268:
269: protected Document getDescriptionForExchangeTarget(
270: ExchangeTarget match) throws JBIException {
271: ServiceEndpoint[] endpoints = getEndpointsForExchangeTarget(match);
272: if (endpoints == null || endpoints.length == 0) {
273: return null;
274: }
275: ServiceEndpoint endpoint = chooseFirstEndpointWithDescriptor(endpoints);
276: if (endpoint == null) {
277: return null;
278: }
279: return getContext().getEndpointDescriptor(endpoint);
280: }
281:
282: /**
283: *
284: * @param match
285: * @return an ServiceEndpoint[] of all the endpoints that matched.
286: * @throws JBIException
287: */
288: protected ServiceEndpoint[] getEndpointsForExchangeTarget(
289: ExchangeTarget match) throws JBIException {
290: ServiceEndpoint[] endpoints;
291: if (match.getEndpoint() != null && match.getService() != null) {
292: ServiceEndpoint endpoint = getContext().getEndpoint(
293: match.getService(), match.getEndpoint());
294: if (endpoint == null) {
295: endpoints = new ServiceEndpoint[0];
296: } else {
297: endpoints = new ServiceEndpoint[] { endpoint };
298: }
299: } else if (match.getService() != null) {
300: endpoints = getContext().getEndpointsForService(
301: match.getService());
302: } else if (interfaceName != null) {
303: endpoints = getContext().getEndpoints(interfaceName);
304: } else {
305: throw new IllegalStateException(
306: "One of interfaceName or serviceName should be provided");
307: }
308: return endpoints;
309: }
310:
311: protected ServiceEndpoint chooseFirstEndpointWithDescriptor(
312: ServiceEndpoint[] endpoints) throws JBIException {
313: for (int i = 0; i < endpoints.length; i++) {
314: if (getContext().getEndpointDescriptor(endpoints[i]) != null) {
315: return endpoints[i];
316: }
317: }
318: return null;
319: }
320:
321: protected abstract void processAsync(MessageExchange exchange)
322: throws Exception;
323:
324: protected abstract void processSync(MessageExchange exchange)
325: throws Exception;
326:
327: public ExchangeTarget getWsdlExchangeTarget() {
328: return wsdlExchangeTarget;
329: }
330:
331: public void setWsdlExchangeTarget(ExchangeTarget wsdlExchangeTarget) {
332: this.wsdlExchangeTarget = wsdlExchangeTarget;
333: }
334:
335: }
|