1 17 package org.apache.servicemix.eip; 18 19 import java.net.URL ; 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 ; 50 51 import com.ibm.wsdl.Constants; 52 53 57 public abstract class EIPEndpoint extends Endpoint implements ExchangeProcessor { 58 59 private ServiceEndpoint activated; 60 private DeliveryChannel channel; 61 private Resource wsdlResource; 62 63 66 protected Store store; 67 70 protected StoreFactory storeFactory; 71 74 protected LockManager lockManager; 75 78 protected TimerManager timerManager; 79 82 protected MessageExchangeFactory exchangeFactory; 83 84 87 protected ExchangeTarget wsdlExchangeTarget; 88 89 92 public MessageExchangeFactory getExchangeFactory() { 93 return exchangeFactory; 94 } 95 98 public void setExchangeFactory(MessageExchangeFactory exchangeFactory) { 99 this.exchangeFactory = exchangeFactory; 100 } 101 104 public Store getStore() { 105 return store; 106 } 107 110 public void setStore(Store store) { 111 this.store = store; 112 } 113 116 public StoreFactory getStoreFactory() { 117 return storeFactory; 118 } 119 122 public void setStoreFactory(StoreFactory storeFactory) { 123 this.storeFactory = storeFactory; 124 } 125 128 public LockManager getLockManager() { 129 return lockManager; 130 } 131 134 public void setLockManager(LockManager lockManager) { 135 this.lockManager = lockManager; 136 } 137 140 public TimerManager getTimerManager() { 141 return timerManager; 142 } 143 146 public void setTimerManager(TimerManager timerManager) { 147 this.timerManager = timerManager; 148 } 149 150 153 public Role getRole() { 154 return Role.PROVIDER; 155 } 156 157 public void activate() throws Exception { 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 { 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 error) throws MessagingException { 223 me.setError(error); 224 send(me); 225 } 226 227 public void start() throws Exception { 228 } 229 230 public void stop() { 231 } 232 233 236 public void process(MessageExchange exchange) throws Exception { 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 248 public Document 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 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 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 ignore) { 301 } 302 } 303 return null; 304 } 305 306 protected Definition getDefinitionFromWsdlExchangeTarget() { 307 if( wsdlExchangeTarget != null ) { 308 try { 309 Document 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 ignore) { 314 } 315 } 316 return null; 317 } 318 319 322 public Resource getWsdlResource() { 323 return wsdlResource; 324 } 325 public void setWsdlResource(Resource wsdlResource) { 326 this.wsdlResource = wsdlResource; 327 } 328 329 protected Document 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 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 ("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 ; 377 378 protected abstract void processSync(MessageExchange exchange) throws Exception ; 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 |