1 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 ; 49 import javax.xml.transform.Source ; 50 51 import java.util.Iterator ; 52 import java.util.Map ; 53 54 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 74 public ServiceMixClientFacade(JBIContainer container) throws JBIException { 75 this(container, new ActivationSpec()); 76 } 77 78 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 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 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 197 public void done(MessageExchange exchange) throws MessagingException { 198 exchange.setStatus(ExchangeStatus.DONE); 199 getDeliveryChannel().send(exchange); 200 } 201 202 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 214 public void fail(MessageExchange exchange, Exception 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 226 public Object request(Map inMessageProperties, Object content) throws JBIException { 227 return request(null, null, inMessageProperties, content); 228 } 229 230 public void send(Map inMessageProperties, Object content) throws JBIException { 231 send(null, null, inMessageProperties, content); 232 } 233 234 public boolean sendSync(Map inMessageProperties, Object content) throws JBIException { 235 return sendSync(null, null, inMessageProperties, content); 236 } 237 238 public void send(EndpointResolver resolver, Map exchangeProperties, Map inMessageProperties, Object 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 exchangeProperties, Map inMessageProperties, Object content) throws JBIException { 245 InOnly exchange = createInOnlyExchange(resolver); 246 populateMessage(exchange, exchangeProperties, inMessageProperties, content); 247 return sendSync(exchange); 248 } 249 250 public Object request(EndpointResolver resolver, Map exchangeProperties, Map inMessageProperties, Object 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 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 service) { 274 return new ServiceNameEndpointResolver(service); 275 } 276 277 public EndpointResolver createResolverInterface(QName interfaceName) { 278 return new InterfaceNameEndpointResolver(interfaceName); 279 } 280 281 public EndpointResolver createResolverForExternalService(QName service) { 282 return new ExternalServiceNameEndpointResolver(service); 283 } 284 285 public EndpointResolver createResolverForExternalInterface(QName interfaceName) { 286 return new ExternalInterfaceNameEndpointResolver(interfaceName); 287 } 288 289 public EndpointResolver createResolverForExternalInterface(QName service, String endpoint) { 290 return new ServiceAndEndpointNameResolver(service, endpoint); 291 } 292 293 public void close() throws JBIException { 294 } 295 296 297 public EndpointFilter getFilter() { 300 return filter; 301 } 302 303 308 public void setFilter(EndpointFilter filter) { 309 this.filter = filter; 310 } 311 312 public PojoMarshaler getMarshaler() { 313 return marshaler; 314 } 315 316 322 public void setMarshaler(PojoMarshaler marshaler) { 323 this.marshaler = marshaler; 324 } 325 326 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 exchangeProperties, Map inMessageProperties, Object 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 properties) { 343 if (properties != null) { 344 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) { 345 Map.Entry entry = (Map.Entry ) iter.next(); 346 exchange.setProperty((String ) entry.getKey(), entry.getValue()); 347 } 348 } 349 } 350 351 protected void populateMessageProperties(NormalizedMessage message, Map properties) { 352 if (properties != null) { 353 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) { 354 Map.Entry entry = (Map.Entry ) iter.next(); 355 message.setProperty((String ) entry.getKey(), entry.getValue()); 356 } 357 } 358 } 359 } 360 | Popular Tags |