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.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.StringTokenizer;
24  
25  import org.jadira.scanner.core.api.Locator;
26  import org.jadira.scanner.core.exception.ClasspathAccessException;
27  
28  /**
29   * Uses the java.class.path system property to obtain a list of URLs that represent the
30   * CLASSPATH
31   */
32  public class JavaClasspathUrlLocator implements Locator<URL> {
33      
34  	@Override
35  	public List<URL> locate() {
36  
37          List<URL> list = new ArrayList<URL>();
38          String classpath = System.getProperty("java.class.path");
39          StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
40  
41          while (tokenizer.hasMoreTokens()) {
42              
43          	String path = tokenizer.nextToken();
44              
45              File fp = new File(path);
46              if (!fp.exists()) {
47                  throw new ClasspathAccessException("File in java.class.path does not exist: " + fp);
48              }
49              
50              try {
51                  list.add(fp.toURI().toURL());
52              } catch (MalformedURLException e) {
53              	throw new ClasspathAccessException("URL was invalid: " + fp.toURI().toString(), e);
54              }
55          }
56          return list;
57      }
58  }
59