1 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 ; 48 import javax.xml.transform.Source ; 49 50 import java.util.Iterator ; 51 import java.util.Map ; 52 53 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 71 public DefaultServiceMixClient(JBIContainer container) throws JBIException { 72 this(container, new ActivationSpec()); 73 } 74 75 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 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 error) throws MessagingException { 188 super.fail(exchange, error); 189 } 190 191 public Object request(Map inMessageProperties, Object content) throws JBIException { 194 return request(null, null, inMessageProperties, content); 195 } 196 197 public void send(Map inMessageProperties, Object content) throws JBIException { 198 send(null, null, inMessageProperties, content); 199 } 200 201 public boolean sendSync(Map inMessageProperties, Object content) throws JBIException { 202 return sendSync(null, null, inMessageProperties, content); 203 } 204 205 public void send(EndpointResolver resolver, Map exchangeProperties, Map inMessageProperties, Object 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 exchangeProperties, Map inMessageProperties, Object content) throws JBIException { 212 InOnly exchange = createInOnlyExchange(resolver); 213 populateMessage(exchange, exchangeProperties, inMessageProperties, content); 214 return sendSync(exchange); 215 } 216 217 public Object request(EndpointResolver resolver, Map exchangeProperties, Map inMessageProperties, Object 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 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 service) { 241 return new ServiceNameEndpointResolver(service); 242 } 243 244 public EndpointResolver createResolverInterface(QName interfaceName) { 245 return new InterfaceNameEndpointResolver(interfaceName); 246 } 247 248 public EndpointResolver createResolverForExternalService(QName service) { 249 return new ExternalServiceNameEndpointResolver(service); 250 } 251 252 public EndpointResolver createResolverForExternalInterface(QName interfaceName) { 253 return new ExternalInterfaceNameEndpointResolver(interfaceName); 254 } 255 256 public EndpointResolver createResolverForExternalInterface(QName service, String endpoint) { 257 return new ServiceAndEndpointNameResolver(service, endpoint); 258 } 259 260 public void close() throws JBIException { 261 container.deactivateComponent(activationSpec.getComponentName()); 262 } 263 264 265 public EndpointFilter getFilter() { 268 return filter; 269 } 270 271 276 public void setFilter(EndpointFilter filter) { 277 this.filter = filter; 278 } 279 280 public PojoMarshaler getMarshaler() { 281 return marshaler; 282 } 283 284 290 public void setMarshaler(PojoMarshaler marshaler) { 291 this.marshaler = marshaler; 292 } 293 294 295 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 exchangeProperties, Map inMessageProperties, Object 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 properties) { 312 if (properties != null) { 313 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) { 314 Map.Entry entry = (Map.Entry ) iter.next(); 315 exchange.setProperty((String ) entry.getKey(), entry.getValue()); 316 } 317 } 318 } 319 320 protected void populateMessageProperties(NormalizedMessage message, Map properties) { 321 if (properties != null) { 322 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) { 323 Map.Entry entry = (Map.Entry ) iter.next(); 324 message.setProperty((String ) entry.getKey(), entry.getValue()); 325 } 326 } 327 } 328 } 329 | Popular Tags |