Index: lutinutil/src/java/org/codelutin/option/Config.java diff -u lutinutil/src/java/org/codelutin/option/Config.java:1.3 lutinutil/src/java/org/codelutin/option/Config.java:1.4 --- lutinutil/src/java/org/codelutin/option/Config.java:1.3 Thu Jan 3 06:06:25 2008 +++ lutinutil/src/java/org/codelutin/option/Config.java Fri Jan 4 09:53:58 2008 @@ -18,14 +18,19 @@ * ##% */ package org.codelutin.option; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import static org.codelutin.i18n.I18n._; import org.codelutin.util.ReflectUtil; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; -import java.net.URI; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -84,6 +89,9 @@ * @author chemit */ public abstract class Config { + /** to use log facility, just put in your code: log.info(\"...\"); */ + static protected Log log = LogFactory.getLog(Config.class); + /** * la méthode à implanter pour initialiser la config (positionnement la source * par exemple) @@ -92,11 +100,11 @@ */ protected abstract void init() throws Exception; - public abstract void save() throws Exception; - /** la catégorie de la config (clef unique non typée) */ protected final String category; + protected final String description; + /** l'univers des clefs possibles dans la configuration */ protected final List universe; @@ -110,21 +118,15 @@ protected Properties tmp; /** la source utilisée pour les opérations IO de la config */ - protected URI source; + protected File source; - protected final boolean initDone; + protected boolean initDone; - protected Config(String category) { + protected Config(String category, String description) { this.category = category; + this.description = description; universe = Collections.unmodifiableList(ReflectUtil.getConstants(getClass(), ConfigPropertyKey.class)); safeKeys = new ArrayList(); - try { - init(); - } catch (Exception e) { - throw new IllegalStateException(e); - } finally { - initDone = true; - } } /** @@ -142,11 +144,37 @@ return null; } - @SuppressWarnings({"unchecked"}) - public ConfigPropertyKey getPropertyKey(String key) { - for (ConfigPropertyKey configPropertyKey : universe) { - if (configPropertyKey.getKey().equalsIgnoreCase(key)) { - return (ConfigPropertyKey) configPropertyKey; + public void save(BufferedOutputStream out) throws IOException { + if (source == null) { + return; + } + store(out, "save configuration [" + category + "] "); + clearModified(); + } + + public void save() throws IOException { + if (source == null) { + return; + } + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(source)); + store(out, "save configuration [" + category + "] "); + clearModified(); + } + + protected void doInit() { + try { + init(); + } catch (Exception e) { + throw new IllegalStateException(e); + } finally { + initDone = true; + } + } + + public ConfigPropertyKey getPropertyKey(String key) { + for (ConfigPropertyKey propertyKey : universe) { + if (propertyKey.getKey().equalsIgnoreCase(key)) { + return propertyKey; } } return null; @@ -192,8 +220,17 @@ * @return true if key is legal and used, * false otherwise. */ + public boolean containsKey(ConfigPropertyKey key) { + return universe.contains(key); + } + + /** + * @param key the given key + * @return true if key is legal and used, + * false otherwise. + */ public boolean containsSafeKey(ConfigPropertyKey key) { - return universe.contains(key) && safeKeys.contains(key); + return containsKey(key) && safeKeys.contains(key); } /** @@ -247,6 +284,16 @@ return result; } + protected ConfigPropertyKey[] getMissingKeys() { + List list = new ArrayList(); + for (ConfigPropertyKey configPropertyKey : universe) { + if (!containsSafeKey(configPropertyKey)) { + list.add(configPropertyKey); + } + } + return list.toArray(new ConfigPropertyKey[list.size()]); + } + /** * Remove a unsafe property using a given unsage key, if there is a such * unsafe property. @@ -413,7 +460,7 @@ * @param comments comment to add in file * @throws IOException if any problem while saving */ - public void store(OutputStream out, String comments) throws IOException { + protected void store(OutputStream out, String comments) throws IOException { getTmp().clear(); try { if (!isEmpty()) { @@ -435,7 +482,7 @@ * @param def the given key to use * @param value the given valuer to set */ - public void setProperty(ConfigPropertyKey def, Object value) { + protected void setProperty(ConfigPropertyKey def, Object value) { checkAuthorizedKey(def); if (initDone && def.isFinal()) { // fatal error , final property @@ -465,8 +512,11 @@ s.append(", unsafe:").append(unsafeData.size()); } s.append('>'); + if (source!=null) { + s.append("\nsource:").append(source); + } if (!isEmpty()) { - List propertyKeys; + List propertyKeys; propertyKeys = getModifiedKeys(); if (!propertyKeys.isEmpty()) { s.append(" (").append(propertyKeys.size()).append(" modified) "); @@ -484,11 +534,11 @@ return s.toString(); } - protected URI getSource() { + public File getSource() { return source; } - protected void setSource(URI source) { + protected void setSource(File source) { this.source = source; } @@ -497,13 +547,30 @@ if (source != null) { return; } - load(source.toURL().openStream()); + InputStream inputStream = new FileInputStream(source); + try { + load(inputStream); + } finally { + if (inputStream != null) { + inputStream.close(); + } + } } protected void loadFromDefaultValue() throws IOException { for (ConfigPropertyKey propertyKey : universe) { + if (propertyKey.isTransient()) { + continue; + } Object defaultValue = propertyKey.getDefaultValue(); if (defaultValue != null) { + if (propertyKey.getType() == File.class) { + File f = (File) defaultValue; + if (!f.isAbsolute()) { + defaultValue = new File(System.getProperty("user.home"), f.getName()); + } + } + log.info(propertyKey.getKey() + " = " + defaultValue); setProperty(propertyKey, defaultValue); } } @@ -513,6 +580,7 @@ for (ConfigPropertyKey propertyKey : universe) { Object val = System.getProperty(propertyKey.getKey()); if (val != null) { + log.info(propertyKey.getKey() + " = " + val); setProperty(propertyKey, val); } } @@ -522,11 +590,20 @@ for (ConfigPropertyKey propertyKey : universe) { Object val = System.getenv(propertyKey.getKey()); if (val != null) { + log.info(propertyKey.getKey() + " = " + val); setProperty(propertyKey, val); } } } + protected void loadFromJvm() { + for (ConfigPropertyKey propertyKey : universe) { + //todo + //log.info(propertyKey.getKey()+" = "); + propertyKey.getDefaultValue(); + } + } + /** * Set a value for a given property using his def and update * also the list of keys used. @@ -652,4 +729,8 @@ key.reset(); } } + + public String getCategory() { + return category; + } }