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 if (!"classpath".equalsIgnoreCase(repository.getProtocol())) {
47 throw new NoTransporterException(repository);
48 }
49
50 String base;
51 try {
52 URI uri = new URI(repository.getUrl());
53 String ssp = uri.getSchemeSpecificPart();
54 if (ssp.startsWith("/")) {
55 base = uri.getPath();
56 if (base == null) {
57 base = "";
58 } else if (base.startsWith("/")) {
59 base = base.substring(1);
60 }
61 } else {
62 base = ssp;
63 }
64 if (!base.isEmpty() && !base.endsWith("/")) {
65 base += '/';
66 }
67 } catch (URISyntaxException e) {
68 throw new NoTransporterException(repository, e);
69 }
70 resourceBase = base;
71
72 Object cl = ConfigUtils.getObject(session, null, ClasspathTransporterFactory.CONFIG_PROP_CLASS_LOADER);
73 if (cl instanceof ClassLoader) {
74 classLoader = (ClassLoader) cl;
75 } else {
76 classLoader = Thread.currentThread().getContextClassLoader();
77 }
78 }
79
80 private URL getResource(TransportTask task) throws Exception {
81 String resource = resourceBase + task.getLocation().getPath();
82 URL url = classLoader.getResource(resource);
83 if (url == null) {
84 throw new ResourceNotFoundException("Could not locate " + resource);
85 }
86 return url;
87 }
88
89 @Override
90 public int classify(Throwable error) {
91 if (error instanceof ResourceNotFoundException) {
92 return ERROR_NOT_FOUND;
93 }
94 return ERROR_OTHER;
95 }
96
97 @Override
98 protected void implPeek(PeekTask task) throws Exception {
99 getResource(task);
100 }
101
102 @Override
103 protected void implGet(GetTask task) throws Exception {
104 URL url = getResource(task);
105 URLConnection conn = url.openConnection();
106 utilGet(task, conn.getInputStream(), true, conn.getContentLength(), false);
107 }
108
109 @Override
110 protected void implPut(PutTask task) throws Exception {
111 throw new UnsupportedOperationException("Uploading to a classpath: repository is not supported");
112 }
113
114 @Override
115 protected void implClose() {}
116 }