KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2002,2005 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: JRMPRegistry.java,v 1.11 2005/03/15 09:53:27 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jndi.ns;
29
30 import java.rmi.RemoteException JavaDoc;
31 import java.rmi.registry.LocateRegistry JavaDoc;
32 import java.rmi.registry.Registry JavaDoc;
33 import java.rmi.server.UnicastRemoteObject JavaDoc;
34
35 import org.objectweb.carol.jndi.registry.ManageableRegistry;
36 import org.objectweb.carol.jndi.registry.RMIFixedPortFirewallSocketFactory;
37 import org.objectweb.carol.rmi.util.PortNumber;
38 import org.objectweb.carol.util.configuration.CarolDefaultValues;
39 import org.objectweb.carol.util.configuration.TraceCarol;
40
41 /**
42  * Class <code> JRMPRegistry </code>
43  * @author Guillaume Riviere
44  */

45 public class JRMPRegistry extends AbsRegistry implements NameService {
46
47     /**
48      * Default port
49      */

50     private static final int DEFAULT_PORT_NUMBER = 1099;
51
52     /**
53      * Instance port number (firewall)
54      */

55     private static int objectPort = 0;
56
57     /**
58      * registry
59      */

60     private static Registry JavaDoc registry = null;
61
62     /**
63      * Default constructor
64      */

65     public JRMPRegistry() {
66         super(DEFAULT_PORT_NUMBER);
67     }
68
69     /**
70      * start Method, Start a new NameService or do nothing if the name service
71      * is all ready start
72      * @throws NameServiceException if a problem occure
73      */

74     public void start() throws NameServiceException {
75         if (TraceCarol.isDebugJndiCarol()) {
76             TraceCarol.debugJndiCarol("JRMPRegistry.start() on port:" + getPort());
77         }
78         try {
79             // Set factory which allow to fix rmi port if defined and if running inside a server
80
if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) {
81                 if (getConfigProperties() != null) {
82                     String JavaDoc propertyName = CarolDefaultValues.SERVER_JRMP_PORT;
83                     objectPort = PortNumber.strToint(getConfigProperties().getProperty(propertyName, "0"),
84                             propertyName);
85                 } else {
86                     TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_JRMP_PORT
87                             + "' defined in carol.properties file.");
88                 }
89             }
90             if (objectPort > 0) {
91                 RMIFixedPortFirewallSocketFactory.register(objectPort);
92             }
93
94
95             if (!isStarted()) {
96
97                 if (objectPort > 0) {
98                     TraceCarol.infoCarol("Using JRMP fixed server port number '" + objectPort + "'.");
99                 }
100
101                 if (getPort() >= 0) {
102                     registry = ManageableRegistry.createManagableRegistry(getPort(), objectPort);
103                     // add a shudown hook for this process
104
Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
105
106                         public void run() {
107                             try {
108                                 JRMPRegistry.this.stop();
109                             } catch (Exception JavaDoc e) {
110                                 TraceCarol.error("JRMPRegistry ShutdownHook problem", e);
111                             }
112                         }
113                     });
114                 } else {
115                     if (TraceCarol.isDebugJndiCarol()) {
116                         TraceCarol.debugJndiCarol("Can't start JRMPRegistry, port=" + getPort() + " is < 0");
117                     }
118                 }
119             } else {
120                 if (TraceCarol.isDebugJndiCarol()) {
121                     TraceCarol.debugJndiCarol("JRMPRegistry is already start on port:" + getPort());
122                 }
123             }
124         } catch (Exception JavaDoc e) {
125             throw new NameServiceException("can not start rmi registry: " + e);
126         }
127     }
128
129     /**
130      * stop Method, Stop a NameService or do nothing if the name service is all
131      * ready stop
132      * @throws NameServiceException if a problem occure
133      */

134     public void stop() throws NameServiceException {
135         if (TraceCarol.isDebugJndiCarol()) {
136             TraceCarol.debugJndiCarol("JRMPRegistry.stop()");
137         }
138         try {
139             if (registry != null) {
140                 UnicastRemoteObject.unexportObject(registry, true);
141             }
142             registry = null;
143         } catch (Exception JavaDoc e) {
144             throw new NameServiceException("can not stop rmi registry: " + e);
145         }
146     }
147
148     /**
149      * isStarted Method, check if a name service is local
150      * @return boolean true if the name service is local
151      */

152     public static boolean isLocal() {
153         return (registry != null);
154     }
155
156     /**
157      * isStarted Method, check if a name service is started
158      * @return boolean true if the name service is started
159      */

160     public boolean isStarted() {
161         if (registry != null) {
162             return true;
163         }
164         try {
165             LocateRegistry.getRegistry(getPort()).list();
166         } catch (RemoteException JavaDoc re) {
167             return false;
168         }
169         return true;
170     }
171
172     /**
173      * @return the registry.
174      */

175     public static Registry JavaDoc getRegistry() {
176         return registry;
177     }
178 }
Popular Tags