package org.jboss.remoting.transport.rmi;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.ServerInvoker;
import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.ExportException;
import java.rmi.server.RemoteStub;
import java.rmi.server.UnicastRemoteObject;
import java.util.Map;
public class RMIServerInvoker extends ServerInvoker implements RMIServerInvokerInf
{
private final RemoteStub stub;
public static final int DEFAULT_REGISTRY_PORT = 3455;
public static final String REGISTRY_PORT_KEY = "registryPort";
public RMIServerInvoker(InvokerLocator locator)
throws RemoteException
{
super(locator);
try
{
if (locator.getPort() != -1)
{
Registry registry = null;
registry = getRegistry(locator);
stub = (RemoteStub) UnicastRemoteObject.exportObject(this, locator.getPort());
if(log.isDebugEnabled())
{
log.debug("Binding registry to " + "remoting/RMIServerInvoker/" + locator.getPort());
}
registry.rebind("remoting/RMIServerInvoker/" + locator.getPort(), this);
}
else
{
stub = UnicastRemoteObject.exportObject(this);
}
}
catch (RemoteException th)
{
th.printStackTrace();
throw th;
}
}
public RMIServerInvoker(InvokerLocator locator, Map configuration) throws RemoteException
{
super(locator, configuration);
try
{
if (locator.getPort() != -1)
{
Registry registry = null;
registry = getRegistry(locator);
stub = (RemoteStub) UnicastRemoteObject.exportObject(this, locator.getPort());
if(log.isDebugEnabled())
{
log.debug("Binding registry to " + "remoting/RMIServerInvoker/" + locator.getPort());
}
registry.rebind("remoting/RMIServerInvoker/" + locator.getPort(), this);
}
else
{
stub = UnicastRemoteObject.exportObject(this);
}
}
catch (RemoteException th)
{
th.printStackTrace();
throw th;
}
}
private Registry getRegistry(InvokerLocator locator)
throws RemoteException
{
Registry registry = null;
int port = DEFAULT_REGISTRY_PORT;
Map params = locator.getParameters();
if(params != null)
{
String value = (String)params.get(REGISTRY_PORT_KEY);
if(value != null)
{
try
{
port = Integer.parseInt(value);
log.debug("Using port " + port + " for rmi registry.");
}
catch (NumberFormatException e)
{
log.error("Can not set the RMIServerInvoker RMI registry to port " + value + ". This is not a valid port number.");
}
}
}
try
{
if(log.isDebugEnabled())
{
log.debug("Creating registry for " + port);
}
registry = LocateRegistry.createRegistry(port);
}
catch( ExportException exportEx)
{
if(log.isDebugEnabled())
{
log.debug("Locating registry for " + port);
}
registry = LocateRegistry.getRegistry(port);
}
catch (Exception e)
{
e.printStackTrace();
}
if(log.isDebugEnabled())
{
log.debug("Got registry: " + registry);
}
return registry;
}
protected String getDefaultDataType()
{
return SerializableMarshaller.DATATYPE;
}
public void destroy()
{
super.destroy();
try
{
try
{
if(log.isDebugEnabled())
{
log.debug("unbinding " + "remoting/RMIServerInvoker/" + locator.getPort() + " from registry running on port " + (locator.getPort() + 1));
}
Registry registry = getRegistry(locator);
registry.unbind("remoting/RMIServerInvoker/" + locator.getPort());
}
catch (RemoteException e)
{
e.printStackTrace();
}
catch (NotBoundException e)
{
e.printStackTrace();
}
UnicastRemoteObject.unexportObject(this, true);
}
catch (java.rmi.NoSuchObjectException e)
{
}
}
public String getMBeanObjectName()
{
return "jboss.remoting:service=invoker,transport=rmi";
}
protected void finalize() throws Throwable
{
destroy();
super.finalize();
}
public boolean isTransportBiDirectional()
{
return true;
}
public final RemoteStub getStub()
{
return stub;
}
public Object transport(Object invocation)
throws RemoteException, IOException
{
return invoke(invocation);
}
}