KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > util > configuration > ServerConfiguration


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

25 package org.objectweb.carol.util.configuration;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31
32 /**
33  * This class handle the configuration of carol (independent of protocols
34  * configuration) It doesn't implement ProtocolConfiguration interface which is
35  * dedicated to protocols configuration
36  * @author Florent Benoit
37  */

38 public class ServerConfiguration {
39
40     /**
41      * Properties used for this server
42      */

43     private Properties JavaDoc properties = null;
44
45     /**
46      * Start nameservice of protocols ?
47      */

48     private boolean startNS = false;
49
50     /**
51      * Set MultiORB InitialContext factory ?
52      */

53     private boolean startJNDI = false;
54
55     /**
56      * Set MultiProDelegate class ?
57      */

58     private boolean startRMI = false;
59
60     /**
61      * Build a server configuration object with the given properties
62      * @param properties the properties need to construct the configuration
63      * @throws ConfigurationException if properties are missing
64      */

65     protected ServerConfiguration(Properties JavaDoc properties) throws ConfigurationException {
66         this.properties = properties;
67         if (properties == null) {
68             throw new ConfigurationException("Cannot build a server configuration withotu properties");
69         }
70
71         // Init values with properties object
72
startNS = getBooleanValue(CarolDefaultValues.START_NS_KEY);
73         startRMI = getBooleanValue(CarolDefaultValues.START_RMI_KEY);
74         startJNDI = getBooleanValue(CarolDefaultValues.START_JNDI_KEY);
75
76         Properties JavaDoc jvmProperties = new Properties JavaDoc();
77         if (startRMI) {
78             jvmProperties.setProperty("javax.rmi.CORBA.PortableRemoteObjectClass", CarolDefaultValues.MULTI_PROD);
79         }
80
81         if (startJNDI) {
82             jvmProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY, CarolDefaultValues.MULTI_JNDI);
83         }
84
85         String JavaDoc protocols = properties.getProperty(CarolDefaultValues.PROTOCOLS_KEY);
86         boolean isMultiProtocols = false;
87         if (protocols != null) {
88             isMultiProtocols = (protocols.split(",").length > 1);
89         }
90
91
92
93         String JavaDoc jndiPrefix = CarolDefaultValues.CAROL_PREFIX + "." + CarolDefaultValues.JNDI_PREFIX;
94         String JavaDoc multiJvmPrefix = CarolDefaultValues.MULTI_RMI_PREFIX + "." + CarolDefaultValues.CAROL_PREFIX + "." + "jvm";
95         String JavaDoc singleJvmPrefix = CarolDefaultValues.CAROL_PREFIX + "." + CarolDefaultValues.JVM_PREFIX;
96         for (Enumeration JavaDoc e = properties.propertyNames(); e.hasMoreElements();) {
97             String JavaDoc key = (String JavaDoc) e.nextElement();
98             // JNDI JVM properties
99
if (key.startsWith(jndiPrefix)) {
100                 jvmProperties.setProperty(key.substring(jndiPrefix.length() + 1), properties.getProperty(key));
101             }
102
103             // Interceptor multi (only in multi protocol)
104
if (key.startsWith(multiJvmPrefix) && isMultiProtocols) {
105                 // extract key
106
String JavaDoc newKey = key.substring(multiJvmPrefix.length() + 1);
107                 jvmProperties.setProperty(newKey, "");
108             }
109
110             // JVM properties
111
if (key.startsWith(singleJvmPrefix)) {
112                 // extract key
113
String JavaDoc newKey = key.substring(singleJvmPrefix.length() + 1);
114                 jvmProperties.setProperty(newKey, properties.getProperty(key));
115             }
116
117         }
118
119
120         // Set jvm Properties previously defined
121
for (Enumeration JavaDoc e = jvmProperties.propertyNames(); e.hasMoreElements();) {
122             String JavaDoc key = (String JavaDoc) e.nextElement();
123             String JavaDoc value = jvmProperties.getProperty(key);
124             System.setProperty(key, value);
125             if (TraceCarol.isDebugCarol()) {
126                 TraceCarol.debugCarol("Set the JVM property '" + key + "' with the value '" + value + "'.");
127             }
128         }
129
130     }
131
132     /**
133      * Gets value of properties object
134      * @param key the key of the properties
135      * @return value stored in a property object
136      * @throws ConfigurationException if properties are missing
137      */

138     protected boolean getBooleanValue(String JavaDoc key) throws ConfigurationException {
139         // properties cannot be null, check in constructor
140
String JavaDoc s = properties.getProperty(key);
141         if (s == null) {
142             throw new ConfigurationException("Property '" + key
143                     + "' was not found in the properties object of the protocol, properties are :'" + properties + "'");
144         }
145         return new Boolean JavaDoc(s.trim()).booleanValue();
146     }
147
148     /**
149      * @return true if JNDI has to be started (set of MultiORB ICTX factory)
150      */

151     public boolean isStartingJNDI() {
152         return startJNDI;
153     }
154
155     /**
156      * @return true if name services have to be launched)
157      */

158     public boolean isStartingNS() {
159         return startNS;
160     }
161
162     /**
163      * @return true if ProdelegateClass has to be set to MultiProDelegate
164      */

165     public boolean isStartingRMI() {
166         return startRMI;
167     }
168
169 }
170
Popular Tags