1 package org.apache.maven.plugins.ear.util; 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.util.Set; 23 import java.util.TreeSet; 24 25 import org.apache.maven.artifact.Artifact; 26 27 /** 28 * An artifact repository used to resolve {@link org.apache.maven.plugins.ear.EarModule}. 29 * 30 * @author <a href="snicoll@apache.org">Stephane Nicoll</a> 31 */ 32 public class ArtifactRepository 33 { 34 private final Set<Artifact> artifacts; 35 36 private final String mainArtifactId; 37 38 private final ArtifactTypeMappingService artifactTypeMappingService; 39 40 /** 41 * Creates a new repository with the specified artifacts. 42 * 43 * @param artifacts the artifacts 44 * @param mainArtifactId the id to use for the main artifact (no classifier) 45 * @param artifactTypeMappingService {@link ArtifactTypeMappingService} 46 */ 47 public ArtifactRepository( Set<Artifact> artifacts, String mainArtifactId, 48 ArtifactTypeMappingService artifactTypeMappingService ) 49 { 50 this.artifacts = artifacts; 51 this.mainArtifactId = mainArtifactId; 52 this.artifactTypeMappingService = artifactTypeMappingService; 53 } 54 55 /** 56 * Returns the artifact with the specified parameters. 57 * <p> 58 * If the artifact is classified and is the only one with the specified groupI, artifactId and type, it will be 59 * returned.</p> 60 * <p> 61 * If the artifact is classified and is not the only one with the specified groupI, artifactId and type, it returns 62 * null.</p> 63 * 64 * If the artifact is not found, it returns null. 65 * 66 * @param groupId the group id 67 * @param artifactId the artifact id 68 * @param type the type 69 * @param classifier the classifier 70 * @return the artifact or null if no artifact were found 71 */ 72 public Artifact getUniqueArtifact( String groupId, String artifactId, String type, String classifier ) 73 { 74 final Set<Artifact> candidates = getArtifacts( groupId, artifactId, type ); 75 if ( candidates.size() == 0 ) 76 { 77 return null; 78 } 79 else if ( candidates.size() == 1 && classifier == null ) 80 { 81 return candidates.iterator().next(); 82 } 83 else if ( classifier != null ) 84 { 85 for ( Artifact a : candidates ) 86 { 87 if ( a.getClassifier() == null && classifier.equals( mainArtifactId ) ) 88 { 89 return a; 90 } 91 else if ( classifier.equals( a.getClassifier() ) ) 92 { 93 return a; 94 } 95 } 96 } 97 // All other cases, classifier is null and more than one candidate ; artifact not found 98 return null; 99 } 100 101 /** 102 * Returns the artifact with the specified parameters. 103 * <p> 104 * If the artifact is classified and is the only one with the specified groupI, artifactId and type, it will be 105 * returned.</p> 106 * <p> 107 * If the artifact is classified and is not the only one with the specified groupI, artifactId and type, it returns 108 * null.</p> 109 * 110 * If the artifact is not found, it returns null. 111 * 112 * @param groupId the group id 113 * @param artifactId the artifact id 114 * @param type the type 115 * @return the artifact or null if no artifact were found 116 */ 117 public Artifact getUniqueArtifact( String groupId, String artifactId, String type ) 118 { 119 return getUniqueArtifact( groupId, artifactId, type, null ); 120 } 121 122 /** 123 * Returns the artifacts with the specified parameters. 124 * 125 * @param groupId the group id 126 * @param artifactId the artifact id 127 * @param type the type 128 * @return the artifacts or an empty set if no artifact were found 129 */ 130 public Set<Artifact> getArtifacts( String groupId, String artifactId, String type ) 131 { 132 final Set<Artifact> result = new TreeSet<Artifact>(); 133 for ( Artifact a : artifacts ) 134 { 135 // If the groupId, the artifactId and if the 136 // artifact's type is known, then we have found a candidate. 137 if ( a.getGroupId().equals( groupId ) && a.getArtifactId().equals( artifactId ) 138 && artifactTypeMappingService.isMappedToType( type, a.getType() ) ) 139 { 140 result.add( a ); 141 142 } 143 } 144 return result; 145 } 146 }