1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package org.apache.commons.classscan.builtin; |
15 | |
|
16 | |
import java.io.File; |
17 | |
import java.io.FileNotFoundException; |
18 | |
import java.io.IOException; |
19 | |
import java.net.URI; |
20 | |
import java.net.URISyntaxException; |
21 | |
import java.net.URL; |
22 | |
|
23 | |
import org.apache.commons.classscan.MetaRegistry; |
24 | |
import org.apache.commons.classscan.spi.ClassPathElementFactory; |
25 | |
import org.apache.commons.classscan.spi.model.SpiClassPathElement; |
26 | |
|
27 | 0 | public class DefaultClassPathElementFactory implements ClassPathElementFactory { |
28 | |
|
29 | |
@Override |
30 | |
public SpiClassPathElement getClassPathElement(MetaRegistry metaRegistry, URL url) throws IOException { |
31 | 0 | String scheme= url.getProtocol(); |
32 | 0 | if(scheme.equals("file")) { |
33 | |
try { |
34 | 0 | return createFileClassPathElement(url); |
35 | 0 | } catch (URISyntaxException e) { |
36 | 0 | FileNotFoundException ex = new FileNotFoundException(url.toExternalForm()+" not found"); |
37 | 0 | ex.initCause(e); |
38 | 0 | throw ex; |
39 | |
} |
40 | |
} |
41 | 0 | else if(scheme.equals("jar")) { |
42 | 0 | return new JarClassPathElement(url); |
43 | |
} |
44 | |
|
45 | 0 | return null; |
46 | |
} |
47 | |
|
48 | |
private SpiClassPathElement createFileClassPathElement(URL url) throws IOException, URISyntaxException { |
49 | 0 | File location = new File(url.toURI()); |
50 | 0 | if (!location.canRead()) { |
51 | 0 | throw new FileNotFoundException(location + " can not be read"); |
52 | |
} |
53 | |
|
54 | 0 | if (location.isDirectory()) { |
55 | 0 | return new FileClassPathElement(location); |
56 | |
} |
57 | |
|
58 | 0 | URI fileUri = location.toURI(); |
59 | 0 | URI jarURI = new URI("jar:" + fileUri.toString() + "!/"); |
60 | 0 | return new JarClassPathElement(jarURI.toURL()); |
61 | |
} |
62 | |
} |