1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.extend.server.deployment;
14
15 import java.io.File;
16 import java.util.NoSuchElementException;
17
18
19 public class ModuleId {
20
21 public static final String JAR_TYPE = "jar";
22
23 protected String groupId;
24
25 protected String artifactId;
26
27 protected String version;
28
29 protected String type;
30
31 protected String classifier;
32
33 protected String stringRepresentation;
34
35 public ModuleId() {
36 }
37
38 public ModuleId(String moduleId) {
39 try {
40 parseModuleIdString(this, moduleId);
41 } catch (NoSuchElementException e) {
42 throw e;
43 }
44 }
45
46 public ModuleId(ModuleId copy) {
47 this.groupId = copy.groupId;
48 this.artifactId = copy.artifactId;
49 this.version = copy.version;
50 this.type = copy.type;
51 this.classifier = copy.classifier;
52 }
53
54 public String getGroupId() {
55 return groupId;
56 }
57
58 public void setGroupId(String groupId) {
59 this.groupId = groupId;
60 }
61
62 public String getArtifactId() {
63 return artifactId;
64 }
65
66 public void setArtifactId(String artifactId) {
67 this.artifactId = artifactId;
68 }
69
70 public String getVersion() {
71 return version;
72 }
73
74 public void setVersion(String version) {
75 this.version = version;
76 }
77
78 public String getType() {
79 return type;
80 }
81
82 public void setType(String type) {
83 this.type = type;
84 }
85
86 public String getClassifier() {
87 return classifier;
88 }
89
90 public void setClassifier(String classifier) {
91 this.classifier = classifier;
92 }
93
94 public String toString() {
95 if (stringRepresentation == null) {
96 updateStringRepresentation();
97 }
98 return stringRepresentation;
99 }
100
101 protected void updateStringRepresentation() {
102 StringBuffer res = new StringBuffer();
103 if (groupId != null) {
104 res.append(groupId);
105 }
106 res.append(":");
107 if (artifactId != null) {
108 res.append(artifactId);
109 }
110 res.append(":");
111 if (version != null) {
112 res.append(version);
113 }
114 if (type != null) {
115 res.append(":");
116 res.append(type);
117 }
118 if (classifier != null) {
119 if (type == null) {
120 res.append(":");
121 }
122 res.append(":");
123 res.append(classifier);
124 }
125 if (res.length() == 0) {
126 res.append("unknown@" + System.identityHashCode(this));
127 }
128 stringRepresentation = res.toString();
129 }
130
131 public int hashCode() {
132 if (stringRepresentation == null) {
133 updateStringRepresentation();
134 }
135 return stringRepresentation.hashCode();
136 }
137
138 public ModuleId toSnapshotArtifact() {
139 if (version.toUpperCase().endsWith("-SNAPSHOT")) {
140 return this;
141 } else {
142 ModuleId snapshot = new ModuleId(this);
143 snapshot.setVersion(version + "-SNAPSHOT");
144 return snapshot;
145 }
146 }
147
148 public ModuleId toNonSnapshotArtifact() {
149 if (!version.toUpperCase().endsWith("-SNAPSHOT")) {
150 return this;
151 } else {
152 ModuleId notSnapshot = new ModuleId(this);
153 notSnapshot.setVersion(version.substring(0, version.length() - 9));
154 return notSnapshot;
155 }
156 }
157
158 public boolean isSnapshot() {
159 if ((version != null) && version.toUpperCase().endsWith("-SNAPSHOT")) {
160 return true;
161 }
162 return false;
163 }
164
165 public String getShortId() {
166 StringBuffer res = new StringBuffer();
167 res.append(groupId).append(':').append(artifactId).append(':').append(version);
168 return res.toString();
169 }
170
171 public String getFullId() {
172 StringBuffer res = new StringBuffer();
173 res.append(groupId).append(':').append(artifactId).append(':').append(version);
174 if (type != null) {
175 res.append(':').append(type);
176 if (classifier != null) {
177 res.append(':').append(classifier);
178 }
179 } else if (classifier != null) {
180 res.append(':').append(':').append(classifier);
181 }
182 return res.toString();
183 }
184
185 public boolean equals(Object o) {
186 if (o instanceof ModuleId) {
187 ModuleId other = (ModuleId)o;
188
189 String type = getType();
190 if (type == null) {
191 type = JAR_TYPE;
192 }
193 String otype = other.getType();
194 if (otype == null) {
195 otype = JAR_TYPE;
196 }
197
198 boolean res = (((other.groupId == groupId) || ((groupId != null) && groupId.equals(other.groupId)))
199 && ((other.artifactId == artifactId) || ((artifactId != null) && artifactId.equals(other.artifactId)))
200 && ((other.version == version) || ((version != null) && version.equals(other.version)))
201 && ((otype == type) || ((type != null) && type.equals(otype)))
202 && ((other.classifier == classifier) || ((classifier != null) && classifier.equals(other.classifier))));
203 return res;
204 } else if (o instanceof String) {
205 return (getFullId().equals(o));
206 }
207 return false;
208 }
209
210 public boolean equalsIgnoreSnapshot(Object o) {
211 if (o instanceof ModuleId) {
212 ModuleId other = (ModuleId)o;
213 String type = getType();
214 if (type == null) {
215 type = JAR_TYPE;
216 }
217 String otype = other.getType();
218 if (otype == null) {
219 otype = JAR_TYPE;
220 }
221
222 if (((other.groupId == groupId) || ((groupId != null) && groupId.equals(other.groupId)))
223 && ((other.artifactId == artifactId) || ((artifactId != null) && artifactId.equals(other.artifactId)))
224 && ((otype == type) || ((type != null) && type.equals(otype)))
225 && ((other.classifier == classifier) || ((classifier != null) && classifier.equals(other.classifier)))) {
226
227 String vt = this.getVersion();
228 String vo = other.getVersion();
229 if (isSnapshot() != other.isSnapshot()) {
230 if (isSnapshot()) {
231 vt = vt.substring(0, vt.length() - 9);
232 } else {
233 vo = vo.substring(0, vo.length() - 9);
234 }
235 }
236 return ((vo == vt) || ((vt != null) && vt.equals(vo)));
237 } else {
238 return false;
239 }
240 } else if (o instanceof String) {
241 return (getFullId().equals(o));
242 }
243 return false;
244 }
245
246 public static ModuleId parseModuleIdString(String moduleId) {
247 ModuleId res = new ModuleId();
248 parseModuleIdString(res, moduleId);
249 return res;
250 }
251
252 public static ModuleId parseModuleIdString(ModuleId moduleId, String s) {
253 int i = s.indexOf(':');
254 if (i < 0) {
255 moduleId.setArtifactId(s);
256 return moduleId;
257 }
258 String p1 = s.substring(0, i).trim();
259 if (p1.length() > 0) {
260 moduleId.setGroupId(p1);
261 }
262 int j = s.indexOf(':', i + 1);
263 if (j < 0) {
264 p1 = s.substring(i + 1).trim();
265 if (p1.length() > 0) {
266 moduleId.setArtifactId(p1);
267 }
268 return moduleId;
269 }
270 p1 = s.substring(i + 1, j).trim();
271 if (p1.length() > 0) {
272 moduleId.setArtifactId(p1);
273 }
274 i = j + 1;
275 j = s.indexOf(':', i);
276 if (j < 0) {
277 p1 = s.substring(i).trim();
278 if (p1.length() > 0) {
279 moduleId.setVersion(p1);
280 }
281 return moduleId;
282 }
283 p1 = s.substring(i, j).trim();
284 if (p1.length() > 0) {
285 moduleId.setVersion(p1);
286 }
287
288 i = j + 1;
289 j = s.indexOf(':', i);
290 if (j < 0) {
291 p1 = s.substring(i).trim();
292 if (p1.length() > 0) {
293 moduleId.setType(p1);
294 }
295 return moduleId;
296 }
297 p1 = s.substring(i, j);
298 if (p1.length() > 0) {
299 moduleId.setType(p1);
300 }
301
302 i = j + 1;
303 j = s.indexOf(':', i);
304 if (j < 0) {
305 p1 = s.substring(i).trim();
306 if (p1.length() > 0) {
307 moduleId.setClassifier(p1);
308 }
309 return moduleId;
310 }
311 p1 = s.substring(i, j);
312 if (p1.length() > 0) {
313 moduleId.setClassifier(p1);
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327
328 return moduleId;
329 }
330
331 public static ModuleId createModuleIdFromFileName(String name) {
332 if (name == null) {
333 return null;
334 }
335 if (name.endsWith("/")) {
336 name = name.substring(0, name.length() - 1);
337 }
338 int q = name.lastIndexOf("/");
339 if (q >= 0) {
340 name = name.substring(q + 1);
341 }
342 ModuleId moduleId = new ModuleId();
343 int i = name.indexOf('#');
344 if (i >= 0) {
345 String s1 = name.substring(0, i);
346 int j = name.indexOf('#', i + 1);
347 if (j >= 0) {
348 String s2 = name.substring(i + 1, j);
349 int k = name.indexOf('#', j + 1);
350 if (k >= 0) {
351 String s3 = name.substring(j + 1, k);
352 int l = name.indexOf('#', k + 1);
353 if (l >= 0) {
354 String s4 = name.substring(k + 1, l);
355 moduleId.setGroupId(s1);
356 moduleId.setArtifactId(s2);
357 moduleId.setVersion(s3);
358 moduleId.setClassifier(s4);
359 String s5 = name.substring(l + 1);
360 if (s5.startsWith(".")) {
361 s5 = s5.substring(1);
362 }
363 moduleId.setType(s5);
364 } else {
365 moduleId.setGroupId(s1);
366 moduleId.setArtifactId(s2);
367 moduleId.setVersion(s3);
368 String s5 = name.substring(k + 1);
369 if (s5.startsWith(".")) {
370 s5 = s5.substring(1);
371 }
372 moduleId.setType(s5);
373 }
374 } else {
375 moduleId.setGroupId(s1);
376 moduleId.setArtifactId(s2);
377 String s5 = name.substring(j + 1);
378 if (s5.startsWith(".")) {
379 s5 = s5.substring(1);
380 }
381 moduleId.setType(s5);
382 }
383 } else {
384 moduleId.setArtifactId(s1);
385 String s5 = name.substring(i + 1);
386 if (s5.startsWith(".")) {
387 s5 = s5.substring(1);
388 }
389 moduleId.setType(s5);
390 }
391 } else {
392 i = name.indexOf('.');
393 if (i >= 0) {
394 String s1 = name.substring(0, i);
395 String s5 = name.substring(i + 1);
396 moduleId.setArtifactId(s1);
397 moduleId.setType(s5);
398 } else {
399 moduleId.setArtifactId(name);
400 }
401 }
402 return moduleId;
403 }
404
405 public static ModuleId createModuleIdFromFileName(File file) {
406 return ModuleId.createModuleIdFromFileName(file.getName());
407 }
408
409
410 }