Index: lutinutil/src/java/org/codelutin/option/Config.java diff -u lutinutil/src/java/org/codelutin/option/Config.java:1.10 lutinutil/src/java/org/codelutin/option/Config.java:1.11 --- lutinutil/src/java/org/codelutin/option/Config.java:1.10 Tue Jan 8 19:00:46 2008 +++ lutinutil/src/java/org/codelutin/option/Config.java Tue Jan 8 20:47:42 2008 @@ -93,6 +93,8 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static protected Log log = LogFactory.getLog(Config.class); + static private final String MAIN_CATEGORY = "main"; + /** * la méthode à implanter pour initialiser la config (positionnement la source * par exemple) @@ -104,6 +106,7 @@ /** la catégorie de la config (clef unique non typée) */ protected final String category; + /** la description de la config */ protected final String description; /** l'univers des clefs possibles dans la configuration */ @@ -121,6 +124,7 @@ /** la source utilisée pour les opérations IO de la config */ protected File source; + /** un flage pour indique si la configuration a complètement été chargée */ protected boolean initDone; protected Config(String category, String description) { @@ -130,6 +134,11 @@ safeKeys = new ArrayList(); } + /** @return true si la configuration est principale */ + public boolean isMain() { + return MAIN_CATEGORY.equals(category); + } + /** * @param key la clef typée de la propriété recherchée * @return la valeur de la propriété, ou la valeur par défaut si non trouvée @@ -274,6 +283,7 @@ return result; } + /** @return la liste des clefs non sûres */ public List getUnsafeKeys() { if (isSafe()) { return Collections.emptyList(); @@ -285,6 +295,7 @@ return result; } + /** @return la liste des clefs modifiées */ public List getModifiedKeys() { List result = new ArrayList(); for (ConfigPropertyKey o : safeKeys) { @@ -295,12 +306,14 @@ return result; } + /** @return la liste des clefs connues et non modifiées */ public List getUnModifiedKeys() { List result = new ArrayList(safeKeys); result.removeAll(getModifiedKeys()); return result; } + /** @return la liste des clefs obligatoires manquantes */ protected ConfigPropertyKey[] getMissingKeys() { List list = new ArrayList(); for (ConfigPropertyKey configPropertyKey : universe) { @@ -367,24 +380,6 @@ } /** - * Fill a {@link Properties} object with all safe properties convert to String - * (should be used to store the config with Properties.store method) - * - * @return a {@link Properties} object filled with safe properties of config - */ - public Properties toProperties() { - Properties result = new Properties(); - if (!isEmpty()) { - for (ConfigPropertyKey key : getSafeKeys()) { - Object val = key.getCurrentValue(); - // always convert the value to String - result.put(key.getKey(), val == null ? "" : val.toString()); - } - } - return result; - } - - /** * Load some properties from a given {@link Properties} object. * * @param data the properties to inject @@ -448,23 +443,6 @@ } /** - * Try to fill the curent instance missing properties using the given - * otherProperties object. - * - * @param otherProperties the other properties object to use - * @return the current instance - */ - public Config fillMissingKeys(Config otherProperties) { - for (ConfigPropertyKey key : universe) { - if (!containsSafeKey(key)) { - Object value = otherProperties.getProperty(key); - setProperty(key, value); - } - } - return this; - } - - /** * Save safe current configuration in a simple properties file. *

*

@@ -483,15 +461,25 @@ try { if (!isEmpty()) { for (ConfigPropertyKey key : getSafeKeys()) { + if (key.isTransient()) { + // never store it + continue; + } Object val = key.getCurrentValue(); - // a shame that Properties.store can only deal with String value - getTmp().put(key.getKey(), val == null ? "" : val.toString()); + String keyStr = key.getKey(); + if (!isMain()) { + // prefix property's key with category + keyStr = category + '.' + keyStr; + } + // a shame that Properties.store can only deal with String values + getTmp().put(keyStr, val == null ? "" : val.toString()); if (props != null) { - props.remove(key.getKey()); + props.remove(keyStr); } } } if (props != null) { + // add optional properties for (Map.Entry entry : props.entrySet()) { if (entry.getValue() != null) { getTmp().put(entry.getKey(), entry.getValue() + ""); @@ -520,9 +508,7 @@ removeProperty(def); return; } - Class klass = def.getType(); - if (klass == String.class || value.getClass() == String.class || value.getClass() == klass) { T typeValue = def.convert(klass, value); if (typeValue != null) { @@ -570,7 +556,6 @@ this.source = source; } - /** @throws IOException if any */ protected void loadFromSource() throws IOException { if (source == null || !source.exists()) { return; @@ -587,7 +572,7 @@ protected void loadFromDefaultValue() throws IOException { for (ConfigPropertyKey key : universe) { - if (key.isTransient() && !(key.isFinal() && key.isStatic())) { + if (key.isTransient() && !(key.isFinal() && key.isStatic())) { continue; } Object defaultValue = key.getDefaultValue(); @@ -603,11 +588,12 @@ log.info(key.getKey() + " = " + defaultValue); setProperty(key, defaultValue); } - } + } } protected void loadFromSystem() { for (ConfigPropertyKey propertyKey : universe) { + //TODO should transform hongrois key in dotted name : userName = user.name Object val = System.getProperty(propertyKey.getKey()); if (val != null) { log.info(propertyKey.getKey() + " = " + val); @@ -618,6 +604,7 @@ protected void loadFromEnv() { for (ConfigPropertyKey propertyKey : universe) { + //TODO should transform hongrois key in dotted name : userName = user.name Object val = System.getenv(propertyKey.getKey()); if (val != null) { log.info(propertyKey.getKey() + " = " + val); @@ -628,6 +615,7 @@ protected void loadFromJvm() { for (ConfigPropertyKey propertyKey : universe) { + //TODO should transform hongrois key in dotted name : userName = user.name //todo //log.info(propertyKey.getKey()+" = "); propertyKey.getDefaultValue(); @@ -641,7 +629,7 @@ * @param def the def to use * @param value the value to set */ - protected void addProperty(ConfigPropertyKey def, T value) { + public void addProperty(ConfigPropertyKey def, T value) { def.setCurrentValue(value); if (!safeKeys.contains(def)) { safeKeys.add(def); @@ -684,6 +672,23 @@ // tmp contains new data to digest for (Object o : tmp.keySet()) { String kStr = o + ""; + if (!isMain()) { + // la clef est préfixée par la catégorie + // on doit retirer ce préfixe + int pos = kStr.indexOf('.'); + if (pos > -1) { + kStr = kStr.substring(pos+1); + } else { + // pour assurer la compatibilité avec les versions + // antiérieures d'Isis qui ne supportaient pas le préfixage + // des clefs par catégorie + if (getKey(kStr)!=null) { + // have a common key with another config + // can not treate this one + continue; + } + } + } ConfigPropertyKey key = getKey(kStr); Object value = tmp.get(o); if (key == null) { @@ -742,24 +747,12 @@ } } - public void clear(boolean b) { - for (ConfigPropertyKey key : getModifiedKeys()) { - key.clear(b); - } - } - public void clearModified() { for (ConfigPropertyKey key : getModifiedKeys()) { key.clearModified(); } } - public void reset() { - for (ConfigPropertyKey key : getModifiedKeys()) { - key.reset(); - } - } - public String getCategory() { return category; }