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
028import static java.util.Objects.requireNonNull;
029
030/**
031 * An artifact descriptor reader that gets data from a simple text file on the classpath. The data file for an artifact
032 * with the coordinates {@code gid:aid:ext:ver} is expected to be named {@code gid_aid_ver.ini} and can optionally have
033 * some prefix. The data file can have the following sections:
034 * <ul>
035 * <li>relocation</li>
036 * <li>dependencies</li>
037 * <li>managedDependencies</li>
038 * <li>repositories</li>
039 * </ul>
040 * The relocation and dependency sections contain artifact coordinates of the form:
041 * 
042 * <pre>
043 * gid:aid:ext:ver[:scope][:optional]
044 * </pre>
045 * 
046 * The dependency sections may also specify exclusions:
047 * 
048 * <pre>
049 * -gid:aid
050 * </pre>
051 * 
052 * A repository definition is of the form:
053 * 
054 * <pre>
055 * id:type:url
056 * </pre>
057 * 
058 * <h2>Example</h2>
059 * 
060 * <pre>
061 * [relocation]
062 * gid:aid:ext:ver
063 * 
064 * [dependencies]
065 * gid:aid:ext:ver:scope
066 * -exclusion:aid
067 * gid:aid2:ext:ver:scope:optional
068 * 
069 * [managed-dependencies]
070 * gid:aid2:ext:ver2:scope
071 * -gid:aid
072 * -gid:aid
073 * 
074 * [repositories]
075 * id:type:file:///test-repo
076 * </pre>
077 */
078public class IniArtifactDescriptorReader
079{
080    private final IniArtifactDataReader reader;
081
082    /**
083     * Use the given prefix to load the artifact descriptions from the classpath.
084     */
085    public IniArtifactDescriptorReader( String prefix )
086    {
087        reader = new IniArtifactDataReader( prefix );
088    }
089
090    /**
091     * Parses the resource {@code $prefix/gid_aid_ver.ini} from the request artifact as an artifact description and
092     * wraps it into an ArtifactDescriptorResult.
093     */
094    public ArtifactDescriptorResult readArtifactDescriptor( RepositorySystemSession session,
095                                                            ArtifactDescriptorRequest request )
096        throws ArtifactDescriptorException
097    {
098        requireNonNull( session, "session cannot be null" );
099        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}