Author: tchemit Date: 2008-01-18 01:48:49 +0000 (Fri, 18 Jan 2008) New Revision: 178 Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/SimExplorerConfig.java Log: la configuration de l'application (s'appuie sur la classe g?\195?\169n?\195?\169r?\195?\169e par le plugin commandline) Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/SimExplorerConfig.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/SimExplorerConfig.java (rev 0) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/SimExplorerConfig.java 2008-01-18 01:48:49 UTC (rev 178) @@ -0,0 +1,131 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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 fr.cemagref.simexplorer.is; + +import fr.cemagref.simexplorer.is.configs.SimExplorerAbstractConfigMain; +import fr.cemagref.simexplorer.is.options.SimExplorerOptionConfig; +import fr.cemagref.simexplorer.is.options.SimExplorerOptionConfigFile; +import org.codelutin.i18n.I18n; +import org.codelutin.i18n.LanguageEnum; +import org.codelutin.i18n.CountryEnum; +import static org.codelutin.i18n.I18n._; +import org.codelutin.option.ConfigPropertyKey; +import org.codelutin.option.OptionParser; + +import java.io.File; +import java.io.IOException; + +/** + * L'implantation concrete de la config principale + * + * @author chemit + */ +public class SimExplorerConfig extends SimExplorerAbstractConfigMain { + + public SimExplorerConfig() { + super(); + String home = System.getProperty("user.home"); + if (home == null) { + // this is a serious fatal error + throw new RuntimeException("simexplorer.error.user.home"); + } + doInit(); + } + + public void init() throws Exception { + log.info("start for category [" + category + "] -------------------------"); + + SimExplorerOptionParser parser = SimExplorer.getContext().getParser(); + initConfigFile(parser); + 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(parser); + + for (String s : this.toString().split("\n")) { + log.info(s); + } + log.info("end for category [" + category + "] ---------------------------"); + } + + protected void initConfigFile(OptionParser parser) { + File file; + if (parser.isOptionEnabled(SimExplorerOptionParser.CONFIG_FILE_OPTION_KEY)) { + // surcharge config file + SimExplorerOptionConfigFile option = SimExplorerOptionParser.CONFIG_FILE_OPTION_KEY.getOptions().get(0); + + file = option.getConfigFile(); + } else { + String home = System.getProperty("user.home", ""); + File root = new File(home); + file = new File(root, SimExplorerConfig.CONFIG_FILE_NAME_PROPERTY_KEY.getDefaultValue().getName()); + if (!file.exists()) { + SimExplorer.getContext().setFirstLaunch(true); + try { + file.createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + if (containsKey(SimExplorerConfig.CONFIG_FILE_NAME_PROPERTY_KEY)) { + setProperty(SimExplorerConfig.CONFIG_FILE_NAME_PROPERTY_KEY, file); + } + setSource(file); + } + + public void initI18n() { + I18n.initISO88591(getLanguage().name(), getCountry().name()); + } + + protected void loadFromOptions(SimExplorerOptionParser parser) throws Exception { + if (!parser.isOptionEnabled(SimExplorerOptionParser.CONFIG_OPTION_KEY)) { + return; + } + // surcharge config file + for (SimExplorerOptionConfig option : SimExplorerOptionParser.CONFIG_OPTION_KEY.getOptions()) { + if (getCategory().equals(SimExplorerOptionParser.MAIN_CONFIG_KEY.getCategory())) { + ConfigPropertyKey<?> propKey = getPropertyKey(option.getKey()); + if (propKey == null) { + // fatal error , could not found a matching configuration property + throw new IllegalArgumentException(_("simexplorer.error.unfound.config.property", category, option.getKey())); + } + Object oldVal = propKey.getCurrentValue(); + setProperty(propKey, option.getValue()); + Object newVal = propKey.getCurrentValue(); + log.info(_("simexplorer.change.config.property", category, propKey, oldVal, newVal)); + } + } + } + + public void setI18n(LanguageEnum language, CountryEnum country) { + setLanguage(language); + setCountry(country); + } +} \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org