Author: tchemit Date: 2011-07-17 14:11:05 +0200 (Sun, 17 Jul 2011) New Revision: 2153 Url: http://nuiton.org/repositories/revision/nuiton-utils/2153 Log: Anomalie #1632: Resource.getURLs is not working under windows Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/Resource.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/Resource.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/Resource.java 2011-07-06 09:25:33 UTC (rev 2152) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/Resource.java 2011-07-17 12:11:05 UTC (rev 2153) @@ -25,6 +25,7 @@ package org.nuiton.util; +import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -32,7 +33,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.lang.reflect.Method; import java.net.JarURLConnection; import java.net.MalformedURLException; @@ -241,20 +241,22 @@ for (URL urlFile : arrayURL) { // EC-20100510 this cause wrong accent encoding //String fileName = urlFile.getFile(); - String fileName; - try { - fileName = urlFile.toURI().getPath(); - } catch (Exception e) { - if (log.isWarnEnabled()) { - log.warn(e); - } - // warning, this can cause wrong encoding !!! - fileName = urlFile.getFile(); - } +// String fileName; +// try { +// fileName = urlFile.toURI().getPath(); +// } catch (Exception e) { +// if (log.isWarnEnabled()) { +// log.warn(e); +// } +// // warning, this can cause wrong encoding !!! +// fileName = urlFile.getFile(); +// } - // TODO deal with encoding in windows, this is very durty, but it - // works... - File file = new File(fileName.replaceAll("%20", " ")); +// // TODO deal with encoding in windows, this is very durty, but it +// // works... + File file = FileUtils.toFile(urlFile); + String fileName = file.getAbsolutePath(); +// File file = new File(fileName.replaceAll("%20", " ")); if (!file.exists()) { // this case should not appear if (log.isDebugEnabled()) { @@ -299,16 +301,16 @@ } public static URL[] getClassPathURLsFromJarManifest(URL jarURL) - throws IOException, URISyntaxException { - JarFile jar = null; + throws IOException { URL[] result; + File jarFile = FileUtils.toFile(jarURL); + if (log.isDebugEnabled()) { + log.debug("class-path jar to scan " + jarFile); + } + JarFile jar = new JarFile(jarFile); try { - String jarPath = jarURL.toURI().getPath(); - File jarFile = new File(jarPath); - if (log.isDebugEnabled()) { - log.debug("class-path jar to scan " + jarPath); - } - jar = new JarFile(jarFile); +// String jarPath = jarURL.toURI().getPath(); +// File jarFile = new File(jarPath); File container = jarFile.getParentFile(); Manifest mf = jar.getManifest(); String classPath = null; @@ -343,7 +345,7 @@ } result[i + 1] = path.toURI().toURL(); } - jar.close(); +// jar.close(); } finally { if (jar != null) { jar.close(); @@ -358,38 +360,46 @@ 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(); + List<URL> result = new ArrayList<URL>(); +// InputStream in = new FileInputStream(zipFile); + ZipInputStream zis = + new ZipInputStream(new FileInputStream(zipFile)); + try { + while (zis.available() != 0) { + ZipEntry entry = zis.getNextEntry(); - if (entry == null) { - break; - } + 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 = getURL(name); - // on ajoute le fichier correspondant au pattern dans la - // liste + String name = entry.getName(); if (log.isTraceEnabled()) { - log.trace("zipFile: " + zipFile + " url: " + url); + log.trace("zipFile: " + zipFile + " name: " + name); } - result.add(url); + if (pattern == null || name.matches(pattern)) { + // on recupere le fichier correspondant au pattern dans + // le classloader + URL url = getURL(name); + // on ajoute le fichier correspondant au pattern dans + // la liste + if (log.isTraceEnabled()) { + log.trace("zipFile: " + zipFile + " url: " + url); + } + result.add(url); + } } + } finally { + zis.close(); } if (log.isTraceEnabled()) { log.trace("found with pattern '" + pattern + "' : " + result); } return result; } catch (IOException eee) { - throw new ResourceException(_("nuitonutil.error.get.url.from.zip", zipFile.getAbsolutePath(), eee.getMessage())); + throw new ResourceException(_("nuitonutil.error.get.url.from.zip", + zipFile.getAbsolutePath(), + eee.getMessage()) + ); } } @@ -400,37 +410,45 @@ } List<URL> result = new ArrayList<URL>(); - InputStream in = new FileInputStream(jarfile); - ZipInputStream zis = new ZipInputStream(in); - while (zis.available() != 0) { - ZipEntry entry = zis.getNextEntry(); +// InputStream in = new FileInputStream(jarfile); + ZipInputStream zis = + new ZipInputStream(new FileInputStream(jarfile)); + try { + while (zis.available() != 0) { + ZipEntry entry = zis.getNextEntry(); - if (entry == null) { - break; - } + if (entry == null) { + break; + } - String name = entry.getName(); - if (log.isTraceEnabled()) { - log.trace("jarfile: " + jarfile + " name: " + name); - } - if (pattern == null || name.matches(pattern)) { - // on recupere le fichier correspondant au pattern dans le - // classloader - URL url = getURL(name); - // on ajoute le fichier correspondant au pattern dans la - // liste + String name = entry.getName(); if (log.isTraceEnabled()) { - log.trace("jarfile: " + jarfile + " url: " + url); + log.trace("jarfile: " + jarfile + " name: " + name); } - result.add(url); + if (pattern == null || name.matches(pattern)) { + // on recupere le fichier correspondant au pattern + // dans le classloader + URL url = getURL(name); + // on ajoute le fichier correspondant au pattern dans + // la liste + if (log.isTraceEnabled()) { + log.trace("jarfile: " + jarfile + " url: " + url); + } + result.add(url); + } } + } finally { + zis.close(); } if (log.isTraceEnabled()) { log.trace("found with pattern '" + pattern + "' : " + result); } return result; } catch (IOException eee) { - throw new ResourceException(_("nuitonutil.error.get.url.from.zip", jarfile.getAbsolutePath(), eee.getMessage())); + throw new ResourceException(_("nuitonutil.error.get.url.from.zip", + jarfile.getAbsolutePath(), + eee.getMessage()) + ); } } @@ -485,7 +503,11 @@ } return urlList; } catch (MalformedURLException eee) { - throw new ResourceException(_("nuitonutil.error.convert.file.to.url", repository + " (pattern " + pattern + ") ", eee.getMessage())); + throw new ResourceException( + _("nuitonutil.error.convert.file.to.url", + repository + " (pattern " + pattern + ") ", + eee.getMessage()) + ); //throw new ResourceException("Le fichier n'a pu être converti en URL", eee); } } @@ -555,9 +577,11 @@ * @throws 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", " ")); +// String fileName = url.getFile(); +// // TODO deal with encoding in windows, this is very durty, but it works... +// File file = new File(fileName.replaceAll("%20", " ")); + File file = FileUtils.toFile(url); + String fileName = file.getAbsolutePath(); if (!file.exists()) { return false; } @@ -566,7 +590,12 @@ if (log.isTraceEnabled()) { log.trace("zip to search " + file); } - return new ZipFile(file).getEntry(directory + '/') != null; + ZipFile zipFile = new ZipFile(file); + try { + return zipFile.getEntry(directory + '/') != null; + } finally { + zipFile.close(); + } } if (file.isDirectory()) { // cas ou le ichier du classLoader est un repertoire