📄 registrydelegate.java
字号:
/*
* This file has been generated by the RMI Plugin for Eclipse.
*/
//package_decl
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;
/**
* <p>
* This class implements the registry delegate concept.
* <p>
* In order to enforce some level of security, the standard RMI
* registry implementation (e.g. <code>rmiregistry.exe</code>)
* only allows processes on the same host to register objects
* in the registry (think of a bank running a registry on one of its servers,
* and doesn't want anybody modifying it).
* So, by design, if a process tries to {@link #bind(String, Remote)}
* an object to a remote registry, an exception will be thrown.
*
* <p>However, the design of a distributed system may require
* remote clients to register themselves in a central registry. If
* such system is deployed in a controlled and trusted environment (e.g., a firewalled
* intranet with tight access control), the security
* risk may be acceptable.
*
* <p>
* The simplest technical solution to the remote registration problem
* is to have a registry delegate. A registry delegate is an object
* that serves as a proxy for the real registry. The delegate itself
* usually appears in the registry under a well known name. It implements the {@link java.rmi.registry.Registry}
* interface and simply delegates all method calls to the appropriate methods of the
* real registry. The delegate is allowed to perform bind and unbind operations because
* it is running on the same host as the registry.
*
* <p>
* The common scenario for starting a registry and creating the delegate is starting a class with
* the following {@link #main(String[])} method:
* <pre>
* public static void main(String[] args) throws AccessException, RemoteException, AlreadyBoundException {
* if (System.getSecurityManager() == null) {
* System.setSecurityManager(new RMISecurityManager());
* }
*
* Registry registry = LocateRegistry.createRegistry(REGISTRY_PORT);
* registry.bind(DELEGATE_NAME, new RegistryDelegate());
*
* do {
* try {
* Thread.sleep(Long.MAX_VALUE);
* } catch (InterruptedException e) {
* ; // do nothing
* } catch (Throwable e) {
* e.printStackTrace();
* System.exit(1);
* }
* } while(true);
* }
* </pre>
*
* The common usage scenario looks something like:
* <pre>
* Registry remoteRegistry = LocateRegistry.getRegistry("remotehost.mycompany.com");
* Registry delegate = (Registry) remoteRegistry.lookup(DELEGATE_NAME);
* delegate.bind("someName", new SomeRemoteObject());
* </pre>
*
* The {@link #getRegistryDelegate(String)} method is a helper method that fetches the
* registry delegate for you.
*
* <p>
* The {@link #main(String[])} method of this class will create
* a local registry on the default port, create a registry delegate and bind it
* under the well known name that you chose in the wizard ({@link #DELEGATE_NAME}).
*
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/rmi/getstart.doc.html#5912">Getting started with RMI</a>
* @see <a href="http://www.jguru.com/faq/view.jsp?EID=1035">RMI FAQ on JGuru</a>
* @see <a href="http://archives.java.sun.com/cgi-bin/wa?A2=ind0402&L=rmi-users&F=&S=&P=2032">RMI Mailing List post</a>
* @see <a href="http://archives.java.sun.com/cgi-bin/wa?A2=ind0104&L=rmi-users&O=D&P=23151">RMI Mailing List post</a>
*
* <!-- This file has been generated by the RMI Plugin for Eclipse. -->
* @author Genady Beryozkin, rmi-info@genady.net
*/
public class RegistryDelegate extends UnicastRemoteObject implements Registry {
/** The local resistry. */
private Registry localRegistry;
/** The name under which the delegate appears in the registry */
public static final String DELEGATE_NAME = "REGISTRY_DELEGATE_NAME";
/**
* Create a delegate for a local registry that is bound
* to the default local port (1099).
* @throws RemoteException if any error occurs.
*/
public RegistryDelegate() throws RemoteException {
this.localRegistry = LocateRegistry.getRegistry();
}
/**
* Create a delegate for a local registry that is bound
* to a user specified port.
* @param port the local registry port.
* @throws RemoteException if any error occurs.
*/
public RegistryDelegate(int port) throws RemoteException {
this.localRegistry = LocateRegistry.getRegistry(port);
}
/**
* Create a delegate for a user provided registry instance.
* The registry is assumed to be a local registry, as there
* is no point in creating a delegate for a remote registry.
* @param reg the registry for which a delegate is to be created.
* @throws RemoteException if any error occurrs.
*/
public RegistryDelegate(Registry reg) throws RemoteException {
this.localRegistry = reg;
}
/* (non-Javadoc)
* @see java.rmi.registry.Registry#bind(java.lang.String, java.rmi.Remote)
*/
public void bind(String name, Remote obj) throws RemoteException, AlreadyBoundException, AccessException {
localRegistry.bind(name, obj);
}
/* (non-Javadoc)
* @see java.rmi.registry.Registry#list()
*/
public String[] list() throws RemoteException, AccessException {
return localRegistry.list();
}
/* (non-Javadoc)
* @see java.rmi.registry.Registry#lookup(java.lang.String)
*/
public Remote lookup(String name) throws RemoteException, NotBoundException, AccessException {
return localRegistry.lookup(name);
}
/* (non-Javadoc)
* @see java.rmi.registry.Registry#rebind(java.lang.String, java.rmi.Remote)
*/
public void rebind(String name, Remote obj) throws RemoteException, AccessException {
localRegistry.rebind(name, obj);
}
/* (non-Javadoc)
* @see java.rmi.registry.Registry#unbind(java.lang.String)
*/
public void unbind(String name) throws RemoteException, NotBoundException, AccessException {
localRegistry.unbind(name);
}
/**
* This method retrieves the registry delegate from a registry that is running on a remote host.
* @param remoteHost name of the remote host.
* @param remotePort port on which the registry accepts requests.
* @return a reference to the registry delegate.
* @throws RemoteException if we could not connect to the registry.
* @throws NotBoundException if the delegate does not appear in the registry.
*/
public static Registry getRegistryDelegate(String remoteHost, int remotePort) throws RemoteException, NotBoundException {
Registry remoteRegistry = LocateRegistry.getRegistry(remoteHost, remotePort);
return (Registry) remoteRegistry.lookup(DELEGATE_NAME);
}
/**
* This method retrieves the registry delegate from a registry that is running on a remote host.
* @param remoteHost name of the remote host.
* @return a reference to the registry delegate.
* @throws RemoteException if we could not connect to the registry.
* @throws NotBoundException if the delegate does not appear in the registry.
*/
public static Registry getRegistryDelegate(String remoteHost) throws RemoteException, NotBoundException {
return getRegistryDelegate(remoteHost, REGISTRY_PORT);
}
/**
* A simple way to run a registry and bind a registry delegate.
* This method should be modified and adapted for the specific needs of a project.
*/
public static void main(String[] args) throws AccessException, RemoteException, AlreadyBoundException {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
Registry registry = LocateRegistry.createRegistry(REGISTRY_PORT);
registry.bind(DELEGATE_NAME, new RegistryDelegate());
do {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
; // do nothing
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
} while(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -