KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > DefaultArtifact


1 package org.apache.maven.artifact;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.handler.ArtifactHandler;
20 import org.apache.maven.artifact.metadata.ArtifactMetadata;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
23 import org.apache.maven.artifact.versioning.ArtifactVersion;
24 import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
25 import org.apache.maven.artifact.versioning.VersionRange;
26 import org.codehaus.plexus.util.StringUtils;
27
28 import java.io.File JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.regex.Matcher JavaDoc;
35
36 /**
37  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl </a>
38  * @version $Id: DefaultArtifact.java 379533 2006-02-21 17:05:38Z jvanzyl $
39  * @todo this should possibly be replaced by type handler
40  */

41 public class DefaultArtifact
42     implements Artifact
43 {
44     private String JavaDoc groupId;
45
46     private String JavaDoc artifactId;
47
48     /**
49      * The resolved version for the artifact after conflict resolution, that has not been transformed.
50      *
51      * @todo should be final
52      */

53     private String JavaDoc baseVersion;
54
55     private final String JavaDoc type;
56
57     private final String JavaDoc classifier;
58
59     private String JavaDoc scope;
60
61     private File JavaDoc file;
62
63     private ArtifactRepository repository;
64
65     private String JavaDoc downloadUrl;
66
67     private ArtifactFilter dependencyFilter;
68
69     private ArtifactHandler artifactHandler;
70
71     private List JavaDoc dependencyTrail;
72
73     private String JavaDoc version;
74
75     private VersionRange versionRange;
76
77     private boolean resolved;
78
79     private boolean release;
80
81     private List JavaDoc availableVersions;
82
83     private Map JavaDoc metadataMap;
84
85     private boolean optional;
86
87     public DefaultArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange, String JavaDoc scope, String JavaDoc type,
88                             String JavaDoc classifier, ArtifactHandler artifactHandler )
89     {
90         this( groupId, artifactId, versionRange, scope, type, classifier, artifactHandler, false );
91     }
92
93     public DefaultArtifact( String JavaDoc groupId, String JavaDoc artifactId, VersionRange versionRange, String JavaDoc scope, String JavaDoc type,
94                             String JavaDoc classifier, ArtifactHandler artifactHandler, boolean optional )
95     {
96         this.groupId = groupId;
97
98         this.artifactId = artifactId;
99
100         this.versionRange = versionRange;
101         
102         selectVersionFromNewRangeIfAvailable();
103
104         this.artifactHandler = artifactHandler;
105
106         this.scope = scope;
107
108         this.type = type;
109
110         if ( classifier == null )
111         {
112             classifier = artifactHandler.getClassifier();
113         }
114
115         this.classifier = classifier;
116
117         this.optional = optional;
118
119         validateIdentity();
120     }
121
122     private void validateIdentity()
123     {
124         if ( empty( groupId ) )
125         {
126             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
127                                                   "The groupId cannot be empty." );
128         }
129
130         if ( artifactId == null )
131         {
132             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
133                                                   "The artifactId cannot be empty." );
134         }
135
136         if ( type == null )
137         {
138             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
139                                                   "The type cannot be empty." );
140         }
141
142         if ( version == null && versionRange == null )
143         {
144             throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type,
145                                                   "The version cannot be empty." );
146         }
147     }
148
149     private boolean empty( String JavaDoc value )
150     {
151         return value == null || value.trim().length() < 1;
152     }
153
154     public String JavaDoc getClassifier()
155     {
156         return classifier;
157     }
158
159     public boolean hasClassifier()
160     {
161         return StringUtils.isNotEmpty( classifier );
162     }
163
164     public String JavaDoc getScope()
165     {
166         return scope;
167     }
168
169     public String JavaDoc getGroupId()
170     {
171         return groupId;
172     }
173
174     public String JavaDoc getArtifactId()
175     {
176         return artifactId;
177     }
178
179     public String JavaDoc getVersion()
180     {
181         return version;
182     }
183
184     public void setVersion( String JavaDoc version )
185     {
186         this.version = version;
187         this.baseVersion = version;
188         this.versionRange = null;
189     }
190
191     public String JavaDoc getType()
192     {
193         return type;
194     }
195
196     public void setFile( File JavaDoc file )
197     {
198         this.file = file;
199     }
200
201     public File JavaDoc getFile()
202     {
203         return file;
204     }
205
206     public ArtifactRepository getRepository()
207     {
208         return repository;
209     }
210
211     public void setRepository( ArtifactRepository repository )
212     {
213         this.repository = repository;
214     }
215
216     // ----------------------------------------------------------------------
217
//
218
// ----------------------------------------------------------------------
219

