1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.index;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.maven.index.artifact.Gav;
30 import org.apache.maven.index.creator.JarFileContentsIndexCreator;
31 import org.apache.maven.index.creator.MavenPluginArtifactInfoIndexCreator;
32 import org.apache.maven.index.creator.MinimalArtifactInfoIndexCreator;
33 import org.apache.maven.index.creator.OsgiArtifactIndexCreator;
34 import org.eclipse.aether.util.version.GenericVersionScheme;
35 import org.eclipse.aether.version.InvalidVersionSpecificationException;
36 import org.eclipse.aether.version.Version;
37 import org.eclipse.aether.version.VersionScheme;
38
39
40
41
42
43
44
45
46 public class ArtifactInfo extends ArtifactInfoRecord {
47 private static final long serialVersionUID = 6028843453477511105L;
48
49
50
51 public static final String ROOT_GROUPS = "rootGroups";
52
53 public static final String ROOT_GROUPS_VALUE = "rootGroups";
54
55 public static final String ROOT_GROUPS_LIST = "rootGroupsList";
56
57 public static final String ALL_GROUPS = "allGroups";
58
59 public static final String ALL_GROUPS_VALUE = "allGroups";
60
61 public static final String ALL_GROUPS_LIST = "allGroupsList";
62
63
64
65
66
67
68 public static final String UINFO = FLD_UINFO.getKey();
69
70
71
72
73 public static final String DELETED = FLD_DELETED.getKey();
74
75
76
77
78 public static final String GROUP_ID = MinimalArtifactInfoIndexCreator.FLD_GROUP_ID_KW.getKey();
79
80
81
82
83 public static final String ARTIFACT_ID = MinimalArtifactInfoIndexCreator.FLD_ARTIFACT_ID_KW.getKey();
84
85
86
87
88 public static final String VERSION = MinimalArtifactInfoIndexCreator.FLD_VERSION_KW.getKey();
89
90
91
92
93 public static final String PACKAGING = MinimalArtifactInfoIndexCreator.FLD_PACKAGING.getKey();
94
95
96
97
98 public static final String CLASSIFIER = MinimalArtifactInfoIndexCreator.FLD_CLASSIFIER.getKey();
99
100
101
102
103 public static final String INFO = MinimalArtifactInfoIndexCreator.FLD_INFO.getKey();
104
105
106
107
108 public static final String NAME = MinimalArtifactInfoIndexCreator.FLD_NAME.getKey();
109
110
111
112
113 public static final String DESCRIPTION = MinimalArtifactInfoIndexCreator.FLD_DESCRIPTION.getKey();
114
115
116
117
118 public static final String LAST_MODIFIED = MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey();
119
120
121
122
123 public static final String SHA1 = MinimalArtifactInfoIndexCreator.FLD_SHA1.getKey();
124
125
126
127
128 public static final String NAMES = JarFileContentsIndexCreator.FLD_CLASSNAMES_KW.getKey();
129
130
131
132
133 public static final String PLUGIN_PREFIX = MavenPluginArtifactInfoIndexCreator.FLD_PLUGIN_PREFIX.getKey();
134
135
136
137
138 public static final String PLUGIN_GOALS = MavenPluginArtifactInfoIndexCreator.FLD_PLUGIN_GOALS.getKey();
139
140
141
142
143 public static final String BUNDLE_SYMBOLIC_NAME = OsgiArtifactIndexCreator.FLD_BUNDLE_SYMBOLIC_NAME.getKey();
144
145
146
147
148 public static final String BUNDLE_VERSION = OsgiArtifactIndexCreator.FLD_BUNDLE_VERSION.getKey();
149
150
151
152
153 public static final String BUNDLE_EXPORT_PACKAGE = OsgiArtifactIndexCreator.FLD_BUNDLE_EXPORT_PACKAGE.getKey();
154
155
156
157
158
159 public static final String BUNDLE_PROVIDE_CAPABILITY =
160 OsgiArtifactIndexCreator.FLD_BUNDLE_PROVIDE_CAPABILITY.getKey();
161
162
163
164
165
166 public static final String BUNDLE_REQUIRE_CAPABILITY =
167 OsgiArtifactIndexCreator.FLD_BUNDLE_REQUIRE_CAPABILITY.getKey();
168
169 public static final Comparator<ArtifactInfo> VERSION_COMPARATOR = new VersionComparator();
170
171 public static final Comparator<ArtifactInfo> REPOSITORY_VERSION_COMPARATOR = new RepositoryVersionComparator();
172
173 public static final Comparator<ArtifactInfo> CONTEXT_VERSION_COMPARATOR = new ContextVersionComparator();
174
175 private String fileName;
176
177 private String fileExtension;
178
179 private String groupId;
180
181 private String artifactId;
182
183 private String version;
184
185 private transient Version artifactVersion;
186
187 private transient float luceneScore;
188
189 private String classifier;
190
191
192
193
194 private String packaging;
195
196 private String name;
197
198 private String description;
199
200 private long lastModified = -1;
201
202 private long size = -1;
203
204 private String md5;
205
206 private String sha1;
207
208 private ArtifactAvailability sourcesExists = ArtifactAvailability.NOT_PRESENT;
209
210 private ArtifactAvailability javadocExists = ArtifactAvailability.NOT_PRESENT;
211
212 private ArtifactAvailability signatureExists = ArtifactAvailability.NOT_PRESENT;
213
214 private String classNames;
215
216 private String repository;
217
218 private String path;
219
220 private String remoteUrl;
221
222 private String context;
223
224
225
226
227 private String prefix;
228
229
230
231
232 private List<String> goals;
233
234
235
236
237
238 private String bundleVersion;
239
240
241
242
243
244 private String bundleSymbolicName;
245
246
247
248
249
250 private String bundleExportPackage;
251
252
253
254
255
256 private String bundleExportService;
257
258
259
260
261
262 private String bundleDescription;
263
264
265
266
267
268 private String bundleName;
269
270
271
272
273
274 private String bundleLicense;
275
276
277
278
279
280 private String bundleDocUrl;
281
282
283
284
285
286 private String bundleImportPackage;
287
288
289
290
291
292 private String bundleRequireBundle;
293
294
295
296
297
298
299 private String bundleProvideCapability;
300
301
302
303
304
305 private String bundleRequireCapability;
306
307
308
309
310
311 private String sha256;
312
313
314
315
316
317 private String bundleFragmentHost;
318
319
320
321
322
323
324 private String bundleRequiredExecutionEnvironment;
325
326 private final Map<String, String> attributes = new HashMap<>();
327
328 private final List<MatchHighlight> matchHighlights = new ArrayList<>();
329
330 private final transient VersionScheme versionScheme;
331
332 public ArtifactInfo() {
333 versionScheme = new GenericVersionScheme();
334 }
335
336 public ArtifactInfo(
337 String repository, String groupId, String artifactId, String version, String classifier, String extension) {
338 this();
339 this.repository = repository;
340 this.groupId = groupId;
341 this.artifactId = artifactId;
342 this.version = version;
343 this.classifier = classifier;
344 this.fileExtension = extension;
345 }
346
347 public Version getArtifactVersion() {
348 if (artifactVersion == null) {
349 try {
350 artifactVersion = versionScheme.parseVersion(version);
351 } catch (InvalidVersionSpecificationException e) {
352
353
354 }
355 }
356
357 return artifactVersion;
358 }
359
360 public float getLuceneScore() {
361 return luceneScore;
362 }
363
364 public void setLuceneScore(float score) {
365 this.luceneScore = score;
366 }
367
368 public String getUinfo() {
369 return groupId + FS + artifactId + FS + version + FS + nvl(classifier) + FS + fileExtension;
370
371
372 }
373
374 public String getRootGroup() {
375 int n = groupId.indexOf('.');
376 if (n > -1) {
377 return groupId.substring(0, n);
378 }
379 return groupId;
380 }
381
382 public Gav calculateGav() {
383 return new Gav(
384 groupId,
385 artifactId,
386 version,
387 classifier,
388 fileExtension,
389 null,
390 null,
391 fileName,
392 false,
393 null,
394 false,
395 null);
396 }
397
398 public Map<String, String> getAttributes() {
399 return attributes;
400 }
401
402 public List<MatchHighlight> getMatchHighlights() {
403 return matchHighlights;
404 }
405
406 @Override
407 public String toString() {
408 final StringBuilder result = new StringBuilder(getUinfo());
409 String packaging = getPackaging();
410 if (packaging != null && !getPackaging().isEmpty()) {
411 result.append("[").append(getPackaging()).append("]");
412 }
413 return result.toString();
414 }
415
416 private static final List<Field> DEFAULT_FIELDS = new ArrayList<>();
417
418 static {
419 DEFAULT_FIELDS.add(MAVEN.GROUP_ID);
420 DEFAULT_FIELDS.add(MAVEN.ARTIFACT_ID);
421 DEFAULT_FIELDS.add(MAVEN.VERSION);
422 DEFAULT_FIELDS.add(MAVEN.PACKAGING);
423 DEFAULT_FIELDS.add(MAVEN.CLASSIFIER);
424 DEFAULT_FIELDS.add(MAVEN.SHA1);
425 DEFAULT_FIELDS.add(MAVEN.NAME);
426 DEFAULT_FIELDS.add(MAVEN.DESCRIPTION);
427 DEFAULT_FIELDS.add(MAVEN.CLASSNAMES);
428 DEFAULT_FIELDS.add(MAVEN.REPOSITORY_ID);
429 }
430
431 private List<Field> fields;
432
433 public Collection<Field> getFields() {
434 if (fields == null) {
435 fields = new ArrayList<>(DEFAULT_FIELDS.size());
436
437 fields.addAll(DEFAULT_FIELDS);
438 }
439
440 return fields;
441 }
442
443
444
445
446
447
448
449 public String getFieldValue(Field field) {
450 if (MAVEN.GROUP_ID.equals(field)) {
451 return groupId;
452 } else if (MAVEN.ARTIFACT_ID.equals(field)) {
453 return artifactId;
454 } else if (MAVEN.VERSION.equals(field)) {
455 return version;
456 } else if (MAVEN.PACKAGING.equals(field)) {
457 return packaging;
458 } else if (MAVEN.CLASSIFIER.equals(field)) {
459 return classifier;
460 } else if (MAVEN.SHA1.equals(field)) {
461 return sha1;
462 } else if (MAVEN.NAME.equals(field)) {
463 return name;
464 } else if (MAVEN.DESCRIPTION.equals(field)) {
465 return description;
466 } else if (MAVEN.CLASSNAMES.equals(field)) {
467 return classNames;
468 } else if (MAVEN.REPOSITORY_ID.equals(field)) {
469 return repository;
470 }
471
472
473 return null;
474 }
475
476 public ArtifactInfo setFieldValue(Field field, String value) {
477 if (MAVEN.GROUP_ID.equals(field)) {
478 groupId = value;
479 } else if (MAVEN.ARTIFACT_ID.equals(field)) {
480 artifactId = value;
481 } else if (MAVEN.VERSION.equals(field)) {
482 version = value;
483 } else if (MAVEN.PACKAGING.equals(field)) {
484 packaging = value;
485 } else if (MAVEN.CLASSIFIER.equals(field)) {
486 classifier = value;
487 } else if (MAVEN.SHA1.equals(field)) {
488 sha1 = value;
489 } else if (MAVEN.NAME.equals(field)) {
490 name = value;
491 } else if (MAVEN.DESCRIPTION.equals(field)) {
492 description = value;
493 } else if (MAVEN.CLASSNAMES.equals(field)) {
494 classNames = value;
495 } else if (MAVEN.REPOSITORY_ID.equals(field)) {
496 repository = value;
497 }
498
499
500 return this;
501 }
502
503
504
505
506
507 public static String nvl(String v) {
508 return v == null ? NA : v;
509 }
510
511 public static String renvl(String v) {
512 return NA.equals(v) ? null : v;
513 }
514
515 public static String lst2str(Collection<String> list) {
516 StringBuilder sb = new StringBuilder();
517 for (String s : list) {
518 sb.append(s).append(ArtifactInfo.FS);
519 }
520 return sb.length() == 0 ? sb.toString() : sb.substring(0, sb.length() - 1);
521 }
522
523 public static List<String> str2lst(String str) {
524 return Arrays.asList(ArtifactInfo.FS_PATTERN.split(str));
525 }
526
527
528
529
530 static class VersionComparator implements Comparator<ArtifactInfo> {
531 public int compare(final ArtifactInfo f1, final ArtifactInfo f2) {
532 int n = f1.groupId.compareTo(f2.groupId);
533 if (n != 0) {
534 return n;
535 }
536
537 n = f1.artifactId.compareTo(f2.artifactId);
538 if (n != 0) {
539 return n;
540 }
541
542 n = -f1.getArtifactVersion().compareTo(f2.getArtifactVersion());
543 if (n != 0) {
544 return n;
545 }
546
547 {
548 final String c1 = f1.classifier;
549 final String c2 = f2.classifier;
550 if (c1 == null) {
551 if (c2 != null) {
552 return -1;
553 }
554 } else {
555 if (c2 == null) {
556 return 1;
557 }
558
559 n = c1.compareTo(c2);
560 if (n != 0) {
561 return n;
562 }
563 }
564 }
565
566 {
567 final String p1 = f1.packaging;
568 final String p2 = f2.packaging;
569 if (p1 == null) {
570 return p2 == null ? 0 : -1;
571 } else {
572 return p2 == null ? 1 : p1.compareTo(p2);
573 }
574 }
575 }
576 }
577
578
579
580
581 static class RepositoryVersionComparator extends VersionComparator {
582 @Override
583 public int compare(final ArtifactInfo f1, final ArtifactInfo f2) {
584 final int n = super.compare(f1, f2);
585 if (n != 0) {
586 return n;
587 }
588
589 final String r1 = f1.repository;
590 final String r2 = f2.repository;
591 if (r1 == null) {
592 return r2 == null ? 0 : -1;
593 } else {
594 return r2 == null ? 1 : r1.compareTo(r2);
595 }
596 }
597 }
598
599
600
601
602 static class ContextVersionComparator extends VersionComparator {
603 @Override
604 public int compare(final ArtifactInfo f1, final ArtifactInfo f2) {
605 final int n = super.compare(f1, f2);
606 if (n != 0) {
607 return n;
608 }
609
610 final String r1 = f1.context;
611 final String r2 = f2.context;
612 if (r1 == null) {
613 return r2 == null ? 0 : -1;
614 } else {
615 return r2 == null ? 1 : r1.compareTo(r2);
616 }
617 }
618 }
619
620 public String getFileName() {
621 return fileName;
622 }
623
624 public void setFileName(String fileName) {
625 this.fileName = fileName;
626 }
627
628 public String getFileExtension() {
629 return fileExtension;
630 }
631
632 public void setFileExtension(String fileExtension) {
633 this.fileExtension = fileExtension;
634 }
635
636 public String getGroupId() {
637 return groupId;
638 }
639
640 public void setGroupId(String groupId) {
641 this.groupId = groupId;
642 }
643
644 public String getArtifactId() {
645 return artifactId;
646 }
647
648 public void setArtifactId(String artifactId) {
649 this.artifactId = artifactId;
650 }
651
652 public String getVersion() {
653 return version;
654 }
655
656 public void setVersion(String version) {
657 this.version = version;
658 }
659
660 public void setArtifactVersion(Version artifactVersion) {
661 this.artifactVersion = artifactVersion;
662 }
663
664 public String getClassifier() {
665 return classifier;
666 }
667
668 public void setClassifier(String classifier) {
669 this.classifier = classifier;
670 }
671
672 public String getPackaging() {
673 return packaging;
674 }
675
676 public void setPackaging(String packaging) {
677 this.packaging = packaging;
678 }
679
680 public String getName() {
681 return name;
682 }
683
684 public void setName(String name) {
685 this.name = name;
686 }
687
688 public String getDescription() {
689 return description;
690 }
691
692 public void setDescription(String description) {
693 this.description = description;
694 }
695
696 public long getLastModified() {
697 return lastModified;
698 }
699
700 public void setLastModified(long lastModified) {
701 this.lastModified = lastModified;
702 }
703
704 public long getSize() {
705 return size;
706 }
707
708 public void setSize(long size) {
709 this.size = size;
710 }
711
712 public String getMd5() {
713 return md5;
714 }
715
716 public void setMd5(String md5) {
717 this.md5 = md5;
718 }
719
720 public String getSha1() {
721 return sha1;
722 }
723
724 public void setSha1(String sha1) {
725 this.sha1 = sha1;
726 }
727
728 public ArtifactAvailability getSourcesExists() {
729 return sourcesExists;
730 }
731
732 public void setSourcesExists(ArtifactAvailability sourcesExists) {
733 this.sourcesExists = sourcesExists;
734 }
735
736 public ArtifactAvailability getJavadocExists() {
737 return javadocExists;
738 }
739
740 public void setJavadocExists(ArtifactAvailability javadocExists) {
741 this.javadocExists = javadocExists;
742 }
743
744 public ArtifactAvailability getSignatureExists() {
745 return signatureExists;
746 }
747
748 public void setSignatureExists(ArtifactAvailability signatureExists) {
749 this.signatureExists = signatureExists;
750 }
751
752 public String getClassNames() {
753 return classNames;
754 }
755
756 public void setClassNames(String classNames) {
757 this.classNames = classNames;
758 }
759
760 public String getRepository() {
761 return repository;
762 }
763
764 public void setRepository(String repository) {
765 this.repository = repository;
766 }
767
768 public String getPath() {
769 return path;
770 }
771
772 public void setPath(String path) {
773 this.path = path;
774 }
775
776 public String getRemoteUrl() {
777 return remoteUrl;
778 }
779
780 public void setRemoteUrl(String remoteUrl) {
781 this.remoteUrl = remoteUrl;
782 }
783
784 public String getContext() {
785 return context;
786 }
787
788 public void setContext(String context) {
789 this.context = context;
790 }
791
792 public String getPrefix() {
793 return prefix;
794 }
795
796 public void setPrefix(String prefix) {
797 this.prefix = prefix;
798 }
799
800 public List<String> getGoals() {
801 return goals;
802 }
803
804 public void setGoals(List<String> goals) {
805 this.goals = goals;
806 }
807
808 public String getBundleVersion() {
809 return bundleVersion;
810 }
811
812 public void setBundleVersion(String bundleVersion) {
813 this.bundleVersion = bundleVersion;
814 }
815
816 public String getBundleSymbolicName() {
817 return bundleSymbolicName;
818 }
819
820 public void setBundleSymbolicName(String bundleSymbolicName) {
821 this.bundleSymbolicName = bundleSymbolicName;
822 }
823
824 public String getBundleExportPackage() {
825 return bundleExportPackage;
826 }
827
828 public void setBundleExportPackage(String bundleExportPackage) {
829 this.bundleExportPackage = bundleExportPackage;
830 }
831
832 public String getBundleExportService() {
833 return bundleExportService;
834 }
835
836 public void setBundleExportService(String bundleExportService) {
837 this.bundleExportService = bundleExportService;
838 }
839
840 public String getBundleDescription() {
841 return bundleDescription;
842 }
843
844 public void setBundleDescription(String bundleDescription) {
845 this.bundleDescription = bundleDescription;
846 }
847
848 public String getBundleName() {
849 return bundleName;
850 }
851
852 public void setBundleName(String bundleName) {
853 this.bundleName = bundleName;
854 }
855
856 public String getBundleLicense() {
857 return bundleLicense;
858 }
859
860 public void setBundleLicense(String bundleLicense) {
861 this.bundleLicense = bundleLicense;
862 }
863
864 public String getBundleDocUrl() {
865 return bundleDocUrl;
866 }
867
868 public void setBundleDocUrl(String bundleDocUrl) {
869 this.bundleDocUrl = bundleDocUrl;
870 }
871
872 public String getBundleImportPackage() {
873 return bundleImportPackage;
874 }
875
876 public void setBundleImportPackage(String bundleImportPackage) {
877 this.bundleImportPackage = bundleImportPackage;
878 }
879
880 public String getBundleRequireBundle() {
881 return bundleRequireBundle;
882 }
883
884 public void setBundleRequireBundle(String bundleRequireBundle) {
885 this.bundleRequireBundle = bundleRequireBundle;
886 }
887
888 public VersionScheme getVersionScheme() {
889 return versionScheme;
890 }
891
892 public void setFields(List<Field> fields) {
893 this.fields = fields;
894 }
895
896 public String getBundleProvideCapability() {
897 return bundleProvideCapability;
898 }
899
900 public void setBundleProvideCapability(String bundleProvideCapability) {
901 this.bundleProvideCapability = bundleProvideCapability;
902 }
903
904 public String getBundleRequireCapability() {
905 return bundleRequireCapability;
906 }
907
908 public void setBundleRequireCapability(String bundleRequireCapability) {
909 this.bundleRequireCapability = bundleRequireCapability;
910 }
911
912 public String getSha256() {
913 return sha256;
914 }
915
916 public void setSha256(String sha256) {
917 this.sha256 = sha256;
918 }
919
920 public String getBundleFragmentHost() {
921 return bundleFragmentHost;
922 }
923
924 public void setBundleFragmentHost(String bundleFragmentHost) {
925 this.bundleFragmentHost = bundleFragmentHost;
926 }
927
928 public String getBundleRequiredExecutionEnvironment() {
929 return bundleRequiredExecutionEnvironment;
930 }
931
932 public void setBundleRequiredExecutionEnvironment(String bundleRequiredExecutionEnvironment) {
933 this.bundleRequiredExecutionEnvironment = bundleRequiredExecutionEnvironment;
934 }
935 }