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