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