package org.jboss.remoting;
import org.jboss.logging.Logger;
import org.jboss.remoting.invocation.InternalInvocation;
import org.jboss.remoting.transport.ClientInvoker;
import org.jboss.util.id.GUID;
import java.io.Serializable;
import java.util.List;
public class ClientInvokerAdapter
implements ClientInterceptor, Serializable
{
static final long serialVersionUID = -1795129092816780782L;
private static Logger log = Logger.getLogger(ClientInvokerAdapter.class);
transient ClassLoader cl;
String internalSessionId = new GUID().toString();
public ClientInvokerAdapter()
{
this(Thread.currentThread().getContextClassLoader());
}
public ClientInvokerAdapter(ClassLoader cl)
{
this.cl = cl;
}
public void addListener(InvokerLocator locator, String subsystem, InvokerCallbackHandler callbackHandler) throws Throwable
{
ClientInvoker invoker = getClientInvoker(locator);
InvokerLocator clientLocator = invoker.getClientLocator();
if(clientLocator != null) {
connect(clientLocator);
invoke(new InvocationRequest(internalSessionId,
subsystem,
new InternalInvocation(InternalInvocation.ADDCLIENTLISTENER,
new Object[]{callbackHandler}),
null,
null,
clientLocator));
disconnect(clientLocator); }
invoke(new InvocationRequest(internalSessionId, subsystem, new InternalInvocation(InternalInvocation.ADDLISTENER, null), null, null, locator));
}
public void removeListener(InvokerLocator locator, String subsystem, InvokerCallbackHandler callbackHandler) throws Throwable
{
ClientInvoker invoker = getClientInvoker(locator);
InvokerLocator clientLocator = invoker.getClientLocator();
if(clientLocator != null) {
connect(clientLocator);
invoke(new InvocationRequest(internalSessionId,
subsystem,
new InternalInvocation(InternalInvocation.REMOVECLIENTLISTENER,
new Object[]{callbackHandler}),
null,
null,
clientLocator));
disconnect(clientLocator);
}
invoke(new InvocationRequest(internalSessionId, subsystem, new InternalInvocation(InternalInvocation.REMOVELISTENER, null), null, null, locator));
}
public List getCallbacks(InvokerLocator locator, String subsystem) throws Throwable
{
return (List)invoke(new InvocationRequest(internalSessionId, subsystem, new InternalInvocation(InternalInvocation.GETCALLBACKS, null), null, null, locator));
}
public Object invoke(InvocationRequest invocation)
throws Throwable
{
ClientInvoker invoker = getClientInvoker(invocation.getLocator());
if (invoker.isConnected()==false)
{
if (log.isDebugEnabled())
{
log.debug("invoke called, but our invoker is disconnected, discarding and fetching another fresh invoker for: "+invoker.getLocator());
}
invoker.connect();
}
return invoker.invoke(invocation);
}
public void connect(InvokerLocator locator) throws Exception
{
ClientInvoker invoker = getClientInvoker(locator);
invoker.connect();
}
public boolean isConnected(InvokerLocator locator) throws Exception
{
ClientInvoker invoker = getClientInvoker(locator);
return invoker.isConnected();
}
public void disconnect(InvokerLocator locator) throws Exception
{
ClientInvoker invoker = getClientInvoker(locator);
invoker.disconnect();
}
protected ClientInvoker getClientInvoker(InvokerLocator locator) throws Exception
{
return InvokerRegistry.createClientInvoker(locator);
}
}