View Javadoc
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;
20  
21  import java.util.Map;
22  
23  import org.eclipse.aether.artifact.Artifact;
24  import org.eclipse.aether.artifact.ArtifactProperties;
25  import org.eclipse.aether.graph.Dependency;
26  import org.eclipse.aether.graph.DependencyNode;
27  
28  /**
29   * In Resolver 1.x line, the "system" scope represented special artifacts. In 2.x resolver testing for this scope
30   * is now delegated to consumer application. Class or component that wants to test for this special dependency scope
31   * should use this interface, with implementation provided by consumer application.
32   * <p>
33   * System is a special scope that tells resolver that dependency is not to be found in any regular repository, so it
34   * should not even try to resolve the artifact from them. Dependency in this scope does not have artifact descriptor
35   * either. Artifacts in this scope should have the "local path" property set, pointing to a file on local system, where
36   * the backing file should reside. Resolution of artifacts in this scope fails, if backing file does not exist
37   * (no property set, or property contains invalid path, or the path points to a non-existent file).
38   *
39   * @since 2.0.0
40   */
41  public interface SystemScopeHandler {
42      /**
43       * Returns {@code true} only, if passed in scope label represents "system" scope (as consumer project defines it).
44       */
45      boolean isSystemScope(String scope);
46  
47      /**
48       * Returns {@code true} if given dependency is in "system" scope.
49       */
50      default boolean isSystemScope(Dependency dependency) {
51          return dependency != null && isSystemScope(dependency.getScope());
52      }
53  
54      /**
55       * Returns {@code true} if given dependency node dependency is in "system" scope.
56       */
57      default boolean isSystemScope(DependencyNode dependencyNode) {
58          return dependencyNode != null
59                  && dependencyNode.getDependency() != null
60                  && isSystemScope(dependencyNode.getDependency());
61      }
62  
63      /**
64       * Returns system path string of provided artifact, or {@code null}.
65       *
66       * @param artifact The artifact that we want system path from, must not be {@code null}.
67       * @return the system path from passed in properties, or {@code null} if not present.
68       */
69      String getSystemPath(Artifact artifact);
70  
71      /**
72       * Sets system path in properties. The passed in {@code systemPath} can be {@code null}, in which case expected
73       * operation is "remove" (or "unset").
74       *
75       * @param properties the properties map, must not be {@code null}.
76       * @param systemPath the system path to set (if not {@code null}) or unset (if {@code null}).
77       */
78      void setSystemPath(Map<String, String> properties, String systemPath);
79  
80      /**
81       * The equivalent of Resolver 1.x "system" scope.
82       */
83      SystemScopeHandler LEGACY = new SystemScopeHandler() {
84          @Override
85          public boolean isSystemScope(String scope) {
86              return "system".equals(scope);
87          }
88  
89          @Override
90          public String getSystemPath(Artifact artifact) {
91              return artifact.getProperty(ArtifactProperties.LOCAL_PATH, null);
92          }
93  
94          @Override
95          public void setSystemPath(Map<String, String> properties, String systemPath) {
96              if (systemPath == null) {
97                  properties.remove(ArtifactProperties.LOCAL_PATH);
98              } else {
99                  properties.put(ArtifactProperties.LOCAL_PATH, systemPath);
100             }
101         }
102     };
103 }