KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > ns > IRMIRegistry


1 package org.objectweb.carol.jndi.ns;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.rmi.registry.LocateRegistry JavaDoc;
5 import java.rmi.registry.Registry JavaDoc;
6 import java.rmi.server.UnicastRemoteObject JavaDoc;
7
8 import org.objectweb.carol.jndi.registry.ManageableRegistry;
9 import org.objectweb.carol.jndi.registry.RMIFixedPortFirewallSocketFactory;
10 import org.objectweb.carol.rmi.util.PortNumber;
11 import org.objectweb.carol.util.configuration.CarolDefaultValues;
12 import org.objectweb.carol.util.configuration.TraceCarol;
13
14 /**
15  * IRMIRegistry
16  *
17  * @author Rafael H. Schloming <rhs@mit.edu>
18  **/

19
20 public class IRMIRegistry extends AbsRegistry implements NameService {
21
22     /**
23      * Default port
24      */

25     private static final int DEFAULT_PORT_NUMBER = 1098;
26
27     /**
28      * Instance port number (firewall)
29      */

30     private static int objectPort = 0;
31
32     /**
33      * registry
34      */

35     private static Registry JavaDoc registry = null;
36
37     /**
38      * Default constructor
39      */

40     public IRMIRegistry() {
41         super(DEFAULT_PORT_NUMBER);
42     }
43
44     /**
45      * start Method, Start a new NameService or do nothing if the name service
46      * is all ready start
47      * @throws NameServiceException if a problem occure
48      */

49     public void start() throws NameServiceException {
50         if (TraceCarol.isDebugJndiCarol()) {
51             TraceCarol.debugJndiCarol("IRMIRegistry.start() on port:" + getPort());
52         }
53         try {
54             // Set factory which allow to fix rmi port if defined and if running inside a server
55
if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) {
56                 if (getConfigProperties() != null) {
57                     String JavaDoc propertyName = CarolDefaultValues.SERVER_IRMI_PORT;
58                     objectPort = PortNumber.strToint(getConfigProperties().getProperty(propertyName, "0"),
59                             propertyName);
60                 } else {
61                     TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_IRMI_PORT
62                             + "' defined in carol.properties file.");
63                 }
64             }
65             if (objectPort > 0) {
66                 RMIFixedPortFirewallSocketFactory.register(objectPort);
67             }
68
69
70             if (!isStarted()) {
71
72                 if (objectPort > 0) {
73                     TraceCarol.infoCarol("Using IRMI fixed server port number '" + objectPort + "'.");
74                 }
75
76                 if (getPort() >= 0) {
77                     registry = ManageableRegistry.createManagableRegistry(getPort(), objectPort);
78                     // add a shudown hook for this process
79
Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
80
81                         public void run() {
82                             try {
83                                 IRMIRegistry.this.stop();
84                             } catch (Exception JavaDoc e) {
85                                 TraceCarol.error("IRMIRegistry ShutdownHook problem", e);
86                             }
87                         }
88                     });
89                 } else {
90                     if (TraceCarol.isDebugJndiCarol()) {
91                         TraceCarol.debugJndiCarol("Can't start IRMIRegistry, port=" + getPort() + " is < 0");
92                     }
93                 }
94             } else {
95                 if (TraceCarol.isDebugJndiCarol()) {
96                     TraceCarol.debugJndiCarol("IRMIRegistry is already start on port:" + getPort());
97                 }
98             }
99         } catch (Exception JavaDoc e) {
100             throw new NameServiceException("can not start rmi registry: " + e);
101         }
102     }
103
104     /**
105      * stop Method, Stop a NameService or do nothing if the name service is all
106      * ready stop
107      * @throws NameServiceException if a problem occure
108      */

109     public void stop() throws NameServiceException {
110         if (TraceCarol.isDebugJndiCarol()) {
111             TraceCarol.debugJndiCarol("IRMIRegistry.stop()");
112         }
113         try {
114             if (registry != null) {
115                 UnicastRemoteObject.unexportObject(registry, true);
116             }
117             registry = null;
118         } catch (Exception JavaDoc e) {
119             throw new NameServiceException("can not stop rmi registry: " + e);
120         }
121     }
122
123     /**
124      * isStarted Method, check if a name service is local
125      * @return boolean true if the name service is local
126      */

127     public static boolean isLocal() {
128         return (registry != null);
129     }
130
131     /**
132      * isStarted Method, check if a name service is started
133      * @return boolean true if the name service is started
134      */

135     public boolean isStarted() {
136         if (registry != null) {
137             return true;
138         }
139         try {
140             LocateRegistry.getRegistry(getPort()).list();
141         } catch (RemoteException JavaDoc re) {
142             return false;
143         }
144         return true;
145     }
146
147     /**
148      * @return the registry.
149      */

150     public static Registry JavaDoc getRegistry() {
151         return registry;
152     }
153
154 }
155
Popular Tags