View Javadoc
1   /*
2    *  Copyright 2012 Chris Pheby
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * Resolves a list of classpaths representing the core 'rt.jar' and any endorsed libraries
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              // Can't resolve using sun.boot.class.path - vendor specific
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