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.FilenameFilter;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.jadira.scanner.core.api.Locator;
25 import org.jadira.scanner.core.exception.ClasspathAccessException;
26 import org.jadira.scanner.core.helper.filenamefilter.JarFilenameFilter;
27
28
29
30
31 public class JdkBaseClasspathUrlLocator implements Locator<URL> {
32
33 private static final FilenameFilter JAR_FILENAME_FILTER = new JarFilenameFilter();
34
35 private boolean includeRtJar;
36
37 public JdkBaseClasspathUrlLocator() {
38 this(true);
39 }
40
41 public JdkBaseClasspathUrlLocator(boolean includeRtJar) {
42 this.includeRtJar = includeRtJar;
43 }
44
45 @Override
46 public List<URL> locate() {
47
48 List<URL> classpaths = new ArrayList<URL>();
49
50 String[] endorsedDirs = System.getProperty("java.endorsed.dirs").split(System.getProperty("path.separator"));
51 processClasspathDefinition(classpaths, endorsedDirs);
52
53 try {
54
55 if (includeRtJar) {
56 URL javaClasspath = new java.io.File(System.getProperty("java.home") + System.getProperty("file.separator") + "lib" + System.getProperty("file.separator") + "rt.jar").toURI().toURL();
57 classpaths.add(javaClasspath);
58 }
59 } catch (MalformedURLException e) {
60 throw new ClasspathAccessException("Problem constructing Java classpath: " + e.getMessage(), e);
61 }
62
63 String[] extensionDirs = System.getProperty("java.ext.dirs").split(System.getProperty("path.separator"));
64 processClasspathDefinition(classpaths, extensionDirs);
65
66
67 return classpaths;
68 }
69
70 private static void processClasspathDefinition(List<URL> classpaths, String[] extensionDirs) {
71 for (int i = 0; i < extensionDirs.length; i++) {
72 java.io.File nextDir = new java.io.File(extensionDirs[i]);
73
74 String[] jars = nextDir.list(JAR_FILENAME_FILTER);
75
76 if (jars != null) {
77 for (int jarIdx = 0; jarIdx < jars.length; jarIdx++) {
78 try {
79 URL nextJar = new URL(nextDir.toURI().toURL().toString() + jars[jarIdx]);
80 classpaths.add(nextJar);
81 } catch (MalformedURLException e) {
82 throw new ClasspathAccessException("Problem constructing Java extension classpath: " + e.getMessage(), e);
83 }
84 }
85 }
86 }
87 }
88 }
89