1 17 package org.apache.servicemix.jbi.messaging; 18 19 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; 20 21 import org.apache.activemq.util.IdGenerator; 22 import org.apache.servicemix.JbiConstants; 23 import org.apache.servicemix.jbi.framework.ComponentContextImpl; 24 25 import javax.jbi.messaging.InOnly; 26 import javax.jbi.messaging.InOptionalOut; 27 import javax.jbi.messaging.InOut; 28 import javax.jbi.messaging.MessageExchange; 29 import javax.jbi.messaging.MessageExchangeFactory; 30 import javax.jbi.messaging.MessagingException; 31 import javax.jbi.messaging.RobustInOnly; 32 import javax.jbi.servicedesc.ServiceEndpoint; 33 import javax.xml.namespace.QName ; 34 35 import java.net.URI ; 36 import java.util.Calendar ; 37 38 43 public class MessageExchangeFactoryImpl implements MessageExchangeFactory { 44 45 private QName interfaceName; 46 private QName serviceName; 47 private QName operationName; 48 private ServiceEndpoint endpoint; 49 private IdGenerator idGenerator; 50 private ComponentContextImpl context; 51 private AtomicBoolean closed; 52 53 57 public MessageExchangeFactoryImpl(IdGenerator idGen, AtomicBoolean closed){ 58 this.idGenerator = idGen; 59 this.closed = closed; 60 } 61 62 protected void checkNotClosed() throws MessagingException { 63 if (closed.get()) { 64 throw new MessagingException("DeliveryChannel has been closed."); 65 } 66 } 67 68 75 public MessageExchange createExchange(URI pattern) throws MessagingException { 76 checkNotClosed(); 77 MessageExchange result = null; 78 if (pattern != null) { 79 if (pattern.equals(MessageExchangeSupport.IN_ONLY)) { 80 result = createInOnlyExchange(); 81 } 82 else if (pattern.equals(MessageExchangeSupport.IN_OUT)) { 83 result = createInOutExchange(); 84 } 85 else if (pattern.equals(MessageExchangeSupport.IN_OPTIONAL_OUT)) { 86 result = createInOptionalOutExchange(); 87 } 88 else if (pattern.equals(MessageExchangeSupport.ROBUST_IN_ONLY)) { 89 result = createRobustInOnlyExchange(); 90 } 91 } 92 if (result == null) { 93 throw new MessagingException("Do not understand pattern: " + pattern); 94 } 95 return result; 96 } 97 98 104 public InOnly createInOnlyExchange() throws MessagingException { 105 checkNotClosed(); 106 InOnlyImpl result = new InOnlyImpl(getExchangeId()); 107 setDefaults(result); 108 return result; 109 } 110 111 117 public RobustInOnly createRobustInOnlyExchange() throws MessagingException { 118 checkNotClosed(); 119 RobustInOnlyImpl result = new RobustInOnlyImpl(getExchangeId()); 120 setDefaults(result); 121 return result; 122 } 123 124 130 public InOut createInOutExchange() throws MessagingException { 131 checkNotClosed(); 132 InOutImpl result = new InOutImpl(getExchangeId()); 133 setDefaults(result); 134 return result; 135 } 136 137 143 public InOptionalOut createInOptionalOutExchange() throws MessagingException { 144 checkNotClosed(); 145 InOptionalOutImpl result = new InOptionalOutImpl(getExchangeId()); 146 setDefaults(result); 147 return result; 148 } 149 150 160 public MessageExchange createExchange(QName serviceName, QName operationName) throws MessagingException { 161 checkNotClosed(); 163 InOptionalOutImpl me = new InOptionalOutImpl(getExchangeId()); 164 setDefaults(me); 165 me.setService(serviceName); 166 me.setOperation(operationName); 167 return me; 168 } 169 170 protected String getExchangeId() { 171 return idGenerator.generateId(); 172 } 173 174 177 public ServiceEndpoint getEndpoint() { 178 return endpoint; 179 } 180 181 185 public void setEndpoint(ServiceEndpoint endpoint) { 186 this.endpoint = endpoint; 187 } 188 189 192 public QName getInterfaceName() { 193 return interfaceName; 194 } 195 196 200 public void setInterfaceName(QName interfaceName) { 201 this.interfaceName = interfaceName; 202 } 203 204 207 public QName getServiceName() { 208 return serviceName; 209 } 210 211 215 public void setServiceName(QName serviceName) { 216 this.serviceName = serviceName; 217 } 218 219 222 public QName getOperationName() { 223 return operationName; 224 } 225 226 227 230 public void setOperationName(QName operationName) { 231 this.operationName = operationName; 232 } 233 234 238 public ComponentContextImpl getContext() { 239 return context; 240 } 241 242 246 public void setContext(ComponentContextImpl context) { 247 this.context = context; 248 } 249 250 protected void setDefaults(MessageExchangeImpl exchange) { 251 exchange.setOperation(getOperationName()); 252 if (endpoint != null) { 253 exchange.setEndpoint(getEndpoint()); 254 } else { 255 exchange.setService(serviceName); 256 exchange.setInterfaceName(interfaceName); 257 } 258 259 if (getContext() != null) { 260 exchange.setSourceContext(getContext()); 261 PojoMarshaler marshaler = getContext().getActivationSpec().getMarshaler(); 262 if (marshaler != null) { 263 exchange.setMarshaler(marshaler); 264 } 265 } 266 exchange.setProperty(JbiConstants.DATESTAMP_PROPERTY_NAME, Calendar.getInstance()); 267 } 268 } | Popular Tags |