Author: chemit
Date: 2008-11-17 23:29:40 +0000 (Mon, 17 Nov 2008)
New Revision: 1236
Added:
lutinutil/trunk/src/main/java/org/codelutin/util/ClassLoaderUtil.java
Modified:
lutinutil/trunk/changelog
lutinutil/trunk/src/main/java/org/codelutin/i18n/I18n.java
lutinutil/trunk/src/main/java/org/codelutin/i18n/I18nLoader.java
lutinutil/trunk/src/main/java/org/codelutin/i18n/Language.java
lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleEntry.java
lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleManager.java
lutinutil/trunk/src/main/java/org/codelutin/util/Resource.java
lutinutil/trunk/src/test/java/org/codelutin/i18n/I18nLoaderTest.java
lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBundleManagerTest.java
lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBunsleScopeTest.java
Log:
- improve i18n loading : skip urls with no i18n directory
- introduce ClassLoaderUtil class
Modified: lutinutil/trunk/changelog
===================================================================
--- lutinutil/trunk/changelog 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/changelog 2008-11-17 23:29:40 UTC (rev 1236)
@@ -1,5 +1,7 @@
ver 1.0 ??? ???
- * Remove deprecated I18n._ and replace by temporary code
+ * 20081118 [chemit] - improve i18n loading : skip urls with no i18n directory
+ - introduce ClassLoaderUtil class
+ * 2008???? [chatellier] Remove deprecated I18n._ and replace by temporary code
ver-0-31 chemit 2008????
* always build javadoc
Modified: lutinutil/trunk/src/main/java/org/codelutin/i18n/I18n.java
===================================================================
--- lutinutil/trunk/src/main/java/org/codelutin/i18n/I18n.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/main/java/org/codelutin/i18n/I18n.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -93,7 +93,7 @@
/** Initialise la librairie avec encoding par defaut et locale par defaut */
public static void init() {
- init(null, null, null);
+ init(null);
}
/**
@@ -102,7 +102,7 @@
* @param locale language to use
*/
public static void init(Locale locale) {
- init(locale, null);
+ getLoader().setLanguage(locale == null ? newLocale(null, null) : locale, getBundleManager());
}
/**
@@ -112,31 +112,10 @@
* @param country une chaine representant le pays à utiliser FR, GB, ...
*/
public static void init(String language, String country) {
- init(language, country, null);
+ init(newLocale(language, country));
}
/**
- * Initialise la librairie
- *
- * @param language une chaine representant la langue à utiliser fr, en, ...
- * @param country une chaine representant le pays à utiliser FR, GB, ...
- * @param encoding l'encoding a utiliser, si null DEFAULT_ENCODING est utilise
- */
- public static void init(String language, String country, String encoding) {
- init(newLocale(language, country), encoding);
- }
-
- /**
- * Initialise la librairie
- *
- * @param newLocale la locale a charger
- * @param encoding l'encoding a utiliser, si null DEFAULT_ENCODING est utilise
- */
- public static void init(Locale newLocale, String encoding) {
- getLoader(encoding).setLanguage(newLocale == null ? newLocale(null, null) : newLocale, getBundleManager());
- }
-
- /**
* Retourne la chaine traduite si possible.
*
* @param message la chaine a traduire
@@ -284,32 +263,16 @@
}
/**
- * Get the i18n loader with good encoding.
+ * Get the i18n loader.
* <p/>
* If no loader found, then instanciate a new one.
- * <p/>
- * If a previous loader is detected, then it is closed.
- * <p/>
- * If <code>encoding</code> is null, then we use default one {@link #DEFAULT_ENCODING}.
- *
- * @param encoding encoding to use for loader
- * @return the required loader with given encoding
+ *
+ * @return the instanciated i18n loader
*/
- protected static synchronized I18nLoader getLoader(String encoding) {
-
- String localEncoding = encoding;
- if (localEncoding == null) {
- localEncoding = DEFAULT_ENCODING;
- }
+ protected static synchronized I18nLoader getLoader() {
+
if (loader == null) {
- loader = new I18nLoader(localEncoding);
- } else {
- if (!loader.getEncoding().equals(localEncoding)) {
- // close previous loader but not the bundle manager
- loader.close();
- // open a new loader
- loader = new I18nLoader(localEncoding);
- }
+ loader = new I18nLoader(DEFAULT_ENCODING);
}
return loader;
}
Modified: lutinutil/trunk/src/main/java/org/codelutin/i18n/I18nLoader.java
===================================================================
--- lutinutil/trunk/src/main/java/org/codelutin/i18n/I18nLoader.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/main/java/org/codelutin/i18n/I18nLoader.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -27,7 +27,7 @@
import java.util.Locale;
/**
- * Classe responsable of loading of I18n system.
+ * Classe responsible of loading of I18n system.
* <p/>
* The invariant of class is property {@link #encoding}.
* <p/>
@@ -44,7 +44,7 @@
private static final Log log = LogFactory.getLog(I18nLoader.class);
/** l'encoding a utiliser pour charger les traductions */
- protected final String encoding;
+ //protected final String encoding;
/** le language actuellement utilise */
protected Language language;
@@ -54,13 +54,13 @@
public I18nLoader(String encoding) {
log.info("encoding "+encoding);
- this.encoding = encoding;
+ //this.encoding = encoding;
}
/** @return the encoding used by this loader, this an invariant */
- public String getEncoding() {
+ /*public String getEncoding() {
return encoding;
- }
+ }*/
/**
* @return current language loaded or null, if no language was load
@@ -132,7 +132,8 @@
Language addLanguage(Locale locale, I18nBundleManager bundleManager) {
Language result;
- result = new Language(locale, encoding);
+ result = new Language(locale);
+ //result = new Language(locale, encoding);
result.load(bundleManager);
if (log.isDebugEnabled()) {
log.debug(result);
Modified: lutinutil/trunk/src/main/java/org/codelutin/i18n/Language.java
===================================================================
--- lutinutil/trunk/src/main/java/org/codelutin/i18n/Language.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/main/java/org/codelutin/i18n/Language.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -59,12 +59,9 @@
/** la locale de la langue */
protected Locale locale;
- /** l'encoding */
- protected String encoding;
-
- public Language(Locale l, String toEncoding) {
+ /** @param l the current locale of the language */
+ public Language(Locale l) {
this.locale = l;
- this.encoding = toEncoding;
}
/**
@@ -101,8 +98,9 @@
}
try {
String result = resource.getProperty(sentence);
- if (null != result && !"".equals(result))
+ if (null != result && !"".equals(result)) {
return result;
+ }
recordNotFound(sentence);
return sentence;
} catch (MissingResourceException eee) {
@@ -169,10 +167,6 @@
return locale;
}
- public String getEncoding() {
- return encoding;
- }
-
public int size() {
return resource == null ? 0 : resource.size();
}
@@ -193,23 +187,16 @@
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof Language)) return false;
-
- Language language = (Language) o;
- return encoding.equals(language.encoding) && locale.equals(language.locale);
+ return this == o || o instanceof Language && locale.equals(((Language) o).locale);
}
@Override
public int hashCode() {
- int result;
- result = locale.hashCode();
- result = 31 * result + encoding.hashCode();
- return result;
+ return locale.hashCode();
}
@Override
public String toString() {
- return "Language <locale: " + locale + ", encoding: " + encoding + ",nbStences:" + (size()) + ">";
+ return "Language <locale: " + locale + ",nbStences:" + (size()) + ">";
}
}
Modified: lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleEntry.java
===================================================================
--- lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleEntry.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleEntry.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -18,7 +18,7 @@
package org.codelutin.i18n.bundle;
import org.codelutin.i18n.I18nFileReader;
-import org.codelutin.i18n.Language;
+import org.codelutin.i18n.I18n;
import java.io.IOException;
import java.io.InputStream;
@@ -107,6 +107,7 @@
// can not match a specialized entry with a general scope
return false;
}
+ // match full locale, or at least a language
return this.locale.equals(locale) ||
(this.scope.ordinal() < scope.ordinal() && locale.getLanguage().equals(this.locale.getLanguage()));
}
@@ -115,26 +116,25 @@
* For a given language, load the resource file of this entry into the <code>resource</code>
* properties object.
*
- * @param language the given language
* @param resource the save of resources already loaded
* @throws IOException if any pb while reading resource file
*/
- public void load(Language language, Properties resource) throws IOException {
+ public void load(Properties resource) throws IOException {
InputStream inputStream = null;
StringBuilder sb = new StringBuilder();
try {
I18nFileReader fileReader = new I18nFileReader();
inputStream = getPath().openStream();
- String encoding = language.getEncoding();
+ //String encoding = language.getEncoding();
if (I18nBundle.log.isDebugEnabled()) {
sb.append(getPath()).append("\n");
}
- //fixme : voir pourquoi comment faire fonctionner avec n'importe quel encoding...
- fileReader.load(inputStream, encoding);
+ // TC 20081117 always use ISO_8859_1_ENCONDING, since java does it like this.
+ fileReader.load(inputStream, I18n.ISO_8859_1_ENCONDING);
if (I18nBundle.log.isDebugEnabled()) {
for (Entry<Object, Object> entry : fileReader.entrySet()) {
- sb.append(encoding).append(" : ").append(entry).append("\n");
+ sb.append(I18n.ISO_8859_1_ENCONDING).append(" : ").append(entry).append("\n");
}
}
resource.putAll(fileReader);
Modified: lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleManager.java
===================================================================
--- lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleManager.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleManager.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -23,6 +23,7 @@
import org.codelutin.i18n.Language;
import org.codelutin.util.Resource;
import org.codelutin.util.StringUtil;
+import org.codelutin.util.ClassLoaderUtil;
import java.io.IOException;
import java.net.URL;
@@ -30,6 +31,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
@@ -47,8 +49,10 @@
private static final Log log = LogFactory.getLog(I18nBundleManager.class);
/** pattern to find all i18n bundles in classloader class path */
- public static final String SEARCH_BUNDLE_PATTERN = ".*18n/.+\\.properties";
+ public static final String SEARCH_BUNDLE_PATTERN = ".*i18n/.+\\.properties";
+ public static final String DIRECTORY_SEARCH_BUNDLE_PATTERN = "i18n";
+
/** le cache de bundles deja charges */
protected List<I18nBundle> cache;
@@ -166,7 +170,7 @@
long t0 = System.nanoTime();
I18nBundleEntry[] entries = getBundleEntries(language.getLocale());
for (I18nBundleEntry entry : entries) {
- entry.load(language, resource);
+ entry.load(resource);
}
log.info(language + ", nbEntries: " + entries.length + ", nbSentences: " + language.size() + " in " + StringUtil.convertTime(System.nanoTime() - t0));
}
@@ -234,24 +238,37 @@
*/
protected URL[] getURLs(URLClassLoader loader, URL... extraUrl) {
- List<URL> urls = new ArrayList<URL>();
try {
- if (loader == null) {
- urls.addAll(Resource.getURLs(SEARCH_BUNDLE_PATTERN));
- } else {
- urls.addAll(Resource.getURLs(SEARCH_BUNDLE_PATTERN, loader));
+ List<URL> urlToSeek = new ArrayList<URL>();
+ urlToSeek.addAll(Arrays.asList(ClassLoaderUtil.getDeepURLs(loader)));
+ if (extraUrl.length > 0) {
+ urlToSeek.addAll(Arrays.asList(extraUrl));
}
- urls.addAll(Arrays.asList(extraUrl));
+ // skip all entries with not a i18n directory to improve loading performance
+ int size = urlToSeek.size();
+ for (Iterator<URL> it = urlToSeek.iterator(); it.hasNext();) {
+ URL url = it.next();
+ if (!Resource.containsDirectDirectory(url, DIRECTORY_SEARCH_BUNDLE_PATTERN)) {
+ if (log.isDebugEnabled()) {
+ log.debug("skip url with no " + DIRECTORY_SEARCH_BUNDLE_PATTERN + " directory : " + url);
+ }
+ it.remove();
+ }
+ }
if (log.isDebugEnabled()) {
+ log.debug("will scan " + urlToSeek.size() + " out of " + size);
+ }
+ List<URL> urls = Resource.getURLs(SEARCH_BUNDLE_PATTERN, urlToSeek.toArray(new URL[urlToSeek.size()]));
+ if (log.isDebugEnabled()) {
for (URL url : urls) {
log.debug(url.toString());
}
}
+ return urls.toArray(new URL[urls.size()]);
} catch (Exception eee) {
log.warn("Unable to find urls for loader : " + loader);
return new URL[0];
}
- return urls.toArray(new URL[urls.size()]);
}
protected List<I18nBundle> detectBundles(URL... urls) {
Added: lutinutil/trunk/src/main/java/org/codelutin/util/ClassLoaderUtil.java
===================================================================
--- lutinutil/trunk/src/main/java/org/codelutin/util/ClassLoaderUtil.java (rev 0)
+++ lutinutil/trunk/src/main/java/org/codelutin/util/ClassLoaderUtil.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -0,0 +1,81 @@
+package org.codelutin.util;
+
+import org.apache.commons.logging.LogFactory;
+
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+/**
+ * A usefull class with method for ClassLoader
+ *
+ * @author chemit
+ */
+public class ClassLoaderUtil {
+
+
+ /** to use log facility, just put in your code: log.info(\"...\"); */
+ private static final org.apache.commons.logging.Log log = LogFactory.getLog(ClassLoaderUtil.class);
+
+ /**
+ * Returns the all urls to be used in a {@link java.net.URLClassLoader}.
+ * <p/>
+ * If classloader has only one url and the url is a jar, try to load in manifest class-path
+ * <p/>
+ * <b>TODO: we should seek in manifest for all jar url found.</b>
+ *
+ * @param loader the classloader (if null will use system one)
+ * @return all the url found in the classloader
+ */
+ static public URL[] getDeepURLs(URLClassLoader loader) {
+ URL[] result = getURLs(loader);
+ if (result.length == 1) {
+ // this could be a jar invocation
+ URL jarURL = result[0];
+ if (Resource.isJar(jarURL.toString())) {
+ // jar invocation
+ try {
+ result = Resource.getClassPathURLsFromJarManifest(jarURL);
+ } catch (Exception e) {
+ log.warn(e);
+ result = new URL[]{jarURL};
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Recupere la liste des urls d'un {@link java.net.URLClassLoader}.
+ * <p/>
+ * Note : Un cas particulier est positionné pour JBoss qui utilise la method getAllURLs.
+ *
+ * @param classLoader le class loader a scanner
+ * @return les urls du classloade.
+ */
+ static public URL[] getURLs(URLClassLoader classLoader) {
+ if (classLoader == null) {
+ classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
+ }
+ Method m;
+ try {
+ // Essai de récupération de la méthode getAllURLs() de
+ // RepositoryClassLoader (JBoss)
+ m = classLoader.getClass().getMethod("getAllURLs");
+ } catch (Exception e) {
+ m = null;
+ }
+ URL[] result;
+ if (m == null) {
+ result = classLoader.getURLs();
+ } else {
+ try {
+ result = (URL[]) m.invoke(classLoader);
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ return result;
+ }
+
+}
Modified: lutinutil/trunk/src/main/java/org/codelutin/util/Resource.java
===================================================================
--- lutinutil/trunk/src/main/java/org/codelutin/util/Resource.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/main/java/org/codelutin/util/Resource.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -50,6 +50,7 @@
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
@@ -75,7 +76,7 @@
*/
static public void addDefaultClassLoader(URL url) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
- addClassLoader(classLoader,url);
+ addClassLoader(classLoader, url);
}
/**
@@ -83,16 +84,16 @@
* lequel il faut rechercher les fichiers.
*
* @param classLoader le classloader a modifier
- * @param url l'url a ajouter
+ * @param url l'url a ajouter
*/
- static public void addClassLoader(ClassLoader classLoader ,URL url) {
+ static public void addClassLoader(ClassLoader classLoader, URL url) {
try {
Method method = URLClassLoader.class.getDeclaredMethod("addURL",
new Class[]{URL.class});
method.setAccessible(true);
method.invoke(classLoader, url);
} catch (Exception eee) {
- throw new RuntimeException("Can't add url in classloader "+classLoader,
+ throw new RuntimeException("Can't add url in classloader " + classLoader,
eee);
}
}
@@ -248,26 +249,17 @@
return getURLs(pattern, (URLClassLoader) null);
}
- static protected URL[] getURLs(URLClassLoader classLoader) {
- Method m;
- try {
- // Essai de récupération de la méthode getAllURLs() de
- // RepositoryClassLoader (JBoss)
- m = classLoader.getClass().getMethod("getAllURLs");
- } catch (Exception e) {
- m = null;
- }
- URL[] result;
- if (m == null) {
- result = classLoader.getURLs();
- } else {
- try {
- result = (URL[]) m.invoke(classLoader);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
- return result;
+ /**
+ * Recupere la liste des urls d'un {@link java.net.URLClassLoader}.
+ * <p/>
+ * Note : Un cas particulier est positionné pour JBoss qui utilise la method getAllURLs.
+ *
+ * @param classLoader le class loader a scanner
+ * @return les urls du classloade.
+ * @deprecated should use now {@link org.codelutin.util.ClassLoaderUtil#getURLs(java.net.URLClassLoader)}
+ */
+ static public URL[] getURLs(URLClassLoader classLoader) {
+ return ClassLoaderUtil.getURLs(classLoader);
}
/**
@@ -284,7 +276,7 @@
if (classLoader == null) {
classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
}
- URL[] arrayURL = getURLs(classLoader);
+ URL[] arrayURL = ClassLoaderUtil.getURLs(classLoader);
return getURLs(pattern, arrayURL);
}
@@ -326,22 +318,37 @@
// TODO deal with encoding in windows, this is very durty, but it
// works...
File file = new File(fileName.replaceAll("%20", " "));
- // cas ou le ichier du classLoader est un fichier jar ou zip
- if (file.exists() && (isJar(fileName) || isZip(fileName))) {
+ if (!file.exists()) {
+ // this case should not appear
+ continue;
+ }
+ if (isJar(fileName)) {
+ // cas ou le ichier du classLoader est un fichier jar
if (log.isDebugEnabled()) {
log.debug("jar to search " + file);
}
urlList.addAll(Resource.getURLsFromJar(file, pattern));
-
+ continue;
+ }
+ if (file.isDirectory()) {
// cas ou le ichier du classLoader est un repertoire
- } else if (file.exists() && file.isDirectory()) {
if (log.isDebugEnabled()) {
- log.debug("file to search " + file);
+ log.debug("directory to search " + file);
}
// on traite le cas ou il peut y avoir des repertoire dans ce
// repertoire
urlList.addAll(Resource.getURLsFromDirectory(file, pattern));
+ continue;
}
+
+ if (isZip(fileName)) {
+ // cas ou le ichier du classLoader est un fichier zip
+ if (log.isDebugEnabled()) {
+ log.debug("zip to search " + file);
+ }
+ urlList.addAll(Resource.getURLsFromZip(file, pattern));
+ }
+
}
if (log.isInfoEnabled()) {
log.info("search URLs pattern: " + pattern + " in "
@@ -392,6 +399,48 @@
return result;
}
+ static public List<URL> getURLsFromZip(File zipFile, String pattern) {
+ try {
+ if (log.isTraceEnabled()) {
+ log.trace("search '" + pattern + "' in " + zipFile);
+ }
+
+ ArrayList<URL> result = new ArrayList<URL>();
+ InputStream in = new FileInputStream(zipFile);
+ ZipInputStream zis = new ZipInputStream(in);
+ while (zis.available() != 0) {
+ ZipEntry entry = zis.getNextEntry();
+
+ if (entry == null) {
+ break;
+ }
+
+ String name = entry.getName();
+ if (log.isTraceEnabled()) {
+ log.trace("zipFile: " + zipFile + " name: " + name);
+ }
+ if (pattern == null || name.matches(pattern)) {
+ // on recupere le fichier correspondant au pattern dans le
+ // classloader
+ URL url = Resource.getURL(name);
+ // on ajoute le fichier correspondant au pattern dans la
+ // liste
+ if (log.isTraceEnabled()) {
+ log.trace("zipFile: " + zipFile + " url: " + url);
+ }
+ result.add(url);
+ }
+ }
+ if (log.isTraceEnabled()) {
+ log.trace("found with pattern '" + pattern + "' : " + result);
+ }
+ return result;
+ } catch (IOException eee) {
+ throw new ResourceException(
+ "Erreur lors de la lecture du fichier compressé", eee);
+ }
+ }
+
static public List<URL> getURLsFromJar(File jarfile, String pattern) {
try {
if (log.isTraceEnabled()) {
@@ -543,25 +592,70 @@
* n'y a pas de type primitif associé.
*/
static public Class getPrimitiveClass(Class clazz) {
- if (clazz == Boolean.class)
+ if (clazz == Boolean.class) {
return Boolean.TYPE;
- if (clazz == Byte.class)
+ }
+ if (clazz == Byte.class) {
return Byte.TYPE;
- if (clazz == Character.class)
+ }
+ if (clazz == Character.class) {
return Character.TYPE;
- if (clazz == Short.class)
+ }
+ if (clazz == Short.class) {
return Short.TYPE;
- if (clazz == Integer.class)
+ }
+ if (clazz == Integer.class) {
return Integer.TYPE;
- if (clazz == Long.class)
+ }
+ if (clazz == Long.class) {
return Long.TYPE;
- if (clazz == Float.class)
+ }
+ if (clazz == Float.class) {
return Float.TYPE;
- if (clazz == Double.class)
+ }
+ if (clazz == Double.class) {
return Double.TYPE;
- if (clazz == Void.class)
+ }
+ if (clazz == Void.class) {
return Void.TYPE;
+ }
return null;
}
+
+ /**
+ * Test if an url contains the given directory with no recurse seeking.
+ *
+ * @param url the url to seek
+ * @param directory the directory to find
+ * @return <code>true</code> if directory was found, <code>false</code> otherwise.
+ * @throws java.io.IOException if any io pb
+ */
+ public static boolean containsDirectDirectory(URL url, String directory) throws IOException {
+ String fileName = url.getFile();
+ // TODO deal with encoding in windows, this is very durty, but it works...
+ File file = new File(fileName.replaceAll("%20", " "));
+ if (!file.exists()) {
+ return false;
+ }
+ if (isJar(fileName) || isZip(fileName)) {
+ // cas ou le fichier du classLoader est un fichier jar ou zip
+ if (log.isTraceEnabled()) {
+ log.trace("zip to search " + file);
+ }
+ return new ZipFile(file).getEntry(directory + "/") != null;
+ }
+ if (file.isDirectory()) {
+ // cas ou le ichier du classLoader est un repertoire
+ if (log.isTraceEnabled()) {
+ log.trace("directory to search " + file);
+ }
+ return new File(file, directory).exists();
+ }
+
+ if (log.isWarnEnabled()) {
+ log.warn("could not treate unknown type of url " + url);
+ }
+ return false;
+ }
} // Resource
Modified: lutinutil/trunk/src/test/java/org/codelutin/i18n/I18nLoaderTest.java
===================================================================
--- lutinutil/trunk/src/test/java/org/codelutin/i18n/I18nLoaderTest.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/test/java/org/codelutin/i18n/I18nLoaderTest.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -18,8 +18,10 @@
*/
package org.codelutin.i18n;
-import junit.framework.TestCase;
import org.codelutin.i18n.bundle.I18nBundleManager;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Test;
import java.util.Locale;
@@ -30,7 +32,7 @@
* @version 1.0
* @since <pre>02/10/2008</pre>
*/
-public class I18nLoaderTest extends TestCase {
+public class I18nLoaderTest {
String encoding;
@@ -39,35 +41,25 @@
I18nLoader loader;
I18nBundleManager bundleManager;
- public I18nLoaderTest(String name) {
- super(name);
-
- }
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- }
-
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
+ @AfterClass
+ protected void afterClass() throws Exception {
I18n.close();
}
+ @Test
public void testGetLoader() throws Exception {
- assertNull(I18n.loader);
+ Assert.assertNull(I18n.loader);
- loader = I18n.getLoader(I18n.DEFAULT_ENCODING);
+ loader = I18n.getLoader();
bundleManager = I18n.getBundleManager();
- assertNotNull(loader);
- assertEquals(I18n.DEFAULT_ENCODING, loader.getEncoding());
- assertEquals(bundleManager.getDefaultLocale(), I18n.DEFAULT_LOCALE);
- assertNull(loader.getLanguage());
+ Assert.assertNotNull(loader);
+ //assertEquals(I18n.DEFAULT_ENCODING, loader.getEncoding());
+ Assert.assertEquals(bundleManager.getDefaultLocale(), I18n.DEFAULT_LOCALE);
+ Assert.assertNull(loader.getLanguage());
}
+ @Test
public void testChangeLocale() throws Exception {
locale = I18n.newLocale("fr_FR");
@@ -93,11 +85,11 @@
assertNbLanguages(4);
}
- private void assertLanguageChanged() {
- assertNotSame(language, loader.getLanguage());
+ protected void assertLanguageChanged() {
+ Assert.assertNotSame(language, loader.getLanguage());
}
- public void testChangeEncoding() throws Exception {
+ /*public void testChangeEncoding() throws Exception {
locale = I18n.newLocale("fr_FR");
encoding = I18n.ISO_8859_1_ENCONDING;
updateLanguage();
@@ -117,15 +109,15 @@
// one language in cache
assertNbLanguages(1);
- }
+ }*/
protected void assertNbLanguages(int i) {
- assertEquals(i, loader.getLanguages().size());
+ Assert.assertEquals(i, loader.getLanguages().size());
}
protected void updateLanguage() {
language = loader == null ? null : loader.getLanguage();
- loader = I18n.getLoader(encoding);
+ loader = I18n.getLoader();
bundleManager = I18n.getBundleManager();
loader.setLanguage(locale, bundleManager);
}
Modified: lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBundleManagerTest.java
===================================================================
--- lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBundleManagerTest.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBundleManagerTest.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -18,13 +18,15 @@
*/
package org.codelutin.i18n.bundle;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
import org.apache.commons.logging.LogFactory;
import org.codelutin.i18n.I18n;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
import java.io.File;
+import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Locale;
@@ -56,7 +58,7 @@
* @version 1.0
* @since <pre>03/02/2008</pre>
*/
-public class I18nBundleManagerTest extends TestCase {
+public class I18nBundleManagerTest {
/** to use log facility, just put in your code: log.info(\"...\"); */
@@ -65,9 +67,9 @@
Locale locale;
- static URLClassLoader loader;
+ URLClassLoader loader;
- private static Integer nbURLs = null;
+ Integer nbURLs = null;
private I18nBundleManager bundleManager;
@@ -184,63 +186,47 @@
}
}
- public I18nBundleManagerTest(String name) {
- super(name);
- }
+ @BeforeClass
+ public void befireClass() throws MalformedURLException {
- @Override
- public void setUp() throws Exception {
- super.setUp();
- if (loader == null) {
- loader = (URLClassLoader) I18nBundleManagerTest.class.getClassLoader();
+ loader = (URLClassLoader) I18nBundleManagerTest.class.getClassLoader();
- URL[] urls = new URL[loader.getURLs().length + 2];
- urls[0] = new File("target" + File.separator + "test-classes" + File.separator + "i18n").toURI().toURL();
- urls[1] = new File("target" + File.separator + "classes" + File.separator + "i18n").toURI().toURL();
+ URL[] urls = new URL[loader.getURLs().length + 2];
- int i = 2;
- for (URL url : loader.getURLs()) {
- urls[i++] = url;
- }
- loader = new URLClassLoader(urls, loader);
+ urls[0] = new File("target" + File.separator + "test-classes" + File.separator + "i18n").toURI().toURL();
+ urls[1] = new File("target" + File.separator + "classes" + File.separator + "i18n").toURI().toURL();
- log.info("use loader " + loader + " (nb urls : " + loader.getURLs().length + ")");
- for (URL url : loader.getURLs()) {
- log.info("url found in classloader : " + url);
- }
+ int i = 2;
+ for (URL url : loader.getURLs()) {
+ urls[i++] = url;
}
+ loader = new URLClassLoader(urls, loader);
+
+ log.info("use loader " + loader + " (nb urls : " + loader.getURLs().length + ")");
+ for (URL url : loader.getURLs()) {
+ log.info("url found in classloader : " + url);
+ }
}
- @Override
- public void tearDown() throws Exception {
- super.tearDown();
+ @After
+ public void after() throws Exception {
locale = null;
}
-
- public static int getNbURLs() {
- if (nbURLs == null) {
- File f;
- f = new File("target" + File.separator + "test-classes" + File.separator + "i18n");
- nbURLs = f.listFiles().length;
- f = new File("target" + File.separator + "classes" + File.separator + "i18n");
- nbURLs += f.listFiles().length;
- }
- return nbURLs;
- }
-
+ @Test
public void testGetURLs() throws Exception {
-
bundleManager = I18n.getBundleManager();
- assertEquals(getNbURLs(), bundleManager.getURLs(loader).length);
+ Assert.assertEquals(getNbURLs(), bundleManager.getURLs(loader).length);
}
+ @Test
public void testDetectBundles() throws Exception {
bundleManager = I18n.getBundleManager();
URL[] urls = bundleManager.getURLs(loader);
- assertEquals(BundleTest.values().length, bundleManager.detectBundles(urls).size());
+ Assert.assertEquals(BundleTest.values().length, bundleManager.detectBundles(urls).size());
}
+ @Test
public void testGetBundles() throws Exception {
updateLanguage(null);
updateLanguage(Locale.FRENCH);
@@ -250,9 +236,21 @@
updateLanguage(Locale.UK);
}
+ protected int getNbURLs() {
+ if (nbURLs == null) {
+ File f;
+ f = new File("target" + File.separator + "test-classes" + File.separator + "i18n");
+ nbURLs = f.listFiles().length;
+ f = new File("target" + File.separator + "classes" + File.separator + "i18n");
+ nbURLs += f.listFiles().length;
+ }
+ return nbURLs;
+ }
+
protected void updateLanguage(Locale newLocale) {
locale = newLocale;
- I18n.init(locale, I18n.DEFAULT_ENCODING);
+ I18n.init(locale);
+ //I18n.init(locale, I18n.DEFAULT_ENCODING);
assertBundlesEntries();
}
@@ -283,12 +281,9 @@
nbEntries += BundleTest.getNbLanguageEntries(isFr, isEn);
}
bundleManager = I18n.getBundleManager();
- assertEquals(nbGene + nbLang + nbFull, bundleManager.getBundles(locale).length);
+ Assert.assertEquals(nbGene + nbLang + nbFull, bundleManager.getBundles(locale).length);
//TODO make eact match with promute logic !
- assertTrue(nbEntries <= bundleManager.getBundleEntries(locale).length);
+ Assert.assertTrue(nbEntries <= bundleManager.getBundleEntries(locale).length);
}
- public static Test suite() {
- return new TestSuite(I18nBundleManagerTest.class);
- }
}
Modified: lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBunsleScopeTest.java
===================================================================
--- lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBunsleScopeTest.java 2008-11-17 19:54:56 UTC (rev 1235)
+++ lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBunsleScopeTest.java 2008-11-17 23:29:40 UTC (rev 1236)
@@ -18,41 +18,46 @@
*/
package org.codelutin.i18n.bundle;
-import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Test;
import java.util.Locale;
/** @author chemit */
-public class I18nBunsleScopeTest extends TestCase {
+public class I18nBunsleScopeTest {
+
Locale locale;
I18nBundleScope excepted;
+ @Test
public void testFullScope() {
excepted = I18nBundleScope.FULL;
locale = new Locale("fr", "FR");
- assertEquals(excepted, I18nBundleScope.valueOf(locale));
+ Assert.assertEquals(excepted, I18nBundleScope.valueOf(locale));
}
+ @Test
public void testLanguageScope() {
excepted = I18nBundleScope.LANGUAGE;
locale = new Locale("fr");
- assertEquals(excepted, I18nBundleScope.valueOf(locale));
+ Assert.assertEquals(excepted, I18nBundleScope.valueOf(locale));
locale = new Locale("fr", "");
- assertEquals(excepted, I18nBundleScope.valueOf(locale));
+ Assert.assertEquals(excepted, I18nBundleScope.valueOf(locale));
}
+ @Test
public void testGeneralScope() {
excepted = I18nBundleScope.GENERAL;
locale = null;
- assertEquals(excepted, I18nBundleScope.valueOf(locale));
+ Assert.assertEquals(excepted, I18nBundleScope.valueOf(locale));
locale = new Locale("");
- assertEquals(excepted, I18nBundleScope.valueOf(locale));
+ Assert.assertEquals(excepted, I18nBundleScope.valueOf(locale));
}
}