KickJava   Java API By Example, From Geeks To Geeks.

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


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,v 1.11 2005/04/07 15:07:07 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.jndi.ns;
29
30 import java.util.Enumeration JavaDoc;
31 import java.util.Hashtable JavaDoc;
32
33 import org.objectweb.carol.util.configuration.ConfigurationRepository;
34 import org.objectweb.carol.util.configuration.Protocol;
35 import org.objectweb.carol.util.configuration.ProtocolConfiguration;
36 import org.objectweb.carol.util.configuration.TraceCarol;
37
38 /**
39  * Class <code> NameServicemanager </code> is the CAROL Name Service manager.
40  * This is the carol API for Nme services management
41  * @author Guillaume Riviere
42  */

43 public class NameServiceManager {
44
45     /**
46      * Default sleep value
47      */

48     private static final int SLEEP_VALUE = 10000;
49
50     /**
51      * Name Service Hashtable
52      */

53     private static Hashtable JavaDoc nsTable;
54
55     /**
56      * private constructor for singleton
57      */

58     private static NameServiceManager current = new NameServiceManager();
59
60     /**
61      * private constructor for unicicity
62      */

63     private NameServiceManager() {
64         if (TraceCarol.isDebugJndiCarol()) {
65             TraceCarol.debugJndiCarol("NameServiceManager.NameServiceManager()");
66         }
67
68         // List of current name server
69
nsTable = new Hashtable JavaDoc();
70         ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations();
71         for (int c = 0; c < protocolConfigurations.length; c++) {
72             ProtocolConfiguration protocolConfiguration = protocolConfigurations[c];
73             String JavaDoc configurationName = protocolConfiguration.getName();
74             Protocol protocol = protocolConfiguration.getProtocol();
75             NameService nsC = null;
76             try {
77                 nsC = (NameService) Class.forName(protocol.getRegistryClassName()).newInstance();
78             } catch (Exception JavaDoc e) {
79                 String JavaDoc msg = "Cannot instantiate registry class '" + protocol.getRegistryClassName() + "'";
80                 TraceCarol.error(msg, e);
81             }
82             nsC.setPort(protocolConfiguration.getPort());
83             nsC.setHost(protocolConfiguration.getHost());
84             nsC.setConfigProperties(protocolConfiguration.getProperties());
85             // Add the NameService in the list of configurations
86
nsTable.put(configurationName, nsC);
87         }
88
89     }
90
91     /**
92      * Method getCurrent
93      * @return NameServiceManager return the current
94      */

95     public static NameServiceManager getNSManagerCurrent() {
96         if (TraceCarol.isDebugJndiCarol()) {
97             TraceCarol.debugJndiCarol("NameServiceManager.getNSManagerCurrent()");
98         }
99         return current;
100     }
101
102     /**
103      * Start all names service
104      * @throws NameServiceException if one of the name services is already start
105      */

106     public static void startNS() throws NameServiceException {
107         if (TraceCarol.isDebugJndiCarol()) {
108             TraceCarol.debugJndiCarol("NameServiceManager.startNS()");
109         }
110         // test if one of the ns is allready started
111
for (Enumeration JavaDoc e = nsTable.keys(); e.hasMoreElements();) {
112             String JavaDoc k = (String JavaDoc) e.nextElement();
113             NameService currentNS = (NameService) nsTable.get(k);
114             if (currentNS.isStarted()) {
115                 throw new NameServiceException("The " + k + " name service is already started");
116             }
117         }
118         // Start all name services
119
startNonStartedNS();
120     }
121
122     /**
123      * Start all non-started names service
124      */

125     public static void startNonStartedNS() {
126         if (TraceCarol.isDebugJndiCarol()) {
127             TraceCarol.debugJndiCarol("NameServiceManager.startNonStartedNS()");
128         }
129         // start name services
130
for (Enumeration JavaDoc e = nsTable.keys(); e.hasMoreElements();) {
131             String JavaDoc k = (String JavaDoc) e.nextElement();
132             NameService currentNS = (NameService) nsTable.get(k);
133
134             try {
135                 currentNS.start();
136                 if (TraceCarol.isInfoCarol()) {
137                     TraceCarol.infoCarol("Name service for " + k + " is started on port " + currentNS.getPort());
138                 }
139             } catch (NameServiceException nse) {
140                 // do nothing, just trace
141
if (TraceCarol.isDebugJndiCarol()) {
142                     TraceCarol
143                             .debugJndiCarol("NameServiceManager.startNonStartedNS() can not start name service: " + k);
144                 }
145             }
146         }
147     }
148
149     /**
150      * Stop all name services
151      * @throws NameServiceException if an exception occure at stoping time
152      */

153     public static void stopNS() throws NameServiceException {
154         if (TraceCarol.isDebugJndiCarol()) {
155             TraceCarol.debugJndiCarol("NameServiceManager.stopNS()");
156         }
157         // stop name services
158
for (Enumeration JavaDoc e = nsTable.keys(); e.hasMoreElements();) {
159             String JavaDoc k = (String JavaDoc) e.nextElement();
160             NameService currentNS = (NameService) nsTable.get(k);
161             currentNS.stop();
162         }
163     }
164
165     /**
166      * Main function: start all registry and wait for control C function
167      * @param args arguments
168      */

169     public static void main(String JavaDoc[] args) {
170
171         // configure logging
172
TraceCarol.configure();
173
174         try {
175             NameServiceManager.startNonStartedNS();
176             // add a shudown hook for this process
177
Runtime.getRuntime().addShutdownHook(new Thread JavaDoc() {
178
179                 public void run() {
180                     try {
181                         NameServiceManager.stopNS();
182                     } catch (Exception JavaDoc e) {
183                         TraceCarol.error("Carol Naming ShutdownHook problem", e);
184                     }
185                 }
186             });
187             while (true) {
188                 Thread.sleep(SLEEP_VALUE);
189             }
190         } catch (Exception JavaDoc e) {
191             e.printStackTrace();
192         }
193     }
194 }
Popular Tags