1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.transport.classpath;
20
21 import javax.inject.Named;
22
23 import org.eclipse.aether.ConfigurationProperties;
24 import org.eclipse.aether.RepositorySystemSession;
25 import org.eclipse.aether.repository.RemoteRepository;
26 import org.eclipse.aether.spi.connector.transport.Transporter;
27 import org.eclipse.aether.spi.connector.transport.TransporterFactory;
28 import org.eclipse.aether.transfer.NoTransporterException;
29
30 import static java.util.Objects.requireNonNull;
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(ClasspathTransporterFactory.NAME)
41 public final class ClasspathTransporterFactory implements TransporterFactory {
42 public static final String NAME = "classpath";
43
44 /**
45 * The key in the repository session's {@link RepositorySystemSession#getConfigProperties() configuration
46 * properties} used to store a {@link ClassLoader} from which resources should be retrieved. If unspecified, the
47 * {@link Thread#getContextClassLoader() context class loader} of the current thread will be used.
48 *
49 * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
50 * @configurationType {@link java.lang.ClassLoader}
51 */
52 public static final String CONFIG_PROP_CLASS_LOADER = ConfigurationProperties.PREFIX_TRANSPORT + NAME + ".loader";
53
54 private float priority;
55
56 @Override
57 public float getPriority() {
58 return priority;
59 }
60
61 /**
62 * Sets the priority of this component.
63 *
64 * @param priority The priority.
65 * @return This component for chaining, never {@code null}.
66 */
67 public ClasspathTransporterFactory setPriority(float priority) {
68 this.priority = priority;
69 return this;
70 }
71
72 @Override
73 public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
74 throws NoTransporterException {
75 requireNonNull(session, "session cannot be null");
76 requireNonNull(repository, "repository cannot be null");
77
78 if (!"classpath".equalsIgnoreCase(repository.getProtocol())) {
79 throw new NoTransporterException(repository);
80 }
81
82 return new ClasspathTransporter(session, repository);
83 }
84 }