1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.transport.classpath;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.net.URLConnection;
25
26 import org.eclipse.aether.RepositorySystemSession;
27 import org.eclipse.aether.repository.RemoteRepository;
28 import org.eclipse.aether.spi.connector.transport.AbstractTransporter;
29 import org.eclipse.aether.spi.connector.transport.GetTask;
30 import org.eclipse.aether.spi.connector.transport.PeekTask;
31 import org.eclipse.aether.spi.connector.transport.PutTask;
32 import org.eclipse.aether.spi.connector.transport.TransportTask;
33 import org.eclipse.aether.transfer.NoTransporterException;
34 import org.eclipse.aether.util.ConfigUtils;
35
36
37
38
39 final class ClasspathTransporter extends AbstractTransporter {
40
41 private final String resourceBase;
42
43 private final ClassLoader classLoader;
44
45 ClasspathTransporter(RepositorySystemSession session, RemoteRepository repository) throws NoTransporterException {
46 String base;
47 try {
48 URI uri = new URI(repository.getUrl());
49 String ssp = uri.getSchemeSpecificPart();
50 if (ssp.startsWith("/")) {
51 base = uri.getPath();
52 if (base == null) {
53 base = "";
54 } else if (base.startsWith("/")) {
55 base = base.substring(1);
56 }
57 } else {
58 base = ssp;
59 }
60 if (!base.isEmpty() && !base.endsWith("/")) {
61 base += '/';
62 }
63 } catch (URISyntaxException e) {
64 throw new NoTransporterException(repository, e);
65 }
66 resourceBase = base;
67
68 Object cl = ConfigUtils.getObject(session, null, ClasspathTransporterFactory.CONFIG_PROP_CLASS_LOADER);
69 if (cl instanceof ClassLoader) {
70 classLoader = (ClassLoader) cl;
71 } else {
72 classLoader = Thread.currentThread().getContextClassLoader();
73 }
74 }
75
76 private URL getResource(TransportTask task) throws Exception {
77 String resource = resourceBase + task.getLocation().getPath();
78 URL url = classLoader.getResource(resource);
79 if (url == null) {
80 throw new ResourceNotFoundException("Could not locate " + resource);
81 }
82 return url;
83 }
84
85 @Override
86 public int classify(Throwable error) {
87 if (error instanceof ResourceNotFoundException) {
88 return ERROR_NOT_FOUND;
89 }
90 return ERROR_OTHER;
91 }
92
93 @Override
94 protected void implPeek(PeekTask task) throws Exception {
95 getResource(task);
96 }
97
98 @Override
99 protected void implGet(GetTask task) throws Exception {
100 URL url = getResource(task);
101 URLConnection conn = url.openConnection();
102 utilGet(task, conn.getInputStream(), true, conn.getContentLength(), false);
103 }
104
105 @Override
106 protected void implPut(PutTask task) throws Exception {
107 throw new UnsupportedOperationException("Uploading to a classpath: repository is not supported");
108 }
109
110 @Override
111 protected void implClose() {}
112 }