View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.util;
35  
36  import info.magnolia.cms.core.SystemProperty;
37  import info.magnolia.init.MagnoliaInitPaths;
38  import info.magnolia.objectfactory.Components;
39  
40  import java.io.File;
41  import java.io.FilenameFilter;
42  import java.io.IOException;
43  import java.io.InputStream;
44  import java.io.UnsupportedEncodingException;
45  import java.net.URL;
46  import java.net.URLClassLoader;
47  import java.net.URLDecoder;
48  import java.util.Collection;
49  import java.util.Enumeration;
50  import java.util.HashSet;
51  import java.util.Set;
52  import java.util.jar.JarEntry;
53  import java.util.jar.JarFile;
54  import java.util.regex.Pattern;
55  
56  import org.apache.commons.beanutils.BeanUtils;
57  import org.apache.commons.io.FileUtils;
58  import org.apache.commons.io.filefilter.TrueFileFilter;
59  import org.apache.commons.lang.ArrayUtils;
60  import org.apache.commons.lang.StringUtils;
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  
65  /**
66   * Util to find resources in the classpath (WEB-INF/lib and WEB-INF/classes).
67   * @author Philipp Bracher
68   * @author Fabrizio Giustina
69   * @version $Revision$ ($Author$)
70   */
71  public class ClasspathResourcesUtil {
72      private static final Logger log = LoggerFactory.getLogger(ClasspathResourcesUtil.class);
73  
74      /**
75       * Filter for filtering the resources.
76       * @author Philipp Bracher
77       * @version $Revision$ ($Author$)
78       */
79      public static interface Filter {
80          public boolean accept(String name);
81      }
82  
83      /**
84       * A filter using a regex pattern.
85       */
86      public static class PatternFilter implements Filter{
87          private final Pattern pattern;
88  
89          public PatternFilter(String pattern) {
90              this.pattern = Pattern.compile(pattern);
91          }
92  
93          @Override
94          public boolean accept(String name) {
95              return pattern.matcher(name).matches();
96          }
97      }
98  
99      private static boolean isCache() {
100         final String devMode = SystemProperty.getProperty("magnolia.develop");
101         return !"true".equalsIgnoreCase(devMode);
102     }
103 
104     /**
105      * Return a collection containing the resource names which match the regular expression.
106      * @return string array of found resources TODO : (lazy) cache ?
107      */
108     public static String[] findResources(String regex) {
109         return findResources(new PatternFilter(regex));
110     }
111 
112     /**
113      * Return a collection containing the resource names which passed the filter.
114      * @param filter
115      * @return string array of found resources TODO : (lazy) cache ?
116      */
117     public static String[] findResources(Filter filter) {
118         final Set<String> resources = new HashSet<String>();
119         final ClassLoader cl = getCurrentClassLoader();
120 
121         // if the classloader is an URLClassloader we have a better method for discovering resources
122         // whis will also fetch files from jars outside WEB-INF/lib, useful during development
123         if (cl instanceof URLClassLoader) {
124             final URLClassLoader urlClassLoader = (URLClassLoader) cl;
125             final URL[] urls = urlClassLoader.getURLs();
126             if(log.isDebugEnabled()){
127                 log.debug("Loading resources from: " + ArrayUtils.toString(urls));
128             }
129             if (urls.length == 1 && urls[0].getPath().endsWith("WEB-INF/classes/")) {
130                 // working around MAGNOLIA-2577
131                 log.warn("Looks like we're in a JBoss 5 expanded war directory, will attempt to load resources from the file system instead; see MAGNOLIA-2577.");
132             } else {
133                 collectFromURLs(resources, urls, filter);
134                 return resources.toArray(new String[resources.size()]);
135             }
136         }
137 
138         try {
139             // be friendly to WAS developers too...
140             // in development mode under RAD 7.5 here we have an instance of com.ibm.ws.classloader.WsClassLoader
141             // and jars are NOT deployed to WEB-INF/lib by default, so they can't be found without this explicit check
142             //
143             // but since we don't want to depend on WAS stuff we just check if the cl exposes a "classPath" property
144             String classpath = BeanUtils.getProperty(cl, "classPath");
145 
146             if (StringUtils.isNotEmpty(classpath)) {
147                 collectFromClasspathString(resources, classpath, filter);
148                 return resources.toArray(new String[resources.size()]);
149             }
150         }
151         catch (Throwable e) {
152             // no, it's not a classloader we can handle in a special way
153         }
154 
155         // no way, we have to assume a standard war structure and look in the WEB-INF/lib and WEB-INF/classes dirs
156         // read the jars in the lib dir
157         collectFromFileSystem(filter, resources);
158         return resources.toArray(new String[resources.size()]);
159     }
160 
161     protected static void collectFromURLs(Collection<String> resources, URL[] urls, Filter filter) {
162         // tomcat classloader is org.apache.catalina.loader.WebappClassLoader
163         for (int j = 0; j < urls.length; j++) {
164             final File tofile = sanitizeToFile(urls[j]);
165             collectFiles(resources, tofile, filter);
166         }
167     }
168 
169     protected static void collectFromClasspathString(Collection<String> resources, String classpath, Filter filter) {
170         String[] paths = classpath.split(File.pathSeparator);
171         for (int j = 0; j < paths.length; j++) {
172             final File tofile = new File(paths[j]);
173             // there can be several missing (optional?) paths here...
174             if (tofile.exists()) {
175                 collectFiles(resources, tofile, filter);
176             }
177         }
178     }
179 
180     protected static void collectFromFileSystem(Filter filter, Collection<String> resources) {
181         //We need to have access to the MAGNOLIA_APP_ROOTDIR. Unfortunately, this is not yet initialised.
182         String rootDire = Components.getComponent(MagnoliaInitPaths.class).getRootPath();
183         String libString = new File(new File(rootDire), "WEB-INF/lib").getAbsolutePath();
184 
185         File dir = new File(libString); //$NON-NLS-1$
186         if (dir.exists()) {
187             File[] files = dir.listFiles(new FilenameFilter() {
188                 @Override
189                 public boolean accept(File file, String name) {
190                     return name.endsWith(".jar");
191                 }
192             });
193 
194             for (int i = 0; i < files.length; i++) {
195                 collectFiles(resources, files[i], filter);
196             }
197         }
198 
199         // read files in WEB-INF/classes
200         String classString = new File(new File(rootDire), "WEB-INF/classes").getAbsolutePath();
201         File classFileDir = new File(classString);
202 
203         if (classFileDir.exists()) {
204             collectFiles(resources, classFileDir, filter);
205         }
206     }
207 
208     protected static File sanitizeToFile(URL url) {
209         try {
210             String fileUrl = url.getFile();
211             // needed because somehow the URLClassLoader has encoded URLs, and getFile does not decode them.
212             fileUrl = URLDecoder.decode(fileUrl, "UTF-8");
213             // needed for Resin - for some reason, its URLs are formed as jar:file:/absolutepath/foo/bar.jar instead of
214             // using the :///abs.. notation
215             fileUrl = StringUtils.removeStart(fileUrl, "file:");
216             fileUrl = StringUtils.removeEnd(fileUrl, "!/");
217             return new File(fileUrl);
218         }
219         catch (UnsupportedEncodingException e) {
220             throw new RuntimeException(e);
221         }
222     }
223 
224     /**
225      * Load resources from jars or directories.
226      * @param resources found resources will be added to this collection
227      * @param jarOrDir a File, can be a jar or a directory
228      * @param filter used to filter resources
229      */
230     private static void collectFiles(Collection<String> resources, File jarOrDir, Filter filter) {
231 
232         if (!jarOrDir.exists()) {
233             log.warn("missing file: {}", jarOrDir.getAbsolutePath());
234             return;
235         }
236 
237         if (jarOrDir.isDirectory()) {
238             log.debug("looking in dir {}", jarOrDir.getAbsolutePath());
239 
240             Collection<File> files = FileUtils.listFiles(jarOrDir, TrueFileFilter.TRUE, TrueFileFilter.TRUE);
241             for (File file : files) {
242                 String name = StringUtils.substringAfter(file.getPath(), jarOrDir.getPath());
243 
244                 // please, be kind to Windows!!!
245                 name = StringUtils.replace(name, "\\", "/");
246                 if (!name.startsWith("/")) {
247                     name = "/" + name;
248                 }
249 
250                 if (filter.accept(name)) {
251                     resources.add(name);
252                 }
253             }
254         }
255         else if (jarOrDir.getName().endsWith(".jar")) {
256             log.debug("looking in jar {}", jarOrDir.getAbsolutePath());
257             JarFile jar;
258             try {
259                 jar = new JarFile(jarOrDir);
260             }
261             catch (IOException e) {
262                 log.error("IOException opening file {}, skipping", jarOrDir.getAbsolutePath());
263                 return;
264             }
265             for (Enumeration<JarEntry> em = jar.entries(); em.hasMoreElements();) {
266                 JarEntry entry = em.nextElement();
267                 if (!entry.isDirectory()) {
268                     if (filter.accept("/" + entry.getName())) {
269                         resources.add("/" + entry.getName());
270                     }
271                 }
272             }
273             try {
274                 jar.close();
275             }
276             catch (IOException e) {
277                 log.error("Failed to close jar file : " + e.getMessage());
278                 log.debug("Failed to close jar file", e);
279             }
280         }
281         else {
282             log.debug("Unknown (not jar) file in classpath: {}, skipping.", jarOrDir.getName());
283         }
284 
285     }
286 
287     public static InputStream getStream(String name) throws IOException {
288         return getStream(name, isCache());
289     }
290 
291     /**
292      * Checks last modified and returns the new content if changed and the cache flag is not set to true.
293      * @param name
294      * @return the input stream
295      * @throws IOException
296      */
297     public static InputStream getStream(String name, boolean cache) throws IOException {
298         if (cache) {
299             return getCurrentClassLoader().getResourceAsStream(StringUtils.removeStart(name, "/"));
300         }
301 
302         // TODO use the last modified attribute
303         URL url = getResource(name);
304         if (url != null) {
305             return url.openStream();
306         }
307 
308         log.debug("Can't find {}", name);
309         return null;
310     }
311 
312     /**
313      * Get the class loader of the current thread.
314      * @return current classloader
315      */
316     private static ClassLoader getCurrentClassLoader() {
317         return Thread.currentThread().getContextClassLoader();
318     }
319 
320     /**
321      * Get the resource using the current class loader. The leading / is removed as the call to class.getResource()
322      * would do.
323      * @param name
324      * @return the resource
325      */
326     public static URL getResource(String name) {
327         return getCurrentClassLoader().getResource(StringUtils.removeStart(name, "/"));
328     }
329 
330 }