[Lutinutil-commits] r1241 - in lutinutil/trunk/src: main/java/org/codelutin/i18n main/java/org/codelutin/i18n/bundle main/java/org/codelutin/util test/java/org/codelutin/i18n test/java/org/codelutin/i18n/bundle test/java/org/codelutin/util
Author: chemit Date: 2008-11-18 16:23:18 +0000 (Tue, 18 Nov 2008) New Revision: 1241 Modified: 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/I18nBundleManager.java lutinutil/trunk/src/main/java/org/codelutin/util/ClassLoaderUtil.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/util/FileCompletionTest.java Log: make a junit4 test (but does not activate here since need user interaction). beforeClass method must be static public seek into manifest class-path in each url jar find fix when no manifest nor classpath in manifest improve i18n loader : only seek into url entries of class path which contains a direct i18n directory : so no more need to scan all jars... Modified: lutinutil/trunk/src/main/java/org/codelutin/i18n/I18nLoader.java =================================================================== --- lutinutil/trunk/src/main/java/org/codelutin/i18n/I18nLoader.java 2008-11-18 11:02:09 UTC (rev 1240) +++ lutinutil/trunk/src/main/java/org/codelutin/i18n/I18nLoader.java 2008-11-18 16:23:18 UTC (rev 1241) @@ -29,8 +29,6 @@ /** * Classe responsible of loading of I18n system. * <p/> - * The invariant of class is property {@link #encoding}. - * <p/> * Contains the current used {@link #language} (can be null, if not set), and the list of already loaded {@link #languages}. * <p/> * <p/> @@ -43,9 +41,6 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ private static final Log log = LogFactory.getLog(I18nLoader.class); - /** l'encoding a utiliser pour charger les traductions */ - //protected final String encoding; - /** le language actuellement utilise */ protected Language language; @@ -53,18 +48,10 @@ protected List<Language> languages; public I18nLoader(String encoding) { - log.info("encoding "+encoding); - //this.encoding = encoding; + log.info("encoding " + encoding); } - /** @return the encoding used by this loader, this an invariant */ - /*public String getEncoding() { - return encoding; - }*/ - - /** - * @return current language loaded or null, if no language was load - */ + /** @return current language loaded or null, if no language was load */ public Language getLanguage() { return language; } @@ -133,7 +120,6 @@ Language addLanguage(Locale locale, I18nBundleManager bundleManager) { Language result; 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-18 11:02:09 UTC (rev 1240) +++ lutinutil/trunk/src/main/java/org/codelutin/i18n/Language.java 2008-11-18 16:23:18 UTC (rev 1241) @@ -38,7 +38,9 @@ /** * This class is used by the i18n class. It encapsulates the translation - * resource for a given language and a given encoding. + * resource for a given language. + * <p/> + * Encoding to be used to read properties files will be ISO-8859-1 since java does it like this. */ public class Language { 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-18 11:02:09 UTC (rev 1240) +++ lutinutil/trunk/src/main/java/org/codelutin/i18n/bundle/I18nBundleManager.java 2008-11-18 16:23:18 UTC (rev 1241) @@ -21,9 +21,9 @@ import org.apache.commons.logging.LogFactory; import org.codelutin.i18n.I18n; import org.codelutin.i18n.Language; +import org.codelutin.util.ClassLoaderUtil; import org.codelutin.util.Resource; import org.codelutin.util.StringUtil; -import org.codelutin.util.ClassLoaderUtil; import java.io.IOException; import java.net.URL; @@ -239,7 +239,7 @@ protected URL[] getURLs(URLClassLoader loader, URL... extraUrl) { try { - List<URL> urlToSeek = new ArrayList<URL>(); + List<URL> urlToSeek = new ArrayList<URL>(); urlToSeek.addAll(Arrays.asList(ClassLoaderUtil.getDeepURLs(loader))); if (extraUrl.length > 0) { urlToSeek.addAll(Arrays.asList(extraUrl)); @@ -256,7 +256,7 @@ } } if (log.isDebugEnabled()) { - log.debug("will scan " + urlToSeek.size() + " out of " + size); + log.debug("detect " + urlToSeek.size() + " i18n capable url (out of " + size + ")"); } List<URL> urls = Resource.getURLs(SEARCH_BUNDLE_PATTERN, urlToSeek.toArray(new URL[urlToSeek.size()])); if (log.isDebugEnabled()) { Modified: lutinutil/trunk/src/main/java/org/codelutin/util/ClassLoaderUtil.java =================================================================== --- lutinutil/trunk/src/main/java/org/codelutin/util/ClassLoaderUtil.java 2008-11-18 11:02:09 UTC (rev 1240) +++ lutinutil/trunk/src/main/java/org/codelutin/util/ClassLoaderUtil.java 2008-11-18 16:23:18 UTC (rev 1241) @@ -5,6 +5,10 @@ import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Stack; /** * A usefull class with method for ClassLoader @@ -21,28 +25,44 @@ * 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) { + Stack<URL> urlToTreate = new Stack<URL>(); + List<URL> urlTreated = new ArrayList<URL>(); + + // first get the urls from classloader URL[] result = getURLs(loader); - if (result.length == 1) { - // this could be a jar invocation - URL jarURL = result[0]; - if (Resource.isJar(jarURL.toString())) { + + urlToTreate.addAll(Arrays.asList(result)); + while (!urlToTreate.isEmpty()) { + URL currentUrl = urlToTreate.pop(); + // save the url + urlTreated.add(currentUrl); + if (Resource.isJar(currentUrl.toString())) { // jar invocation try { - result = Resource.getClassPathURLsFromJarManifest(jarURL); + URL[] newArrayURLs = Resource.getClassPathURLsFromJarManifest(currentUrl); + if (newArrayURLs == null) { + continue; + } + List<URL> newURLs = Arrays.asList(newArrayURLs); + for (URL newURL : newURLs) { + if (!urlTreated.contains(newURL) && !urlToTreate.contains(newURL)) { + urlToTreate.add(newURL); + } + } } catch (Exception e) { - log.warn(e); - result = new URL[]{jarURL}; + if (log.isDebugEnabled()) { + // this is not a such error, but some jar can not be + log.debug("error with url" + currentUrl + " for reason : " + e.getMessage()); + } } } } - return result; + return urlTreated.toArray(new URL[urlToTreate.size()]); } /** @@ -59,8 +79,7 @@ } Method m; try { - // Essai de récupération de la méthode getAllURLs() de - // RepositoryClassLoader (JBoss) + // Essai de récupération de la méthode getAllURLs() de RepositoryClassLoader (JBoss) m = classLoader.getClass().getMethod("getAllURLs"); } catch (Exception e) { m = null; @@ -78,4 +97,14 @@ return result; } + static public void printLoader(ClassLoader loader) { + log.info(loader); + if (loader instanceof URLClassLoader) { + URL[] urls = getURLs((URLClassLoader) loader); + for (URL url : urls) { + log.info(url); + } + } + } + } Modified: lutinutil/trunk/src/main/java/org/codelutin/util/Resource.java =================================================================== --- lutinutil/trunk/src/main/java/org/codelutin/util/Resource.java 2008-11-18 11:02:09 UTC (rev 1240) +++ lutinutil/trunk/src/main/java/org/codelutin/util/Resource.java 2008-11-18 16:23:18 UTC (rev 1241) @@ -365,15 +365,22 @@ try { String jarPath = jarURL.toURI().getPath(); File jarFile = new File(jarPath); - if (log.isInfoEnabled()) { - log.info("class-path jar to scan " + jarPath); + if (log.isDebugEnabled()) { + log.debug("class-path jar to scan " + jarPath); } jar = new JarFile(jarFile); File container = jarFile.getParentFile(); Manifest mf = jar.getManifest(); - String classPath = mf.getMainAttributes().getValue( - Attributes.Name.CLASS_PATH); - String[] paths = classPath.split(" "); + String classPath = null; + if (mf != null && mf.getMainAttributes() != null) { + classPath = mf.getMainAttributes().getValue(Attributes.Name.CLASS_PATH); + } + String[] paths; + if (classPath != null) { + paths = classPath.split(" "); + } else { + paths = new String[0]; + } result = new URL[paths.length + 1]; result[0] = jarURL; File path; Modified: lutinutil/trunk/src/test/java/org/codelutin/i18n/I18nLoaderTest.java =================================================================== --- lutinutil/trunk/src/test/java/org/codelutin/i18n/I18nLoaderTest.java 2008-11-18 11:02:09 UTC (rev 1240) +++ lutinutil/trunk/src/test/java/org/codelutin/i18n/I18nLoaderTest.java 2008-11-18 16:23:18 UTC (rev 1241) @@ -34,7 +34,6 @@ */ public class I18nLoaderTest { - String encoding; Locale locale; Language language; @@ -42,7 +41,7 @@ I18nBundleManager bundleManager; @AfterClass - protected void afterClass() throws Exception { + public static void afterClass() throws Exception { I18n.close(); } 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-18 11:02:09 UTC (rev 1240) +++ lutinutil/trunk/src/test/java/org/codelutin/i18n/bundle/I18nBundleManagerTest.java 2008-11-18 16:23:18 UTC (rev 1241) @@ -67,7 +67,7 @@ Locale locale; - URLClassLoader loader; + static URLClassLoader loader; Integer nbURLs = null; @@ -187,7 +187,7 @@ } @BeforeClass - public void befireClass() throws MalformedURLException { + public static void befireClass() throws MalformedURLException { loader = (URLClassLoader) I18nBundleManagerTest.class.getClassLoader(); Modified: lutinutil/trunk/src/test/java/org/codelutin/util/FileCompletionTest.java =================================================================== --- lutinutil/trunk/src/test/java/org/codelutin/util/FileCompletionTest.java 2008-11-18 11:02:09 UTC (rev 1240) +++ lutinutil/trunk/src/test/java/org/codelutin/util/FileCompletionTest.java 2008-11-18 16:23:18 UTC (rev 1241) @@ -18,17 +18,30 @@ */ package org.codelutin.util; -import java.io.IOException; +public class FileCompletionTest { -public class FileCompletionTest { - /** - * - * @param args - * @throws IOException - */ - public static void main(String[] args) throws IOException { - FileCompletion fc = new FileCompletion(true,true); - if (fc.consoleAvailable()) + @org.junit.Ignore + public void testMe() throws Exception { + FileCompletion fc = new FileCompletion(true, true); + if (fc.consoleAvailable()) { + new Thread(new Runnable() { + + public void run() { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // do not care... + } finally { + System.console().readLine("!q"); + } + } + }).start(); System.out.println(fc.read()); + } } + + @org.junit.Test + public void testDummy() { + // do nothing, but avoid surefire warning (test class with no test) + } }
participants (1)
-
chemit@users.labs.libre-entreprise.org