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