001package org.eclipse.aether.internal.test.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.RepositorySystemSession;
023import org.eclipse.aether.artifact.Artifact;
024import org.eclipse.aether.resolution.ArtifactDescriptorException;
025import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
026import org.eclipse.aether.resolution.ArtifactDescriptorResult;
027
028/**
029 * An artifact descriptor reader that gets data from a simple text file on the classpath. The data file for an artifact
030 * with the coordinates {@code gid:aid:ext:ver} is expected to be named {@code gid_aid_ver.ini} and can optionally have
031 * some prefix. The data file can have the following sections:
032 * <ul>
033 * <li>relocation</li>
034 * <li>dependencies</li>
035 * <li>managedDependencies</li>
036 * <li>repositories</li>
037 * </ul>
038 * The relocation and dependency sections contain artifact coordinates of the form:
039 * 
040 * <pre>
041 * gid:aid:ext:ver[:scope][:optional]
042 * </pre>
043 * 
044 * The dependency sections may also specify exclusions:
045 * 
046 * <pre>
047 * -gid:aid
048 * </pre>
049 * 
050 * A repository definition is of the form:
051 * 
052 * <pre>
053 * id:type:url
054 * </pre>
055 * 
056 * <h2>Example</h2>
057 * 
058 * <pre>
059 * [relocation]
060 * gid:aid:ext:ver
061 * 
062 * [dependencies]
063 * gid:aid:ext:ver:scope
064 * -exclusion:aid
065 * gid:aid2:ext:ver:scope:optional
066 * 
067 * [managed-dependencies]
068 * gid:aid2:ext:ver2:scope
069 * -gid:aid
070 * -gid:aid
071 * 
072 * [repositories]
073 * id:type:file:///test-repo
074 * </pre>
075 */
076public class IniArtifactDescriptorReader
077{
078    private IniArtifactDataReader reader;
079
080    /**
081     * Use the given prefix to load the artifact descriptions from the classpath.
082     */
083    public IniArtifactDescriptorReader( String prefix )
084    {
085        reader = new IniArtifactDataReader( prefix );
086    }
087
088    /**
089     * Parses the resource {@code $prefix/gid_aid_ver.ini} from the request artifact as an artifact description and
090     * wraps it into an ArtifactDescriptorResult.
091     */
092    public ArtifactDescriptorResult readArtifactDescriptor( RepositorySystemSession session,
093                                                            ArtifactDescriptorRequest request )
094        throws ArtifactDescriptorException
095    {
096        ArtifactDescriptorResult result = new ArtifactDescriptorResult( request );
097        for ( Artifact artifact = request.getArtifact();; )
098        {
099            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}