package org.jboss.mq;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import javax.jms.Destination;
public class SpyDestination implements Destination, Serializable
{
static final long serialVersionUID = -451227938651163471L;
protected final static int NULL = 0;
protected final static int OBJECT = 1;
protected final static int SPY_QUEUE = 2;
protected final static int SPY_TOPIC = 3;
protected final static int SPY_TEMP_QUEUE = 4;
protected final static int SPY_TEMP_TOPIC = 5;
protected String name;
protected int hash;
public static void writeDest(ObjectOutput out, Destination dest) throws IOException
{
if (dest == null)
out.writeByte(NULL);
else if (dest instanceof SpyTemporaryQueue)
{
out.writeByte(SPY_TEMP_QUEUE);
out.writeUTF(((SpyTemporaryQueue) dest).getName());
}
else if (dest instanceof SpyTemporaryTopic)
{
out.writeByte(SPY_TEMP_TOPIC);
out.writeUTF(((SpyTemporaryTopic) dest).getName());
}
else if (dest instanceof SpyQueue)
{
out.writeByte(SPY_QUEUE);
out.writeUTF(((SpyQueue) dest).getName());
}
else if (dest instanceof SpyTopic)
{
out.writeByte(SPY_TOPIC);
out.writeUTF(((SpyTopic) dest).getName());
DurableSubscriptionID id = ((SpyTopic) dest).durableSubscriptionID;
if (id == null)
{
out.writeByte(NULL);
}
else
{
out.writeByte(OBJECT);
writeString(out, id.getClientID());
writeString(out, id.getSubscriptionName());
writeString(out, id.getSelector());
}
}
else
{
out.writeByte(OBJECT);
out.writeObject(dest);
}
}
public static Destination readDest(ObjectInput in) throws IOException
{
byte destType = in.readByte();
if (destType == NULL)
return null;
else if (destType == SPY_TEMP_QUEUE)
return new SpyTemporaryQueue(in.readUTF(), null);
else if (destType == SPY_TEMP_TOPIC)
return new SpyTemporaryTopic(in.readUTF(), null);
else if (destType == SPY_QUEUE)
return new SpyQueue(in.readUTF());
else if (destType == SPY_TOPIC)
{
String name = in.readUTF();
destType = in.readByte();
if (destType == NULL)
return new SpyTopic(name);
else
{
String clientId = readString(in);
String subName = readString(in);
String selector = readString(in);
return new SpyTopic(new SpyTopic(name), clientId, subName, selector);
}
}
else
{
try
{
return (Destination) in.readObject();
}
catch (ClassNotFoundException e)
{
throw new IOException("Class not found for unknown destination.");
}
}
}
SpyDestination(String name)
{
this.name = name;
hash = name.hashCode();
}
public String getName()
{
return name;
}
public int hashCode()
{
return hash;
}
private static void writeString(ObjectOutput out, String s) throws IOException
{
if (s == null)
out.writeByte(NULL);
else
{
out.writeByte(OBJECT);
out.writeUTF(s);
}
}
private static String readString(ObjectInput in) throws IOException
{
byte b = in.readByte();
if (b == NULL)
return null;
else
return in.readUTF();
}
}