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