1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.plugins.ear.util;
20
21 import java.util.Set;
22 import java.util.TreeSet;
23
24 import org.apache.maven.artifact.Artifact;
25
26 /**
27 * An artifact repository used to resolve {@link org.apache.maven.plugins.ear.EarModule}.
28 *
29 * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
30 */
31 public class ArtifactRepository {
32 private final Set<Artifact> artifacts;
33
34 private final String mainArtifactId;
35
36 private final ArtifactTypeMappingService artifactTypeMappingService;
37
38 /**
39 * Creates a new repository with the specified artifacts.
40 *
41 * @param artifacts the artifacts
42 * @param mainArtifactId the id to use for the main artifact (no classifier)
43 * @param artifactTypeMappingService {@link ArtifactTypeMappingService}
44 */
45 public ArtifactRepository(
46 Set<Artifact> artifacts, String mainArtifactId, ArtifactTypeMappingService artifactTypeMappingService) {
47 this.artifacts = artifacts;
48 this.mainArtifactId = mainArtifactId;
49 this.artifactTypeMappingService = artifactTypeMappingService;
50 }
51
52 /**
53 * Returns the artifact with the specified parameters.
54 * <p>
55 * If the artifact is classified and is the only one with the specified groupI, artifactId and type, it will be
56 * returned.</p>
57 * <p>
58 * If the artifact is classified and is not the only one with the specified groupI, artifactId and type, it returns
59 * null.</p>
60 *
61 * If the artifact is not found, it returns null.
62 *
63 * @param groupId the group id
64 * @param artifactId the artifact id
65 * @param type the type
66 * @param classifier the classifier
67 * @return the artifact or null if no artifact were found
68 */
69 public Artifact getUniqueArtifact(String groupId, String artifactId, String type, String classifier) {
70 final Set<Artifact> candidates = getArtifacts(groupId, artifactId, type);
71 if (candidates.size() == 0) {
72 return null;
73 } else if (candidates.size() == 1 && classifier == null) {
74 return candidates.iterator().next();
75 } else if (classifier != null) {
76 for (Artifact a : candidates) {
77 if (a.getClassifier() == null && classifier.equals(mainArtifactId)) {
78 return a;
79 } else if (classifier.equals(a.getClassifier())) {
80 return a;
81 }
82 }
83 }
84 // All other cases, classifier is null and more than one candidate ; artifact not found
85 return null;
86 }
87
88 /**
89 * Returns the artifact with the specified parameters.
90 * <p>
91 * If the artifact is classified and is the only one with the specified groupI, artifactId and type, it will be
92 * returned.</p>
93 * <p>
94 * If the artifact is classified and is not the only one with the specified groupI, artifactId and type, it returns
95 * null.</p>
96 *
97 * If the artifact is not found, it returns null.
98 *
99 * @param groupId the group id
100 * @param artifactId the artifact id
101 * @param type the type
102 * @return the artifact or null if no artifact were found
103 */
104 public Artifact getUniqueArtifact(String groupId, String artifactId, String type) {
105 return getUniqueArtifact(groupId, artifactId, type, null);
106 }
107
108 /**
109 * Returns the artifacts with the specified parameters.
110 *
111 * @param groupId the group id
112 * @param artifactId the artifact id
113 * @param type the type
114 * @return the artifacts or an empty set if no artifact were found
115 */
116 public Set<Artifact> getArtifacts(String groupId, String artifactId, String type) {
117 final Set<Artifact> result = new TreeSet<>();
118 for (Artifact a : artifacts) {
119 // If the groupId, the artifactId and if the
120 // artifact's type is known, then we have found a candidate.
121 if (a.getGroupId().equals(groupId)
122 && a.getArtifactId().equals(artifactId)
123 && artifactTypeMappingService.isMappedToType(type, a.getType())) {
124 result.add(a);
125 }
126 }
127 return result;
128 }
129 }