KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > naming > NamingService


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.naming;
10
11 import java.rmi.NoSuchObjectException JavaDoc;
12 import java.rmi.NotBoundException JavaDoc;
13 import java.rmi.RemoteException JavaDoc;
14 import java.rmi.registry.LocateRegistry JavaDoc;
15 import java.rmi.registry.Registry JavaDoc;
16 import java.rmi.server.UnicastRemoteObject JavaDoc;
17
18 /**
19  * An MBean that wraps rmiregistry. <p>
20  * Calling {@link #start} will launch rmiregistry in the same JVM; this way
21  * rmiregistry will have in its classpath the same classes the JVM has.
22  *
23  * @version $Revision: 1.11 $
24  */

25 public class NamingService implements NamingServiceMBean
26 {
27    private int m_port;
28    private Registry JavaDoc m_registry;
29    private boolean m_running;
30
31    /**
32     * Creates a new instance of NamingService with the default rmiregistry port (1099).
33     */

34    public NamingService()
35    {
36       this(Registry.REGISTRY_PORT);
37    }
38
39    /**
40     * Creates a new instance of NamingService with the specified port.
41     */

42    public NamingService(int port)
43    {
44       setPort(port);
45    }
46
47    public void setPort(int port)
48    {
49       if (isRunning()) throw new IllegalStateException JavaDoc("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 JavaDoc
64    {
65       if (!isRunning())
66       {
67          m_registry = LocateRegistry.createRegistry(getPort());
68          m_running = true;
69       }
70    }
71
72    public void stop() throws NoSuchObjectException JavaDoc
73    {
74       if (isRunning())
75       {
76          m_running = !UnicastRemoteObject.unexportObject(m_registry, true);
77       }
78    }
79
80    public String JavaDoc[] list() throws RemoteException JavaDoc
81    {
82       if (!isRunning()) throw new IllegalStateException JavaDoc("NamingService is not running");
83       return m_registry.list();
84    }
85
86    public void unbind(String JavaDoc name) throws RemoteException JavaDoc, NotBoundException JavaDoc
87    {
88       if (!isRunning()) throw new IllegalStateException JavaDoc("NamingService is not running");
89       m_registry.unbind(name);
90    }
91 }
92
Popular Tags