/**
* <a href="http://servicemix.org">ServiceMix: The open source ESB</a>
*
* Copyright 2005 RAJD Consultancy Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix.jbi.messaging;
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
import org.servicemix.components.util.CopyTransformer;
import org.servicemix.jbi.framework.ComponentNameSpace;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.jbi.servicedesc.ServiceEndpoint;
import javax.transaction.Transaction;
import javax.xml.namespace.QName;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.net.URI;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* ExchangePacket is responsible for carrying MessageExchange payloads
*
* @version $Revision: 708 $
*/
public class ExchangePacket implements Externalizable {
private static final long serialVersionUID = -9110837382914609624L;
private URI pattern;
private String exchangeId;
private ComponentNameSpace destinationId;
private ComponentNameSpace sourceId;
private ExchangeStatus status = ExchangeStatus.ACTIVE;
private QName serviceName;
private QName interfaceName;
private QName operationName;
private Exception error;
private Map properties;
private Map messages;
private ServiceEndpoint endpoint;
private transient Transaction transactionContext;
private Boolean persistent;
private boolean aborted;
public ExchangePacket() {
}
public ExchangePacket(ExchangePacket packet) throws MessagingException {
this.destinationId = packet.destinationId;
this.endpoint = null; // packet.endpoint;
this.error = null;
this.exchangeId = null; //???;
this.interfaceName = packet.interfaceName;
CopyTransformer ct = new CopyTransformer();
if (packet.messages != null && packet.messages.size() > 0) {
for (Iterator it = packet.messages.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
NormalizedMessage copy = new NormalizedMessageImpl();
ct.transform(null, (NormalizedMessage) entry.getValue(), copy);
setMessage(copy, (String) entry.getKey());
}
}
this.operationName = packet.operationName;
this.pattern = packet.pattern;
if (packet.properties != null && packet.properties.size() > 0) {
getProperties().putAll(packet.properties);
}
this.serviceName = packet.serviceName;
this.sourceId = packet.sourceId;
this.status = packet.status;
this.transactionContext = packet.transactionContext;
this.persistent = packet.persistent;
}
/**
* @return Returns the endpoint.
*/
public ServiceEndpoint getEndpoint() {
return endpoint;
}
/**
* @param endpoint The endpoint to set.
*/
public void setEndpoint(ServiceEndpoint endpoint) {
this.endpoint = endpoint;
}
/**
* @return Returns the transactionContext.
*/
public Transaction getTransactionContext() {
return transactionContext;
}
/**
* @param transactionContext The transactionContext to set.
*/
public void setTransactionContext(Transaction transactionContext) {
this.transactionContext = transactionContext;
}
/**
* @return Returns the interfaceName.
*/
public QName getInterfaceName() {
return interfaceName;
}
/**
* @param interfaceName The interfaceName to set.
*/
public void setInterfaceName(QName interfaceName) {
this.interfaceName = interfaceName;
}
/**
* @return Returns the operationName.
*/
public QName getOperationName() {
return operationName;
}
/**
* @param operationName The operationName to set.
*/
public void setOperationName(QName operationName) {
this.operationName = operationName;
}
/**
* @return Returns the serviceName.
*/
public QName getServiceName() {
return serviceName;
}
/**
* @param serviceName The serviceName to set.
*/
public void setServiceName(QName serviceName) {
this.serviceName = serviceName;
}
/**
* @param status The status to set.
*/
public void setStatus(ExchangeStatus status) {
this.status = status;
}
/**
* @return the status
*/
public ExchangeStatus getStatus() {
return status;
}
/**
* @return Returns the pattern.
*/
public URI getPattern() {
return pattern;
}
/**
* @param pattern The pattern to set.
*/
public void setPattern(URI pattern) {
this.pattern = pattern;
}
/**
* @return Returns the error.
*/
public Exception getError() {
return error;
}
/**
* @param error The error to set.
*/
public void setError(Exception error) {
this.error = error;
this.status = ExchangeStatus.ERROR;
}
/**
* @return Returns the exchangeId.
*/
public String getExchangeId() {
return exchangeId;
}
/**
* @param exchangeId The exchangeId to set.
*/
public void setExchangeId(String exchangeId) {
this.exchangeId = exchangeId;
}
/**
* @return Returns the messages.
*/
public Map getMessages() {
if (messages == null) {
messages = new ConcurrentHashMap();
}
return messages;
}
/**
* get a NormalizedMessage based on the message reference
*
* @param name
* @return a NormalizedMessage
*/
public NormalizedMessage getMessage(String name) {
if (messages != null) {
return (NormalizedMessage) messages.get(name);
}
return null;
}
/**
* set a NormalizedMessage with a named reference
*
* @param message
* @param name
* @throws MessagingException
*/
public void setMessage(NormalizedMessage message, String name) throws MessagingException {
getMessages().put(name, message);
}
/**
* @return Returns the properties.
*/
public Map getProperties() {
if (properties == null) {
properties = new ConcurrentHashMap();
}
return properties;
}
/**
* @param name
* @return the proerty from the exchange
*/
public Object getProperty(String name) {
if (properties != null) {
return properties.get(name);
}
return null;
}
/**
* set a named property on the exchange
*
* @param name
* @param value
*/
public void setProperty(String name, Object value) {
if (value == null) {
if (properties != null) {
properties.remove(name);
}
} else {
getProperties().put(name, value);
}
}
/**
* @return property names
*/
public Set getPropertyNames() {
if (properties != null) {
return Collections.unmodifiableSet(properties.keySet());
}
return Collections.EMPTY_SET;
}
/**
* @return Returns the sourceId.
*/
public ComponentNameSpace getSourceId() {
return sourceId;
}
/**
* @param sourceId The sourceId to set.
*/
public void setSourceId(ComponentNameSpace sourceId) {
this.sourceId = sourceId;
}
/**
* @return Returns the destinationId.
*/
public ComponentNameSpace getDestinationId() {
return destinationId;
}
/**
* @param destinationId The destinationId to set.
*/
public void setDestinationId(ComponentNameSpace destinationId) {
this.destinationId = destinationId;
}
/**
* @return pretty print
*/
public String toString() {
return "ExchangePacket[: id=" + exchangeId + ", serviceDest=" + serviceName + ",endpoint=" + endpoint + "]";
}
/**
* Write to a Stream
* @param out
* @throws IOException
*/
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(pattern.toString());
out.writeUTF(exchangeId != null ? exchangeId : "");
out.writeUTF(status.toString());
out.writeObject(destinationId);
out.writeObject(sourceId);
out.writeObject(serviceName);
out.writeObject(interfaceName);
out.writeObject(error);
out.writeObject(properties);
out.writeObject(messages);
out.writeObject(endpoint);
out.writeByte((persistent == null) ? 0 : persistent.booleanValue() ? 1 : 2);
}
/**
* Read from a stream
*
* @param in
* @throws IOException
* @throws ClassNotFoundException
*/
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
pattern = URI.create(in.readUTF());
exchangeId = in.readUTF();
status = ExchangeStatus.valueOf(in.readUTF());
destinationId = (ComponentNameSpace) in.readObject();
sourceId = (ComponentNameSpace) in.readObject();
serviceName = (QName) in.readObject();
interfaceName = (QName) in.readObject();
error = (Exception) in.readObject();
properties = (Map) in.readObject();
messages = (Map) in.readObject();
endpoint = (ServiceEndpoint) in.readObject();
byte p = in.readByte();
persistent = (p == 0) ? null : p == 1 ? Boolean.TRUE : Boolean.FALSE;
}
/**
* Creates a copy of the packet so it can be sent to another destination
* @throws MessagingException
*/
public ExchangePacket copy() throws MessagingException {
return new ExchangePacket(this);
}
public Boolean getPersistent() {
return persistent;
}
public void setPersistent(Boolean persistent) {
this.persistent = persistent;
}
public boolean isAborted() {
return aborted;
}
public void setAborted(boolean timedOut) {
this.aborted = timedOut;
}
}