001/* 002 * Copyright 2012 Chris Pheby 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.jadira.scanner.file.locator; 017 018import java.io.File; 019import java.net.MalformedURLException; 020import java.net.URL; 021import java.util.ArrayList; 022import java.util.List; 023import java.util.StringTokenizer; 024 025import org.jadira.scanner.core.api.Locator; 026import org.jadira.scanner.core.exception.ClasspathAccessException; 027 028/** 029 * Uses the java.class.path system property to obtain a list of URLs that represent the 030 * CLASSPATH 031 */ 032public class JavaClasspathUrlLocator implements Locator<URL> { 033 034 @Override 035 public List<URL> locate() { 036 037 List<URL> list = new ArrayList<URL>(); 038 String classpath = System.getProperty("java.class.path"); 039 StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator); 040 041 while (tokenizer.hasMoreTokens()) { 042 043 String path = tokenizer.nextToken(); 044 045 File fp = new File(path); 046 if (!fp.exists()) { 047 throw new ClasspathAccessException("File in java.class.path does not exist: " + fp); 048 } 049 050 try { 051 list.add(fp.toURI().toURL()); 052 } catch (MalformedURLException e) { 053 throw new ClasspathAccessException("URL was invalid: " + fp.toURI().toString(), e); 054 } 055 } 056 return list; 057 } 058} 059