[Lutinutil-commits] r1257 - in lutinutil/tags: . 0.29.1 0.29.1/src/java/org/codelutin/util
Author: schorlet Date: 2008-11-27 09:57:52 +0000 (Thu, 27 Nov 2008) New Revision: 1257 Added: lutinutil/tags/0.29.1/ lutinutil/tags/0.29.1/src/ lutinutil/tags/0.29.1/src/java/org/codelutin/util/Resource.java Removed: lutinutil/tags/0.29.1/src/ lutinutil/tags/0.29.1/src/java/org/codelutin/util/Resource.java Log: version de maintenance pour simexplorer Copied: lutinutil/tags/0.29.1 (from rev 1254, lutinutil/branches/0.29-SNAPSHOT) Copied: lutinutil/tags/0.29.1/src (from rev 1253, lutinutil/branches/0.29-SNAPSHOT/src) Deleted: lutinutil/tags/0.29.1/src/java/org/codelutin/util/Resource.java =================================================================== --- lutinutil/branches/0.29-SNAPSHOT/src/java/org/codelutin/util/Resource.java 2008-11-21 15:51:51 UTC (rev 1253) +++ lutinutil/tags/0.29.1/src/java/org/codelutin/util/Resource.java 2008-11-27 09:57:52 UTC (rev 1257) @@ -1,522 +0,0 @@ -/**##%% -* Copyright (C) 2002, 2003, 2004 Code Lutin, C�dric Pineau, Benjamin Poussin -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*##%%**/ - -/** -* Resource.java -* -* Created: Sun Apr 14 2002 -* -* @author POUSSIN Benjamin <bpoussin@free.fr> -* Copyright Code Lutin -* @version $Revision$ -* -* Mise a jour: $Date$ -* par : $Author$ -*/ - -package org.codelutin.util; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.Method; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import java.util.jar.Attributes; -import java.util.jar.JarFile; -import java.util.jar.Manifest; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -import javax.swing.ImageIcon; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - - -/** -* Cette class permet de recherche un fichier en indiquant son nom -* avec son chemin. Cette librairie ira ensuite chercher ce fichier -* sur le syst�me de fichier, et s'il n'est pas trouv� dans le -* classpath. Le fichier peut donc �tre dans un fichier .jar ou .zip. -*/ -public class Resource { // Resource - - /** to use log facility, just put in your code: log.info(\"...\"); */ - private static final Log log = LogFactory.getLog(Resource.class); - - protected Resource() { - - } - - /** - * Permet d'ajouter dans le classloader par defaut une nouvelle URL - * dans lequel il faut rechercher les fichiers. - * @param url l'url a ajouter - */ - static public void addDefaultClassLoader(URL url){ - try{ - ClassLoader classLoader = ClassLoader.getSystemClassLoader(); - - 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 default classloader", eee); - } - } - - /** - * recherche la ressource nom - * @param name nom de la ressource - * @return l'url de la ressource - * @throws ResourceNotFoundException si la resource n'a pas ete trouvee - */ - static public URL getURL(String name) { - URL url = getURLOrNull(name); - if (url != null){ - return url; - } - - throw new ResourceNotFoundException("La resource " + name + - " n'a pas �t� trouv�e"); - } - - /** - * recherche la ressource nom - * @param name le nom de la ressource - * @return l'url de la ressource ou null - */ - static public URL getURLOrNull(String name) { - // on recherche d'abord sur le filesystem - File file = new File(name); - if (file.exists()){ - try{ - return file.toURI().toURL(); - }catch(MalformedURLException eee){ - log.warn("Le fichier '" + file + "' a �t� trouv�, mais il n'a pas pu etre converti en URL"); - } - } - - //on ne l'a pas trouve on recherche dans le classpath - - // on supprime le / devant le nom de la ressource, sinon elle - // n'est pas trouve (pas de recherche dans les differents - // element du classpath. - if(name.length() > 1 && name.startsWith("/")){ - name = name.substring(1); - } - URL url = ClassLoader.getSystemClassLoader().getResource(name); - if (url != null){ - return url; - } - - ClassLoader cl = Resource.class.getClassLoader(); - url = cl.getResource(name); - return url; - } - - /** - * Retourne l'icon demande - * @param name le nom de l'icone - * @return Retourne l'icon demande ou null s'il n'est pas trouv� - */ - static public ImageIcon getIcon(String name){ - try{ - return new ImageIcon(getURL(name)); - }catch(Exception eee){ - log.warn("Can't find icon: " + name, eee); - return null; - } - } - - - /** - * Recherche et retourne l'objet properties de configuration � utiliser. - * Les diff�rents fichiers trouv� sont chain�. L'ordre de recherche est - * le classpath, le fichier dans /etc et enfin le chemin sur le filesystem - * @param filename le nom du fichier � rechercher - * @return l'objet Properties de configuration - * @throws java.io.IOException si pb - */ - static public Properties getConfigProperties(String filename) throws IOException { - Properties result; - result = getConfigProperties(filename, null); - return result; - } - - static public Properties getConfigProperties(String filename, Properties parent) throws IOException { - Properties result; - if (parent != null) { - result = new Properties(parent); - } else { - result = new Properties(); - } - - URL inClasspath = ClassLoader.getSystemClassLoader().getResource(filename); - if (inClasspath == null) { - inClasspath = Resource.class.getResource(filename); - } - if (inClasspath == null) { - inClasspath = getURLOrNull(filename); - } - if(inClasspath != null){ - log.info("Chargement du fichier de config: " + inClasspath); - result.load(inClasspath.openStream()); - result = new Properties(result); - } - - File etcConfig = new File("/etc/" + filename); - if(etcConfig.exists()){ - log.info("Chargement du fichier de config: " + etcConfig); - result.load(etcConfig.toURI().toURL().openStream()); - result = new Properties(result); - } - - File config = new File(filename); - if(config.exists()){ - log.info("Chargement du fichier de config: " + config); - result.load(config.toURI().toURL().openStream()); - result = new Properties(result); - } - - return result; - } - - /** - * Retourner la liste des fichiers du classLoader. Ces fichiers doivent - * correspondent au pattern donne - * - * @param pattern le nom du fichier a extraire du - * fichier compress� ou durepertoire doit correspondre - * au pattern (repertoire + nom compris). - * @return la liste des urls correspondant au pattern - */ - static public List<URL> getURLs(String pattern) { - return getURLs(pattern, (URLClassLoader) null); - } - - static private 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; - } - - /** - * Retourner la liste des fichiers du classLoader. Ces fichiers doivent - * correspondent au pattern donne - * - *@param classLoader le classLoader - *@param pattern le nom du fichier a extraire du - * fichier compress� ou durepertoire doit correspondre - * au pattern (repertoire + nom compris). - *@return la liste des urls correspondant au pattern - */ - static public List<URL> getURLs(String pattern, URLClassLoader classLoader) { - if(classLoader == null){ - classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader(); - } - URL[] arrayURL = getURLs(classLoader); - return getURLs(pattern,arrayURL); - } - - /** - * Retourner la liste des fichiers du classLoader. Ces fichiers doivent - * correspondent au pattern donne - * - *@param arrayURL les urls ou chercher - *@param pattern le nom du fichier a extraire du - * fichier compress� ou durepertoire doit correspondre - * au pattern (repertoire + nom compris). - *@return la liste des urls correspondant au pattern - */ - static public List<URL> getURLs(String pattern, URL... arrayURL ) { - long t0 = System.nanoTime(); - - HashList<URL> urlList = new HashList<URL>(); - - if (arrayURL.length == 1) { - URL jarURL = arrayURL[0]; - if (isJar(jarURL.toString())) { - // jar invocation - try { - arrayURL = getClassPathURLsFromJarManifest(jarURL); - } catch (Exception e) { - log.warn(e); - arrayURL = new URL[]{jarURL}; - } - } - } - if (log.isDebugEnabled()) { - for (URL url : arrayURL) { - log.debug("found url " + url); - } - } - - for (URL urlFile : arrayURL) { - String fileName = urlFile.getFile(); - //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 (log.isDebugEnabled()) { - log.debug("jar to search " + file); - } - urlList.addAll(Resource.getURLsFromJar(file, pattern)); - - // cas ou le ichier du classLoader est un repertoire - } else if (file.exists() && file.isDirectory()) { - if (log.isDebugEnabled()) { - log.debug("file to search " + file); - } - // on traite le cas ou il peut y avoir des repertoire dans ce repertoire - urlList.addAll(Resource.getURLsFromDirectory(file, pattern)); - } - } - if (log.isInfoEnabled()) { - log.info("search URLs pattern: " + pattern + " in " + arrayURL.length+" urls in "+StringUtil.convertTime(System.nanoTime()-t0)); - } - return urlList; - } - - static public URL[] getClassPathURLsFromJarManifest(URL jarURL) throws IOException, URISyntaxException { - JarFile jar=null; - URL[] result; - try { - String jarPath = jarURL.toURI().getPath(); - File jarFile = new File(jarPath); - if (log.isInfoEnabled()) { - log.info("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(" "); - result = new URL[paths.length + 1]; - result[0] = jarURL; - File path; - for (int i = 0; i < paths.length; i++) { - String s = paths[i]; - if (s.startsWith(".") || !s.startsWith("/")) { - // relative url - path =new File(container,s); - } else { - path =new File(s); - } - if (log.isDebugEnabled()) { - log.debug(path); - } - result[i + 1] = path.toURI().toURL(); - } - jar.close(); - } finally { - if (jar!=null) { - jar.close(); - } - } - return result; - } - - static public List<URL> getURLsFromJar(File jarfile, String pattern) { - try{ - if(log.isTraceEnabled()) { - log.trace("search '" + pattern + "' in " + jarfile); - } - - ArrayList<URL> result = new ArrayList<URL>(); - InputStream in = new FileInputStream(jarfile); - 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("jarfile: " + jarfile + " 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("jarfile: " + jarfile + " 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); - } - } - - /** - * Retourner la liste des fichiers correspondant au pattern donne, aucun - * ordre ne doit sur le fichier ne doit �tre suppos�. - * - *@param repository repertoire dans lequel on recherche les fichiers - *@param pattern le nom du fichier a extraire du - * fichier du repertoire doit correspondre - * au pattern (repertoire + nom compris). - * si le pattern est null, tous les fichiers trouv� - * sont retourn�. - * - *@return la liste des urls correspondant au pattern - */ - static public List<URL> getURLsFromDirectory(File repository, String pattern) { - try{ - if(log.isTraceEnabled()) { - log.trace("search '" + pattern + "' in " + repository); - } - - HashList<URL> urlList = new HashList<URL>(); - File[] filesList = repository.listFiles(); - - if (filesList != null) { - - for (File file : filesList) { - - String name = file.getAbsolutePath(); - - if (log.isTraceEnabled()) { - log.trace("directory: " + repository + " name: " + name); - } - - // cas de recursivite : repertoire dans un repertoire - if (file.exists() && file.isDirectory()) { - urlList.addAll(Resource.getURLsFromDirectory(file, pattern)); - // si le fichier du repertoire n'est pas un repertoire on verifie s'il correspond au pattern - } else if (pattern == null || name.matches(pattern)) { - URL url = file.toURI().toURL(); - if (log.isTraceEnabled()) { - log.trace("directory: " + repository + " url: " + url); - } - urlList.add(url); - } - } - } - if(log.isTraceEnabled()) { - log.trace("found with pattern '" + pattern + "' : " + urlList); - } - return urlList; - }catch(MalformedURLException eee){ - throw new ResourceException("Le fichier n'a pu �tre converti en URL", eee); - } - } - - /** - * Verifie si le fichier est un fichier jar - * @param name nom du fichier a tester - * @return vrai si le fichier se termine par .jar faux sinon - */ - static public boolean isJar(String name){ - if (name != null && name.length() > 4) { - String ext = name.substring(name.length() - 4, name.length()); - return ".jar".equalsIgnoreCase(ext); - } - return false; - } - - /** - * Verifie si le fichier est un fichier zip - * @param name nom du fichier a tester - * @return vrai si le fichier se termine par .zip faux sinon - */ - static public boolean isZip(String name){ - if (name != null && name.length() > 4) { - String ext = name.substring(name.length() - 4, name.length()); - return ".zip".equalsIgnoreCase(ext); - } - return false; - } - - /** - * Verifie si la classe est de type primtif - * @param clazz nom de la classe a tester - * @return vrai si le classe est de type primitif faux sinon - */ - static public boolean isPrimitive(Class clazz){ - return clazz.isPrimitive() - || clazz == Boolean.class - || clazz == Byte.class - || clazz == Character.class - || clazz == Short.class - || clazz == Integer.class - || clazz == Long.class - || clazz == Float.class - || clazz == Double.class; - } - - /** - * Retourne la classe du type primitf associ� avec la classe de de l'objet - * pass� en parametre. Par exemple si la classe pass�e en param�tre est - * Integer alors le resultat sera la classe de int. - * @param clazz la classe dont on souhaite le type primitf - * @return le type primitif associ� a la classe en param�tre, ou null - * s'il n'y a pas de type primitif associ�. - */ - static public Class getPrimitiveClass(Class clazz) { - if (clazz == Boolean.class) return Boolean.TYPE; - if (clazz == Byte.class) return Byte.TYPE; - if (clazz == Character.class) return Character.TYPE; - if (clazz == Short.class) return Short.TYPE; - if (clazz == Integer.class) return Integer.TYPE; - if (clazz == Long.class) return Long.TYPE; - if (clazz == Float.class) return Float.TYPE; - if (clazz == Double.class) return Double.TYPE; - if (clazz == Void.class) return Void.TYPE; - return null; - } - -// /** -// * Test -// */ -// static public void main(String [] args) throws Exception { -// String r = args.length>0?args[0]:"/org/codelutin/lutinbuilder/LutinBuilder.class"; -// System.out.println(getURL(r)); -// } - -} // Resource Copied: lutinutil/tags/0.29.1/src/java/org/codelutin/util/Resource.java (from rev 1254, lutinutil/branches/0.29-SNAPSHOT/src/java/org/codelutin/util/Resource.java) =================================================================== --- lutinutil/tags/0.29.1/src/java/org/codelutin/util/Resource.java (rev 0) +++ lutinutil/tags/0.29.1/src/java/org/codelutin/util/Resource.java 2008-11-27 09:57:52 UTC (rev 1257) @@ -0,0 +1,528 @@ +/**##%% +* Copyright (C) 2002, 2003, 2004 Code Lutin, C�dric Pineau, Benjamin Poussin +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%%**/ + +/** +* Resource.java +* +* Created: Sun Apr 14 2002 +* +* @author POUSSIN Benjamin <bpoussin@free.fr> +* Copyright Code Lutin +* @version $Revision$ +* +* Mise a jour: $Date$ +* par : $Author$ +*/ + +package org.codelutin.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import javax.swing.ImageIcon; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + + +/** +* Cette class permet de recherche un fichier en indiquant son nom +* avec son chemin. Cette librairie ira ensuite chercher ce fichier +* sur le syst�me de fichier, et s'il n'est pas trouv� dans le +* classpath. Le fichier peut donc �tre dans un fichier .jar ou .zip. +*/ +public class Resource { // Resource + + /** to use log facility, just put in your code: log.info(\"...\"); */ + private static final Log log = LogFactory.getLog(Resource.class); + + protected Resource() { + + } + + /** + * Permet d'ajouter dans le classloader par defaut une nouvelle URL + * dans lequel il faut rechercher les fichiers. + * @param url l'url a ajouter + */ + static public void addDefaultClassLoader(URL url){ + try{ + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + + 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 default classloader", eee); + } + } + + /** + * recherche la ressource nom + * @param name nom de la ressource + * @return l'url de la ressource + * @throws ResourceNotFoundException si la resource n'a pas ete trouvee + */ + static public URL getURL(String name) { + URL url = getURLOrNull(name); + if (url != null){ + return url; + } + + throw new ResourceNotFoundException("La resource " + name + + " n'a pas �t� trouv�e"); + } + + /** + * recherche la ressource nom + * @param name le nom de la ressource + * @return l'url de la ressource ou null + */ + static public URL getURLOrNull(String name) { + // on recherche d'abord sur le filesystem + File file = new File(name); + if (file.exists()){ + try{ + return file.toURI().toURL(); + }catch(MalformedURLException eee){ + log.warn("Le fichier '" + file + "' a �t� trouv�, mais il n'a pas pu etre converti en URL"); + } + } + + //on ne l'a pas trouve on recherche dans le classpath + + // on supprime le / devant le nom de la ressource, sinon elle + // n'est pas trouve (pas de recherche dans les differents + // element du classpath. + if(name.length() > 1 && name.startsWith("/")){ + name = name.substring(1); + } + URL url = ClassLoader.getSystemClassLoader().getResource(name); + if (url != null){ + return url; + } + + ClassLoader cl = Resource.class.getClassLoader(); + url = cl.getResource(name); + return url; + } + + /** + * Retourne l'icon demande + * @param name le nom de l'icone + * @return Retourne l'icon demande ou null s'il n'est pas trouv� + */ + static public ImageIcon getIcon(String name){ + try{ + return new ImageIcon(getURL(name)); + }catch(Exception eee){ + log.warn("Can't find icon: " + name, eee); + return null; + } + } + + + /** + * Recherche et retourne l'objet properties de configuration � utiliser. + * Les diff�rents fichiers trouv� sont chain�. L'ordre de recherche est + * le classpath, le fichier dans /etc et enfin le chemin sur le filesystem + * @param filename le nom du fichier � rechercher + * @return l'objet Properties de configuration + * @throws java.io.IOException si pb + */ + static public Properties getConfigProperties(String filename) throws IOException { + Properties result; + result = getConfigProperties(filename, null); + return result; + } + + static public Properties getConfigProperties(String filename, Properties parent) throws IOException { + Properties result; + if (parent != null) { + result = new Properties(parent); + } else { + result = new Properties(); + } + + URL inClasspath = ClassLoader.getSystemClassLoader().getResource(filename); + if (inClasspath == null) { + inClasspath = Resource.class.getResource(filename); + } + if (inClasspath == null) { + inClasspath = getURLOrNull(filename); + } + if(inClasspath != null){ + log.info("Chargement du fichier de config: " + inClasspath); + result.load(inClasspath.openStream()); + result = new Properties(result); + } + + File etcConfig = new File("/etc/" + filename); + if(etcConfig.exists()){ + log.info("Chargement du fichier de config: " + etcConfig); + result.load(etcConfig.toURI().toURL().openStream()); + result = new Properties(result); + } + + File config = new File(filename); + if(config.exists()){ + log.info("Chargement du fichier de config: " + config); + result.load(config.toURI().toURL().openStream()); + result = new Properties(result); + } + + return result; + } + + /** + * Retourner la liste des fichiers du classLoader. Ces fichiers doivent + * correspondent au pattern donne + * + * @param pattern le nom du fichier a extraire du + * fichier compress� ou durepertoire doit correspondre + * au pattern (repertoire + nom compris). + * @return la liste des urls correspondant au pattern + */ + static public List<URL> getURLs(String pattern) { + return getURLs(pattern, (URLClassLoader) null); + } + + static private 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; + } + + /** + * Retourner la liste des fichiers du classLoader. Ces fichiers doivent + * correspondent au pattern donne + * + *@param classLoader le classLoader + *@param pattern le nom du fichier a extraire du + * fichier compress� ou durepertoire doit correspondre + * au pattern (repertoire + nom compris). + *@return la liste des urls correspondant au pattern + */ + static public List<URL> getURLs(String pattern, URLClassLoader classLoader) { + if(classLoader == null){ + classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader(); + } + URL[] arrayURL = getURLs(classLoader); + return getURLs(pattern,arrayURL); + } + + /** + * Retourner la liste des fichiers du classLoader. Ces fichiers doivent + * correspondent au pattern donne + * + *@param arrayURL les urls ou chercher + *@param pattern le nom du fichier a extraire du + * fichier compress� ou durepertoire doit correspondre + * au pattern (repertoire + nom compris). + *@return la liste des urls correspondant au pattern + */ + static public List<URL> getURLs(String pattern, URL... arrayURL ) { + long t0 = System.nanoTime(); + + HashList<URL> urlList = new HashList<URL>(); + + if (arrayURL.length == 1) { + URL jarURL = arrayURL[0]; + if (isJar(jarURL.toString())) { + // jar invocation + try { + arrayURL = getClassPathURLsFromJarManifest(jarURL); + } catch (Exception e) { + log.warn(e); + arrayURL = new URL[]{jarURL}; + } + } + } + if (log.isDebugEnabled()) { + for (URL url : arrayURL) { + log.debug("found url " + url); + } + } + + for (URL urlFile : arrayURL) { + String fileName = urlFile.getFile(); + //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 (log.isDebugEnabled()) { + log.debug("jar to search " + file); + } + urlList.addAll(Resource.getURLsFromJar(file, pattern)); + + // cas ou le ichier du classLoader est un repertoire + } else if (file.exists() && file.isDirectory()) { + if (log.isDebugEnabled()) { + log.debug("file to search " + file); + } + // on traite le cas ou il peut y avoir des repertoire dans ce repertoire + urlList.addAll(Resource.getURLsFromDirectory(file, pattern)); + } + } + if (log.isInfoEnabled()) { + log.info("search URLs pattern: " + pattern + " in " + arrayURL.length+" urls in "+StringUtil.convertTime(System.nanoTime()-t0)); + } + return urlList; + } + + static public URL[] getClassPathURLsFromJarManifest(URL jarURL) throws IOException, URISyntaxException { + JarFile jar=null; + URL[] result; + try { + String jarPath = jarURL.toURI().getPath(); + File jarFile = new File(jarPath); + if (log.isInfoEnabled()) { + log.info("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(" "); + result = new URL[paths.length + 1]; + result[0] = jarURL; + File path; + for (int i = 0; i < paths.length; i++) { + String s = paths[i]; + // test de l'existence d'un protocole dans le path (genre file:...) + if (s.indexOf(':') != -1) { + result[i + 1] = new URL(s); + continue; + } + + if (s.startsWith(".") || !s.startsWith("/")) { + // relative url + path =new File(container,s); + } else { + path =new File(s); + } + if (log.isDebugEnabled()) { + log.debug(path); + } + result[i + 1] = path.toURI().toURL(); + } + jar.close(); + } finally { + if (jar!=null) { + jar.close(); + } + } + return result; + } + + static public List<URL> getURLsFromJar(File jarfile, String pattern) { + try{ + if(log.isTraceEnabled()) { + log.trace("search '" + pattern + "' in " + jarfile); + } + + ArrayList<URL> result = new ArrayList<URL>(); + InputStream in = new FileInputStream(jarfile); + 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("jarfile: " + jarfile + " 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("jarfile: " + jarfile + " 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); + } + } + + /** + * Retourner la liste des fichiers correspondant au pattern donne, aucun + * ordre ne doit sur le fichier ne doit �tre suppos�. + * + *@param repository repertoire dans lequel on recherche les fichiers + *@param pattern le nom du fichier a extraire du + * fichier du repertoire doit correspondre + * au pattern (repertoire + nom compris). + * si le pattern est null, tous les fichiers trouv� + * sont retourn�. + * + *@return la liste des urls correspondant au pattern + */ + static public List<URL> getURLsFromDirectory(File repository, String pattern) { + try{ + if(log.isTraceEnabled()) { + log.trace("search '" + pattern + "' in " + repository); + } + + HashList<URL> urlList = new HashList<URL>(); + File[] filesList = repository.listFiles(); + + if (filesList != null) { + + for (File file : filesList) { + + String name = file.getAbsolutePath(); + + if (log.isTraceEnabled()) { + log.trace("directory: " + repository + " name: " + name); + } + + // cas de recursivite : repertoire dans un repertoire + if (file.exists() && file.isDirectory()) { + urlList.addAll(Resource.getURLsFromDirectory(file, pattern)); + // si le fichier du repertoire n'est pas un repertoire on verifie s'il correspond au pattern + } else if (pattern == null || name.matches(pattern)) { + URL url = file.toURI().toURL(); + if (log.isTraceEnabled()) { + log.trace("directory: " + repository + " url: " + url); + } + urlList.add(url); + } + } + } + if(log.isTraceEnabled()) { + log.trace("found with pattern '" + pattern + "' : " + urlList); + } + return urlList; + }catch(MalformedURLException eee){ + throw new ResourceException("Le fichier n'a pu �tre converti en URL", eee); + } + } + + /** + * Verifie si le fichier est un fichier jar + * @param name nom du fichier a tester + * @return vrai si le fichier se termine par .jar faux sinon + */ + static public boolean isJar(String name){ + if (name != null && name.length() > 4) { + String ext = name.substring(name.length() - 4, name.length()); + return ".jar".equalsIgnoreCase(ext); + } + return false; + } + + /** + * Verifie si le fichier est un fichier zip + * @param name nom du fichier a tester + * @return vrai si le fichier se termine par .zip faux sinon + */ + static public boolean isZip(String name){ + if (name != null && name.length() > 4) { + String ext = name.substring(name.length() - 4, name.length()); + return ".zip".equalsIgnoreCase(ext); + } + return false; + } + + /** + * Verifie si la classe est de type primtif + * @param clazz nom de la classe a tester + * @return vrai si le classe est de type primitif faux sinon + */ + static public boolean isPrimitive(Class clazz){ + return clazz.isPrimitive() + || clazz == Boolean.class + || clazz == Byte.class + || clazz == Character.class + || clazz == Short.class + || clazz == Integer.class + || clazz == Long.class + || clazz == Float.class + || clazz == Double.class; + } + + /** + * Retourne la classe du type primitf associ� avec la classe de de l'objet + * pass� en parametre. Par exemple si la classe pass�e en param�tre est + * Integer alors le resultat sera la classe de int. + * @param clazz la classe dont on souhaite le type primitf + * @return le type primitif associ� a la classe en param�tre, ou null + * s'il n'y a pas de type primitif associ�. + */ + static public Class getPrimitiveClass(Class clazz) { + if (clazz == Boolean.class) return Boolean.TYPE; + if (clazz == Byte.class) return Byte.TYPE; + if (clazz == Character.class) return Character.TYPE; + if (clazz == Short.class) return Short.TYPE; + if (clazz == Integer.class) return Integer.TYPE; + if (clazz == Long.class) return Long.TYPE; + if (clazz == Float.class) return Float.TYPE; + if (clazz == Double.class) return Double.TYPE; + if (clazz == Void.class) return Void.TYPE; + return null; + } + +// /** +// * Test +// */ +// static public void main(String [] args) throws Exception { +// String r = args.length>0?args[0]:"/org/codelutin/lutinbuilder/LutinBuilder.class"; +// System.out.println(getURL(r)); +// } + +} // Resource
participants (1)
-
schorlet@users.labs.libre-entreprise.org