View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2010, 2013 Sonatype, Inc.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *    Sonatype, Inc. - initial API and implementation
10   *******************************************************************************/
11  package org.eclipse.aether.artifact;
12  
13  import java.io.File;
14  import java.util.Collections;
15  import java.util.HashMap;
16  import java.util.Map;
17  import java.util.regex.Matcher;
18  import java.util.regex.Pattern;
19  
20  /**
21   * A simple artifact. <em>Note:</em> Instances of this class are immutable and the exposed mutators return new objects
22   * rather than changing the current instance.
23   */
24  public final class DefaultArtifact
25      extends AbstractArtifact
26  {
27  
28      private final String groupId;
29  
30      private final String artifactId;
31  
32      private final String version;
33  
34      private final String classifier;
35  
36      private final String extension;
37  
38      private final File file;
39  
40      private final Map<String, String> properties;
41  
42      /**
43       * Creates a new artifact with the specified coordinates. If not specified in the artifact coordinates, the
44       * artifact's extension defaults to {@code jar} and classifier to an empty string.
45       * 
46       * @param coords The artifact coordinates in the format
47       *            {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}, must not be {@code null}.
48       */
49      public DefaultArtifact( String coords )
50      {
51          this( coords, Collections.<String, String> emptyMap() );
52      }
53  
54      /**
55       * Creates a new artifact with the specified coordinates and properties. If not specified in the artifact
56       * coordinates, the artifact's extension defaults to {@code jar} and classifier to an empty string.
57       * 
58       * @param coords The artifact coordinates in the format
59       *            {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}, must not be {@code null}.
60       * @param properties The artifact properties, may be {@code null}.
61       */
62      public DefaultArtifact( String coords, Map<String, String> properties )
63      {
64          Pattern p = Pattern.compile( "([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?:([^: ]+)" );
65          Matcher m = p.matcher( coords );
66          if ( !m.matches() )
67          {
68              throw new IllegalArgumentException( "Bad artifact coordinates " + coords
69                  + ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>" );
70          }
71          groupId = m.group( 1 );
72          artifactId = m.group( 2 );
73          extension = get( m.group( 4 ), "jar" );
74          classifier = get( m.group( 6 ), "" );
75          version = m.group( 7 );
76          file = null;
77          this.properties = copyProperties( properties );
78      }
79  
80      private static String get( String value, String defaultValue )
81      {
82          return ( value == null || value.length() <= 0 ) ? defaultValue : value;
83      }
84  
85      /**
86       * Creates a new artifact with the specified coordinates and no classifier. Passing {@code null} for any of the
87       * coordinates is equivalent to specifying an empty string.
88       * 
89       * @param groupId The group identifier of the artifact, may be {@code null}.
90       * @param artifactId The artifact identifier of the artifact, may be {@code null}.
91       * @param extension The file extension of the artifact, may be {@code null}.
92       * @param version The version of the artifact, may be {@code null}.
93       */
94      public DefaultArtifact( String groupId, String artifactId, String extension, String version )
95      {
96          this( groupId, artifactId, "", extension, version );
97      }
98  
99      /**
100      * Creates a new artifact with the specified coordinates. Passing {@code null} for any of the coordinates is
101      * equivalent to specifying an empty string.
102      * 
103      * @param groupId The group identifier of the artifact, may be {@code null}.
104      * @param artifactId The artifact identifier of the artifact, may be {@code null}.
105      * @param classifier The classifier of the artifact, may be {@code null}.
106      * @param extension The file extension of the artifact, may be {@code null}.
107      * @param version The version of the artifact, may be {@code null}.
108      */
109     public DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version )
110     {
111         this( groupId, artifactId, classifier, extension, version, null, (File) null );
112     }
113 
114     /**
115      * Creates a new artifact with the specified coordinates. Passing {@code null} for any of the coordinates is
116      * equivalent to specifying an empty string. The optional artifact type provided to this constructor will be used to
117      * determine the artifact's classifier and file extension if the corresponding arguments for this constructor are
118      * {@code null}.
119      * 
120      * @param groupId The group identifier of the artifact, may be {@code null}.
121      * @param artifactId The artifact identifier of the artifact, may be {@code null}.
122      * @param classifier The classifier of the artifact, may be {@code null}.
123      * @param extension The file extension of the artifact, may be {@code null}.
124      * @param version The version of the artifact, may be {@code null}.
125      * @param type The artifact type from which to query classifier, file extension and properties, may be {@code null}.
126      */
127     public DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version,
128                             ArtifactType type )
129     {
130         this( groupId, artifactId, classifier, extension, version, null, type );
131     }
132 
133     /**
134      * Creates a new artifact with the specified coordinates and properties. Passing {@code null} for any of the
135      * coordinates is equivalent to specifying an empty string. The optional artifact type provided to this constructor
136      * will be used to determine the artifact's classifier and file extension if the corresponding arguments for this
137      * constructor are {@code null}. If the artifact type specifies properties, those will get merged with the
138      * properties passed directly into the constructor, with the latter properties taking precedence.
139      * 
140      * @param groupId The group identifier of the artifact, may be {@code null}.
141      * @param artifactId The artifact identifier of the artifact, may be {@code null}.
142      * @param classifier The classifier of the artifact, may be {@code null}.
143      * @param extension The file extension of the artifact, may be {@code null}.
144      * @param version The version of the artifact, may be {@code null}.
145      * @param properties The properties of the artifact, may be {@code null} if none.
146      * @param type The artifact type from which to query classifier, file extension and properties, may be {@code null}.
147      */
148     public DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version,
149                             Map<String, String> properties, ArtifactType type )
150     {
151         this.groupId = emptify( groupId );
152         this.artifactId = emptify( artifactId );
153         if ( classifier != null || type == null )
154         {
155             this.classifier = emptify( classifier );
156         }
157         else
158         {
159             this.classifier = emptify( type.getClassifier() );
160         }
161         if ( extension != null || type == null )
162         {
163             this.extension = emptify( extension );
164         }
165         else
166         {
167             this.extension = emptify( type.getExtension() );
168         }
169         this.version = emptify( version );
170         this.file = null;
171         this.properties = merge( properties, ( type != null ) ? type.getProperties() : null );
172     }
173 
174     private static Map<String, String> merge( Map<String, String> dominant, Map<String, String> recessive )
175     {
176         Map<String, String> properties;
177 
178         if ( ( dominant == null || dominant.isEmpty() ) && ( recessive == null || recessive.isEmpty() ) )
179         {
180             properties = Collections.emptyMap();
181         }
182         else
183         {
184             properties = new HashMap<String, String>();
185             if ( recessive != null )
186             {
187                 properties.putAll( recessive );
188             }
189             if ( dominant != null )
190             {
191                 properties.putAll( dominant );
192             }
193             properties = Collections.unmodifiableMap( properties );
194         }
195 
196         return properties;
197     }
198 
199     /**
200      * Creates a new artifact with the specified coordinates, properties and file. Passing {@code null} for any of the
201      * coordinates is equivalent to specifying an empty string.
202      * 
203      * @param groupId The group identifier of the artifact, may be {@code null}.
204      * @param artifactId The artifact identifier of the artifact, may be {@code null}.
205      * @param classifier The classifier of the artifact, may be {@code null}.
206      * @param extension The file extension of the artifact, may be {@code null}.
207      * @param version The version of the artifact, may be {@code null}.
208      * @param properties The properties of the artifact, may be {@code null} if none.
209      * @param file The resolved file of the artifact, may be {@code null}.
210      */
211     public DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version,
212                             Map<String, String> properties, File file )
213     {
214         this.groupId = emptify( groupId );
215         this.artifactId = emptify( artifactId );
216         this.classifier = emptify( classifier );
217         this.extension = emptify( extension );
218         this.version = emptify( version );
219         this.file = file;
220         this.properties = copyProperties( properties );
221     }
222 
223     DefaultArtifact( String groupId, String artifactId, String classifier, String extension, String version, File file,
224                      Map<String, String> properties )
225     {
226         // NOTE: This constructor assumes immutability of the provided properties, for internal use only
227         this.groupId = emptify( groupId );
228         this.artifactId = emptify( artifactId );
229         this.classifier = emptify( classifier );
230         this.extension = emptify( extension );
231         this.version = emptify( version );
232         this.file = file;
233         this.properties = properties;
234     }
235 
236     private static String emptify( String str )
237     {
238         return ( str == null ) ? "" : str;
239     }
240 
241     public String getGroupId()
242     {
243         return groupId;
244     }
245 
246     public String getArtifactId()
247     {
248         return artifactId;
249     }
250 
251     public String getVersion()
252     {
253         return version;
254     }
255 
256     public String getClassifier()
257     {
258         return classifier;
259     }
260 
261     public String getExtension()
262     {
263         return extension;
264     }
265 
266     public File getFile()
267     {
268         return file;
269     }
270 
271     public Map<String, String> getProperties()
272     {
273         return properties;
274     }
275 
276 }