Coverage Report - org.apache.maven.artifact.ant.AbstractArtifactWithRepositoryTask
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractArtifactWithRepositoryTask
15%
8/55
0%
0/22
2.571
 
 1  
 package org.apache.maven.artifact.ant;
 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 java.math.BigInteger;
 23  
 import java.security.MessageDigest;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 
 28  
 import org.apache.maven.model.Repository;
 29  
 import org.apache.tools.ant.BuildException;
 30  
 import org.apache.tools.ant.Project;
 31  
 
 32  
 /**
 33  
  * Base class for atifact tasks that are able to download artifact from repote repositories. 
 34  
  * @version $Id: AbstractArtifactWithRepositoryTask.java 772278 2009-05-06 14:31:49Z pgier $
 35  
  */
 36  1
 public abstract class AbstractArtifactWithRepositoryTask
 37  
     extends AbstractArtifactTask
 38  
 {
 39  
     /**
 40  
      * List of Ant Tasks RemoteRepository-ies
 41  
      */
 42  1
     private List remoteRepositories = new ArrayList();
 43  
 
 44  
     /**
 45  
      * Get the default remote repository.
 46  
      * @return central repository 
 47  
      */
 48  
     private static RemoteRepository getDefaultRemoteRepository()
 49  
     {
 50  
         // TODO: could we utilize the super POM for this?
 51  0
         RemoteRepository remoteRepository = new RemoteRepository();
 52  0
         remoteRepository.setId( "central" );
 53  0
         remoteRepository.setUrl( "http://repo1.maven.org/maven2" );
 54  0
         RepositoryPolicy snapshots = new RepositoryPolicy();
 55  0
         snapshots.setEnabled( false );
 56  0
         remoteRepository.addSnapshots( snapshots );
 57  0
         return remoteRepository;
 58  
     }
 59  
 
 60  
     private static String statusAsString( RepositoryPolicy policy )
 61  
     {
 62  0
         return ( policy == null ) || policy.isEnabled() ? "enabled" : "disabled";
 63  
     }
 64  
 
 65  
     protected List createRemoteArtifactRepositories()
 66  
     {
 67  0
         return createRemoteArtifactRepositories( null );
 68  
     }
 69  
 
 70  
     /**
 71  
      * Create the list of ArtifactRepository-ies where artifacts can be downloaded. If
 72  
      * no remote repository has been configured, adds central repository.
 73  
      * @param pomRepositories additionnal repositories defined in pom (or null if none)
 74  
      * @return the list of ArtifactRepository-ies
 75  
      * @see #createRemoteArtifactRepository(RemoteRepository)
 76  
      */
 77  
     protected List createRemoteArtifactRepositories( List pomRepositories )
 78  
     {
 79  0
         List remoteRepositories = new ArrayList();
 80  0
         remoteRepositories.addAll( getRemoteRepositories() );
 81  
 
 82  0
         if ( getRemoteRepositories().isEmpty() )
 83  
         {
 84  0
             remoteRepositories.add( getDefaultRemoteRepository() );
 85  
         }
 86  
 
 87  0
         if ( pomRepositories != null )
 88  
         {
 89  0
             for ( Iterator i = pomRepositories.iterator(); i.hasNext(); )
 90  
             {
 91  0
                 Repository pomRepository = (Repository) i.next();
 92  
             
 93  0
                 remoteRepositories.add( createAntRemoteRepository( pomRepository ) );
 94  0
             }
 95  
         }
 96  
 
 97  0
         log( "Using remote repositories:", Project.MSG_VERBOSE );
 98  0
         List list = new ArrayList();
 99  0
         for ( Iterator i = remoteRepositories.iterator(); i.hasNext(); )
 100  
         {
 101  0
             RemoteRepository remoteRepository = (RemoteRepository) i.next();
 102  0
             updateRepositoryWithSettings( remoteRepository );
 103  
     
 104  0
             StringBuffer msg = new StringBuffer();
 105  0
             msg.append( "  - id=" + remoteRepository.getId() );
 106  0
             msg.append( ", url=" + remoteRepository.getUrl() );
 107  0
             msg.append( ", releases=" + statusAsString( remoteRepository.getReleases() ) );
 108  0
             msg.append( ", snapshots=" + statusAsString( remoteRepository.getSnapshots() ) );
 109  0
             if ( remoteRepository.getAuthentication() != null )
 110  
             {
 111  0
                 msg.append( ", authentication=" + remoteRepository.getAuthentication().getUserName() );
 112  
             }
 113  0
             if ( remoteRepository.getProxy() != null )
 114  
             {
 115  0
                 msg.append( ", proxy=" + remoteRepository.getProxy().getHost() );
 116  
             }
 117  0
             getProject().log( msg.toString(), Project.MSG_VERBOSE );
 118  
     
 119  0
             list.add( createRemoteArtifactRepository( remoteRepository ) );
 120  0
         }
 121  0
         return list;
 122  
     }
 123  
 
 124  
     public List getRemoteRepositories()
 125  
     {
 126  0
         return remoteRepositories;
 127  
     }
 128  
 
 129  
     /**
 130  
      * This is called automatically by ant when the task is initialized.
 131  
      * Need to use "addConfigured..." instead of "add..." because the 
 132  
      * repository Id and URL need to be set before the method is called.
 133  
      * 
 134  
      * @param remoteRepository
 135  
      */
 136  
     public void addConfiguredRemoteRepository( RemoteRepository remoteRepository )
 137  
     {
 138  
         // Validate the url and id parameters before adding the repository
 139  0
         if ( remoteRepository.getUrl() == null )
 140  
         {
 141  0
             throw new BuildException( "Each remote repository must specify a url." );
 142  
         }
 143  0
         if ( remoteRepository.getId() == null || remoteRepository.getId().equals( remoteRepository.getUrl() ) )
 144  
         {
 145  0
             log( "Each remote repository must specify a unique id. For backward-compatibility, "
 146  
                  + "a default id will be used. In future releases, a missing repository id will raise an error.",
 147  
                   Project.MSG_WARN );
 148  0
             remoteRepository.setId( generateDefaultRepositoryId( remoteRepository ) );
 149  
         }
 150  0
         remoteRepositories.add( remoteRepository );
 151  0
     }
 152  
     
 153  1
     public final String MD5_ALGO_NAME = "MD5";
 154  
     
 155  1
     public final String UTF_ENC_NAME = "UTF-8";
 156  
     
 157  
     /**
 158  
      * Generates an MD5 digest based on the url of the repository.
 159  
      * This is safer to use for the id than the url.  
 160  
      * MANTTASKS-142
 161  
      * 
 162  
      * @param repository
 163  
      * @return
 164  
      */
 165  
     public String generateDefaultRepositoryId( RemoteRepository repository )
 166  
     {
 167  
         try
 168  
         {
 169  1
             MessageDigest md = MessageDigest.getInstance( MD5_ALGO_NAME );
 170  1
             md.update( repository.getUrl().getBytes( UTF_ENC_NAME ) );
 171  1
             BigInteger digest = new BigInteger( md.digest() );
 172  1
             return digest.toString( 16 );
 173  
         }
 174  0
         catch ( Exception e )
 175  
         {
 176  0
             log( "Unable to generate unique repository Id: " + e, Project.MSG_WARN );
 177  0
             return "default";
 178  
         }
 179  
     }
 180  
 }