KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2002,2004 - 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: JeremieRegistry.java,v 1.9 2005/03/10 12:21:46 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jndi.ns;
29
30 import java.rmi.RemoteException JavaDoc;
31 import java.rmi.registry.Registry JavaDoc;
32
33 import org.objectweb.jeremie.binding.moa.UnicastRemoteObject;
34 import org.objectweb.jeremie.services.registry.LocateRegistry;
35
36 import org.objectweb.carol.rmi.util.PortNumber;
37 import org.objectweb.carol.util.configuration.CarolDefaultValues;
38 import org.objectweb.carol.util.configuration.TraceCarol;
39
40 /**
41  * Class <code> JeremieRegistry </code>
42  * @author Guillaume Riviere
43  * @author Florent Benoit (Refactoring)
44  */

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

50     private static final int DEFAULT_PORT_NUMBER = 12340;
51
52     /**
53      * Jeremie registry
54      */

55     private Registry JavaDoc registry = null;
56
57     /**
58      * Default constructor
59      */

60     public JeremieRegistry() {
61         super(DEFAULT_PORT_NUMBER);
62     }
63
64     /**
65      * start Method, Start a new NameService or do nothing if the name service
66      * is all ready start
67      * @throws NameServiceException if a problem occure
68      */

69     public void start() throws NameServiceException {
70         if (TraceCarol.isDebugJndiCarol()) {
71             TraceCarol.debugJndiCarol("JeremieRegistry.start() on port:" + getPort());
72         }
73
74         // Fix jeremie port if running inside a server
75
if (System.getProperty(CarolDefaultValues.SERVER_MODE, "false").equalsIgnoreCase("true")) {
76             if (getConfigProperties() != null) {
77                 String JavaDoc propertyName = CarolDefaultValues.SERVER_JEREMIE_PORT;
78                 int jeremiePort = PortNumber.strToint(getConfigProperties().getProperty(propertyName, "0"),
79                         propertyName);
80                 if (jeremiePort > 0) {
81                     TraceCarol.infoCarol("Using Jeremie fixed server port number '" + jeremiePort + "'.");
82                     System.setProperty("org.objectweb.jeremie.stub_factories.defaultport", String.valueOf(jeremiePort));
83                 }
84             } else {
85                 TraceCarol.debugCarol("No properties '" + CarolDefaultValues.SERVER_IIOP_PORT
86                         + "' defined in carol.properties file.");
87             }
88         }
89
90         try {
91             if (!isStarted()) {
92                 if (getPort() >= 0) {
93                     registry = LocateRegistry.createRegistry(getPort());
94                     // add a shudown hook for this process
95
Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
96
97                         public void run() {
98                             try {
99                                 JeremieRegistry.this.stop();
100                             } catch (Exception JavaDoc e) {
101                                 TraceCarol.error("JeremieRegistry ShutdownHook problem", e);
102                             }
103                         }
104                     });
105                 } else {
106                     if (TraceCarol.isDebugJndiCarol()) {
107                         TraceCarol.debugJndiCarol("Can't start JeremieRegistry, port=" + getPort() + " is < 0");
108                     }
109                 }
110
111             } else {
112                 if (TraceCarol.isDebugJndiCarol()) {
113                     TraceCarol.debugJndiCarol("JeremieRegistry is already start on port:" + getPort());
114                 }
115             }
116         } catch (Exception JavaDoc e) {
117             throw new NameServiceException("can not start jeremie registry: " + e);
118         }
119     }
120
121     /**
122      * stop Method, Stop a NameService or do nothing if the name service is all
123      * ready stop
124      * @throws NameServiceException if a problem occure
125      */

126     public void stop() throws NameServiceException {
127         if (TraceCarol.isDebugJndiCarol()) {
128             TraceCarol.debugJndiCarol("JeremieRegistry.stop()");
129         }
130         try {
131             if (registry != null) {
132                 UnicastRemoteObject.unexportObject(registry, true);
133             }
134             registry = null;
135         } catch (Exception JavaDoc e) {
136             throw new NameServiceException("can not stop jeremie registry: " + e);
137         }
138     }
139
140     /**
141      * isStarted Method, check if a name service is started
142      * @return boolean true if the name service is started
143      */

144     public boolean isStarted() {
145         if (registry != null) {
146             return true;
147         }
148         try {
149             LocateRegistry.getRegistry(getPort()).list();
150         } catch (RemoteException JavaDoc re) {
151             return false;
152         }
153         return true;
154     }
155 }
Popular Tags