Author: bpoussin Date: 2010-06-01 12:54:42 +0200 (Tue, 01 Jun 2010) New Revision: 51 Url: http://chorem.org/repositories/revision/bow/51 Log: modification de la configuration (creation d'une vrai classe de config, multi-thread enable) Added: trunk/src/main/java/org/chorem/bow/BowConfig.java Modified: trunk/src/main/java/org/chorem/bow/AliasServlet.java trunk/src/main/java/org/chorem/bow/ControllerServlet.java Modified: trunk/src/main/java/org/chorem/bow/AliasServlet.java =================================================================== --- trunk/src/main/java/org/chorem/bow/AliasServlet.java 2010-06-01 10:02:53 UTC (rev 50) +++ trunk/src/main/java/org/chorem/bow/AliasServlet.java 2010-06-01 10:54:42 UTC (rev 51) @@ -25,10 +25,8 @@ protected String bowUrl = ""; public AliasServlet() throws ArgumentsParserException { - ApplicationConfig config = new ApplicationConfig(); - config.setConfigFileName("bow.properties"); - config.parse(new String[]{}); - bowUrl = config.getOption("bow.url"); + BowConfig config = BowConfig.getInstance(); + bowUrl = config.getBowUrl(); } @Override Added: trunk/src/main/java/org/chorem/bow/BowConfig.java =================================================================== --- trunk/src/main/java/org/chorem/bow/BowConfig.java (rev 0) +++ trunk/src/main/java/org/chorem/bow/BowConfig.java 2010-06-01 10:54:42 UTC (rev 51) @@ -0,0 +1,140 @@ +/* *##% + * Copyright (c) 2010 poussin. All rights reserved. + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + *##%*/ + +package org.chorem.bow; + +import static org.nuiton.i18n.I18n._; + +import java.io.File; +import org.apache.commons.lang.UnhandledException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.ApplicationConfig; + +/** + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ +public class BowConfig extends ApplicationConfig { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(BowConfig.class); + + /** Singleton instance. */ + protected static BowConfig instance = null; + + public BowConfig() { + super(); + + // set default option (included configuration file name : important) + for (Option o : Option.values()) { + if (o.defaultValue != null) { + setDefaultOption(o.key, o.defaultValue); + } + } + } + + public static synchronized BowConfig getInstance() { + if (instance == null) { + try { + instance = new BowConfig(); + instance.parse(new String[]{}); + instance.setDataDirInSystemProps(); + } catch (Exception e) { + throw new UnhandledException(e); + } + } + return instance; + } + + /** + * Get version as string. + * + * @return version + */ + public String getVersion() { + return getOption(Option.APPLICATION_VERSION.key); + } + + /** + * Get bow url as string. + * + * @return url + */ + public String getBowUrl() { + return getOption(Option.BOW_URL.key); + } + + public File getDataDirAsFile() { + File option = getOptionAsFile(Option.DATA_DIR.key); + return option; + } + + /** + * Set {@code solr} and {@code jms} system configuration. + * + * This is the "only" way to configure embedded solr. + */ + protected void setDataDirInSystemProps() { + String value = System.getProperty(Option.DATA_DIR.key, null); + if (value == null) { + value = getOption(Option.DATA_DIR.key); + if (log.isInfoEnabled()) { + log.info("Setting system property " + Option.DATA_DIR.key + " : " + value); + } + System.setProperty(Option.DATA_DIR.key, value); + env.put(Option.DATA_DIR.key, value); + } + } + + /** + * Vradi option definition. + */ + public static enum Option { + + CONFIG_FILE(CONFIG_FILE_NAME, _("bow.config.configFileName.description"), + "bow.properties", String.class, false, false), + APPLICATION_VERSION("application.version", _("bow.config.application.version.description"), + null, String.class, true, true), + BOW_URL("bow.url", _("bow.config.bow.url.description"), + null, String.class, false, false), + DATA_DIR("solr.data.dir", _("bow.config.data.dir.description"), + System.getProperty("user.home") + "/.bow/solr", String.class, false, false); + + public final String key; + public final String description; + public final String defaultValue; + public final Class<?> type; + public final boolean isTransient; + public final boolean isFinal; + + private Option(String key, String description, String defaultValue, + Class<?> type, boolean isTransient, boolean isFinal) { + this.key = key; + this.description = description; + this.defaultValue = defaultValue; + this.type = type; + this.isFinal = isFinal; + this.isTransient = isTransient; + } + } + +} Modified: trunk/src/main/java/org/chorem/bow/ControllerServlet.java =================================================================== --- trunk/src/main/java/org/chorem/bow/ControllerServlet.java 2010-06-01 10:02:53 UTC (rev 50) +++ trunk/src/main/java/org/chorem/bow/ControllerServlet.java 2010-06-01 10:54:42 UTC (rev 51) @@ -49,11 +49,9 @@ protected String url = ""; public ControllerServlet() throws ArgumentsParserException, Exception { - ApplicationConfig config = new ApplicationConfig(); - config.setConfigFileName("bow.properties"); - config.parse(new String[]{}); - version = config.getOption("application.version"); - url = config.getOption("bow.url"); + BowConfig config = BowConfig.getInstance(); + version = config.getVersion(); + url = config.getBowUrl(); if (url == null) { throw new Exception("No bow.url=\"SERVER URL\" in bow.properties"); }