1 8 9 package mx4j.tools.naming; 10 11 import java.rmi.NoSuchObjectException ; 12 import java.rmi.NotBoundException ; 13 import java.rmi.RemoteException ; 14 import java.rmi.registry.LocateRegistry ; 15 import java.rmi.registry.Registry ; 16 import java.rmi.server.UnicastRemoteObject ; 17 18 25 public class NamingService implements NamingServiceMBean 26 { 27 private int m_port; 28 private Registry m_registry; 29 private boolean m_running; 30 31 34 public NamingService() 35 { 36 this(Registry.REGISTRY_PORT); 37 } 38 39 42 public NamingService(int port) 43 { 44 setPort(port); 45 } 46 47 public void setPort(int port) 48 { 49 if (isRunning()) throw new IllegalStateException ("NamingService is running, cannot change the port"); 50 m_port = port; 51 } 52 53 public int getPort() 54 { 55 return m_port; 56 } 57 58 public boolean isRunning() 59 { 60 return m_running; 61 } 62 63 public void start() throws RemoteException 64 { 65 if (!isRunning()) 66 { 67 m_registry = LocateRegistry.createRegistry(getPort()); 68 m_running = true; 69 } 70 } 71 72 public void stop() throws NoSuchObjectException 73 { 74 if (isRunning()) 75 { 76 m_running = !UnicastRemoteObject.unexportObject(m_registry, true); 77 } 78 } 79 80 public String [] list() throws RemoteException 81 { 82 if (!isRunning()) throw new IllegalStateException ("NamingService is not running"); 83 return m_registry.list(); 84 } 85 86 public void unbind(String name) throws RemoteException , NotBoundException 87 { 88 if (!isRunning()) throw new IllegalStateException ("NamingService is not running"); 89 m_registry.unbind(name); 90 } 91 } 92 | Popular Tags |