1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
67 public class ClasspathResourcesUtil {
68 private static final Logger log = LoggerFactory.getLogger(ClasspathResourcesUtil.class);
69
70
71
72
73 public static interface Filter {
74 public boolean accept(String name);
75 }
76
77
78
79
80 public static class PatternFilter implements Filter {
81 private final Pattern pattern;
82
83 public PatternFilter(String pattern) {
84 this.pattern = Pattern.compile(pattern);
85 }
86
87 @Override
88 public boolean accept(String name) {
89 return pattern.matcher(name).matches();
90 }
91 }
92
93 private static boolean isCache() {
94 final String devMode = SystemProperty.getProperty("magnolia.develop");
95 return !"true".equalsIgnoreCase(devMode);
96 }
97
98
99
100
101
102
103 public static String[] findResources(String regex) {
104 return findResources(new PatternFilter(regex));
105 }
106
107
108
109
110
111
112 public static String[] findResources(Filter filter) {
113 final Set<String> resources = new HashSet<String>();
114 final ClassLoader cl = getCurrentClassLoader();
115
116
117
118 if (cl instanceof URLClassLoader) {
119 final URLClassLoader urlClassLoader = (URLClassLoader) cl;
120 final URL[] urls = urlClassLoader.getURLs();
121 if (log.isDebugEnabled()) {
122 log.debug("Loading resources from: " + ArrayUtils.toString(urls));
123 }
124 if (urls.length == 1 && urls[0].getPath().endsWith("WEB-INF/classes/")) {
125
126 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.");
127 } else {
128 collectFromURLs(resources, urls, filter);
129 return resources.toArray(new String[resources.size()]);
130 }
131 }
132
133 try {
134
135
136
137
138
139 String classpath = BeanUtils.getProperty(cl, "classPath");
140
141 if (StringUtils.isNotEmpty(classpath)) {
142 collectFromClasspathString(resources, classpath, filter);
143 return resources.toArray(new String[resources.size()]);
144 }
145 } catch (Throwable e) {
146
147 }
148
149
150
151 collectFromFileSystem(filter, resources);
152 return resources.toArray(new String[resources.size()]);
153 }
154
155 protected static void collectFromURLs(Collection<String> resources, URL[] urls, Filter filter) {
156
157 for (int j = 0; j < urls.length; j++) {
158 final File tofile = sanitizeToFile(urls[j]);
159 if (tofile != null) {
160 collectFiles(resources, tofile, filter);
161 }
162 }
163 }
164
165 protected static void collectFromClasspathString(Collection<String> resources, String classpath, Filter filter) {
166 String[] paths = classpath.split(File.pathSeparator);
167 for (int j = 0; j < paths.length; j++) {
168 final File tofile = new File(paths[j]);
169
170 if (tofile.exists()) {
171 collectFiles(resources, tofile, filter);
172 }
173 }
174 }
175
176 protected static void collectFromFileSystem(Filter filter, Collection<String> resources) {
177
178 String rootDire = Components.getComponent(MagnoliaInitPaths.class).getRootPath();
179 String libString = new File(new File(rootDire), "WEB-INF/lib").getAbsolutePath();
180
181 File dir = new File(libString);
182 if (dir.exists()) {
183 File[] files = dir.listFiles(new FilenameFilter() {
184 @Override
185 public boolean accept(File file, String name) {
186 return name.endsWith(".jar");
187 }
188 });
189
190 for (int i = 0; i < files.length; i++) {
191 collectFiles(resources, files[i], filter);
192 }
193 }
194
195
196 String classString = new File(new File(rootDire), "WEB-INF/classes").getAbsolutePath();
197 File classFileDir = new File(classString);
198
199 if (classFileDir.exists()) {
200 collectFiles(resources, classFileDir, filter);
201 }
202 }
203
204 public static File sanitizeToFile(URL url) {
205 if (!"file".equals(url.getProtocol()) && !StringUtils.startsWith(url.toString(), "jar:file:")) {
206 log.warn("Cannot load resources '{}' from '{}' protocol. Only 'file' and 'jar-file' protocols are supported.", url, url.getProtocol());
207 return null;
208 }
209 try {
210 String fileUrl = url.getFile();
211
212 fileUrl = URLDecoder.decode(fileUrl, "UTF-8");
213
214
215 fileUrl = StringUtils.removeStart(fileUrl, "file:");
216 fileUrl = StringUtils.removeEnd(fileUrl, "!/");
217 return new File(fileUrl);
218 } catch (UnsupportedEncodingException e) {
219 throw new RuntimeException(e);
220 }
221 }
222
223
224
225
226
227
228
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
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 } catch (IOException e) {
261 log.error("IOException opening file {}, skipping", jarOrDir.getAbsolutePath());
262 return;
263 }
264 for (Enumeration<JarEntry> em = jar.entries(); em.hasMoreElements();) {
265 JarEntry entry = em.nextElement();
266 if (!entry.isDirectory()) {
267 if (filter.accept("/" + entry.getName())) {
268 resources.add("/" + entry.getName());
269 }
270 }
271 }
272 try {
273 jar.close();
274 } catch (IOException e) {
275 log.error("Failed to close jar file : " + e.getMessage());
276 log.debug("Failed to close jar file", e);
277 }
278 }
279 else {
280 log.debug("Unknown (not jar) file in classpath: {}, skipping.", jarOrDir.getName());
281 }
282
283 }
284
285 public static InputStream getStream(String name) throws IOException {
286 return getStream(name, isCache());
287 }
288
289
290
291
292
293
294 public static InputStream getStream(String name, boolean cache) throws IOException {
295 if (cache) {
296 return getCurrentClassLoader().getResourceAsStream(StringUtils.removeStart(name, "/"));
297 }
298
299
300 URL url = getResource(name);
301 if (url != null) {
302 return url.openStream();
303 }
304
305 log.debug("Can't find {}", name);
306 return null;
307 }
308
309
310
311
312
313
314
315
316
317
318 public static ClassLoader getCurrentClassLoader() {
319 return Thread.currentThread().getContextClassLoader();
320 }
321
322
323
324
325
326
327
328 public static URL getResource(String name) {
329 return getCurrentClassLoader().getResource(StringUtils.removeStart(name, "/"));
330 }
331
332 }