package org.jboss.jms.client.container;
import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
import org.jboss.aop.joinpoint.MethodInvocation;
public class ClientIDInterceptor
implements Interceptor
{
private String clientID;
private boolean determinedClientID = false;
public String getName()
{
return "ClientIDInterceptor";
}
public Object invoke(Invocation invocation) throws Throwable
{
String methodName = ((MethodInvocation) invocation).getMethod().getName();
if (methodName.equals("getClientID"))
return clientID;
if (methodName.equals("setClientID"))
{
setClientID(invocation);
return null;
}
addClientID(invocation);
try
{
return invocation.invokeNext();
}
finally
{
if (determinedClientID == false)
{
clientID = (String) invocation.getResponseAttachment("JMSClientID");
if (clientID == null)
throw new IllegalStateException("Unable to determine clientID");
determinedClientID = true;
}
}
}
protected void addClientID(Invocation invocation)
throws Throwable
{
if (determinedClientID)
invocation.getMetaData().addMetaData("JMS", "clientID", clientID);
else
invocation.getMetaData().addMetaData("JMS", "clientID", null);
}
protected void setClientID(Invocation invocation)
throws Throwable
{
if (determinedClientID)
throw new IllegalStateException("Client id is already set");
MethodInvocation mi = (MethodInvocation) invocation;
clientID = (String) mi.getArguments()[0];
if (clientID == null)
throw new IllegalArgumentException("Null client id");
invocation.invokeNext();
determinedClientID = true;
}
}