001 /* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 package org.apache.commons.classscan.builtin; 015 016 import java.io.File; 017 import java.io.FileNotFoundException; 018 import java.io.IOException; 019 import java.net.URI; 020 import java.net.URISyntaxException; 021 import java.net.URL; 022 023 import org.apache.commons.classscan.MetaRegistry; 024 import org.apache.commons.classscan.spi.ClassPathElementFactory; 025 import org.apache.commons.classscan.spi.model.SpiClassPathElement; 026 027 public class DefaultClassPathElementFactory implements ClassPathElementFactory { 028 029 @Override 030 public SpiClassPathElement getClassPathElement(MetaRegistry metaRegistry, URL url) throws IOException { 031 String scheme= url.getProtocol(); 032 if(scheme.equals("file")) { 033 try { 034 return createFileClassPathElement(url); 035 } catch (URISyntaxException e) { 036 FileNotFoundException ex = new FileNotFoundException(url.toExternalForm()+" not found"); 037 ex.initCause(e); 038 throw ex; 039 } 040 } 041 else if(scheme.equals("jar")) { 042 return new JarClassPathElement(url); 043 } 044 045 return null; 046 } 047 048 private SpiClassPathElement createFileClassPathElement(URL url) throws IOException, URISyntaxException { 049 File location = new File(url.toURI()); 050 if (!location.canRead()) { 051 throw new FileNotFoundException(location + " can not be read"); 052 } 053 054 if (location.isDirectory()) { 055 return new FileClassPathElement(location); 056 } 057 058 URI fileUri = location.toURI(); 059 URI jarURI = new URI("jar:" + fileUri.toString() + "!/"); 060 return new JarClassPathElement(jarURI.toURL()); 061 } 062 }