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