File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\ModuleDependency.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Olivier Lamy
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.maven;
25
26 import org.apache.commons.collections.comparators.ReverseComparator;
27 import org.apache.maven.artifact.versioning.ArtifactVersion;
28 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
29 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
30 import org.apache.maven.artifact.versioning.VersionRange;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.model.Plugin;
33 import org.apache.maven.model.ReportPlugin;
34 import org.apache.maven.model.Extension;
35
36 import java.io.Serializable;
37 import java.util.Collection;
38 import java.util.SortedMap;
39 import java.util.TreeMap;
40
41 import hudson.Functions;
42
43 /**
44 * group id + artifact id + version and a flag to know if it's a plugin
45 *
46 * @author Kohsuke Kawaguchi
47 * @see ModuleName
48 */
49 public final class ModuleDependency implements Serializable {
50 public final String groupId;
51 public final String artifactId;
52 /**
53 * Version, possibly a version range.
54 */
55 public final String version;
56
57 /**
58 * @since 1.395
59 */
60 public final boolean plugin;
61
62 /**
63 * Cached result of {@code VersionRange.createFromVersionSpec(version)}
64 *
65 * @see #getVersionAsRange()
66 */
67 private VersionRange range;
68
69 /**
70 * Cache of the parsed form of {@link #version}
71 *
72 * @see #parseVersion()
73 */
74 private ArtifactVersion parsedVersion;
75
76 public ModuleDependency(String groupId, String artifactId, String version) {
77 this(groupId, artifactId, version, false);
78 }
79
80 public ModuleDependency(String groupId, String artifactId, String version, boolean plugin) {
81 this.groupId = groupId.intern();
82 this.artifactId = artifactId.intern();
83 if(version==null)
84 this.version = UNKNOWN;
85 else
86 this.version = version.intern();
87 this.plugin = plugin;
88 }
89
90 public ModuleDependency(ModuleName name, String version) {
91 this(name.groupId,name.artifactId,version,false);
92 }
93
94 public ModuleDependency(ModuleName name, String version, boolean plugin) {
95 this(name.groupId,name.artifactId,version,plugin);
96 }
97
98 public ModuleDependency(org.apache.maven.model.Dependency dep) {
99 this(dep.getGroupId(),dep.getArtifactId(),dep.getVersion());
100 }
101
102 public ModuleDependency(MavenProject project) {
103 this(project.getGroupId(),project.getArtifactId(),project.getVersion());
104 }
105
106 public ModuleDependency(Plugin p) {
107 this(p.getGroupId(),p.getArtifactId(), Functions.defaulted(p.getVersion(),NONE),true);
108 }
109
110 public ModuleDependency(ReportPlugin p) {
111 this(p.getGroupId(),p.getArtifactId(),p.getVersion(),true);
112 }
113
114 public ModuleDependency(Extension ext) {
115 this(ext.getGroupId(),ext.getArtifactId(),ext.getVersion());
116 }
117
118 private ModuleDependency(String groupId, String artifactId, boolean plugin) {
119 // to be used only by the withUnknownVersion() method
120 // where we know that groupId and artifactId are already interned
121 // and where we want an UNKNOWN version
122 this.groupId = groupId;
123 this.artifactId = artifactId;
124 this.version = UNKNOWN;
125 this.plugin = plugin;
126 }
127
128 public ModuleName getName() {
129 return new ModuleName(groupId,artifactId);
130 }
131
132 /**
133 * Returns groupId+artifactId plus unknown version.
134 */
135 public ModuleDependency withUnknownVersion() {
136 if (UNKNOWN.equals(version))
137 return this;
138 else
139 return new ModuleDependency(groupId,artifactId,plugin);
140 }
141
142 public boolean equals(Object o) {
143 if (this == o) return true;
144 if (o == null || getClass() != o.getClass()) return false;
145
146 ModuleDependency that = (ModuleDependency) o;
147
148 return this.artifactId.equals(that.artifactId)
149 && this.groupId.equals(that.groupId)
150 && this.version.equals(that.version)
151 && this.plugin == that.plugin;
152 }
153
154 public int hashCode() {
155 int result;
156 result = groupId.hashCode();
157 result = 31 * result + artifactId.hashCode();
158 result = 31 * result + version.hashCode();
159 result = 31 * result + (plugin ? 1 : 2);
160 return result;
161 }
162
163 /**
164 * Returns true if the version specification is a version range per maven version range syntax.
165 *
166 * @return true if version specification is a range.
167 */
168 public boolean isVersionRange() {
169 return version.startsWith("[") || version.startsWith("(");
170 }
171
172 public VersionRange getVersionAsRange() throws InvalidVersionSpecificationException {
173 if (range==null)
174 range = VersionRange.createFromVersionSpec(version);
175 return range;
176 }
177
178 public ArtifactVersion parseVersion() {
179 if (parsedVersion==null)
180 parsedVersion = new DefaultArtifactVersion(version);
181 return parsedVersion;
182 }
183
184 /**
185 * Upon reading from the disk, intern strings.
186 */
187 protected Object readResolve() {
188 return new ModuleDependency(groupId,artifactId,version,plugin);
189 }
190
191 /**
192 * For compatibility reason, this value may be used in the verion field
193 * to indicate that the version is unknown.
194 */
195 public static final String UNKNOWN = "*";
196
197 /**
198 * When a plugin dependency is specified without giving a version,
199 * the semantics of that is the latest released plugin.
200 * In this case, we don't want the {@link ModuleDependency} version to become
201 * {@link #UNKNOWN}, which would match any builds of the plugin.
202 *
203 * <p>
204 * So we use this constant to indicate a version, and this will not match
205 * anything.
206 *
207 * @see #ModuleDependency(Plugin)
208 */
209 public static final String NONE = "-";
210
211 private static final long serialVersionUID = 1L;
212
213 /**
214 * Checks whether this ModuleDependency is satisfied by the dependency of the given ModuleDependency.
215 * If the version string is a defined version, then it does a comparison. If the version string
216 * is a version range if parses this and caters for this.
217 *
218 * @param other The dependency to check for.
219 * @return true if contained false otherwise.
220 */
221 public boolean contains(ModuleDependency other) {
222 if (other == null || !getName().equals(other.getName()))
223 return false;
224
225 try {
226 return isVersionRange()
227 ? getVersionAsRange().containsVersion(other.parseVersion())
228 : parseVersion().compareTo(other.parseVersion()) == 0;
229 } catch (InvalidVersionSpecificationException ivse) {
230 return false;
231 }
232 }
233
234 /**
235 * Given a list of ModuleDependencies (of the same groupId and artifactId),
236 * picks the {@link ModuleDependency} that satisfies the constraint and has the highest version.
237 *
238 * @param candidates
239 * List that represents specific (non-range) versions.
240 * @return The highest satisfying ModuleDependency or null if none can be found.
241 */
242 public ModuleDependency findHighestFrom(Collection<ModuleDependency> candidates) {
243 //Create a sorted map of the ModuleDependnecies sorted on version (descending order).
244 SortedMap<ArtifactVersion, ModuleDependency> sorted = new TreeMap<ArtifactVersion, ModuleDependency>(new ReverseComparator());
245 for (ModuleDependency candidate : candidates) {
246 sorted.put(candidate.parseVersion(), candidate);
247 }
248
249 //Now find the highest version that satisfies this dependency.
250 for (ModuleDependency e : sorted.values()) {
251 if (contains(e))
252 return e;
253 }
254
255 // non found
256 return null;
257 }
258
259 @Override
260 public String toString() {
261 return "ModuleDependency{" +
262 "groupId='" + groupId + '\'' +
263 ", artifactId='" + artifactId + '\'' +
264 ", version='" + version + '\'' +
265 ", plugin=" + plugin +
266 '}';
267 }
268 }
269