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 992370 2010-09-03 16:48:59Z 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 * @param artifactTypeMappingService
48 */
49 public ArtifactRepository( Set artifacts, String mainArtifactId,
50 ArtifactTypeMappingService artifactTypeMappingService )
51 {
52 this.artifacts = artifacts;
53 this.mainArtifactId = mainArtifactId;
54 this.artifactTypeMappingService = artifactTypeMappingService;
55 }
56
57 /**
58 * Returns the artifact with the specified parameters.
59 * <p/>
60 * If the artifact is classified and is the only one with the specified
61 * groupI, artifactId and type, it will be returned.
62 * <p/>
63 * If the artifact is classified and is not the only one with the specified
64 * groupI, artifactId and type, it returns null.
65 * <p/>
66 * If the artifact is not found, it returns null.
67 *
68 * @param groupId the group id
69 * @param artifactId the artifact id
70 * @param type the type
71 * @param classifier the classifier
72 * @return the artifact or null if no artifact were found
73 */
74 public Artifact getUniqueArtifact( String groupId, String artifactId, String type, String classifier )
75 {
76 final Set candidates = getArtifacts( groupId, artifactId, type );
77 if ( candidates.size() == 0 )
78 {
79 return null;
80 }
81 else if ( candidates.size() == 1 && classifier == null )
82 {
83 return (Artifact) candidates.iterator().next();
84 }
85 else if ( classifier != null )
86 {
87 final Iterator it = candidates.iterator();
88 while ( it.hasNext() )
89 {
90 Artifact a = (Artifact) it.next();
91 if ( a.getClassifier() == null && classifier.equals( mainArtifactId ) )
92 {
93 return a;
94 }
95 else if ( classifier.equals( a.getClassifier() ) )
96 {
97 return a;
98 }
99 }
100 }
101 // All other cases, classifier is null and more than one candidate ; artifact not found
102 return null;
103 }
104
105 /**
106 * Returns the artifact with the specified parameters.
107 * <p/>
108 * If the artifact is classified and is the only one with the specified
109 * groupI, artifactId and type, it will be returned.
110 * <p/>
111 * If the artifact is classified and is not the only one with the specified
112 * groupI, artifactId and type, it returns null.
113 * <p/>
114 * If the artifact is not found, it returns null.
115 *
116 * @param groupId the group id
117 * @param artifactId the artifact id
118 * @param type the type
119 * @return the artifact or null if no artifact were found
120 */
121 public Artifact getUniqueArtifact( String groupId, String artifactId, String type )
122 {
123 return getUniqueArtifact( groupId, artifactId, type, null );
124 }
125
126 /**
127 * Returns the artifacts with the specified parameters.
128 *
129 * @param groupId the group id
130 * @param artifactId the artifact id
131 * @param type the type
132 * @return the artifacts or an empty set if no artifact were found
133 */
134 public Set getArtifacts( String groupId, String artifactId, String type )
135 {
136 final Set result = new TreeSet();
137 final Iterator it = artifacts.iterator();
138 while ( it.hasNext() )
139 {
140 Artifact a = (Artifact) it.next();
141
142 // If the groupId, the artifactId and if the
143 // artifact's type is known, then we have found a candidate.
144 if ( a.getGroupId().equals( groupId ) && a.getArtifactId().equals( artifactId ) &&
145 artifactTypeMappingService.isMappedToType( type, a.getType() ) )
146 {
147 result.add( a );
148
149 }
150 }
151 return result;
152 }
153 }