1 17 package org.apache.servicemix.jbi.messaging; 18 19 import org.apache.servicemix.components.util.CopyTransformer; 20 import org.apache.servicemix.jbi.framework.ComponentNameSpace; 21 22 import javax.jbi.messaging.ExchangeStatus; 23 import javax.jbi.messaging.Fault; 24 import javax.jbi.messaging.MessagingException; 25 import javax.jbi.messaging.NormalizedMessage; 26 import javax.jbi.servicedesc.ServiceEndpoint; 27 import javax.transaction.Transaction ; 28 import javax.xml.namespace.QName ; 29 30 import java.io.ByteArrayInputStream ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.Externalizable ; 33 import java.io.IOException ; 34 import java.io.ObjectInput ; 35 import java.io.ObjectInputStream ; 36 import java.io.ObjectOutput ; 37 import java.io.ObjectOutputStream ; 38 import java.net.URI ; 39 import java.util.Collections ; 40 import java.util.HashMap ; 41 import java.util.Map ; 42 import java.util.Set ; 43 44 49 public class ExchangePacket implements Externalizable { 50 51 private static final long serialVersionUID = -9110837382914609624L; 52 53 protected URI pattern; 54 protected String exchangeId; 55 protected ComponentNameSpace destinationId; 56 protected ComponentNameSpace sourceId; 57 protected ExchangeStatus status = ExchangeStatus.ACTIVE; 58 protected QName serviceName; 59 protected QName interfaceName; 60 protected QName operationName; 61 protected Exception error; 62 protected Map properties; 63 protected NormalizedMessageImpl in; 64 protected NormalizedMessageImpl out; 65 protected FaultImpl fault; 66 protected ServiceEndpoint endpoint; 67 protected transient Transaction transactionContext; 68 protected Boolean persistent; 69 protected boolean aborted; 70 71 72 public ExchangePacket() { 73 } 74 75 public ExchangePacket(ExchangePacket packet) throws MessagingException { 76 this.destinationId = packet.destinationId; 77 this.endpoint = null; this.error = null; 79 this.exchangeId = null; this.interfaceName = packet.interfaceName; 81 CopyTransformer ct = new CopyTransformer(); 82 if (packet.in != null) { 83 in = new NormalizedMessageImpl(); 84 ct.transform(null, packet.in, in); 85 } 86 if (packet.out != null) { 87 out = new NormalizedMessageImpl(); 88 ct.transform(null, packet.out, out); 89 } 90 if (packet.fault != null) { 91 fault = new FaultImpl(); 92 ct.transform(null, packet.fault, fault); 93 } 94 this.operationName = packet.operationName; 95 this.pattern = packet.pattern; 96 if (packet.properties != null && packet.properties.size() > 0) { 97 getProperties().putAll(packet.properties); 98 } 99 this.serviceName = packet.serviceName; 100 this.sourceId = packet.sourceId; 101 this.status = packet.status; 102 this.transactionContext = packet.transactionContext; 103 this.persistent = packet.persistent; 104 } 105 106 109 public ServiceEndpoint getEndpoint() { 110 return endpoint; 111 } 112 113 116 public void setEndpoint(ServiceEndpoint endpoint) { 117 this.endpoint = endpoint; 118 } 119 120 123 public Transaction getTransactionContext() { 124 return transactionContext; 125 } 126 127 130 public void setTransactionContext(Transaction transactionContext) { 131 this.transactionContext = transactionContext; 132 } 133 134 137 public QName getInterfaceName() { 138 return interfaceName; 139 } 140 141 144 public void setInterfaceName(QName interfaceName) { 145 this.interfaceName = interfaceName; 146 } 147 148 151 public QName getOperationName() { 152 return operationName; 153 } 154 155 158 public void setOperationName(QName operationName) { 159 this.operationName = operationName; 160 } 161 162 165 public QName getServiceName() { 166 return serviceName; 167 } 168 169 172 public void setServiceName(QName serviceName) { 173 this.serviceName = serviceName; 174 } 175 176 179 public void setStatus(ExchangeStatus status) { 180 this.status = status; 181 } 182 183 186 public ExchangeStatus getStatus() { 187 return status; 188 } 189 190 193 public URI getPattern() { 194 return pattern; 195 } 196 197 200 public void setPattern(URI pattern) { 201 this.pattern = pattern; 202 } 203 204 207 public Exception getError() { 208 return error; 209 } 210 211 214 public void setError(Exception error) { 215 this.error = error; 216 this.status = ExchangeStatus.ERROR; 217 } 218 219 222 public String getExchangeId() { 223 return exchangeId; 224 } 225 226 229 public void setExchangeId(String exchangeId) { 230 this.exchangeId = exchangeId; 231 } 232 233 236 public Map getProperties() { 237 if (properties == null) { 238 properties = new HashMap (); 241 } 242 return properties; 243 } 244 245 249 public Object getProperty(String name) { 250 if (properties != null) { 251 return properties.get(name); 252 } 253 return null; 254 } 255 256 262 public void setProperty(String name, Object value) { 263 if (value == null) { 264 if (properties != null) { 265 properties.remove(name); 266 } 267 } else { 268 getProperties().put(name, value); 269 } 270 } 271 272 275 public Set getPropertyNames() { 276 if (properties != null) { 277 return Collections.unmodifiableSet(properties.keySet()); 278 } 279 return Collections.EMPTY_SET; 280 } 281 282 285 public ComponentNameSpace getSourceId() { 286 return sourceId; 287 } 288 289 292 public void setSourceId(ComponentNameSpace sourceId) { 293 this.sourceId = sourceId; 294 } 295 296 299 public ComponentNameSpace getDestinationId() { 300 return destinationId; 301 } 302 303 306 public void setDestinationId(ComponentNameSpace destinationId) { 307 this.destinationId = destinationId; 308 } 309 310 313 public Fault getFault() { 314 return fault; 315 } 316 317 320 public void setFault(FaultImpl fault) { 321 this.fault = fault; 322 } 323 324 327 public NormalizedMessage getIn() { 328 return in; 329 } 330 331 334 public void setIn(NormalizedMessageImpl in) { 335 this.in = in; 336 } 337 338 341 public NormalizedMessage getOut() { 342 return out; 343 } 344 345 348 public void setOut(NormalizedMessageImpl out) { 349 this.out = out; 350 } 351 352 355 public String toString() { 356 return "ExchangePacket[: id=" + exchangeId + ", serviceDest=" + serviceName + ",endpoint=" + endpoint + "]"; 357 } 358 359 364 public void writeExternal(ObjectOutput output) throws IOException { 365 output.writeUTF(pattern.toString()); 366 output.writeUTF(exchangeId != null ? exchangeId : ""); 367 output.writeUTF(status.toString()); 368 output.writeObject(destinationId); 369 output.writeObject(sourceId); 370 output.writeObject(serviceName); 371 output.writeObject(interfaceName); 372 output.writeObject(operationName); 373 output.writeObject(error); 374 output.writeObject(properties); 375 output.writeObject(in); 376 output.writeObject(out); 377 output.writeObject(fault); 378 output.writeObject(endpoint); 379 output.writeByte((persistent == null) ? 0 : persistent.booleanValue() ? 1 : 2); 380 } 381 382 389 public void readExternal(ObjectInput input) throws IOException , ClassNotFoundException { 390 pattern = URI.create(input.readUTF()); 391 exchangeId = input.readUTF(); 392 status = ExchangeStatus.valueOf(input.readUTF()); 393 destinationId = (ComponentNameSpace) input.readObject(); 394 sourceId = (ComponentNameSpace) input.readObject(); 395 serviceName = (QName ) input.readObject(); 396 interfaceName = (QName ) input.readObject(); 397 operationName = (QName ) input.readObject(); 398 error = (Exception ) input.readObject(); 399 properties = (Map ) input.readObject(); 400 in = (NormalizedMessageImpl) input.readObject(); 401 out = (NormalizedMessageImpl) input.readObject(); 402 fault = (FaultImpl) input.readObject(); 403 endpoint = (ServiceEndpoint) input.readObject(); 404 byte p = input.readByte(); 405 persistent = (p == 0) ? null : p == 1 ? Boolean.TRUE : Boolean.FALSE; 406 } 407 408 412 public ExchangePacket copy() throws MessagingException { 413 return new ExchangePacket(this); 414 } 415 416 public Boolean getPersistent() { 417 return persistent; 418 } 419 420 public void setPersistent(Boolean persistent) { 421 this.persistent = persistent; 422 } 423 424 public boolean isAborted() { 425 return aborted; 426 } 427 428 public void setAborted(boolean timedOut) { 429 this.aborted = timedOut; 430 } 431 432 437 public byte[] getData() throws IOException { 438 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 439 ObjectOutputStream out = new ObjectOutputStream (buffer); 440 out.writeObject(this); 441 out.close(); 442 return buffer.toByteArray(); 443 } 444 445 452 public static ExchangePacket readPacket(byte[] data) throws IOException , ClassNotFoundException { 453 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (data)); 454 return (ExchangePacket) ois.readObject(); 455 } 456 457 } | Popular Tags |