220     public String JavaDoc getId()
221     {
222         return getDependencyConflictId() + ":" + getBaseVersion();
223     }
224
225     public String JavaDoc getDependencyConflictId()
226     {
227         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
228         sb.append( getGroupId() );
229         sb.append( ":" );
230         appendArtifactTypeClassifierString( sb );
231         return sb.toString();
232     }
233
234     private void appendArtifactTypeClassifierString( StringBuffer JavaDoc sb )
235     {
236         sb.append( getArtifactId() );
237         sb.append( ":" );
238         sb.append( getType() );
239         if ( hasClassifier() )
240         {
241             sb.append( ":" );
242             sb.append( getClassifier() );
243         }
244     }
245
246     public void addMetadata( ArtifactMetadata metadata )
247     {
248         if ( metadataMap == null )
249         {
250             metadataMap = new HashMap JavaDoc();
251         }
252
253         ArtifactMetadata m = (ArtifactMetadata) metadataMap.get( metadata.getKey() );
254         if ( m != null )
255         {
256             m.merge( metadata );
257         }
258         else
259         {
260             metadataMap.put( metadata.getKey(), metadata );
261         }
262     }
263
264     public Collection JavaDoc getMetadataList()
265     {
266         return metadataMap == null ? Collections.EMPTY_LIST : metadataMap.values();
267     }
268
269     // ----------------------------------------------------------------------
270
// Object overrides
271
// ----------------------------------------------------------------------
272

