View Javadoc
1   package org.apache.maven.repository.metadata;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collection;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.ArtifactScopeEnum;
26  
27  /**
28   * Artifact Metadata that is resolved independent of Artifact itself.
29   *
30   * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
31   */
32  public class ArtifactMetadata
33  {
34      /**
35       * standard glorified artifact coordinates
36       */
37      protected String groupId;
38      protected String artifactId;
39      protected String version;
40      protected String type;
41      protected ArtifactScopeEnum artifactScope;
42      protected String classifier;
43  
44      /**
45       * explanation: why this MD was chosen over it's siblings
46       * in the resulting structure (classpath for now)
47       */
48      protected String why;
49  
50      /** dependencies of the artifact behind this metadata */
51      protected Collection<ArtifactMetadata> dependencies;
52  
53      /** metadata URI */
54      protected String uri;
55  
56      /** is metadata found anywhere */
57      protected boolean resolved = false;
58  
59      /** does the actual artifact for this metadata exists */
60      protected boolean artifactExists = false;
61      /** artifact URI */
62      protected String artifactUri;
63  
64      /** error message  */
65      private String error;
66  
67      //------------------------------------------------------------------
68      /**
69       *
70       */
71      public ArtifactMetadata( String name )
72      {
73          if ( name == null )
74          {
75              return;
76          }
77          int ind1 = name.indexOf( ':' );
78          int ind2 = name.lastIndexOf( ':' );
79  
80          if ( ind1 == -1 || ind2 == -1 )
81          {
82              return;
83          }
84  
85          this.groupId = name.substring( 0, ind1 );
86          if ( ind1 == ind2 )
87          {
88              this.artifactId = name.substring( ind1 + 1 );
89          }
90          else
91          {
92              this.artifactId = name.substring( ind1 + 1, ind2 );
93              this.version = name.substring( ind2 + 1 );
94          }
95      }
96  
97      public ArtifactMetadata( String groupId, String name, String version )
98      {
99          this( groupId, name, version, null );
100     }
101 
102     public ArtifactMetadata( String groupId, String name, String version, String type )
103     {
104         this( groupId, name, version, type, null );
105     }
106 
107     public ArtifactMetadata( String groupId, String name, String version, String type, ArtifactScopeEnum artifactScope )
108     {
109         this( groupId, name, version, type, artifactScope, null );
110     }
111 
112     public ArtifactMetadata( String groupId, String name, String version, String type, ArtifactScopeEnum artifactScope,
113                              String classifier )
114     {
115         this( groupId, name, version, type, artifactScope, classifier, null );
116     }
117 
118     public ArtifactMetadata( String groupId, String name, String version, String type, ArtifactScopeEnum artifactScope,
119                              String classifier, String artifactUri )
120     {
121         this( groupId, name, version, type, artifactScope, classifier, artifactUri, null, true, null );
122     }
123 
124     @SuppressWarnings( "checkstyle:parameternumber" )
125     public ArtifactMetadata( String groupId, String name, String version, String type, ArtifactScopeEnum artifactScope,
126                              String classifier, String artifactUri, String why, boolean resolved, String error )
127     {
128         this.groupId = groupId;
129         this.artifactId = name;
130         this.version = version;
131         this.type = type;
132         this.artifactScope = artifactScope;
133         this.classifier = classifier;
134         this.artifactUri = artifactUri;
135         this.why = why;
136         this.resolved = resolved;
137         this.error = error;
138     }
139 
140     @SuppressWarnings( "checkstyle:parameternumber" )
141     public ArtifactMetadata( String groupId, String name, String version, String type, String scopeString,
142                              String classifier, String artifactUri, String why, boolean resolved, String error )
143     {
144         this( groupId, name, version, type,
145               scopeString == null ? ArtifactScopeEnum.DEFAULT_SCOPE : ArtifactScopeEnum.valueOf( scopeString ),
146               classifier, artifactUri, why, resolved, error );
147     }
148 
149     public ArtifactMetadata( Artifact af )
150     {
151         /*
152         if ( af != null )
153         {
154             init( af );
155         }
156         */
157     }
158     //------------------------------------------------------------------
159 //    public void init( ArtifactMetadata af )
160 //    {
161 //        setGroupId( af.getGroupId() );
162 //        setArtifactId( af.getArtifactId() );
163 //        setVersion( af.getVersion() );
164 //        setType( af.getType() );
165 //        setScope( af.getScope() );
166 //        setClassifier( af.getClassifier() );
167 //        //setUri( af.getDownloadUrl() );
168 //
169 //        this.resolved = af.isResolved();
170 //    }
171 
172     @Override
173     public String toString()
174     {
175         return groupId + ":" + artifactId + ":" + version;
176     }
177 
178     public String toDomainString()
179     {
180         return groupId + ":" + artifactId;
181     }
182 
183     public String getGroupId()
184     {
185         return groupId;
186     }
187 
188     public void setGroupId( String groupId )
189     {
190         this.groupId = groupId;
191     }
192 
193     public String getArtifactId()
194     {
195         return artifactId;
196     }
197 
198     public void setArtifactId( String name )
199     {
200         this.artifactId = name;
201     }
202 
203     public String getVersion()
204     {
205         return version;
206     }
207 
208     public void setVersion( String version )
209     {
210         this.version = version;
211     }
212 
213     public String getType()
214     {
215         return type;
216     }
217 
218     public String getCheckedType()
219     {
220         return type == null ? "jar" : type;
221     }
222 
223     public void setType( String type )
224     {
225         this.type = type;
226     }
227 
228     public ArtifactScopeEnum getArtifactScope()
229     {
230         return artifactScope == null ? ArtifactScopeEnum.DEFAULT_SCOPE : artifactScope;
231     }
232 
233     public void setArtifactScope( ArtifactScopeEnum artifactScope )
234     {
235         this.artifactScope = artifactScope;
236     }
237 
238     public void setScope( String scope )
239     {
240         this.artifactScope = scope == null ? ArtifactScopeEnum.DEFAULT_SCOPE : ArtifactScopeEnum.valueOf( scope );
241     }
242 
243     public String getClassifier()
244     {
245         return classifier;
246     }
247 
248     public void setClassifier( String classifier )
249     {
250         this.classifier = classifier;
251     }
252 
253     public boolean isResolved()
254     {
255         return resolved;
256     }
257 
258     public void setResolved( boolean resolved )
259     {
260         this.resolved = resolved;
261     }
262 
263     public String getUri()
264     {
265         return uri;
266     }
267 
268     public void setUri( String uri )
269     {
270         this.uri = uri;
271     }
272 
273     public String getScope()
274     {
275         return getArtifactScope().getScope();
276     }
277 
278     public ArtifactScopeEnum getScopeAsEnum()
279     {
280         return artifactScope == null ? ArtifactScopeEnum.DEFAULT_SCOPE : artifactScope;
281     }
282 
283     public boolean isArtifactExists()
284     {
285         return artifactExists;
286     }
287 
288     public void setArtifactExists( boolean artifactExists )
289     {
290         this.artifactExists = artifactExists;
291     }
292 
293 
294     public Collection<ArtifactMetadata> getDependencies()
295     {
296         return dependencies;
297     }
298 
299     public void setDependencies( Collection<ArtifactMetadata> dependencies )
300     {
301         this.dependencies = dependencies;
302     }
303 
304     public String getArtifactUri()
305     {
306         return artifactUri;
307     }
308 
309     public void setArtifactUri( String artifactUri )
310     {
311         this.artifactUri = artifactUri;
312     }
313 
314 
315     public String getWhy()
316     {
317         return why;
318     }
319 
320     public void setWhy( String why )
321     {
322         this.why = why;
323     }
324 
325     public String getError()
326     {
327         return error;
328     }
329 
330     public void setError( String error )
331     {
332         this.error = error;
333     }
334 
335     public boolean isError()
336     {
337         return error == null;
338     }
339 
340     public String getDependencyConflictId()
341     {
342         return groupId + ":" + artifactId;
343     }
344 }