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