package com.csii.rmi.server;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.csii.rmi.RemoteObjectDescriptor;
import com.csii.rmi.RemoteSecurityManager;
import com.csii.rmi.util.Utils;
import com.csii.service.comm.Server;
import com.csii.service.comm.rmi.RMINaming;
public class SimpleRMIServer implements Server
{
protected Log log = LogFactory.getLog(getClass());
private String registryHost = "127.0.0.1";
//注册端口
private int registryPort = 10099;
private String registHostIp;
private RMIServerSocketFactory rmiServerSocketFactory;
private RMIClientSocketFactory rmiClientSocketFactory;
//传输端口
private int port = 10098;
private boolean alive = false;
//远程对象集合
private RemoteObjectDescriptor[] remoteObjects;
public void setRegistHostIp(String registHostIp){
this.registHostIp = registHostIp;
}
// private boolean isListening(String registryHost, int registryPort)
// {
// try
// {
// Registry registry = LocateRegistry.getRegistry(registryHost, registryPort, this.rmiClientSocketFactory);
//
// Remote testService = new Remote()
// {
// };
// Remote remote = UnicastRemoteObject.exportObject(testService, this.port, this.rmiClientSocketFactory, this.rmiServerSocketFactory);
// try
// {
// registry.rebind("InnerTest", remote);
// }
// catch (Exception e)
// {
// return false;
// }
// try
// {
// registry.unbind("InnerTest");
// }
// catch (Exception e) {
// this.log.error("unbind", e);
// return false;
// }
//
// return true;
// } catch (Exception e1) {
// }
// return false;
// }
public synchronized void start(){
if (isAlive()){
return;
}
try{
//公网访问ip
System.setProperty("java.rmi.server.hostname", this.registHostIp);
if (this.log.isDebugEnabled())
this.log.debug("cannot find rmiRegistry at " + this.registryHost + ":" + this.registryPort);
if (!Utils.isLocalAddress(this.registryHost))
throw new RuntimeException("Cannot located remote Registry Server.");
try{
if (this.log.isDebugEnabled())
this.log.debug("create rmiRegistry at " + this.registryHost + ":" + this.registryPort);
//在本地主机上创建并导出 Registry 实例,该本地主机使用的是与该实例通信的自定义套接字工厂
LocateRegistry.createRegistry(this.registryPort, this.rmiClientSocketFactory, this.rmiServerSocketFactory);
//安全设置
RemoteSecurityManager.setup();
} catch (Exception e) {
throw new RuntimeException("Cannot started Registry Server.", e);
}
//返回本地创建的指定 host 和 port 上对远程对象 Registry 的远程引用。
Registry registry = LocateRegistry.getRegistry(this.registryHost, this.registryPort, this.rmiClientSocketFactory);
if (this.log.isDebugEnabled()) {
this.log.debug("find rmiRegistry" + registry);
}
for (int i = 0; i < this.remoteObjects.length; i++){
RemoteObjectDescriptor desc = this.remoteObjects[i];
try{
RMINaming.rebind(desc.getName(), desc.getBean(), desc.getInterfaces(), this.port, this.rmiClientSocketFactory, this.rmiServerSocketFactory);
}catch (Exception e){
this.log.error("rebind fail, try to rebind ", e);
RMINaming.unbind(desc.getName(), this.rmiClientSocketFactory);
RMINaming.bind(desc.getName(), desc.getBean(), desc.getInterfaces(), this.port, this.rmiClientSocketFactory, this.rmiServerSocketFactory);
}
}
this.alive = true;
this.log.info("Started... " + this);
}catch (Exception e){
this.log.error("start", e);
throw new RuntimeException(e);
}
}
public boolean isAlive()
{
return this.alive;
}
public synchronized void shutdown()
{
if (!isAlive()) {
return;
}
this.alive = false;
try
{
Registry registry = LocateRegistry.getRegistry(this.registryHost, this.registryPort, this.rmiClientSocketFactory);
for (int i = 0; i < this.remoteObjects.length; i++)
{
RemoteObjectDescriptor desc = this.remoteObjects[i];
RMINaming.unbind(desc.getName(), this.rmiClientSocketFactory);
}
}
catch (Exception e)
{
this.log.error("shutdown", e);
}
}
public void setRmiServerSocketFactory(RMIServerSocketFactory rmiServerSocketFactory)
{
this.rmiServerSocketFactory = rmiServerSocketFactory;
}
public void setRmiClientSocketFactory(RMIClientSocketFactory rmiClientSocketFactory)
{
this.rmiClientSocketFactory = rmiClientSocketFactory;
}
public void setRemoteObjects(RemoteObjectDescriptor[] remoteObjects)
{
this.remoteObjects = remoteObjects;
}
public void setRegistryHost(String registryHost)
{
this.registryHost = registryHost;
}
public void setRegistryPort(int registryPort) {
this.registryPort = registryPort;
}
public void setPort(int port) {
this.port = port;
}
public void setRemoteObjectList(List list)
{
this.remoteObjects = ((RemoteObjectDescriptor[])list.toArray(new RemoteObjectDescriptor[0]));
}
public void restart()
{
}
}