001package org.apache.maven.project;
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 java.io.File;
023
024import org.eclipse.aether.RepositorySystemSession;
025import org.eclipse.aether.artifact.Artifact;
026import org.eclipse.aether.metadata.Metadata;
027import org.eclipse.aether.repository.LocalArtifactRegistration;
028import org.eclipse.aether.repository.LocalArtifactRequest;
029import org.eclipse.aether.repository.LocalArtifactResult;
030import org.eclipse.aether.repository.LocalMetadataRegistration;
031import org.eclipse.aether.repository.LocalMetadataRequest;
032import org.eclipse.aether.repository.LocalMetadataResult;
033import org.eclipse.aether.repository.LocalRepository;
034import org.eclipse.aether.repository.LocalRepositoryManager;
035import org.eclipse.aether.repository.RemoteRepository;
036
037/**
038 * @author Benjamin Bentmann
039 */
040public class LegacyLocalRepositoryManager
041    implements LocalRepositoryManager
042{
043
044    private final LocalRepository repository;
045
046    public LegacyLocalRepositoryManager( File basedir )
047    {
048        this.repository = new LocalRepository( basedir.getAbsoluteFile(), "legacy" );
049    }
050
051    public LocalRepository getRepository()
052    {
053        return repository;
054    }
055
056    public String getPathForLocalArtifact( Artifact artifact )
057    {
058        StringBuilder path = new StringBuilder( 128 );
059
060        path.append( artifact.getGroupId() ).append( '/' );
061
062        path.append( artifact.getExtension() ).append( 's' ).append( '/' );
063
064        path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
065
066        if ( artifact.getClassifier().length() > 0 )
067        {
068            path.append( '-' ).append( artifact.getClassifier() );
069        }
070
071        path.append( '.' ).append( artifact.getExtension() );
072
073        return path.toString();
074    }
075
076    public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
077    {
078        return getPathForLocalArtifact( artifact );
079    }
080
081    public String getPathForLocalMetadata( Metadata metadata )
082    {
083        return getPath( metadata, "local" );
084    }
085
086    public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
087    {
088        return getPath( metadata, getRepositoryKey( repository, context ) );
089    }
090
091    String getRepositoryKey( RemoteRepository repository, String context )
092    {
093        return repository.getId();
094    }
095
096    private String getPath( Metadata metadata, String repositoryKey )
097    {
098        StringBuilder path = new StringBuilder( 128 );
099
100        if ( metadata.getGroupId().length() > 0 )
101        {
102            path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
103
104            if ( metadata.getArtifactId().length() > 0 )
105            {
106                path.append( metadata.getArtifactId() ).append( '/' );
107
108                if ( metadata.getVersion().length() > 0 )
109                {
110                    path.append( metadata.getVersion() ).append( '/' );
111                }
112            }
113        }
114
115        path.append( insertRepositoryKey( metadata.getType(), repositoryKey ) );
116
117        return path.toString();
118    }
119
120    private String insertRepositoryKey( String filename, String repositoryKey )
121    {
122        String result;
123        int idx = filename.indexOf( '.' );
124        if ( idx < 0 )
125        {
126            result = filename + '-' + repositoryKey;
127        }
128        else
129        {
130            result = filename.substring( 0, idx ) + '-' + repositoryKey + filename.substring( idx );
131        }
132        return result;
133    }
134
135    public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
136    {
137        String path = getPathForLocalArtifact( request.getArtifact() );
138        File file = new File( getRepository().getBasedir(), path );
139
140        LocalArtifactResult result = new LocalArtifactResult( request );
141        if ( file.isFile() )
142        {
143            result.setFile( file );
144            result.setAvailable( true );
145        }
146
147        return result;
148    }
149
150    public void add( RepositorySystemSession session, LocalArtifactRegistration request )
151    {
152        // noop
153    }
154
155    public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
156    {
157        LocalMetadataResult result = new LocalMetadataResult( request );
158
159        String path;
160
161        Metadata metadata = request.getMetadata();
162        String context = request.getContext();
163        RemoteRepository remote = request.getRepository();
164
165        if ( remote != null )
166        {
167            path = getPathForRemoteMetadata( metadata, remote, context );
168        }
169        else
170        {
171            path = getPathForLocalMetadata( metadata );
172        }
173
174        File file = new File( getRepository().getBasedir(), path );
175        if ( file.isFile() )
176        {
177            result.setFile( file );
178        }
179
180        return result;
181    }
182
183    public void add( RepositorySystemSession session, LocalMetadataRegistration request )
184    {
185        // noop
186    }
187
188    public String toString()
189    {
190        return String.valueOf( getRepository() );
191    }
192}