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: NameServiceManager.java 409 2005-03-04 14:06:32Z430 2005-03-10 12:21:46Z benoitf $
26: * --------------------------------------------------------------------------
27: */
28: package org.objectweb.carol.jndi.ns;
29:
30: import java.util.Enumeration;
31: import java.util.Hashtable;
32:
33: import org.objectweb.carol.util.configuration.CarolConfiguration;
34: import org.objectweb.carol.util.configuration.RMIConfiguration;
35: import org.objectweb.carol.util.configuration.TraceCarol;
36:
37: /**
38: * Class <code> NameServicemanager </code> is the CAROL Name Service manager.
39: * This is the carol API for Nme services management
40: * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
41: */
42: public class NameServiceManager {
43:
44: /**
45: * Default sleep value
46: */
47: private static final int SLEEP_VALUE = 10000;
48:
49: /**
50: * Name Service Hashtable
51: */
52: private static Hashtable nsTable;
53:
54: /**
55: * private constructor for singleton
56: */
57: private static NameServiceManager current = new NameServiceManager();
58:
59: /**
60: * private constructor for unicicity
61: */
62: private NameServiceManager() {
63: if (TraceCarol.isDebugJndiCarol()) {
64: TraceCarol.debugJndiCarol("NameServiceManager.NameServiceManager()");
65: }
66: try {
67: nsTable = new Hashtable();
68: //get rmi configuration hashtable
69: Hashtable allRMIConfiguration = CarolConfiguration.getAllRMIConfiguration();
70: //int nbProtocol = allRMIConfiguration.size();
71: for (Enumeration e = allRMIConfiguration.elements(); e.hasMoreElements();) {
72: RMIConfiguration currentConf = (RMIConfiguration) e.nextElement();
73: String rmiName = currentConf.getName();
74: NameService nsC = (NameService) Class.forName(currentConf.getNameService()).newInstance();
75: nsC.setPort(currentConf.getPort());
76: nsC.setHost(currentConf.getHost());
77: nsC.setConfigProperties(currentConf.getConfigProperties());
78: // get the Name Service
79: nsTable.put(rmiName, nsC);
80: }
81: } catch (Exception e) {
82: String msg = "NameServiceManager.NameServiceManager() fail";
83: TraceCarol.error(msg, e);
84: }
85: }
86:
87: /**
88: * Method getCurrent
89: * @return NameServiceManager return the current
90: */
91: public static NameServiceManager getNSManagerCurrent() {
92: if (TraceCarol.isDebugJndiCarol()) {
93: TraceCarol.debugJndiCarol("NameServiceManager.getNSManagerCurrent()");
94: }
95: return current;
96: }
97:
98: /**
99: * Start all names service
100: * @throws NameServiceException if one of the name services is already start
101: */
102: public static void startNS() throws NameServiceException {
103: if (TraceCarol.isDebugJndiCarol()) {
104: TraceCarol.debugJndiCarol("NameServiceManager.startNS()");
105: }
106: // test if one of the ns is allready started
107: for (Enumeration e = nsTable.keys(); e.hasMoreElements();) {
108: String k = (String) e.nextElement();
109: NameService currentNS = (NameService) nsTable.get(k);
110: if (currentNS.isStarted()) {
111: throw new NameServiceException("The " + k + " name service is allready started");
112: }
113: }
114: // Start all name services
115: startNonStartedNS();
116: }
117:
118: /**
119: * Start all non-started names service
120: */
121: public static void startNonStartedNS() {
122: if (TraceCarol.isDebugJndiCarol()) {
123: TraceCarol.debugJndiCarol("NameServiceManager.startNonStartedNS()");
124: }
125: // start name services
126: for (Enumeration e = nsTable.keys(); e.hasMoreElements();) {
127: String k = (String) e.nextElement();
128: NameService currentNS = (NameService) nsTable.get(k);
129:
130: try {
131: currentNS.start();
132: if (TraceCarol.isInfoCarol()) {
133: TraceCarol.infoCarol("Name service for " + k + " is started on port " + currentNS.getPort());
134: }
135: } catch (NameServiceException nse) {
136: // do nothing, just trace
137: if (TraceCarol.isDebugJndiCarol()) {
138: TraceCarol
139: .debugJndiCarol("NameServiceManager.startNonStartedNS() can not start name service: " + k);
140: }
141: }
142: }
143: }
144:
145: /**
146: * Stop all name services
147: * @throws NameServiceException if an exception occure at stoping time
148: */
149: public static void stopNS() throws NameServiceException {
150: if (TraceCarol.isDebugJndiCarol()) {
151: TraceCarol.debugJndiCarol("NameServiceManager.stopNS()");
152: }
153: // stop name services
154: for (Enumeration e = nsTable.keys(); e.hasMoreElements();) {
155: String k = (String) e.nextElement();
156: NameService currentNS = (NameService) nsTable.get(k);
157: currentNS.stop();
158: }
159: }
160:
161: /**
162: * Main function: start all registry and wait for control C function
163: * @param args arguments
164: */
165: public static void main(String[] args) {
166:
167: // configure logging
168: TraceCarol.configure();
169:
170: try {
171: NameServiceManager.startNonStartedNS();
172: // add a shudown hook for this process
173: Runtime.getRuntime().addShutdownHook(new Thread() {
174:
175: public void run() {
176: try {
177: NameServiceManager.stopNS();
178: } catch (Exception e) {
179: TraceCarol.error("Carol Naming ShutdownHook problem", e);
180: }
181: }
182: });
183: while (true) {
184: Thread.sleep(SLEEP_VALUE);
185: }
186: } catch (Exception e) {
187: e.printStackTrace();
188: }
189: }
190: }