|
From: Erik V. <ev...@us...> - 2012-03-13 14:41:28
|
rails/common/ConfigProfile.java | 115 +++++++++++++++++++---------------------
1 file changed, 57 insertions(+), 58 deletions(-)
New commits:
commit 089e86da5284d15b956862b8de9ba9df3ba3b3b3
Author: Erik Vos <eri...@xs...>
Date: Tue Mar 13 15:40:04 2012 +0100
Fixed Config.profile.SetParent() so it no longer crashes if parent is null.
diff --git a/rails/common/ConfigProfile.java b/rails/common/ConfigProfile.java
index c6bcb7d..ca418d3 100644
--- a/rails/common/ConfigProfile.java
+++ b/rails/common/ConfigProfile.java
@@ -2,10 +2,7 @@ package rails.common;
import java.io.File;
import java.io.FilenameFilter;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
+import java.util.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
@@ -22,13 +19,13 @@ import rails.util.Util;
public final class ConfigProfile implements Comparable<ConfigProfile> {
protected static Logger log =
- Logger.getLogger(ConfigProfile.class.getPackage().getName());
-
+ Logger.getLogger(ConfigProfile.class.getPackage().getName());
+
// available profile types
public enum Type {SYSTEM(0), PREDEFINED(1), USER(2);
- private Integer sort; Type(int sort) {this.sort = sort;}
+ private Integer sort; Type(int sort) {this.sort = sort;}
};
-
+
// Filename extension of profiles
public static final String PROFILE_EXTENSION = ".rails_profile";
private static final String PREDEFINED_EXTENSION = ".predefined";
@@ -38,57 +35,57 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
private static final String PROFILE_FOLDER = "profiles/";
// predefined inside jar
private static final String PREDEFINED_FOLDER = "data/profiles/";
-
+
// predefined default profiles
private static final String ROOT_PROFILE = "root";
private static final String TEST_PROFILE = "test";
-
+
// the profile selected at the start ...
private static final String STANDARD_PROFILE = "pbem";
// ... unless a cli option has been set
private static final String CLI_AND_RECENT_OPTION ="profile";
-
-
+
+
// file that stores the list of predefined profiles
private static final String LIST_OF_PROFILES = "LIST_OF_PROFILES";
-
+
// property key of predefined profile in user profile
private static final String PARENT_KEY = "profile.parent";
private static final String FINAL_KEY = "profile.final";
// map of all profiles
- private static final Map<String, ConfigProfile> profiles = new HashMap<String, ConfigProfile>();
-
+ private static final Map<String, ConfigProfile> profiles = new HashMap<String, ConfigProfile>();
+
// root profile
private static ConfigProfile root;
-
+
// profile type
private final Type type;
-
+
// profile name
private final String name;
// profile properties
private final Properties properties = new Properties();
-
+
// profile loaded
private boolean loaded = false;
-
+
// profile parent
private ConfigProfile parent = null;
-
+
static void loadRoot() {
root = new ConfigProfile(Type.SYSTEM, ROOT_PROFILE);
root.load();
}
-
+
static ConfigProfile loadTest() {
ConfigProfile test = new ConfigProfile(Type.SYSTEM, TEST_PROFILE);
test.load();
return test;
}
-
+
static void readPredefined() {
Properties list = new Properties();
String filePath = PREDEFINED_FOLDER + LIST_OF_PROFILES;
@@ -97,7 +94,7 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
new ConfigProfile(Type.PREDEFINED, name);
}
}
-
+
static void readUser() {
File userFolder = SystemOS.get().getConfigurationFolder(PROFILE_FOLDER, false);
if (userFolder == null) return;
@@ -106,30 +103,30 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
new ConfigProfile(Type.USER, FilenameUtils.getBaseName(fileName));
}
}
-
+
static ConfigProfile getStartProfile() {
// first checks cli
String profile = System.getProperty(CLI_AND_RECENT_OPTION);
if (Util.hasValue(profile) && profiles.containsKey(profile)) {
return profiles.get(profile);
- }
+ }
// second check recent
profile = Config.getRecent(CLI_AND_RECENT_OPTION);
if (Util.hasValue(profile) && profiles.containsKey(profile)) {
return profiles.get(profile);
- }
+ }
// third return standard profile
return profiles.get(STANDARD_PROFILE);
}
-
+
static ConfigProfile getProfile(String name) {
return profiles.get(name);
}
-
+
static Collection<ConfigProfile> getProfiles() {
return profiles.values();
}
-
+
private ConfigProfile(Type type, String name) {
this.type = type;
this.name = name;
@@ -137,34 +134,36 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
profiles.put(name, this);
}
}
-
+
public Type getType() {
return type;
}
-
+
public String getName() {
return name;
}
-
+
boolean isLoaded() {
return loaded;
}
-
+
boolean isFinal() {
if (!loaded && type == Type.USER) return true;
-
+
if (Util.hasValue(properties.getProperty(FINAL_KEY))) {
return Util.parseBoolean(properties.getProperty(FINAL_KEY));
}
return false;
}
-
+
ConfigProfile setParent(ConfigProfile parent) {
- this.parent = parent;
- properties.setProperty(PARENT_KEY, parent.getName());
+ if (parent != null) {
+ this.parent = parent;
+ properties.setProperty(PARENT_KEY, parent.getName());
+ }
return this;
}
-
+
private ConfigProfile setParent(String name) {
return setParent(profiles.get(name));
}
@@ -172,7 +171,7 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
ConfigProfile getParent() {
return parent;
}
-
+
String getProperty(String key) {
if (this == root || properties.containsKey(key)) {
return properties.getProperty(key);
@@ -180,7 +179,7 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
return parent.getProperty(key);
}
}
-
+
void setProperty(String key, String value) {
if (!parent.getProperty(key).equals(value)) {
properties.setProperty(key, value);
@@ -188,8 +187,8 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
properties.remove(key);
}
}
-
-
+
+
void makeActive(){
// check if is already loaded
if (!isLoaded()) {
@@ -201,13 +200,13 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
ConfigProfile deriveUserProfile(String name) {
ConfigProfile newProfile = new ConfigProfile(Type.USER, name);
-
+
ConfigProfile reference;
if (isFinal()) {
// set reference for final to the own parent
reference = parent;
- } else {
- // otherwise to this
+ } else {
+ // otherwise to this
reference = this;
}
newProfile.setParent(reference);
@@ -218,16 +217,16 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
newProfile.setProperty(key, properties.getProperty(key));
}
}
-
+
return newProfile;
}
-
+
boolean load() {
// loaded is set independent of success
loaded = true;
// ... the same for clearing the current selection
properties.clear();
-
+
// loading
boolean result;
if (type == Type.USER) {
@@ -244,21 +243,21 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
if (parent == null) {
setParent(root);
}
-
- // set save directory to the working directory for predefined values
+
+ // set save directory to the working directory for predefined values
// TODO: This is a hack as workaround to be replaced in the future
if (type == Type.PREDEFINED && !Util.hasValue(properties.getProperty("save.directory"))) {
properties.put("save.directory", System.getProperty("user.dir"));
}
-
+
// check if parent has been loaded, otherwise load parent
if (!parent.isLoaded()) {
result = result && parent.load();
}
-
+
return result;
}
-
+
private boolean loadUser() {
File folder = SystemOS.get().getConfigurationFolder(PROFILE_FOLDER, false);
if (folder == null) {
@@ -266,9 +265,9 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
} else {
File profile = new File(folder, name + PROFILE_EXTENSION);
return Util.loadProperties(properties, profile);
- }
+ }
}
-
+
private boolean loadResource(){
String filePath = null;
switch(type) {
@@ -281,13 +280,13 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
}
return Util.loadPropertiesFromResource(properties, filePath);
}
-
+
boolean store() {
if (type != Type.USER) return false;
loaded = true;
File folder = SystemOS.get().getConfigurationFolder(PROFILE_FOLDER, true);
if (folder == null) {
- return false;
+ return false;
} else {
File profile = new File(folder, name + PROFILE_EXTENSION);
return Util.storeProperties(properties, profile);
@@ -295,7 +294,7 @@ public final class ConfigProfile implements Comparable<ConfigProfile> {
}
private int compare(ConfigProfile a, ConfigProfile b) {
- if (a.type.sort != b.type.sort) {
+ if (a.type.sort != b.type.sort) {
return a.type.sort.compareTo(b.type.sort);
} else {
return a.getName().compareTo(b.getName());
|