Coverage Report - org.apache.maven.project.ProjectUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectUtils
80 %
35/44
64 %
14/22
3,5
 
 1  
 package org.apache.maven.project;
 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 org.apache.maven.artifact.InvalidRepositoryException;
 23  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 24  
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 25  
 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
 26  
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 27  
 import org.apache.maven.model.DeploymentRepository;
 28  
 import org.apache.maven.model.Repository;
 29  
 import org.apache.maven.model.RepositoryBase;
 30  
 import org.apache.maven.model.RepositoryPolicy;
 31  
 import org.codehaus.plexus.PlexusContainer;
 32  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 33  
 
 34  
 import java.util.ArrayList;
 35  
 import java.util.Iterator;
 36  
 import java.util.List;
 37  
 
 38  
 public final class ProjectUtils
 39  
 {
 40  
     private ProjectUtils()
 41  0
     {
 42  0
     }
 43  
 
 44  
     public static List buildArtifactRepositories( List repositories,
 45  
                                                   ArtifactRepositoryFactory artifactRepositoryFactory,
 46  
                                                   PlexusContainer container )
 47  
         throws InvalidRepositoryException
 48  
     {
 49  
 
 50  410
         List repos = new ArrayList();
 51  
 
 52  410
         for ( Iterator i = repositories.iterator(); i.hasNext(); )
 53  
         {
 54  386
             Repository mavenRepo = (Repository) i.next();
 55  
 
 56  386
             ArtifactRepository artifactRepo =
 57  
                 buildArtifactRepository( mavenRepo, artifactRepositoryFactory, container );
 58  
 
 59  386
             if ( !repos.contains( artifactRepo ) )
 60  
             {
 61  386
                 repos.add( artifactRepo );
 62  
             }
 63  386
         }
 64  410
         return repos;
 65  
     }
 66  
 
 67  
     public static ArtifactRepository buildDeploymentArtifactRepository( DeploymentRepository repo,
 68  
                                                                         ArtifactRepositoryFactory artifactRepositoryFactory,
 69  
                                                                         PlexusContainer container )
 70  
         throws InvalidRepositoryException
 71  
     {
 72  2
         if ( repo != null )
 73  
         {
 74  2
             String id = repo.getId();
 75  2
             String url = repo.getUrl();
 76  
 
 77  
             // TODO: make this a map inside the factory instead, so no lookup needed
 78  2
             ArtifactRepositoryLayout layout = getRepositoryLayout( repo, container );
 79  
 
 80  2
             return artifactRepositoryFactory.createDeploymentArtifactRepository( id, url, layout,
 81  
                                                                                  repo.isUniqueVersion() );
 82  
         }
 83  
         else
 84  
         {
 85  0
             return null;
 86  
         }
 87  
     }
 88  
 
 89  
     public static ArtifactRepository buildArtifactRepository( Repository repo,
 90  
                                                               ArtifactRepositoryFactory artifactRepositoryFactory,
 91  
                                                               PlexusContainer container )
 92  
         throws InvalidRepositoryException
 93  
     {
 94  386
         if ( repo != null )
 95  
         {
 96  386
             String id = repo.getId();
 97  386
             String url = repo.getUrl();
 98  
 
 99  386
             if ( id == null || id.trim().length() < 1 )
 100  
             {
 101  0
                 throw new InvalidRepositoryException( "Repository ID must not be empty (URL is: " + url + ").", new IllegalArgumentException( "repository.id" ) );
 102  
             }
 103  
 
 104  386
             if ( url == null || url.trim().length() < 1 )
 105  
             {
 106  0
                 throw new InvalidRepositoryException( "Repository URL must not be empty (ID is: " + id + ").", new IllegalArgumentException( "repository.url" ) );
 107  
             }
 108  
 
 109  
             // TODO: make this a map inside the factory instead, so no lookup needed
 110  386
             ArtifactRepositoryLayout layout = getRepositoryLayout( repo, container );
 111  
 
 112  386
             ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() );
 113  386
             ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() );
 114  
 
 115  386
             return artifactRepositoryFactory.createArtifactRepository( id, url, layout, snapshots, releases );
 116  
         }
 117  
         else
 118  
         {
 119  0
             return null;
 120  
         }
 121  
     }
 122  
 
 123  
     private static ArtifactRepositoryPolicy buildArtifactRepositoryPolicy( RepositoryPolicy policy )
 124  
     {
 125  772
         boolean enabled = true;
 126  772
         String updatePolicy = null;
 127  772
         String checksumPolicy = null;
 128  
 
 129  772
         if ( policy != null )
 130  
         {
 131  474
             enabled = policy.isEnabled();
 132  474
             if ( policy.getUpdatePolicy() != null )
 133  
             {
 134  109
                 updatePolicy = policy.getUpdatePolicy();
 135  
             }
 136  474
             if ( policy.getChecksumPolicy() != null )
 137  
             {
 138  0
                 checksumPolicy = policy.getChecksumPolicy();
 139  
             }
 140  
         }
 141  
 
 142  772
         return new ArtifactRepositoryPolicy( enabled, updatePolicy, checksumPolicy );
 143  
     }
 144  
 
 145  
     private static ArtifactRepositoryLayout getRepositoryLayout( RepositoryBase mavenRepo, PlexusContainer container )
 146  
         throws InvalidRepositoryException
 147  
     {
 148  388
         String layout = mavenRepo.getLayout();
 149  
 
 150  
         ArtifactRepositoryLayout repositoryLayout;
 151  
         try
 152  
         {
 153  388
             repositoryLayout = (ArtifactRepositoryLayout) container.lookup( ArtifactRepositoryLayout.ROLE, layout );
 154  
         }
 155  0
         catch ( ComponentLookupException e )
 156  
         {
 157  0
             throw new InvalidRepositoryException( "Cannot find layout implementation corresponding to: \'" + layout +
 158  
                 "\' for remote repository with id: \'" + mavenRepo.getId() + "\'.", e );
 159  388
         }
 160  388
         return repositoryLayout;
 161  
     }
 162  
 
 163  
 }