View Javadoc
1   package org.eclipse.aether.transport.classpath;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A transporter reading from the classpath.
40   */
41  final class ClasspathTransporter
42      extends AbstractTransporter
43  {
44  
45      private final String resourceBase;
46  
47      private final ClassLoader classLoader;
48  
49      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 }