r2181 - trunk/nuiton-utils/src/main/java/org/nuiton/util
Author: tchemit Date: 2011-08-24 13:34:31 +0200 (Wed, 24 Aug 2011) New Revision: 2181 Url: http://nuiton.org/repositories/revision/nuiton-utils/2181 Log: Anomalie #1706: [ApplicationConfig] Bad encoding while loading config files Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java 2011-08-23 07:37:25 UTC (rev 2180) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java 2011-08-24 11:34:31 UTC (rev 2181) @@ -39,10 +39,12 @@ import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.PrintStream; +import java.io.Reader; import java.io.Serializable; +import java.io.Writer; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -50,6 +52,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.net.URI; import java.net.URL; import java.sql.Date; import java.sql.Time; @@ -301,6 +304,9 @@ /** Configuration file key option. */ static final public String CONFIG_FILE_NAME = "config.file"; + /** Configuration encoding key option. */ + static final public String CONFIG_ENCODING = "config.encoding"; + /**Permet d'associer un nom de contexte pour prefixer les options {@link #CONFIG_PATH} et {@link #CONFIG_FILE_NAME}. */ static final public String APP_NAME = "app.name"; @@ -410,6 +416,7 @@ */ public ApplicationConfig() { setConfigFileName(getClass().getSimpleName()); + setEncoding("utf-8"); // init extra-converters ConverterUtil.initConverters(); @@ -569,17 +576,12 @@ } // Ano #687 : create parentFile before using it in FileWriter - boolean dirCreated = FileUtil.createDirectoryIfNecessary(file.getParentFile()); + boolean dirCreated = + FileUtil.createDirectoryIfNecessary(file.getParentFile()); if (dirCreated && log.isDebugEnabled()) { log.debug("Creation of config directory " + file.getParent()); } - - OutputStream stream = new FileOutputStream(file); - try { - prop.store(stream, "Last saved " + new java.util.Date()); - } finally { - stream.close(); - } + saveResource(file, prop, "Last saved " + new java.util.Date()); } /** @@ -702,6 +704,29 @@ } /** + * Get the encoding used to read/write resources. + * + * This value is stored as an option using the + * {@link #getEncodingOption()} key. + * + * @return the encoding used to read/write resources. + * @since 2.3 + */ + public String getEncoding() { + return getOption(getEncodingOption()); + } + + /** + * Set the new encoding option. + * + * @param encoding the new value of the option encoding + * @since 2.3 + */ + public void setEncoding(String encoding) { + setDefaultOption(getEncodingOption(),encoding); + } + + /** * All argument in aliases as key is substitued by target. * * @param alias alias string as '-v' @@ -763,6 +788,20 @@ } /** + * Obtains the key used to store the option encoding. + * + * @return the encoding option'key + * @since 2.3 + */ + protected String getEncodingOption() { + String optionName = CONFIG_ENCODING; + if (getOption(APP_NAME) != null) { + optionName = getOption(APP_NAME) + "." + optionName; + } + return optionName; + } + + /** * Use appName to add a context in config.file and config.path options. * * Ex for an application named 'pollen' : {@code config.file} option becomes @@ -1595,12 +1634,7 @@ log.info("Loading configuration file (classpath) : " + inClasspath); } - InputStream stream = inClasspath.openStream(); - try { - classpath.load(stream); - } finally { - stream.close(); - } + loadResource(inClasspath.toURI(), classpath); } // system directory @@ -1610,12 +1644,7 @@ if (log.isInfoEnabled()) { log.info("Loading configuration file (etc) : " + etcConfig); } - InputStream stream = etcConfig.toURI().toURL().openStream(); - try { - etcfile.load(stream); - } finally { - stream.close(); - } + loadResource(etcConfig.toURI(), etcfile); } else { if (log.isDebugEnabled()) { @@ -1644,12 +1673,7 @@ log.info("Loading configuration file (home) : " + homeConfig); } - InputStream stream = homeConfig.toURI().toURL().openStream(); - try { - homefile.load(stream); - } finally { - stream.close(); - } + loadResource(homeConfig.toURI(), homefile); } else { if (log.isDebugEnabled()) { @@ -1664,12 +1688,7 @@ if (log.isInfoEnabled()) { log.info("Loading configuration file (curr) : " + config); } - InputStream stream = config.toURI().toURL().openStream(); - try { - curfile.load(stream); - } finally { - stream.close(); - } + loadResource(config.toURI(), curfile); } else { if (log.isDebugEnabled()) { @@ -1739,6 +1758,49 @@ } /** + * Load a resources given by his {@code uri} to the given + * {@code properties} argument. + * + * @param uri the uri to load + * @param properties the properties file to load + * @throws IOException if something occurs bad while loading resource + * @since 2.3 + * @see Properties#load(Reader) + */ + protected void loadResource(URI uri, Properties properties) throws IOException { + InputStreamReader reader = + new InputStreamReader(uri.toURL().openStream(), getEncoding()); + try { + properties.load(reader); + } finally { + reader.close(); + } + } + + /** + * Save the given {@code properties} into the given {@code file} with + * the given {@code comment}. + * + * @param file the location where to store the properties + * @param properties the properties file to save + * @param comment the comment to add in the saved file + * @throws IOException if something occurs bad while saving resource + * @since 2.3 + * @see Properties#store(Writer, String) + */ + protected void saveResource(File file, + Properties properties, + String comment) throws IOException { + Writer reader = + new OutputStreamWriter(new FileOutputStream(file), getEncoding()); + try { + properties.store(reader, comment); + } finally { + reader.close(); + } + } + + /** * For debugging. */ public void printConfig() {
participants (1)
-
tchemit@users.nuiton.org