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