1 package org.jadira.scanner.classpath.projector;
2
3 import java.io.File;
4 import java.io.FileFilter;
5 import java.io.FilenameFilter;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Enumeration;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.jar.JarEntry;
14 import java.util.jar.JarFile;
15
16 import org.jadira.scanner.core.api.Projector;
17 import org.jadira.scanner.core.exception.ClasspathAccessException;
18
19 import de.schlichtherle.io.archive.zip.ZipEntry;
20 import de.schlichtherle.util.zip.ZipFile;
21
22 public class ClasspathProjector implements Projector<File> {
23
24 public static final ClasspathProjector SINGLETON = new ClasspathProjector();
25
26 private static final Map<File, List<File>> PROJECTION_CACHE = new HashMap<File, List<File>>();
27
28 public ClasspathProjector() {
29
30 }
31
32 @Override
33 public List<File> project(File segment) {
34
35 List<File> files = PROJECTION_CACHE.get(segment);
36 if (files != null) {
37 return files;
38 }
39
40 files = new ArrayList<File>();
41 boolean isArchive = false;
42
43 if (segment instanceof de.schlichtherle.io.File) {
44 if (((de.schlichtherle.io.File)segment).isArchive()) {
45 isArchive = true;
46 }
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 if (!isArchive) {
99
100 File[] dirs = segment.listFiles(new FileFilter() {
101
102 @Override
103 public boolean accept(File file) {
104 return file.isDirectory();
105 }
106 });
107
108 for (File dir : dirs) {
109 files.addAll(project(dir));
110 }
111
112 File[] classes = segment.listFiles(new FileFilter() {
113
114 @Override
115 public boolean accept(File file) {
116 return !file.isDirectory() && file.getName().endsWith(".class");
117 }
118 });
119 files.addAll(Arrays.asList(classes));
120
121 } else if (segment.getPath().endsWith("jar")) {
122
123 JarFile jarFile = null;
124 try {
125 jarFile = new JarFile(segment.getPath());
126 Enumeration<JarEntry> entries = jarFile.entries();
127 while (entries.hasMoreElements()) {
128 JarEntry next = entries.nextElement();
129 if (next.getName().endsWith(".class")) {
130 files.add(new de.schlichtherle.io.File(segment.getPath() + System.getProperty("file.separator") + next.getName()));
131 }
132 }
133 } catch (IOException e) {
134 throw new ClasspathAccessException("Could not open JarFile: " + e.getMessage(), e);
135 } finally {
136 if (jarFile != null) {
137 try {
138 jarFile.close();
139 } catch (IOException e) {
140
141 }
142 }
143 }
144 } else if (segment.getPath().endsWith("zip")) {
145 ZipFile zipFile = null;
146 try {
147 zipFile = new ZipFile(segment.getPath());
148 @SuppressWarnings("unchecked")
149 Enumeration<ZipEntry> entries = zipFile.entries();
150 while (entries.hasMoreElements()) {
151 ZipEntry next = entries.nextElement();
152 if (next.getName().endsWith(".class")) {
153 files.add(new de.schlichtherle.io.File(segment.getPath() + System.getProperty("file.separator") + next.getName()));
154 }
155 }
156 } catch (IOException e) {
157 throw new ClasspathAccessException("Could not open ZipFile: " + e.getMessage(), e);
158 } finally {
159 if (zipFile != null) {
160 try {
161 zipFile.close();
162 } catch (IOException e) {
163
164 }
165 }
166 }
167 } else {
168
169 File[] classes = ((de.schlichtherle.io.File)segment).listFiles(new FilenameFilter() {
170
171 @Override
172 public boolean accept(File dir, String name) {
173 return name.endsWith(".class");
174 }
175 });
176 files.addAll(Arrays.asList(classes));
177 }
178 PROJECTION_CACHE.put(segment, files);
179 return files;
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 }