1 15 package org.apache.hivemind.management.mbeans; 16 17 import java.rmi.NoSuchObjectException ; 18 import java.rmi.Remote ; 19 import java.rmi.RemoteException ; 20 import java.rmi.registry.LocateRegistry ; 21 import java.rmi.registry.Registry ; 22 import java.rmi.server.UnicastRemoteObject ; 23 24 import javax.management.MBeanRegistration ; 25 import javax.management.MBeanServer ; 26 import javax.management.ObjectName ; 27 28 37 public class NamingService implements NamingServiceMBean, MBeanRegistration 38 { 39 private int _port; 40 41 private Remote _registry; 42 43 private boolean _running; 44 45 48 public NamingService() 49 { 50 this(Registry.REGISTRY_PORT); 51 } 52 53 56 public NamingService(int port) 57 { 58 _port = port; 59 } 60 61 public void setPort(int port) 62 { 63 _port = port; 64 } 65 66 public int getPort() 67 { 68 return _port; 69 } 70 71 public boolean isRunning() 72 { 73 return _running; 74 } 75 76 public void start() throws RemoteException 77 { 78 if (!isRunning()) 79 { 80 _registry = LocateRegistry.createRegistry(getPort()); 81 _running = true; 82 } 83 } 84 85 public void stop() throws NoSuchObjectException 86 { 87 if (isRunning()) 88 { 89 _running = !UnicastRemoteObject.unexportObject(_registry, true); 90 } 91 } 92 93 97 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception 98 { 99 return name; 100 } 101 102 105 public void postRegister(Boolean arg0) 106 { 107 } 108 109 112 public void preDeregister() throws Exception 113 { 114 } 115 116 119 public void postDeregister() 120 { 121 try 122 { 123 stop(); 124 } 125 catch (NoSuchObjectException ignore) 126 { 127 } 128 } 129 } | Popular Tags |