273     public String JavaDoc toString()
274     {
275         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
276         if ( getGroupId() != null )
277         {
278             sb.append( getGroupId() );
279             sb.append( ":" );
280         }
281         appendArtifactTypeClassifierString( sb );
282         sb.append( ":" );
283         if ( version != null || baseVersion != null )
284         {
285             sb.append( getBaseVersion() );
286         }
287         else
288         {
289             sb.append( versionRange.toString() );
290         }
291         if ( scope != null )
292         {
293             sb.append( ":" );
294             sb.append( scope );
295         }
296         return sb.toString();
297     }
298
299     public int hashCode()
300     {
301         int result = 17;
302         result = 37 * result + groupId.hashCode();
303         result = 37 * result + artifactId.hashCode();
304         result = 37 * result + type.hashCode();
305         if ( version != null )
306         {
307             result = 37 * result + version.hashCode();
308         }
309         result = 37 * result + ( classifier != null ? classifier.hashCode() : 0 );
310         return result;
311     }
312
313     public boolean equals( Object JavaDoc o )
314     {
315         if ( o == this )
316         {
317             return true;
318         }
319
320         if ( !( o instanceof Artifact ) )
321         {
322             return false;
323         }
324
325         Artifact a = (Artifact) o;
326
327         if ( !a.getGroupId().equals( groupId ) )
328         {
329             return false;
330         }
331         else if ( !a.getArtifactId().equals( artifactId ) )
332         {
333             return false;
334         }
335         else if ( !a.getVersion().equals( version ) )
336         {
337             return false;
338         }
339         else if ( !a.getType().equals( type ) )
340         {
341             return false;
342         }
343         else if ( a.getClassifier() == null ? classifier != null : !a.getClassifier().equals( classifier ) )
344         {
345             return false;
346         }
347
348         // We don't consider the version range in the comparison, just the resolved version
349

350         return true;
351     }
352
353     public String JavaDoc getBaseVersion()
354     {
355         if ( baseVersion == null )
356         {
357             baseVersion = version;
358
359             if ( version == null )
360             {
361                 throw new NullPointerException JavaDoc( "version was null for " + groupId + ":" + artifactId );
362             }
363         }
364         return baseVersion;
365     }
366
367     public void setBaseVersion( String JavaDoc baseVersion )
368     {
369         this.baseVersion = baseVersion;
370     }
371
372     public int compareTo( Object JavaDoc o )
373     {
374         Artifact a = (Artifact) o;
375
376         int result = groupId.compareTo( a.getGroupId() );
377         if ( result == 0 )
378         {
379             result = artifactId.compareTo( a.getArtifactId() );
380             if ( result == 0 )
381             {
382                 result = type.compareTo( a.getType() );
383                 if ( result == 0 )
384                 {
385                     if ( classifier == null )
386                     {
387                         if ( a.getClassifier() != null )
388                         {
389                             result = 1;
390                         }
391                     }
392                     else
393                     {
394                         if ( a.getClassifier() != null )
395                         {
396                             result = classifier.compareTo( a.getClassifier() );
397                         }
398                         else
399                         {
400                             result = -1;
401                         }
402                     }
403                     if ( result == 0 )
404                     {
405                         // We don't consider the version range in the comparison, just the resolved version
406
result = version.compareTo( a.getVersion() );
407                     }
408                 }
409             }
410         }
411         return result;
412     }
413
414     public void updateVersion( String JavaDoc version, ArtifactRepository localRepository )
415     {
416         setResolvedVersion( version );
417         setFile( new File JavaDoc( localRepository.getBasedir(), localRepository.pathOf( this ) ) );
418     }
419
420     public String JavaDoc getDownloadUrl()
421     {
422         return downloadUrl;
423     }
424
425     public void setDownloadUrl( String JavaDoc downloadUrl )
426     {
427         this.downloadUrl = downloadUrl;
428     }
429
430     public ArtifactFilter getDependencyFilter()
431     {
432         return dependencyFilter;
433     }
434
435     public void setDependencyFilter( ArtifactFilter artifactFilter )
436     {
437         this.dependencyFilter = artifactFilter;
438     }
439
440     public ArtifactHandler getArtifactHandler()
441     {
442         return artifactHandler;
443     }
444
445     public List JavaDoc getDependencyTrail()
446     {
447         return dependencyTrail;
448     }
449
450     public void setDependencyTrail( List JavaDoc dependencyTrail )
451     {
452         this.dependencyTrail = dependencyTrail;
453     }
454
455     public void setScope( String JavaDoc scope )
456     {
457         this.scope = scope;
458     }
459
460     public VersionRange getVersionRange()
461     {
462         return versionRange;
463     }
464
465     public void setVersionRange( VersionRange versionRange )
466     {
467         this.versionRange = versionRange;
468         
469         selectVersionFromNewRangeIfAvailable();
470     }
471     
472     private void selectVersionFromNewRangeIfAvailable()
473     {
474         if ( versionRange != null && versionRange.getRecommendedVersion() != null )
475         {
476             selectVersion( versionRange.getRecommendedVersion().toString() );
477         }
478         else
479         {
480             this.version = null;
481             this.baseVersion = null;
482         }
483     }
484
485     public void selectVersion( String JavaDoc version )
486     {
487         this.version = version;
488         this.baseVersion = version;
489     }
490
491     public void setGroupId( String JavaDoc groupId )
492     {
493         this.groupId = groupId;
494     }
495
496     public void setArtifactId( String JavaDoc artifactId )
497     {
498         this.artifactId = artifactId;
499     }
500
501     public boolean isSnapshot()
502     {
503         if ( version != null || baseVersion != null )
504         {
505             Matcher JavaDoc m = VERSION_FILE_PATTERN.matcher( getBaseVersion() );
506             if ( m.matches() )
507             {
508                 setBaseVersion( m.group( 1 ) + "-" + SNAPSHOT_VERSION );
509                 return true;
510             }
511             else
512             {
513                 return getBaseVersion().endsWith( SNAPSHOT_VERSION ) || getBaseVersion().equals( LATEST_VERSION );
514             }
515         }
516         else
517         {
518             return false;
519         }
520     }
521
522     public void setResolved( boolean resolved )
523     {
524         this.resolved = resolved;
525     }
526
527     public boolean isResolved()
528     {
529         return resolved;
530     }
531
532     public void setResolvedVersion( String JavaDoc version )
533     {
534         this.version = version;
535         // retain baseVersion
536
}
537
538     public void setArtifactHandler( ArtifactHandler artifactHandler )
539     {
540         this.artifactHandler = artifactHandler;
541     }
542
543     public void setRelease( boolean release )
544     {
545         this.release = release;
546     }
547
548     public boolean isRelease()
549     {
550         return release;
551     }
552
553     public List JavaDoc getAvailableVersions()
554     {
555         return availableVersions;
556     }
557
558     public void setAvailableVersions( List JavaDoc availableVersions )
559     {
560         this.availableVersions = availableVersions;
561     }
562
563     public boolean isOptional()
564     {
565         return optional;
566     }
567
568     public ArtifactVersion getSelectedVersion()
569         throws OverConstrainedVersionException
570     {
571         return versionRange.getSelectedVersion( this );
572     }
573
574     public boolean isSelectedVersionKnown()
575         throws OverConstrainedVersionException
576     {
577         return versionRange.isSelectedVersionKnown( this );
578     }
579
580     public void setOptional( boolean optional )
581     {
582         this.optional = optional;
583     }
584 }
585
Popular Tags