1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jadira.scanner.file.locator;
17
18 import java.io.File;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Set;
24
25 import javax.servlet.ServletContext;
26
27 import org.jadira.scanner.core.api.Locator;
28 import org.jadira.scanner.core.exception.ClasspathAccessException;
29
30
31
32
33 public class WebappClasspathUrlLocator implements Locator<URL> {
34
35 private final ServletContext servletContext;
36
37 public WebappClasspathUrlLocator(ServletContext servletContext) {
38 this.servletContext = servletContext;
39 }
40
41 @Override
42 public List<URL> locate() {
43
44 List<URL> list = new ArrayList<URL>();
45 @SuppressWarnings("unchecked") Set<String> libJars = servletContext.getResourcePaths("/WEB-INF/lib");
46 for (String jar : libJars) {
47 try {
48 list.add(servletContext.getResource(jar));
49 } catch (MalformedURLException e) {
50 throw new ClasspathAccessException(e);
51 }
52 }
53 list.add(findWebInfClassesPath(servletContext));
54 return list;
55 }
56
57
58
59
60
61
62
63
64 private static URL findWebInfClassesPath(ServletContext servletContext) {
65 String path = servletContext.getRealPath("/WEB-INF/classes");
66 if (path == null) {
67 return null;
68 }
69 File fp = new File(path);
70 if (!fp.exists()) {
71 return null;
72 }
73 try {
74 return fp.toURI().toURL();
75 } catch (MalformedURLException e) {
76 throw new ClasspathAccessException(e);
77 }
78 }
79 }