1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
package net.sf.tlc.core.impl; |
7 |
|
|
8 |
|
import java.io.File; |
9 |
|
import java.io.FileInputStream; |
10 |
|
import java.io.IOException; |
11 |
|
import java.util.ArrayList; |
12 |
|
import java.util.Arrays; |
13 |
|
import java.util.logging.Level; |
14 |
|
import java.util.logging.Logger; |
15 |
|
|
16 |
|
import net.sf.tlc.core.PropertyManager; |
17 |
|
import net.sf.tlc.core.TargetFinder; |
18 |
|
import net.sf.tlc.model.Target; |
19 |
|
import net.sf.tlc.util.ClassPathUtils; |
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
10 |
public class DefaultFinder implements TargetFinder { |
27 |
|
|
28 |
30 |
private static final Logger logger = Logger.getLogger(DefaultFinder.class.getName()); |
29 |
|
|
30 |
|
private static final String DEFAULT_TARGET_DIR = "targets"; |
31 |
|
|
32 |
|
private static final String TLC_TARGET_DIR_KEY = "tlc.target.dir"; |
33 |
|
|
34 |
|
private final File targetPath; |
35 |
|
|
36 |
|
private final ClassLoader cl; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
20 |
public DefaultFinder(final ClassLoader cl, class="keyword">final PropertyManager pm) { |
45 |
20 |
this.cl = cl; |
46 |
20 |
this.targetPath = new File(pm.getProperty(TLC_TARGET_DIR_KEY, DEFAULT_TARGET_DIR)); |
47 |
20 |
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
public final Target[] listTargets() { |
55 |
10 |
final ArrayList results = new ArrayList(); |
56 |
10 |
if (targetPath.isDirectory() && targetPath.exists() && targetPath.canRead()) { |
57 |
10 |
final File[] files = targetPath.listFiles(); |
58 |
52 |
for (int i = 0; i < files.length; ++i) { |
59 |
42 |
final File file = files[i]; |
60 |
42 |
if (file.isFile() && file.canRead()) { |
61 |
32 |
final String fileName = file.getAbsolutePath(); |
62 |
|
|
63 |
32 |
if (fileName.endsWith(".jar")) { |
64 |
10 |
results.addAll(Arrays.asList(inspectJar(file))); |
65 |
22 |
} else if (fileName.endsWith(".class")) { |
66 |
20 |
final Target target = checkIfTargetClass(file); |
67 |
20 |
if (null != target) { |
68 |
20 |
results.add(target); |
69 |
|
} |
70 |
|
} |
71 |
|
} |
72 |
|
} |
73 |
|
} |
74 |
10 |
return (Target[]) results.toArray(new Target[0]); |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private Target[] inspectJar(final File file) { |
82 |
10 |
final ArrayList targets = new ArrayList(); |
83 |
|
try { |
84 |
10 |
final Target[] jarTargets = JarInspector.listTargets(file); |
85 |
50 |
for (int i = 0; i < jarTargets.length; ++i) { |
86 |
|
try { |
87 |
40 |
final Class c = cl.loadClass(jarTargets[i].getName()); |
88 |
40 |
if (FinderHelper.isTargetClass(c)) { |
89 |
20 |
targets.add(jarTargets[i]); |
90 |
|
} |
91 |
0 |
} catch (ClassNotFoundException e) { |
92 |
0 |
logger.log(Level.WARNING, "ClassNotFoundException on " + jarTargets[i].getName(), e); |
93 |
20 |
} |
94 |
|
} |
95 |
0 |
} catch (IOException e) { |
96 |
0 |
logger.log(Level.SEVERE, "IOException on \"" + file.getAbsolutePath() + "\"", e); |
97 |
5 |
} |
98 |
10 |
return (Target[]) targets.toArray(new Target[0]); |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
private Target checkIfTargetClass(final File classFile) { |
108 |
20 |
Target result = null; |
109 |
20 |
FileInputStream is = null; |
110 |
|
try { |
111 |
20 |
is = new FileInputStream(classFile); |
112 |
20 |
final String classFilename = classFile.getName(); |
113 |
20 |
System.out.println("attempting to load \"" + classFilename + "\""); |
114 |
30 |
final String className = ClassPathUtils.toClassName(targetPath.getAbsolutePath(), classFile |
115 |
20 |
.getAbsolutePath()); |
116 |
20 |
final Class loadedClass = cl.loadClass(className); |
117 |
|
|
118 |
20 |
if (FinderHelper.isTargetClass(loadedClass)) { |
119 |
20 |
result = new Target(Target.TARGET_IS_CLASS, targetPath.getAbsolutePath(), className); |
120 |
|
} |
121 |
10 |
} catch (NoClassDefFoundError e) { |
122 |
0 |
logger.log(Level.WARNING, "NoClassDefFoundError inspecting " + classFile.getAbsolutePath(), e); |
123 |
0 |
} catch (ClassNotFoundException e) { |
124 |
0 |
logger.log(Level.WARNING, "ClassNotFoundException inspecting " + classFile.getAbsolutePath(), e); |
125 |
0 |
} catch (IOException e) { |
126 |
0 |
logger.log(Level.WARNING, "IOException inspecting " + classFile.getAbsolutePath(), e); |
127 |
10 |
} finally { |
128 |
10 |
if (null != is) { |
129 |
|
try { |
130 |
20 |
is.close(); |
131 |
0 |
} catch (IOException e) { |
132 |
0 |
logger.log(Level.SEVERE, "IOException closing InputStream", e); |
133 |
20 |
} |
134 |
|
} |
135 |
20 |
} |
136 |
20 |
return result; |
137 |
|
} |
138 |
|
|
139 |
|
} |