Index: lutinutil/src/java/org/codelutin/util/MyPropertiesUtil.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/MyPropertiesUtil.java:1.1 --- /dev/null Sun Dec 9 19:11:53 2007 +++ lutinutil/src/java/org/codelutin/util/MyPropertiesUtil.java Sun Dec 9 19:11:48 2007 @@ -0,0 +1,199 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Benjamin Poussin, Tony Chemit, and others +* +* +* 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.util; + +import static org.apache.commons.logging.LogFactory.getLog; +import static org.codelutin.i18n.I18n._; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.util.Properties; + +/** + * Some usefull class and methods for {@link MyProperties} + * + * @author chemit + */ +public class MyPropertiesUtil { + + static private org.apache.commons.logging.Log log = getLog(MyPropertiesUtil.class); + + /** + * This is the contract to be realised by any key to be used in + * {@link MyProperties}. + *

+ * Concrete implementation of this class must always be an extends + * of {@link Enum}. + * + * @author tony + */ + public static interface MyPropertyDef { + + /** + * @return the name of encapsuling Enum entry + * @see Enum#name() + */ + String name(); + + /** + * @return the rank of encapsuling Enum entry + * @see Enum#ordinal() + */ + int ordinal(); + + } + + /** + * A simple typed property key implementation. + *

+ * + * @author chemit + */ + + public static class MyPropertyKey { + + /** The def of the property : should be an Enum value */ + protected MyPropertyDef def; + + /** the key to use in stored property files */ + protected String key; + + /** the typed value of the property */ + protected Class type; + + public MyPropertyKey(MyPropertyDef def, String key, Class type) { + this.def = def; + this.key = key; + this.type = type; + } + + public MyPropertyDef getDef() { + return def; + } + + public String getKey() { + return key; + } + + public Class getType() { + return type; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(this.getClass().getSimpleName()); + sb.append('@').append(Math.abs(hashCode())); + sb.append('<'); + sb.append(def == null ? null : (def.getClass().getSimpleName() + '#' + def)); + sb.append("('").append(key); + sb.append("' instanceOf ").append(type == null ? null : type.getSimpleName()); + sb.append(")>"); + return sb.toString(); + } + + } + + /** + * The contract to be realized by any {@link MyPropertyKey} to be used a a + * configuration key. + *

+ * TODO Configure it from an Annotation + * + * @author chemit + */ + + public static class MyConfigurationKey extends MyPropertyKey { + + protected boolean mandatory; + + public MyConfigurationKey(MyPropertyDef def, String key, Class type, + boolean mandatory) { + super(def, key, type); + this.mandatory = mandatory; + } + + /** + * @return true if key is mandatory in configuration, + * false otherwise + */ + public boolean isMandatory() { + return mandatory; + } + + @Override + public String toString() { + String s = super.toString(); + if (!mandatory) s += "[optional]"; + return s; + } + + } + + /** + * Load a properties files fro a given uri. + * + * @param uri the uri where to find the properties file + * @param safe flag to say throw Exception if errors + * @param verbose flag to print verbose logs + * @return the loaded properties file or null, if not safe and has some problems + */ + public static Properties loadConfig(URI uri, boolean safe, boolean verbose) { + Properties properties = null; + if (uri == null) { + if (safe) { + throw new NullPointerException(_("lutinutil.error.config.not.found", uri)); + } + return properties; + } + try { + log.info(_("lutinutil.message.config.load", uri)); + InputStream inputstream = uri.toURL().openStream(); + properties = new Properties(); + properties.load(inputstream); + log.info(_("lutinutil.message.config.loaded", uri, + (properties == null ? 0 : properties.keySet().size()))); + if (verbose) { + if (properties != null) { + for (Object o : properties.keySet()) { + log.info("<<< " + o + " = " + properties.getProperty(o + "")); + } + } + } + } catch (FileNotFoundException e) { + String message = _("lutinutil.error.config.not.found", uri); + log.warn(message); + if (safe) { + throw new RuntimeException(message, e); + } + properties = null; + } catch (IOException e) { + String message = _("lutinutil.error.config.load", uri, e.getMessage()); + log.warn(message); + if (safe) { + throw new RuntimeException(message, e); + } + properties = null; + } + return properties; + } + +}