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 javax.inject.Inject;
23  import javax.inject.Named;
24  
25  import org.eclipse.aether.RepositorySystemSession;
26  import org.eclipse.aether.repository.RemoteRepository;
27  import org.eclipse.aether.spi.connector.transport.Transporter;
28  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
29  import org.eclipse.aether.spi.locator.Service;
30  import org.eclipse.aether.spi.locator.ServiceLocator;
31  import org.eclipse.aether.spi.log.Logger;
32  import org.eclipse.aether.spi.log.LoggerFactory;
33  import org.eclipse.aether.spi.log.NullLoggerFactory;
34  import org.eclipse.aether.transfer.NoTransporterException;
35  
36  /**
37   * A transporter factory for repositories using the {@code classpath:} protocol. As example, getting an item named
38   * {@code some/file.txt} from a repository with the URL {@code classpath:/base/dir} results in retrieving the resource
39   * {@code base/dir/some/file.txt} from the classpath. The classpath to load the resources from is given via a
40   * {@link ClassLoader} that can be configured via the configuration property {@link #CONFIG_PROP_CLASS_LOADER}.
41   * <p>
42   * <em>Note:</em> Such repositories are read-only and uploads to them are generally not supported.
43   */
44  @Named( "classpath" )
45  public final class ClasspathTransporterFactory
46      implements TransporterFactory, Service
47  {
48  
49      /**
50       * The key in the repository session's {@link RepositorySystemSession#getConfigProperties() configuration
51       * properties} used to store a {@link ClassLoader} from which resources should be retrieved. If unspecified, the
52       * {@link Thread#getContextClassLoader() context class loader} of the current thread will be used.
53       */
54      public static final String CONFIG_PROP_CLASS_LOADER = "aether.connector.classpath.loader";
55  
56      private Logger logger = NullLoggerFactory.LOGGER;
57  
58      private float priority;
59  
60      /**
61       * Creates an (uninitialized) instance of this transporter factory. <em>Note:</em> In case of manual instantiation
62       * by clients, the new factory needs to be configured via its various mutators before first use or runtime errors
63       * will occur.
64       */
65      public ClasspathTransporterFactory()
66      {
67          // enables default constructor
68      }
69  
70      @Inject
71      ClasspathTransporterFactory( LoggerFactory loggerFactory )
72      {
73          setLoggerFactory( loggerFactory );
74      }
75  
76      public void initService( ServiceLocator locator )
77      {
78          setLoggerFactory( locator.getService( LoggerFactory.class ) );
79      }
80  
81      /**
82       * Sets the logger factory to use for this component.
83       * 
84       * @param loggerFactory The logger factory to use, may be {@code null} to disable logging.
85       * @return This component for chaining, never {@code null}.
86       */
87      public ClasspathTransporterFactory setLoggerFactory( LoggerFactory loggerFactory )
88      {
89          this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, ClasspathTransporter.class );
90          return this;
91      }
92  
93      public float getPriority()
94      {
95          return priority;
96      }
97  
98      /**
99       * Sets the priority of this component.
100      * 
101      * @param priority The priority.
102      * @return This component for chaining, never {@code null}.
103      */
104     public ClasspathTransporterFactory setPriority( float priority )
105     {
106         this.priority = priority;
107         return this;
108     }
109 
110     public Transporter newInstance( RepositorySystemSession session, RemoteRepository repository )
111         throws NoTransporterException
112     {
113         return new ClasspathTransporter( session, repository, logger );
114     }
115 
116 }