KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > mbeans > NamingService


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management.mbeans;
16
17 import java.rmi.NoSuchObjectException JavaDoc;
18 import java.rmi.Remote JavaDoc;
19 import java.rmi.RemoteException JavaDoc;
20 import java.rmi.registry.LocateRegistry JavaDoc;
21 import java.rmi.registry.Registry JavaDoc;
22 import java.rmi.server.UnicastRemoteObject JavaDoc;
23
24 import javax.management.MBeanRegistration JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 /**
29  * MBean that starts an rmiregistry.
30  * <p>
31  * Calling {@link #start} will launch rmiregistry in the same JVM; this way rmiregistry will have in
32  * its classpath the same classes the JVM has.
33  *
34  * @author Achim Huegen
35  * @since 1.1
36  */

37 public class NamingService implements NamingServiceMBean, MBeanRegistration JavaDoc
38 {
39     private int _port;
40
41     private Remote JavaDoc _registry;
42
43     private boolean _running;
44
45     /**
46      * Creates a new instance of NamingService with the default rmiregistry port (1099).
47      */

48     public NamingService()
49     {
50         this(Registry.REGISTRY_PORT);
51     }
52
53     /**
54      * Creates a new instance of NamingService with the specified port.
55      */

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 JavaDoc
77     {
78         if (!isRunning())
79         {
80             _registry = LocateRegistry.createRegistry(getPort());
81             _running = true;
82         }
83     }
84
85     public void stop() throws NoSuchObjectException JavaDoc
86     {
87         if (isRunning())
88         {
89             _running = !UnicastRemoteObject.unexportObject(_registry, true);
90         }
91     }
92
93     /**
94      * @see javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer,
95      * javax.management.ObjectName)
96      */

97     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
98     {
99         return name;
100     }
101
102     /**
103      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
104      */

105     public void postRegister(Boolean JavaDoc arg0)
106     {
107     }
108
109     /**
110      * @see javax.management.MBeanRegistration#preDeregister()
111      */

112     public void preDeregister() throws Exception JavaDoc
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 JavaDoc ignore)
126         {
127         }
128     }
129 }
Popular Tags