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.internal.test.util;
20  
21  import org.eclipse.aether.RepositorySystemSession;
22  import org.eclipse.aether.artifact.Artifact;
23  import org.eclipse.aether.resolution.ArtifactDescriptorException;
24  import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
25  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
26  
27  import static java.util.Objects.requireNonNull;
28  
29  /**
30   * An artifact descriptor reader that gets data from a simple text file on the classpath. The data file for an artifact
31   * with the coordinates {@code gid:aid:ext:ver} is expected to be named {@code gid_aid_ver.ini} and can optionally have
32   * some prefix. The data file can have the following sections:
33   * <ul>
34   * <li>relocation</li>
35   * <li>dependencies</li>
36   * <li>managedDependencies</li>
37   * <li>repositories</li>
38   * </ul>
39   * The relocation and dependency sections contain artifact coordinates of the form:
40   *
41   * <pre>
42   * gid:aid:ext:ver[:scope][:optional]
43   * </pre>
44   *
45   * The dependency sections may also specify exclusions:
46   *
47   * <pre>
48   * -gid:aid
49   * </pre>
50   *
51   * A repository definition is of the form:
52   *
53   * <pre>
54   * id:type:url
55   * </pre>
56   *
57   * <h2>Example</h2>
58   *
59   * <pre>
60   * [relocation]
61   * gid:aid:ext:ver
62   *
63   * [dependencies]
64   * gid:aid:ext:ver:scope
65   * -exclusion:aid
66   * gid:aid2:ext:ver:scope:optional
67   *
68   * [managed-dependencies]
69   * gid:aid2:ext:ver2:scope
70   * -gid:aid
71   * -gid:aid
72   *
73   * [repositories]
74   * id:type:file:///test-repo
75   * </pre>
76   */
77  public class IniArtifactDescriptorReader {
78      private final IniArtifactDataReader reader;
79  
80      /**
81       * Use the given prefix to load the artifact descriptions from the classpath.
82       */
83      public IniArtifactDescriptorReader(String prefix) {
84          reader = new IniArtifactDataReader(prefix);
85      }
86  
87      /**
88       * Parses the resource {@code $prefix/gid_aid_ver.ini} from the request artifact as an artifact description and
89       * wraps it into an ArtifactDescriptorResult.
90       */
91      public ArtifactDescriptorResult readArtifactDescriptor(
92              RepositorySystemSession session, ArtifactDescriptorRequest request) throws ArtifactDescriptorException {
93          requireNonNull(session, "session cannot be null");
94          requireNonNull(request, "request cannot be null");
95          ArtifactDescriptorResult result = new ArtifactDescriptorResult(request);
96          for (Artifact artifact = request.getArtifact(); ; ) {
97              String resourceName = String.format(
98                      "%s_%s_%s.ini", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
99              try {
100                 ArtifactDescription data = reader.parse(resourceName);
101                 if (data.getRelocation() != null) {
102                     result.addRelocation(artifact);
103                     result.setArtifact(data.getRelocation());
104                     artifact = data.getRelocation();
105                 } else {
106                     result.setArtifact(artifact);
107                     result.setDependencies(data.getDependencies());
108                     result.setManagedDependencies(data.getManagedDependencies());
109                     result.setRepositories(data.getRepositories());
110                     return result;
111                 }
112             } catch (Exception e) {
113                 throw new ArtifactDescriptorException(result, e.getMessage(), e);
114             }
115         }
116     }
117 }