|
From: <ste...@us...> - 2010-07-18 17:24:42
|
Revision: 1350
http://rails.svn.sourceforge.net/rails/?rev=1350&view=rev
Author: stefanfrey
Date: 2010-07-18 17:24:36 +0000 (Sun, 18 Jul 2010)
Log Message:
-----------
Further updates to the configuration mechanism and integration of Eriks fix
Modified Paths:
--------------
trunk/18xx/rails/util/Config.java
trunk/18xx/rails/util/ConfigItem.java
trunk/18xx/rails/util/LocalText.java
Modified: trunk/18xx/rails/util/Config.java
===================================================================
--- trunk/18xx/rails/util/Config.java 2010-07-18 16:36:38 UTC (rev 1349)
+++ trunk/18xx/rails/util/Config.java 2010-07-18 17:24:36 UTC (rev 1350)
@@ -1,8 +1,18 @@
/* $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.*;
-import java.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 org.apache.log4j.Logger;
@@ -35,7 +45,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();
@@ -44,27 +54,28 @@
private static boolean profilesLoaded = false;
private static final String TEST_PROFILE_SELECTION = "test";
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";
-
+ private static final String DEFAULTPROFILE_PROPERTY = "default.profile";
+ private static final String PROFILENAME_PROPERTY = "profile.name";
+
/** 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() {
@@ -75,7 +86,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);
@@ -84,7 +95,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;
@@ -95,12 +106,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();
@@ -108,14 +119,28 @@
log.debug("Configuration setup = " + configPanels);
return configPanels;
}
-
+
/**
- * First tries to return {key}.{gameName}, if undefined returns {key}
+ * updates the profile according to the changes in configitems
*/
+ public static void updateProfile() {
+ for (List<ConfigItem> items:configPanels.values()) {
+ for (ConfigItem item:items) {
+ if (!item.hasNewValue() || item.getNewValue().equals(defaultProperties.get(item.name))) continue;
+ userProperties.setProperty(item.name, item.getNewValue());
+ log.debug("Changed property name = " + item.name + " to value = " + item.getNewValue());
+ item.setNewValue(null);
+ }
+ }
+ }
+
+ /**
+ * 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}
*/
@@ -126,11 +151,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();
@@ -140,19 +165,8 @@
return defaultValue;
}
+
-
- private static boolean storePropertyFile(Properties properties, String filepath) {
- File outFile = new File(filepath);
- boolean result = true;
- try {
- properties.store(new FileOutputStream(outFile), "Automatically generated, do not edit");
- } catch (IOException e) {
- result = false;
- }
- return result;
- }
-
/**
* save active Profile
*/
@@ -164,38 +178,38 @@
return false;
}
}
-
+
/**
- * change active Profile
+ * change active Profile
*/
public static boolean changeActiveProfile(String profileName) {
readConfigSetupXML();
- loadPropertyProfile(profileName);
+ loadProfile(profileName);
selectedProfile = profileName;
return true;
}
-
+
/**
* create new profile
*/
public static boolean createUserProfile(String profileName, String defaultProfile) {
userProperties = new Properties();
defaultProperties = new Properties();
-
-
+
// add to list of user profiles
userProfiles.setProperty(profileName, "");
// define and load default profile
String defaultConfigFile = defaultProfiles.getProperty(defaultProfile);
- userProperties.setProperty(DEFAULT_PROFILE_PROPERTY, defaultProfile);
+ userProperties.setProperty(PROFILENAME_PROPERTY, profileName);
+ userProperties.setProperty(DEFAULTPROFILE_PROPERTY, defaultProfile);
loadPropertyFile(defaultProperties, defaultConfigFile, true);
setSaveDirDefaults();
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()) {
@@ -203,50 +217,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);
+ return userProperties.getProperty(DEFAULTPROFILE_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)
*/
@@ -254,7 +268,7 @@
userProfiles.setProperty(selectedProfile, filepath);
return storePropertyFile(userProfiles, userProfilesFile);
}
-
+
/**
* returns filename of active profile, (null if undefined or default profile)
*/
@@ -269,7 +283,7 @@
return Util.hasValue(userProfiles.getProperty(selectedProfile));
}
-
+
/**
* activates settings used for testing
*/
@@ -278,13 +292,22 @@
* Set the system property that tells log4j to use this file. (Note:
* this MUST be done before updating Config)
*/
- System.setProperty("log4j.configuration", LOG4J_CONFIG_FILE);
+ String log4jSelection = System.getProperty("log4j.configuration");
+ if (!Util.hasValue(log4jSelection)) {
+ log4jSelection = LOG4J_CONFIG_FILE;
+ }
+ System.setProperty("log4j.configuration", log4jSelection);
+ System.out.println("log4j.configuration = " + log4jSelection);
+
+ // delayed setting of logger
+ log = Logger.getLogger(Config.class.getPackage().getName());
+
legacyConfigFile = false;
selectedProfile = TEST_PROFILE_SELECTION;
initialLoad();
}
-
+
/**
* activates configuration settings based on default settings
*/
@@ -293,15 +316,16 @@
* Set the system property that tells log4j to use this file. (Note:
* this MUST be done before updating Config)
*/
- String log4jSelection = System.getProperty(LOG4J_CMDLINE);
+ String log4jSelection = System.getProperty("log4j.configuration");
if (!Util.hasValue(log4jSelection)) {
log4jSelection = LOG4J_CONFIG_FILE;
}
System.setProperty("log4j.configuration", log4jSelection);
- System.out.println("Log4j selection = " + log4jSelection);
+ System.out.println("log4j.configuration = " + log4jSelection);
+ // delayed setting of logger
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>
@@ -314,31 +338,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) {
@@ -348,79 +372,137 @@
}
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);
if (!propertiesLoaded) {
- loadPropertyProfile(selectedProfile);
+ loadProfile(selectedProfile);
propertiesLoaded = true;
}
}
-
+
+
/**
- * loads a user profile and the according default profile
+ * loads an external user profile
+ * defined by the filepath
+ */
+ public static boolean loadProfileFromFile(File file) {
+ String filepath = file.getAbsolutePath();
+ if (loadPropertyFile(userProperties, filepath, false)) {
+ String profile = userProperties.getProperty(PROFILENAME_PROPERTY);
+ if (!Util.hasValue(profile)) {
+ profile = STANDARD_PROFILE_SELECTION;
+ }
+ selectedProfile = profile;
+ setActiveFilepath(filepath);
+ loadDefaultProfile();
+ setSaveDirDefaults();
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+
+ /**
+ * loads a user profile
* if not defined or loadable, creates a default user profile
*/
- private static void loadPropertyProfile(String userProfile) {
+ private static void loadProfile(String userProfile) {
// 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
loadPropertyFile(userProperties, userConfigFile, false)) {
- String defaultConfig = userProperties.getProperty(DEFAULT_PROFILE_PROPERTY);
- if (defaultConfig != null) {
- defaultConfigFile = defaultProfiles.getProperty(defaultConfig);
- }
- } else {
+ // do nothing, only side effects
+ } else { // if not defined or loadable, define userprofile with file association
userProfiles.setProperty(userProfile, "");
}
+
+ // check if profilename is defined in user properties
+ if (!Util.hasValue(userProfiles.getProperty(PROFILENAME_PROPERTY))) {
+ userProperties.setProperty(PROFILENAME_PROPERTY, userProfile);
+ }
+ loadDefaultProfile();
+ setSaveDirDefaults();
+ }
+
+ /**
+ * loads the associated default profile
+ * if none is defined, uses standard default profile
+ */
+ private static void loadDefaultProfile() {
+ String defaultConfigFile = null;
+ String defaultConfig = userProperties.getProperty(DEFAULTPROFILE_PROPERTY);
+ if (defaultConfig != null) {
+ defaultConfigFile = defaultProfiles.getProperty(defaultConfig);
+ }
if (defaultConfigFile == null) {
defaultConfigFile = defaultProfiles.getProperty(DEFAULT_PROFILE_SELECTION);
- userProperties.setProperty(DEFAULT_PROFILE_PROPERTY, DEFAULT_PROFILE_SELECTION);
+ userProperties.setProperty(DEFAULTPROFILE_PROPERTY, DEFAULT_PROFILE_SELECTION);
}
loadPropertyFile(defaultProperties, defaultConfigFile, true);
- setSaveDirDefaults();
}
/**
* This method loads a property file.
*
- * @param filename - file key name as a String.
+ * @param properties - the property to store
+ * @param filepath - filname as a String.
* @param resource - if TRUE, loaded from jar (via classloader), otherwise from filesystem
* @return TRUE if load was successful
*/
- private static boolean loadPropertyFile(Properties properties, String filename, boolean resource) {
-
- boolean result = true;
+ private static boolean loadPropertyFile(Properties properties, String filepath, boolean resource) {
+
+ boolean result = true;
try {
- log.info("Loading properties from file " + filename);
+ log.info("Loading properties from file " + filepath);
InputStream inFile;
if (resource) {
- inFile = Config.class.getClassLoader().getResourceAsStream(filename);
+ inFile = Config.class.getClassLoader().getResourceAsStream(filepath);
} else {
- inFile = new FileInputStream(filename);
+ inFile = new FileInputStream(filepath);
}
properties.load(inFile);
} catch (Exception e) {
System.err.println(e + " whilst loading properties file "
- + filename);
+ + filepath);
// e.printStackTrace(System.err);
result = false;
}
return result;
}
+ /**
+ * This method stores a property file
+ * @param properties -
+ * @param filepath
+ * @return
+ */
+
+ private static boolean storePropertyFile(Properties properties, String filepath) {
+ File outFile = new File(filepath);
+ boolean result = true;
+ try {
+ properties.store(new FileOutputStream(outFile), "Automatically generated, do not edit");
+ log.info("Storing properties to file " + filepath);
+ } catch (IOException e) {
+ result = false;
+ }
+ return result;
+ }
+
+
private static void setSaveDirDefaults() {
if (!Util.hasValue(defaultProperties.getProperty("save.directory"))) {
log.debug("Setting save directory to "+System.getProperty("user.dir"));
Modified: trunk/18xx/rails/util/ConfigItem.java
===================================================================
--- trunk/18xx/rails/util/ConfigItem.java 2010-07-18 16:36:38 UTC (rev 1349)
+++ trunk/18xx/rails/util/ConfigItem.java 2010-07-18 17:24:36 UTC (rev 1350)
@@ -68,7 +68,7 @@
formatMask = tag.getAttributeAsString("formatMask");
// optional: helpText
- helpText = tag.getAttributeAsString("formatMask");
+ helpText = tag.getAttributeAsString("helpText");
newValue = null;
}
@@ -82,7 +82,7 @@
}
public void setNewValue(String newValue) {
- if (newValue.equals(getConfigValue())) {
+ if (newValue == null || newValue.equals(getConfigValue())) {
this.newValue = null;
} else {
this.newValue = newValue;
Modified: trunk/18xx/rails/util/LocalText.java
===================================================================
--- trunk/18xx/rails/util/LocalText.java 2010-07-18 16:36:38 UTC (rev 1349)
+++ trunk/18xx/rails/util/LocalText.java 2010-07-18 17:24:36 UTC (rev 1350)
@@ -83,8 +83,8 @@
}
}
- // special treatment for test
- if (localeCode.equals("te_st0")) {
+ // special treatment for te_ST (test)
+ if (localeCode.equals("te_ST")) {
StringBuffer s = new StringBuffer(key);
if (parameters != null)
for (Object o:parameters)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|