Author: tchemit Date: 2011-08-04 13:40:26 +0200 (Thu, 04 Aug 2011) New Revision: 1941 Url: http://nuiton.org/repositories/revision/i18n/1941 Log: Evolution #1656: UserInitializer should always try to copy files Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/init/UserI18nInitializer.java Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/init/UserI18nInitializer.java =================================================================== --- trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/init/UserI18nInitializer.java 2011-08-04 09:50:59 UTC (rev 1940) +++ trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/init/UserI18nInitializer.java 2011-08-04 11:40:26 UTC (rev 1941) @@ -28,10 +28,10 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.i18n.format.I18nMessageFormatter; import org.nuiton.i18n.bundle.I18nBundle; import org.nuiton.i18n.bundle.I18nBundleEntry; import org.nuiton.i18n.bundle.I18nBundleUtil; +import org.nuiton.i18n.format.I18nMessageFormatter; import java.io.File; import java.io.FileOutputStream; @@ -125,26 +125,26 @@ boolean isNew = !directory.exists(); - if (isNew) { + // creates the user directory and fill it with i18n resources + // coming from default initializer + createUserI18nLayout(directory); - // creates the user directory and fill it with i18n resources - // coming from default initializer - createUserI18nLayout(directory); - } - // use a new classloader directly on the directory setLoader(new URLClassLoader(new URL[]{directory.toURI().toURL()})); + // now can grab bundles from the user external directory I18nBundle[] bundles = super.resolvBundles(); if (!isNew) { + + // will update directory if required updateUserI18nLayout(directory, bundles); } return bundles; } /** - * Creates the user i18n structure. + * Creates the user i18n structure or fill it if required. * <p/> * will use the default initializer to obtain i18n resources from default * system, then copy them to the user directory. @@ -154,10 +154,9 @@ */ protected void createUserI18nLayout(File directory) throws Exception { - // user i18n directory does not exists - // create it and fill it - - boolean b = directory.mkdirs(); + // if user i18n directory does not exists create it + // Then fill it (if files not exists) + boolean b = directory.exists() || directory.mkdirs(); if (!b) { throw new IOException("could not create directory " + directory); } @@ -170,13 +169,18 @@ File f = new File(directory, filename); - FileOutputStream outStream = new FileOutputStream(f); - try { + FileOutputStream outStream; + + if (!f.exists()) { + outStream = new FileOutputStream(f); + try { - definition.store(outStream, "Generated by " + getClass().getName()); - } finally { + definition.store(outStream, + "Generated by " + getClass().getName()); + } finally { - outStream.close(); + outStream.close(); + } } // detect bundles urls @@ -203,6 +207,12 @@ File dst = new File(directory, name); + if (dst.exists()) { + + if (log.isDebugEnabled()) { + log.debug("Keep user file " + dst); + } + } if (log.isInfoEnabled()) { log.info("Create user file to create : " + dst); } @@ -220,7 +230,7 @@ /** * Hook to update the user i18n structure. * <p/> - * If you wants to do something specific, overrides this method. + * If you wants to do something specific, overriddes this method. * * @param directory the user directory where are i18n resources * @param bundles the user i18n bundles