From: <ev...@us...> - 2010-07-18 16:36:44
|
Revision: 1349 http://rails.svn.sourceforge.net/rails/?rev=1349&view=rev Author: evos Date: 2010-07-18 16:36:38 +0000 (Sun, 18 Jul 2010) Log Message: ----------- Suspended logger initialization until after the system property log4j.configuration is set; otherwise teh latter is just ignored. Modified Paths: -------------- trunk/18xx/rails/util/Config.java Modified: trunk/18xx/rails/util/Config.java =================================================================== --- trunk/18xx/rails/util/Config.java 2010-07-18 08:28:37 UTC (rev 1348) +++ trunk/18xx/rails/util/Config.java 2010-07-18 16:36:38 UTC (rev 1349) @@ -1,18 +1,8 @@ /* $Header: /Users/blentz/rails_rcs/cvs/18xx/rails/util/Config.java,v 1.13 2010/06/24 21:48:08 stefanfrey Exp $*/ package rails.util; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.io.*; +import java.util.*; import org.apache.log4j.Logger; @@ -29,8 +19,7 @@ */ public final class Config { - protected static Logger log = - Logger.getLogger(Config.class.getPackage().getName()); + protected static Logger log; /** Commandline options */ private static final String LOG4J_CMDLINE = "log4j"; @@ -46,7 +35,7 @@ /** Log 4j configuration */ private static final String LOG4J_CONFIG_FILE = "log4j.properties"; - + /** Rails profile configurations */ private static String defaultProfilesFile = "default.profiles"; private static Properties defaultProfiles = new Properties(); @@ -57,25 +46,25 @@ private static final String DEFAULT_PROFILE_SELECTION = "default"; private static final String DEFAULT_PROFILE_PROPERTY = "default.profile"; private static final String STANDARD_PROFILE_SELECTION = "user"; - + /** selected profile */ private static String selectedProfile; private static boolean legacyConfigFile; - + /** properties storage. */ private static Properties defaultProperties = new Properties(); private static Properties userProperties = new Properties(); private static boolean propertiesLoaded = false; - + /** Map that holds the panel, which contains config items */ - private static Map<String, List<ConfigItem>> configPanels = null; - + private static Map<String, List<ConfigItem>> configPanels = null; + /** * Hidden constructor, the class is never instantiated, everything is static */ private Config() {} - /** + /** * Reads the config.xml file that defines all config items */ public static void readConfigSetupXML() { @@ -86,7 +75,7 @@ Tag configTag = Tag.findTopTagInFile(CONFIG_XML_FILE, directories, CONFIG_TAG); log.debug("Opened config xml, filename = " + CONFIG_XML_FILE); - + configPanels = new LinkedHashMap<String, List<ConfigItem>>(); // find panels List<Tag> panelTags = configTag.getChildren(PANEL_TAG); @@ -95,7 +84,7 @@ // find name attribute String panelName = panelTag.getAttributeAsString("name"); if (!Util.hasValue(panelName)) continue; - + // find items List<Tag> itemTags = panelTag.getChildren(ITEM_TAG); if (itemTags == null || itemTags.size() == 0) continue; @@ -106,12 +95,12 @@ configPanels.put(panelName, panelItems); } } - + } catch (ConfigurationException e) { log.error("Configuration error in setup of "); } } - + public static Map<String, List<ConfigItem>> getConfigPanels() { if (configPanels == null) { readConfigSetupXML(); @@ -119,14 +108,14 @@ log.debug("Configuration setup = " + configPanels); return configPanels; } - + /** - * First tries to return {key}.{gameName}, if undefined returns {key} + * First tries to return {key}.{gameName}, if undefined returns {key} */ public static String getGameSpecific(String key) { return Config.getSpecific(key, GameManager.getInstance().getGameName()); } - + /** * First tries to return {key}.{appendix}, if undefined returns {key} */ @@ -137,11 +126,11 @@ } return value; } - + public static String get(String key) { return get(key, ""); } - + public static String get(String key, String defaultValue) { if (defaultProperties.isEmpty() || !propertiesLoaded) { initialLoad(); @@ -151,19 +140,19 @@ return defaultValue; } - + private static boolean storePropertyFile(Properties properties, String filepath) { File outFile = new File(filepath); boolean result = true; - try { + try { properties.store(new FileOutputStream(outFile), "Automatically generated, do not edit"); } catch (IOException e) { result = false; } return result; } - + /** * save active Profile */ @@ -175,9 +164,9 @@ return false; } } - + /** - * change active Profile + * change active Profile */ public static boolean changeActiveProfile(String profileName) { readConfigSetupXML(); @@ -185,7 +174,7 @@ selectedProfile = profileName; return true; } - + /** * create new profile */ @@ -193,7 +182,7 @@ userProperties = new Properties(); defaultProperties = new Properties(); - + // add to list of user profiles userProfiles.setProperty(profileName, ""); // define and load default profile @@ -205,8 +194,8 @@ selectedProfile = profileName; return true; } - - + + private static Map<String, String> convertProperties(Properties properties) { Map<String, String> converted = new HashMap<String, String>(); for (Object key:properties.keySet()) { @@ -214,50 +203,50 @@ } return converted; } - - /** - * get all default profiles + + /** + * get all default profiles */ public static List<String> getDefaultProfiles() { List<String> profiles = new ArrayList<String>(convertProperties(defaultProfiles).keySet()); Collections.sort(profiles); return profiles; } - + public static String getDefaultProfileSelection() { return DEFAULT_PROFILE_SELECTION; } - /** - * get all user profiles + /** + * get all user profiles */ public static List<String> getUserProfiles() { List<String> profiles = new ArrayList<String>(convertProperties(userProfiles).keySet()); Collections.sort(profiles); return profiles; } - + /** * returns name of (active) default profile */ public static String getDefaultProfileName() { return userProperties.getProperty(DEFAULT_PROFILE_PROPERTY); } - + /** * returns name of active profile */ public static String getActiveProfileName() { return selectedProfile; } - + /** * returns true if legacy configfile is used */ public static boolean isLegacyConfigFile() { return legacyConfigFile; } - + /** * sets filename for an active profile (and store list of profiles) */ @@ -265,7 +254,7 @@ userProfiles.setProperty(selectedProfile, filepath); return storePropertyFile(userProfiles, userProfilesFile); } - + /** * returns filename of active profile, (null if undefined or default profile) */ @@ -280,7 +269,7 @@ return Util.hasValue(userProfiles.getProperty(selectedProfile)); } - + /** * activates settings used for testing */ @@ -295,7 +284,7 @@ initialLoad(); } - + /** * activates configuration settings based on default settings */ @@ -310,7 +299,9 @@ } System.setProperty("log4j.configuration", log4jSelection); System.out.println("Log4j selection = " + log4jSelection); - + + log = Logger.getLogger(Config.class.getPackage().getName()); + /* * Check if the profile has been set from the command line * to do this is adding an option to the java command: -Dprofile=<profile-name> @@ -323,31 +314,31 @@ /* * Check if the property file has been set on the command line. The way * to do this is adding an option to the java command: -Dconfigfile=<property-filename> - * + * * This is for legacy reasons only */ configSelection = System.getProperty(CONFIGFILE_CMDLINE); - + if (Util.hasValue(configSelection)) { System.out.println("Cmdline configfile selection (legacy!) = " + configSelection); legacyConfigFile = true; } } - + /* if nothing has selected so far, choose standardProfile */ if (!Util.hasValue(configSelection)) { configSelection = STANDARD_PROFILE_SELECTION; } - + selectedProfile = configSelection; if (!legacyConfigFile) { System.out.println("Profile selection = " + selectedProfile); } - + initialLoad(); } - + private static void initialLoad() { if (legacyConfigFile) { if (!propertiesLoaded) { @@ -357,13 +348,13 @@ } return; } - + if (!profilesLoaded) { loadPropertyFile(defaultProfiles, defaultProfilesFile, true); loadPropertyFile(userProfiles, userProfilesFile, false); profilesLoaded = true; } - + /* Tell the properties loader to read this file. */ log.info("Selected profile = " + selectedProfile); @@ -372,7 +363,7 @@ propertiesLoaded = true; } } - + /** * loads a user profile and the according default profile * if not defined or loadable, creates a default user profile @@ -381,8 +372,8 @@ // reset properties userProperties = new Properties(); defaultProperties = new Properties(); - - // check if the profile is already defined under userProfiles + + // check if the profile is already defined under userProfiles String userConfigFile = userProfiles.getProperty(userProfile); String defaultConfigFile = null; if (Util.hasValue(userConfigFile) && // load user profile @@ -410,13 +401,13 @@ * @return TRUE if load was successful */ private static boolean loadPropertyFile(Properties properties, String filename, boolean resource) { - - boolean result = true; + + boolean result = true; try { log.info("Loading properties from file " + filename); InputStream inFile; if (resource) { - inFile = Config.class.getClassLoader().getResourceAsStream(filename); + inFile = Config.class.getClassLoader().getResourceAsStream(filename); } else { inFile = new FileInputStream(filename); } @@ -429,7 +420,7 @@ } return result; } - + private static void setSaveDirDefaults() { if (!Util.hasValue(defaultProperties.getProperty("save.directory"))) { log.debug("Setting save directory to "+System.getProperty("user.dir")); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |