KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > config > ProActiveConfiguration


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.config;
32
33 import org.apache.log4j.ConsoleAppender;
34 import org.apache.log4j.Level;
35 import org.apache.log4j.Logger;
36 import org.apache.log4j.PatternLayout;
37
38 import org.objectweb.proactive.core.config.xml.MasterFileHandler;
39
40 import java.util.HashMap JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43
44 public class ProActiveConfiguration {
45     //protected static Logger logger = Logger.getLogger(ProActiveConfiguration.class);
46
protected HashMap JavaDoc loadedProperties;
47     protected HashMap JavaDoc addedProperties;
48     protected static ProActiveConfiguration singleton;
49     protected static boolean isLoaded = false;
50
51     private ProActiveConfiguration() {
52         this.loadedProperties = new HashMap JavaDoc();
53         this.addedProperties = new HashMap JavaDoc();
54         //setDefaultProActiveHome();
55
}
56
57     protected static synchronized void createConfiguration() {
58         if (ProActiveConfiguration.singleton == null) {
59             ProActiveConfiguration.singleton = new ProActiveConfiguration();
60         }
61     }
62
63     /**
64      * Load the default configuration
65      * The default file is located in the same directory as the ProActiceConfiguration
66      * class with the name proacticeConfiguration
67      * It is obtained using Class.geressource
68      * If the property proactive.configuration is set then its value is used
69      * as the configuration file
70      */

71     public static void load() {
72         if (!isLoaded) {
73             String JavaDoc filename = null;
74             if (System.getProperty("proactive.configuration") != null) {
75                 filename = System.getProperty("proactive.configuration");
76             } else {
77                 // if (logger.isDebugEnabled()) {
78
// logger.debug("******** No proactive.configuration");
79
// }
80
filename = ProActiveConfiguration.class.getResource(
81                         "ProActiveConfiguration.xml").toString();
82             }
83
84             // if (logger.isDebugEnabled()) {
85
// System.out.println("****** Reading configuration from " +
86
// filename);
87
// }
88
ProActiveConfiguration.load(filename);
89             isLoaded = true;
90         }
91     }
92
93     /**
94      * Load the configuration given in filename
95      * @param filename an XML file name
96      */

97     public static void load(String JavaDoc filename) {
98         if (!isLoaded) {
99             MasterFileHandler.createMasterFileHandler(filename,
100                 ProActiveConfiguration.getConfiguration());
101             ProActiveConfiguration.getConfiguration().addProperties();
102             isLoaded = true;
103         }
104     }
105
106     public synchronized static ProActiveConfiguration getConfiguration() {
107         if (ProActiveConfiguration.singleton == null) {
108             ProActiveConfiguration.createConfiguration();
109         }
110         return singleton;
111     }
112
113     /**
114      * Called by the parser when a property has been found
115      * @param name name of the property
116      * @param value value of the property
117      */

118     public void propertyFound(String JavaDoc name, String JavaDoc value) {
119         this.loadedProperties.put(name, value);
120     }
121
122     /**
123      * Add the loaded properties to the system
124      */

125     public void addProperties() {
126         // we don't override existing value
127
Iterator JavaDoc it = loadedProperties.keySet().iterator();
128         String JavaDoc name = null;
129         String JavaDoc value = null;
130         while (it.hasNext()) {
131             name = (String JavaDoc) it.next();
132             value = (String JavaDoc) this.loadedProperties.get(name);
133             setProperty(name, value);
134         }
135         loadDefaultProperties();
136     }
137
138     /**
139      * Dump loaded properties, i.e properties found in the configuration file
140      */

141     public void dumpLoadedProperties() {
142         Iterator JavaDoc it = loadedProperties.keySet().iterator();
143         while (it.hasNext()) {
144             String JavaDoc name = (String JavaDoc) it.next();
145
146             // System.out.println("Name = " + name);
147
// System.out.println("Value = " + this.loadedProperties.get(name));
148
}
149     }
150
151     /**
152      * Dump properties added to the system, i.e loaded properties
153      * which were not already in the system
154      */

155     public void dumpAddedProperties() {
156         Iterator JavaDoc it = addedProperties.keySet().iterator();
157         while (it.hasNext()) {
158             String JavaDoc name = (String JavaDoc) it.next();
159
160             // System.out.println("Name = " + name);
161
// System.out.println("Value = " + this.addedProperties.get(name));
162
}
163     }
164
165     public static String JavaDoc getLocationServerClass() {
166         return System.getProperty("proactive.locationserver");
167     }
168
169     public static String JavaDoc getLocationServerRmi() {
170         return System.getProperties().getProperty("proactive.locationserver.rmi");
171     }
172
173     public static String JavaDoc getACState() {
174         return System.getProperty("proactive.future.ac");
175     }
176
177     public static String JavaDoc getSchemaValidationState() {
178         return System.getProperty("schema.validation");
179     }
180
181     //To be used for the launcher
182
// /**
183
// * Sets the value of proactive.home if not already set
184
// */
185
// private void setDefaultProActiveHome() {
186
// File file = null;
187
// if (System.getProperty("proactive.home") == null) {
188
// String location = ProActiveConfiguration.class.getResource(
189
// "ProActiveConfiguration.class").getPath();
190
// try {
191
// file = new File(location, "/../../../../../../../../ProActive/").getCanonicalFile();
192
// String proactivehome = file.getCanonicalPath();
193
// System.setProperty("proactive.home", proactivehome);
194
// } catch (IOException e) {
195
// System.err.println(
196
// "WARNING: Unable to set proactive.home property. ProActive dir cannot be found! ");
197
// }
198
// }
199
// }
200

201     /**
202      * Sets mandatory properties if forgotten by users
203      */

204     private void loadDefaultProperties() {
205         setProperty("proactive.communication.protocol", "rmi");
206         setProperty("proactive.future.ac", "enable");
207         setProperty("schema.validation", "disable");
208         if (System.getProperty("log4j.configuration") == null) {
209             loadLogger();
210         }
211     }
212
213     /**
214      *
215      */

216     private void loadLogger() {
217         //if logger is not defined create default logger with level info that logs
218
// on the console
219
Logger logger = Logger.getLogger("org.objectweb.proactive");
220         logger.setAdditivity(false);
221         logger.setLevel(Level.INFO);
222         logger.addAppender(new ConsoleAppender(new PatternLayout()));
223     }
224
225     private void setProperty(String JavaDoc name, String JavaDoc value) {
226         if (System.getProperty(name) == null) {
227             System.setProperty(name, value);
228             this.addedProperties.put(name, value);
229         }
230     }
231 }
232
Popular Tags