1 13 package info.magnolia.cms.util; 14 15 import info.magnolia.cms.core.Path; 16 import info.magnolia.cms.core.SystemProperty; 17 18 import java.io.File ; 19 import java.io.FilenameFilter ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.util.jar.JarEntry ; 29 import java.util.jar.JarFile ; 30 31 import org.apache.commons.io.FileUtils; 32 import org.apache.commons.io.filefilter.TrueFileFilter; 33 import org.apache.commons.lang.StringUtils; 34 import org.slf4j.Logger; 35 import org.slf4j.LoggerFactory; 36 37 38 43 public class ClasspathResourcesUtil { 44 45 48 private static Logger log = LoggerFactory.getLogger(ClasspathResourcesUtil.class); 49 50 55 public static abstract class Filter { 56 public abstract boolean accept(String name); 57 } 58 59 private static boolean isCache() { 60 final String devMode = SystemProperty.getProperty("magnolia.develop"); 61 return !"true".equalsIgnoreCase(devMode); 62 } 63 64 69 public static String [] findResources(Filter filter) { 70 71 Collection resources = new ArrayList (); 72 73 ClassLoader cl = getCurrentClassLoader(); 74 75 if (cl instanceof URLClassLoader ) { 78 URL [] urls = ((URLClassLoader ) cl).getURLs(); 80 for (int j = 0; j < urls.length; j++) { 81 final File tofile = sanitizeToFile(urls[j]); 82 collectFiles(resources, tofile, filter); 83 } 84 } else { 85 87 File dir = new File (Path.getAbsoluteFileSystemPath("WEB-INF/lib")); if (dir.exists()) { 90 File [] files = dir.listFiles(new FilenameFilter () { 91 92 public boolean accept(File file, String name) { 93 return name.endsWith(".jar"); 94 } 95 }); 96 97 for (int i = 0; i < files.length; i++) { 98 collectFiles(resources, files[i], filter); 99 } 100 } 101 102 File classFileDir = new File (Path.getAbsoluteFileSystemPath("WEB-INF/classes")); 104 if (classFileDir.exists()) { 105 collectFiles(resources, classFileDir, filter); 106 } 107 } 108 109 return (String []) resources.toArray(new String [resources.size()]); 110 } 111 112 protected static File sanitizeToFile(URL url) { 113 String fileUrl = url.getFile(); 114 115 fileUrl = StringUtils.removeStart(fileUrl, "file:"); 117 fileUrl = StringUtils.removeEnd(fileUrl, "!/"); 118 119 return new File (fileUrl); 120 } 121 122 128 private static void collectFiles(Collection resources, File jarOrDir, Filter filter) { 129 130 if (!jarOrDir.exists()) { 131 log.warn("missing file: {}", jarOrDir.getAbsolutePath()); 132 return; 133 } 134 135 if (jarOrDir.isDirectory()) { 136 if (log.isDebugEnabled()) { 137 log.debug("looking in dir {}", jarOrDir.getAbsolutePath()); 138 } 139 140 Collection files = FileUtils.listFiles(jarOrDir, new TrueFileFilter() { 141 }, new TrueFileFilter() { 142 }); 143 for (Iterator iter = files.iterator(); iter.hasNext();) { 144 File file = (File ) iter.next(); 145 String name = StringUtils.substringAfter(file.getPath(), jarOrDir.getPath()); 146 147 name = StringUtils.replace(name, "\\", "/"); 149 if (!name.startsWith("/")) { 150 name = "/" + name; 151 } 152 153 if (filter.accept(name)) { 154 resources.add(name); 155 } 156 } 157 } 158 else if (jarOrDir.getName().endsWith(".jar")) { 159 if (log.isDebugEnabled()) { 160 log.debug("looking in jar {}", jarOrDir.getAbsolutePath()); 161 } 162 JarFile jar; 163 try { 164 jar = new JarFile (jarOrDir); 165 } 166 catch (IOException e) { 167 log.error("IOException opening file {}, skipping", jarOrDir.getAbsolutePath()); 168 return; 169 } 170 for (Enumeration em = jar.entries(); em.hasMoreElements();) { 171 JarEntry entry = (JarEntry ) em.nextElement(); 172 if (!entry.isDirectory()) { 173 if (filter.accept("/" + entry.getName())) { 174 resources.add("/" + entry.getName()); 175 } 176 } 177 } 178 try { 179 jar.close(); 180 } catch (IOException e) { 181 log.error("Failed to close jar file : "+e.getMessage()); 182 log.debug("Failed to close jar file",e); 183 } 184 } 185 else { 186 if (log.isDebugEnabled()) { 187 log.debug("Unknown (not jar) file in classpath: {}, skipping.", jarOrDir.getName()); 188 } 189 } 190 191 } 192 193 public static InputStream getStream(String name) throws IOException { 194 return getStream(name, isCache()); 195 } 196 197 203 public static InputStream getStream(String name, boolean cache) throws IOException { 204 if (cache) { 205 return getCurrentClassLoader().getResourceAsStream(StringUtils.removeStart(name, "/")); 206 } 207 208 URL url = getResource(name); 210 if (url != null) { 211 return url.openStream(); 212 } 213 214 if (log.isDebugEnabled()) { 215 log.debug("Can't find {}", name); 216 } 217 return null; 218 } 219 220 224 private static ClassLoader getCurrentClassLoader() { 225 return Thread.currentThread().getContextClassLoader(); 226 } 227 228 234 public static URL getResource(String name) { 235 return getCurrentClassLoader().getResource(StringUtils.removeStart(name, "/")); 236 } 237 238 } 239 | Popular Tags |