org/objectweb/proactive/core/config/ProActiveConfiguration.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.config;
00032 
00033 import java.util.ArrayList;
00034 import java.util.HashMap;
00035 import java.util.Iterator;
00036 import java.util.List;
00037 
00038 import org.apache.log4j.ConsoleAppender;
00039 import org.apache.log4j.Level;
00040 import org.apache.log4j.Logger;
00041 import org.apache.log4j.PatternLayout;
00042 import org.objectweb.proactive.core.UniqueID;
00043 import org.objectweb.proactive.core.config.xml.MasterFileHandler;
00044 import org.objectweb.proactive.core.util.log.Loggers;
00045 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00046 
00047 
00054 public class ProActiveConfiguration {
00055     protected HashMap<String, String> loadedProperties;
00056     protected HashMap addedProperties;
00057     protected static ProActiveConfiguration singleton;
00058     protected static boolean isLoaded = false;
00059     protected static boolean defaultConfigAlreadyLoaded = false;
00060     protected static List<String> jvmDefinedProperties = new ArrayList<String>();
00061 
00062     private ProActiveConfiguration() {
00063         this.loadedProperties = new HashMap<String, String>();
00064         this.addedProperties = new HashMap();
00065     }
00066 
00067     protected static synchronized void createConfiguration() {
00068         if (ProActiveConfiguration.singleton == null) {
00069             ProActiveConfiguration.singleton = new ProActiveConfiguration();
00070         }
00071     }
00072 
00081     public static void load() {
00082         if (!isLoaded) {
00083             loadDefaultConfig();
00084             isLoaded = true;
00085         }
00086     }
00087 
00088     private static void loadDefaultConfig() {
00089         String filename = null;
00090         filename = ProActiveConfiguration.class.getResource(
00091                 "ProActiveConfiguration.xml").toString();
00092         MasterFileHandler.createMasterFileHandler(filename,
00093             ProActiveConfiguration.getConfiguration());
00094         ProActiveConfiguration.getConfiguration().loadProperties();
00095         if (System.getProperty("log4j.configuration") == null) {
00096             loadDefaultLogger();
00097         }
00098         defaultConfigAlreadyLoaded = true;
00099     }
00100 
00105     public static void load(String filename) {
00106         if (!isLoaded) {
00107             if (!defaultConfigAlreadyLoaded) {
00108                 loadDefaultConfig();
00109             }
00110             MasterFileHandler.createMasterFileHandler(filename,
00111                 ProActiveConfiguration.getConfiguration());
00112             ProActiveConfiguration.getConfiguration().loadProperties();
00113             isLoaded = true;
00114         }
00115     }
00116 
00117     public synchronized static ProActiveConfiguration getConfiguration() {
00118         if (ProActiveConfiguration.singleton == null) {
00119             ProActiveConfiguration.createConfiguration();
00120         }
00121         return singleton;
00122     }
00123 
00129     public void propertyFound(String name, String value) {
00130         this.loadedProperties.put(name, value);
00131     }
00132 
00136     public void loadProperties() {
00137         Iterator<String> it = loadedProperties.keySet().iterator();
00138         String name = null;
00139         String value = null;
00140         while (it.hasNext()) {
00141             name = it.next();
00142             value = this.loadedProperties.get(name);
00143             if (!defaultConfigAlreadyLoaded) {
00144                 // JVM parameters cannot be overriden
00145                 if (System.getProperty(name) == null) {
00146                     System.setProperty(name, value);
00147                 } else {
00148                     jvmDefinedProperties.add(name);
00149                 }
00150             } else {
00151                 if (!jvmDefinedProperties.contains(name)) {
00152                     // override default properties, except JVM defined properties
00153                     System.setProperty(name, value);
00154                 }
00155             }
00156         }
00157     }
00158 
00159     //    /**
00160     //     * Dump loaded properties, i.e properties found in the configuration file
00161     //     */
00162     //    public void dumpLoadedProperties() {
00163     //        Iterator it = loadedProperties.keySet().iterator();
00164     //        while (it.hasNext()) {
00165     //            String name = (String) it.next();
00166     //
00167     //            //            System.out.println("Name = " + name);
00168     //            //            System.out.println("Value = " + this.loadedProperties.get(name));
00169     //        }
00170     //    }
00171     //
00172     //    /**
00173     //     * Dump properties added to the system, i.e loaded properties
00174     //     * which were not already in the system
00175     //     */
00176     //    public void dumpAddedProperties() {
00177     //        Iterator it = addedProperties.keySet().iterator();
00178     //        while (it.hasNext()) {
00179     //            String name = (String) it.next();
00180     //
00181     //            //            System.out.println("Name = " + name);
00182     //            //            System.out.println("Value = " + this.addedProperties.get(name));
00183     //        }
00184     //    }
00185     public static String getLocationServerClass() {
00186         return System.getProperty("proactive.locationserver");
00187     }
00188 
00189     public static String getLocationServerRmi() {
00190         return System.getProperties().getProperty("proactive.locationserver.rmi");
00191     }
00192 
00193     public static String getACState() {
00194         return System.getProperty("proactive.future.ac");
00195     }
00196 
00197     public static String getSchemaValidationState() {
00198         return System.getProperty("schema.validation");
00199     }
00200 
00201     // FAULT TOLERANCE
00202     public static String getFTState() {
00203         return System.getProperty("proactive.ft");
00204     }
00205 
00206     public static String getCheckpointServer() {
00207         return System.getProperty("proactive.ft.server.checkpoint");
00208     }
00209 
00210     public static String getLocationServer() {
00211         return System.getProperty("proactive.ft.server.location");
00212     }
00213 
00214     public static String getRecoveryServer() {
00215         return System.getProperty("proactive.ft.server.recovery");
00216     }
00217 
00218     public static String getGlobalFTServer() {
00219         return System.getProperty("proactive.ft.server.global");
00220     }
00221 
00222     public static String getTTCValue() {
00223         return System.getProperty("proactive.ft.ttc");
00224     }
00225 
00226     public static String getAttachedResourceServer() {
00227         return System.getProperty("proactive.ft.server.resource");
00228     }
00229 
00230     public static String getFTProtocol() {
00231         return System.getProperty("proactive.ft.protocol");
00232     }
00233     
00234     public static boolean osgiServletEnabled() {
00235         return "enabled".equals(System.getProperty("proactive.http.servlet"));
00236     }
00237 
00238     // Cached value since isForwarder is frequently called
00239     private static boolean isForwarder;
00240 
00241     static {
00242         String prop = System.getProperty("proactive.hierarchicalRuntime");
00243         isForwarder = ((prop != null) &&
00244             (prop.equals("true") || prop.equals("root")));
00245     }
00246 
00247     public static boolean isForwarder() {
00248         return isForwarder;
00249     }
00250 
00251     public static String getGroupInformation() {
00252         return System.getProperty("proactive.groupInformation",
00253             UniqueID.getCurrentVMID().toString() + "~-1");
00254     }
00255 
00256     //To be used for the launcher 
00257     //    /**
00258     //     * Sets the value of proactive.home if not already set
00259     //     */
00260     //    private void setDefaultProActiveHome() {
00261     //        File file = null;
00262     //        if (System.getProperty("proactive.home") == null) {
00263     //            String location = ProActiveConfiguration.class.getResource(
00264     //                    "ProActiveConfiguration.class").getPath();
00265     //            try {
00266     //                file = new File(location, "/../../../../../../../../ProActive/").getCanonicalFile();
00267     //                String proactivehome = file.getCanonicalPath();
00268     //                System.setProperty("proactive.home", proactivehome);
00269     //            } catch (IOException e) {
00270     //                System.err.println(
00271     //                    "WARNING: Unable to set proactive.home property. ProActive dir cannot be found! ");
00272     //            }
00273     //        }
00274     //    }
00275 
00279     private static void loadDefaultLogger() {
00280         //if logger is not defined create default logger with level info that logs
00281         // on the console
00282         Logger logger = ProActiveLogger.getLogger(Loggers.CORE);
00283         logger.setAdditivity(false);
00284         logger.setLevel(Level.INFO);
00285         logger.addAppender(new ConsoleAppender(new PatternLayout()));
00286     }
00287 }

Generated on Mon Jan 22 15:16:07 2007 for ProActive by  doxygen 1.5.1