1 package org.apache.maven.index;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.maven.index.artifact.Gav;
31 import org.apache.maven.index.creator.JarFileContentsIndexCreator;
32 import org.apache.maven.index.creator.MavenPluginArtifactInfoIndexCreator;
33 import org.apache.maven.index.creator.MinimalArtifactInfoIndexCreator;
34 import org.apache.maven.index.creator.OsgiArtifactIndexCreator;
35 import org.codehaus.plexus.util.StringUtils;
36 import org.sonatype.aether.util.version.GenericVersionScheme;
37 import org.sonatype.aether.version.InvalidVersionSpecificationException;
38 import org.sonatype.aether.version.Version;
39 import org.sonatype.aether.version.VersionScheme;
40
41
42
43
44
45
46
47
48 public class ArtifactInfo
49 extends ArtifactInfoRecord
50 {
51 private static final long serialVersionUID = 6028843453477511104L;
52
53
54
55 public static final String ROOT_GROUPS = "rootGroups";
56
57 public static final String ROOT_GROUPS_VALUE = "rootGroups";
58
59 public static final String ROOT_GROUPS_LIST = "rootGroupsList";
60
61 public static final String ALL_GROUPS = "allGroups";
62
63 public static final String ALL_GROUPS_VALUE = "allGroups";
64
65 public static final String ALL_GROUPS_LIST = "allGroupsList";
66
67
68
69
70
71
72 public static final String UINFO = FLD_UINFO.getKey();
73
74
75
76
77 public static final String DELETED = FLD_DELETED.getKey();
78
79
80
81
82 public static final String GROUP_ID = MinimalArtifactInfoIndexCreator.FLD_GROUP_ID_KW.getKey();
83
84
85
86
87 public static final String ARTIFACT_ID = MinimalArtifactInfoIndexCreator.FLD_ARTIFACT_ID_KW.getKey();
88
89
90
91
92 public static final String VERSION = MinimalArtifactInfoIndexCreator.FLD_VERSION_KW.getKey();
93
94
95
96
97 public static final String PACKAGING = MinimalArtifactInfoIndexCreator.FLD_PACKAGING.getKey();
98
99
100
101
102 public static final String CLASSIFIER = MinimalArtifactInfoIndexCreator.FLD_CLASSIFIER.getKey();
103
104
105
106
107 public static final String INFO = MinimalArtifactInfoIndexCreator.FLD_INFO.getKey();
108
109
110
111
112 public static final String NAME = MinimalArtifactInfoIndexCreator.FLD_NAME.getKey();
113
114
115
116
117 public static final String DESCRIPTION = MinimalArtifactInfoIndexCreator.FLD_DESCRIPTION.getKey();
118
119
120
121
122 public static final String LAST_MODIFIED = MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey();
123
124
125
126
127 public static final String SHA1 = MinimalArtifactInfoIndexCreator.FLD_SHA1.getKey();
128
129
130
131
132 public static final String NAMES = JarFileContentsIndexCreator.FLD_CLASSNAMES_KW.getKey();
133
134
135
136
137 public static final String PLUGIN_PREFIX = MavenPluginArtifactInfoIndexCreator.FLD_PLUGIN_PREFIX.getKey();
138
139
140
141
142 public static final String PLUGIN_GOALS = MavenPluginArtifactInfoIndexCreator.FLD_PLUGIN_GOALS.getKey();
143
144
145
146
147
148 public static final String BUNDLE_SYMBOLIC_NAME = OsgiArtifactIndexCreator.FLD_BUNDLE_SYMBOLIC_NAME.getKey();
149
150
151
152
153 public static final String BUNDLE_VERSION = OsgiArtifactIndexCreator.FLD_BUNDLE_VERSION.getKey();
154
155
156
157
158 public static final String BUNDLE_EXPORT_PACKAGE = OsgiArtifactIndexCreator.FLD_BUNDLE_EXPORT_PACKAGE.getKey();
159
160 public static final Comparator<ArtifactInfo> VERSION_COMPARATOR = new VersionComparator();
161
162 public static final Comparator<ArtifactInfo> REPOSITORY_VERSION_COMPARATOR = new RepositoryVersionComparator();
163
164 public static final Comparator<ArtifactInfo> CONTEXT_VERSION_COMPARATOR = new ContextVersionComparator();
165
166 public String fname;
167
168 public String fextension;
169
170 public String groupId;
171
172 public String artifactId;
173
174 public String version;
175
176 private transient Version artifactVersion;
177
178 private transient float luceneScore;
179
180 public String classifier;
181
182
183
184
185 public String packaging;
186
187 public String name;
188
189 public String description;
190
191 public long lastModified = -1;
192
193 public long size = -1;
194
195 public String md5;
196
197 public String sha1;
198
199 public ArtifactAvailablility sourcesExists = ArtifactAvailablility.NOT_PRESENT;
200
201 public ArtifactAvailablility javadocExists = ArtifactAvailablility.NOT_PRESENT;
202
203 public ArtifactAvailablility signatureExists = ArtifactAvailablility.NOT_PRESENT;
204
205 public String classNames;
206
207 public String repository;
208
209 public String path;
210
211 public String remoteUrl;
212
213 public String context;
214
215
216
217
218 public String prefix;
219
220
221
222
223 public List<String> goals;
224
225
226
227
228
229 public String bundleVersion;
230
231
232
233
234
235 public String bundleSymbolicName;
236
237
238
239
240
241 public String bundleExportPackage;
242
243
244
245
246
247 public String bundleExportService;
248
249
250
251
252
253 public String bundleDescription;
254
255
256
257
258
259 public String bundleName;
260
261
262
263
264
265 public String bundleLicense;
266
267
268
269
270
271 public String bundleDocUrl;
272
273
274
275
276
277 public String bundleImportPackage;
278
279
280
281
282
283 public String bundleRequireBundle;
284
285 private String uinfo = null;
286
287 private final Map<String, String> attributes = new HashMap<String, String>();
288
289 private final List<MatchHighlight> matchHighlights = new ArrayList<MatchHighlight>();
290
291 private final transient VersionScheme versionScheme;
292
293 public ArtifactInfo()
294 {
295 versionScheme = new GenericVersionScheme();
296 }
297
298 public ArtifactInfo( String repository, String groupId, String artifactId, String version, String classifier )
299 {
300 this();
301 this.repository = repository;
302 this.groupId = groupId;
303 this.artifactId = artifactId;
304 this.version = version;
305 this.classifier = classifier;
306 }
307
308 public Version getArtifactVersion()
309 {
310 if ( artifactVersion == null )
311 {
312 try
313 {
314 artifactVersion = versionScheme.parseVersion( version );
315 }
316 catch ( InvalidVersionSpecificationException e )
317 {
318
319
320 }
321 }
322
323 return artifactVersion;
324 }
325
326 public float getLuceneScore()
327 {
328 return luceneScore;
329 }
330
331 public void setLuceneScore( float score )
332 {
333 this.luceneScore = score;
334 }
335
336 public String getUinfo()
337 {
338 if ( uinfo == null )
339 {
340 uinfo = new StringBuilder()
341 .append( groupId ).append( FS )
342 .append( artifactId ).append( FS )
343 .append( version ).append( FS )
344 .append( nvl( classifier ) )
345 .append( StringUtils.isEmpty( classifier ) || StringUtils.isEmpty( packaging ) ? "" : FS + packaging )
346 .toString();
347 }
348
349 return uinfo;
350 }
351
352 public String getRootGroup()
353 {
354 int n = groupId.indexOf( '.' );
355 if ( n > -1 )
356 {
357 return groupId.substring( 0, n );
358 }
359 return groupId;
360 }
361
362 public Gav calculateGav()
363 {
364 return new Gav( groupId, artifactId, version, classifier, fextension, null,
365 null,
366 fname,
367 false,
368 null,
369 false,
370 null );
371 }
372
373 public Map<String, String> getAttributes()
374 {
375 return attributes;
376 }
377
378 public List<MatchHighlight> getMatchHighlights()
379 {
380 return matchHighlights;
381 }
382
383 @Override
384 public String toString()
385 {
386 return new StringBuilder( groupId ).append( ':' ).append( artifactId )
387 .append( ':' ).append( version )
388 .append( ':' ).append( classifier )
389 .append( ':' ).append( packaging ).toString();
390 }
391
392 private static final List<Field> DEFAULT_FIELDS = new ArrayList<Field>();
393 static
394 {
395 DEFAULT_FIELDS.add( MAVEN.GROUP_ID );
396 DEFAULT_FIELDS.add( MAVEN.ARTIFACT_ID );
397 DEFAULT_FIELDS.add( MAVEN.VERSION );
398 DEFAULT_FIELDS.add( MAVEN.PACKAGING );
399 DEFAULT_FIELDS.add( MAVEN.CLASSIFIER );
400 DEFAULT_FIELDS.add( MAVEN.SHA1 );
401 DEFAULT_FIELDS.add( MAVEN.NAME );
402 DEFAULT_FIELDS.add( MAVEN.DESCRIPTION );
403 DEFAULT_FIELDS.add( MAVEN.CLASSNAMES );
404 DEFAULT_FIELDS.add( MAVEN.REPOSITORY_ID );
405 }
406
407 private List<Field> fields;
408
409 public Collection<Field> getFields()
410 {
411 if ( fields == null )
412 {
413 fields = new ArrayList<Field>( DEFAULT_FIELDS.size() );
414
415 fields.addAll( DEFAULT_FIELDS );
416 }
417
418 return fields;
419 }
420
421
422
423
424
425
426
427 public String getFieldValue( Field field )
428 {
429 if ( MAVEN.GROUP_ID.equals( field ) )
430 {
431 return groupId;
432 }
433 else if ( MAVEN.ARTIFACT_ID.equals( field ) )
434 {
435 return artifactId;
436 }
437 else if ( MAVEN.VERSION.equals( field ) )
438 {
439 return version;
440 }
441 else if ( MAVEN.PACKAGING.equals( field ) )
442 {
443 return packaging;
444 }
445 else if ( MAVEN.CLASSIFIER.equals( field ) )
446 {
447 return classifier;
448 }
449 else if ( MAVEN.SHA1.equals( field ) )
450 {
451 return sha1;
452 }
453 else if ( MAVEN.NAME.equals( field ) )
454 {
455 return name;
456 }
457 else if ( MAVEN.DESCRIPTION.equals( field ) )
458 {
459 return description;
460 }
461 else if ( MAVEN.CLASSNAMES.equals( field ) )
462 {
463 return classNames;
464 }
465 else if ( MAVEN.REPOSITORY_ID.equals( field ) )
466 {
467 return repository;
468 }
469
470
471 return null;
472 }
473
474 public ArtifactInfo setFieldValue( Field field, String value )
475 {
476 if ( MAVEN.GROUP_ID.equals( field ) )
477 {
478 groupId = value;
479 }
480 else if ( MAVEN.ARTIFACT_ID.equals( field ) )
481 {
482 artifactId = value;
483 }
484 else if ( MAVEN.VERSION.equals( field ) )
485 {
486 version = value;
487 }
488 else if ( MAVEN.PACKAGING.equals( field ) )
489 {
490 packaging = value;
491 }
492 else if ( MAVEN.CLASSIFIER.equals( field ) )
493 {
494 classifier = value;
495 }
496 else if ( MAVEN.SHA1.equals( field ) )
497 {
498 sha1 = value;
499 }
500 else if ( MAVEN.NAME.equals( field ) )
501 {
502 name = value;
503 }
504 else if ( MAVEN.DESCRIPTION.equals( field ) )
505 {
506 description = value;
507 }
508 else if ( MAVEN.CLASSNAMES.equals( field ) )
509 {
510 classNames = value;
511 }
512 else if ( MAVEN.REPOSITORY_ID.equals( field ) )
513 {
514 repository = value;
515 }
516
517
518 return this;
519 }
520
521
522
523
524
525 public static String nvl( String v )
526 {
527 return v == null ? NA : v;
528 }
529
530 public static String renvl( String v )
531 {
532 return NA.equals( v ) ? null : v;
533 }
534
535 public static String lst2str( Collection<String> list )
536 {
537 StringBuilder sb = new StringBuilder();
538 for ( String s : list )
539 {
540 sb.append( s ).append( ArtifactInfo.FS );
541 }
542 return sb.length() == 0 ? sb.toString() : sb.substring( 0, sb.length() - 1 );
543 }
544
545 public static List<String> str2lst( String str )
546 {
547 return Arrays.asList( ArtifactInfo.FS_PATTERN.split( str ) );
548 }
549
550
551
552
553 static class VersionComparator
554 implements Comparator<ArtifactInfo>
555 {
556 public int compare( final ArtifactInfo f1, final ArtifactInfo f2 )
557 {
558 int n = f1.groupId.compareTo( f2.groupId );
559 if ( n != 0 )
560 {
561 return n;
562 }
563
564 n = f1.artifactId.compareTo( f2.artifactId );
565 if ( n != 0 )
566 {
567 return n;
568 }
569
570 n = -f1.getArtifactVersion().compareTo( f2.getArtifactVersion() );
571 if ( n != 0 )
572 {
573 return n;
574 }
575
576 {
577 final String c1 = f1.classifier;
578 final String c2 = f2.classifier;
579 if ( c1 == null )
580 {
581 if ( c2 != null )
582 {
583 return -1;
584 }
585 }
586 else
587 {
588 if ( c2 == null )
589 {
590 return 1;
591 }
592
593 n = c1.compareTo( c2 );
594 if ( n != 0 )
595 {
596 return n;
597 }
598 }
599 }
600
601 {
602 final String p1 = f1.packaging;
603 final String p2 = f2.packaging;
604 if ( p1 == null )
605 {
606 return p2 == null ? 0 : -1;
607 }
608 else
609 {
610 return p2 == null ? 1 : p1.compareTo( p2 );
611 }
612 }
613 }
614 }
615
616
617
618
619 static class RepositoryVersionComparator
620 extends VersionComparator
621 {
622 @Override
623 public int compare( final ArtifactInfo f1, final ArtifactInfo f2 )
624 {
625 final int n = super.compare( f1, f2 );
626 if ( n != 0 )
627 {
628 return n;
629 }
630
631 final String r1 = f1.repository;
632 final String r2 = f2.repository;
633 if ( r1 == null )
634 {
635 return r2 == null ? 0 : -1;
636 }
637 else
638 {
639 return r2 == null ? 1 : r1.compareTo( r2 );
640 }
641 }
642 }
643
644
645
646
647 static class ContextVersionComparator
648 extends VersionComparator
649 {
650 @Override
651 public int compare( final ArtifactInfo f1, final ArtifactInfo f2 )
652 {
653 final int n = super.compare( f1, f2 );
654 if ( n != 0 )
655 {
656 return n;
657 }
658
659 final String r1 = f1.context;
660 final String r2 = f2.context;
661 if ( r1 == null )
662 {
663 return r2 == null ? 0 : -1;
664 }
665 else
666 {
667 return r2 == null ? 1 : r1.compareTo( r2 );
668 }
669 }
670 }
671
672 }