001    package org.apache.maven.repository;
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    
022    import java.io.File;
023    import java.io.IOException;
024    import java.net.MalformedURLException;
025    import java.net.URL;
026    import java.util.Collection;
027    
028    import org.codehaus.plexus.util.FileUtils;
029    import org.sonatype.aether.artifact.Artifact;
030    import org.sonatype.aether.repository.RemoteRepository;
031    import org.sonatype.aether.spi.connector.ArtifactDownload;
032    import org.sonatype.aether.spi.connector.ArtifactUpload;
033    import org.sonatype.aether.spi.connector.MetadataDownload;
034    import org.sonatype.aether.spi.connector.MetadataUpload;
035    import org.sonatype.aether.spi.connector.RepositoryConnector;
036    import org.sonatype.aether.transfer.ArtifactNotFoundException;
037    import org.sonatype.aether.transfer.ArtifactTransferException;
038    
039    /**
040     * @author Benjamin Bentmann
041     */
042    public class TestRepositoryConnector
043        implements RepositoryConnector
044    {
045    
046        private RemoteRepository repository;
047    
048        private File basedir;
049    
050        public TestRepositoryConnector( RemoteRepository repository )
051        {
052            this.repository = repository;
053            try
054            {
055                basedir = FileUtils.toFile( new URL( repository.getUrl() ) );
056            }
057            catch ( MalformedURLException e )
058            {
059                throw new IllegalStateException( e );
060            }
061        }
062    
063        public void close()
064        {
065        }
066    
067        public void get( Collection<? extends ArtifactDownload> artifactDownloads,
068                         Collection<? extends MetadataDownload> metadataDownloads )
069        {
070            if ( artifactDownloads != null )
071            {
072                for ( ArtifactDownload download : artifactDownloads )
073                {
074                    File remoteFile = new File( basedir, path( download.getArtifact() ) );
075                    try
076                    {
077                        FileUtils.copyFile( remoteFile, download.getFile() );
078                    }
079                    catch ( IOException e )
080                    {
081                        if ( !remoteFile.exists() )
082                        {
083                            download.setException( new ArtifactNotFoundException( download.getArtifact(), repository ) );
084                        }
085                        else
086                        {
087                            download.setException( new ArtifactTransferException( download.getArtifact(), repository, e ) );
088                        }
089                    }
090                }
091            }
092        }
093    
094        private String path( Artifact artifact )
095        {
096            StringBuilder path = new StringBuilder( 128 );
097    
098            path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
099    
100            path.append( artifact.getArtifactId() ).append( '/' );
101    
102            path.append( artifact.getBaseVersion() ).append( '/' );
103    
104            path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
105    
106            if ( artifact.getClassifier().length() > 0 )
107            {
108                path.append( '-' ).append( artifact.getClassifier() );
109            }
110    
111            path.append( '.' ).append( artifact.getExtension() );
112    
113            return path.toString();
114        }
115    
116        public void put( Collection<? extends ArtifactUpload> artifactUploads,
117                         Collection<? extends MetadataUpload> metadataUploads )
118        {
119            // TODO Auto-generated method stub
120    
121        }
122    
123    }