Index: lutincommandline/src/java/org/codelutin/option/OptionParser.java diff -u lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.2 --- lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.1.1.1 Sat Feb 9 15:05:36 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionParser.java Fri Feb 22 12:35:37 2008 @@ -30,9 +30,12 @@ import org.codelutin.util.ArrayUtil; import org.codelutin.util.ReflectUtil; +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.Writer; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -257,10 +260,72 @@ return !isOptionEnabled(key) ? null : getOptions(key)[0]; } + /** + * Obtenir une instance de configuration à partir de sa clef. + * + * @param key la clef de la configuration recherchée + * @return l'instance de la configuration + */ public C getConfig(ConfigKey key) { return key.getConfig(); } + /** + * Sauvegarde l'ensemble des configurations, en se basant sur le fichier source + * de la configuration principale. + * + * @throws IOException si probleme lors de la sauvegarde disque + * @throws IllegalStateException si pas de config main, ou pas de fichier ou + * sauvegarder. + */ + public void save() throws IOException, IllegalStateException { + List configs = getConfigKeys(); + if (configs.isEmpty()) { + // no config + return; + } + Config main = null; + List others = new ArrayList(); + for (ConfigKey configKey : configs) { + Config config = configKey.getConfig(); + if (config.isMain()) { + main = config; + } else { + others.add(config); + } + } + if (main == null || main.getSource() == null) { + // no main config, or source is null + throw new IllegalStateException("could not find main config, nor his source file"); + } + BufferedOutputStream out = null; + try { + out = new BufferedOutputStream(new FileOutputStream(main.getSource())); + main.save(out); + for (Config other : others) { + other.save(out); + } + } finally { + if (out != null) { + out.flush(); + out.close(); + } + } + } + + /** + * Sauvegarder de manière silencieuse l'ensemble des configurations. + * + * @see #save() + */ + public void saveSafely() { + try { + save(); + } catch (Exception e) { + log.warn("lutinutil.error.unsafe.save.config" + e.getMessage(), e); + } + } + /** @return true 0; Index: lutincommandline/src/java/org/codelutin/option/SimpleConfigImpl.java diff -u /dev/null lutincommandline/src/java/org/codelutin/option/SimpleConfigImpl.java:1.1 --- /dev/null Fri Feb 22 12:35:42 2008 +++ lutincommandline/src/java/org/codelutin/option/SimpleConfigImpl.java Fri Feb 22 12:35:37 2008 @@ -0,0 +1,142 @@ +/* +* ##% Copyright (C) 2007, 2008 Code Lutin, Tony Chemit +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package org.codelutin.option; + +import static org.codelutin.i18n.I18n._; + +import java.io.File; +import java.io.IOException; + +/** + * Simple config implementation, which is connected to a parser, + * + * delegates all save stuff to parser. + * + * @author chemit + */ +public abstract class SimpleConfigImpl extends Config { + + protected abstract T getParser(); + + protected abstract OptionKey getConfigOptionKey(); + + protected abstract OptionKey getConfigFileOptionKey(); + + protected abstract ConfigPropertyKey getFileNameConfigKey(); + + protected SimpleConfigImpl(String category, String description) { + super(category, description); + String home = System.getProperty("user.home"); + if (home == null) { + // this is a serious fatal error + throw new RuntimeException("lutinutil.error.user.home"); + } + doInit(); + } + + @Override + public void save() throws IOException { + // always save all configs in context in the same time + getParser().save(); + } + + protected void init() throws Exception { + log.info("start for category [" + category + "] -------------------------"); + initConfigFile(getParser(),getConfigOptionKey(), getFileNameConfigKey()); + log.info("config file : " + getSource()); + // chargement des valeurs par défaut + loadFromDefaultValue(); + // après le chargement des valeurs par défaut + // la configuration n'est pas modifiée + clearModified(); + // surcharge à partir du fichier de configuration de l'utilisateur + loadFromSource(); + // surcharge à partir des propriétés système + loadFromSystem(); + // surcharge à partir des propriétés de la jvm + loadFromJvm(); + // surcharge à partir de l'option config de la ligne de commande + loadFromOptions(getParser(), getConfigOptionKey()); + for (String s : this.toString().split("\n")) { + log.info(s); + } + log.info("end for category [" + category + "] ---------------------------"); + } + + protected void initConfigFile(T parser, OptionKey configFileOptionKey, ConfigPropertyKey fileNameConfigKey) { + File file; + if (parser.isOptionEnabled(configFileOptionKey)) { + // surcharge config file + Option option = configFileOptionKey.getOptions().get(0); + + file = getConfigFile(option); + } else { + String home = System.getProperty("user.home", ""); + File root = new File(home); + file = new File(root, fileNameConfigKey.getDefaultValue().getName()); + } + if (containsKey(fileNameConfigKey)) { + setProperty(fileNameConfigKey, file); + } + setSource(file); + } + + protected void loadFromOptions(T parser, OptionKey configOptionKey) throws Exception { + if (!parser.isOptionEnabled(configOptionKey)) { + return; + } + // surcharge config + for (Option option : configOptionKey.getOptions()) { + if (category.equals(getCategory(option))) { + String s = getOptionKey(option); + ConfigPropertyKey propKey = getPropertyKey(s); + if (propKey == null) { + // fatal error , could not found a matching configuration property + throw new IllegalArgumentException(_("lutinutil.error.unfound.config.property", category, s)); + } + Object oldVal = propKey.getCurrentValue(); + setProperty(propKey, getOptionValue(option)); + Object newVal = propKey.getCurrentValue(); + log.info(_("lutinutil.change.config.property", category, propKey, oldVal, newVal)); + } + } + } + + /** + * @param option the option to test + * @return the category of an IsisOptionConfig + */ + protected String getCategory(Option option) { + String value = option.getConstantArgumentValue(0); + ConfigKey configKey = getParser().getConfigKey(value); + return configKey == null ? null : configKey.getCategory(); + } + + + protected String getOptionKey(Option option) { + return option.getValuedArgumentValue(0, String.class, null, false); + } + + protected String getOptionValue(Option option) { + return option.getValuedArgumentValue(1, String.class, null, false); + } + + protected File getConfigFile(Option option) { + return option.getValuedArgumentValue(0, File.class, null, false); + } +}