001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.hivemind.management.mbeans; 016 017 import java.rmi.NoSuchObjectException; 018 import java.rmi.Remote; 019 import java.rmi.RemoteException; 020 import java.rmi.registry.LocateRegistry; 021 import java.rmi.registry.Registry; 022 import java.rmi.server.UnicastRemoteObject; 023 024 import javax.management.MBeanRegistration; 025 import javax.management.MBeanServer; 026 import javax.management.ObjectName; 027 028 /** 029 * MBean that starts an rmiregistry. 030 * <p> 031 * Calling {@link #start} will launch rmiregistry in the same JVM; this way rmiregistry will have in 032 * its classpath the same classes the JVM has. 033 * 034 * @author Achim Huegen 035 * @since 1.1 036 */ 037 public class NamingService implements NamingServiceMBean, MBeanRegistration 038 { 039 private int _port; 040 041 private Remote _registry; 042 043 private boolean _running; 044 045 /** 046 * Creates a new instance of NamingService with the default rmiregistry port (1099). 047 */ 048 public NamingService() 049 { 050 this(Registry.REGISTRY_PORT); 051 } 052 053 /** 054 * Creates a new instance of NamingService with the specified port. 055 */ 056 public NamingService(int port) 057 { 058 _port = port; 059 } 060 061 public void setPort(int port) 062 { 063 _port = port; 064 } 065 066 public int getPort() 067 { 068 return _port; 069 } 070 071 public boolean isRunning() 072 { 073 return _running; 074 } 075 076 public void start() throws RemoteException 077 { 078 if (!isRunning()) 079 { 080 _registry = LocateRegistry.createRegistry(getPort()); 081 _running = true; 082 } 083 } 084 085 public void stop() throws NoSuchObjectException 086 { 087 if (isRunning()) 088 { 089 _running = !UnicastRemoteObject.unexportObject(_registry, true); 090 } 091 } 092 093 /** 094 * @see javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer, 095 * javax.management.ObjectName) 096 */ 097 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception 098 { 099 return name; 100 } 101 102 /** 103 * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean) 104 */ 105 public void postRegister(Boolean arg0) 106 { 107 } 108 109 /** 110 * @see javax.management.MBeanRegistration#preDeregister() 111 */ 112 public void preDeregister() throws Exception 113 { 114 } 115 116 /** 117 * @see javax.management.MBeanRegistration#postDeregister() 118 */ 119 public void postDeregister() 120 { 121 try 122 { 123 stop(); 124 } 125 catch (NoSuchObjectException ignore) 126 { 127 } 128 } 129 }