001    package org.apache.maven.repository.metadata;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Collection;
023    
024    import org.apache.maven.artifact.Artifact;
025    import org.apache.maven.artifact.ArtifactScopeEnum;
026    
027    /**
028     * Artifact Metadata that is resolved independent of Artifact itself.
029     *
030     * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
031     *
032     */
033    public class ArtifactMetadata
034    {
035        /**
036         * standard glorified artifact coordinates
037         */
038        protected String groupId;
039        protected String artifactId;
040        protected String version;
041        protected String type;
042        protected ArtifactScopeEnum artifactScope;
043        protected String classifier;
044    
045        /**
046         * explanation: why this MD was chosen over it's siblings
047         * in the resulting structure (classpath for now)
048         */
049        protected String why;
050    
051        /** dependencies of the artifact behind this metadata */
052        protected Collection<ArtifactMetadata> dependencies;
053    
054        /** metadata URI */
055        protected String uri;
056    
057        /** is metadata found anywhere */
058        protected boolean resolved = false;
059    
060        /** does the actual artifact for this metadata exists */
061        protected boolean artifactExists = false;
062        /** artifact URI */
063        protected String artifactUri;
064    
065        /** error message  */
066        private String error;
067    
068        //------------------------------------------------------------------
069        /**
070         *
071         */
072        public ArtifactMetadata( String name )
073        {
074            if ( name == null )
075            {
076                return;
077            }
078            int ind1 = name.indexOf( ':' );
079            int ind2 = name.lastIndexOf( ':' );
080    
081            if ( ind1 == -1 || ind2 == -1 )
082            {
083                return;
084            }
085    
086            this.groupId = name.substring( 0, ind1 );
087            if ( ind1 == ind2 )
088            {
089                this.artifactId = name.substring( ind1 + 1 );
090            }
091            else
092            {
093                this.artifactId = name.substring( ind1 + 1, ind2 );
094                this.version = name.substring( ind2 + 1 );
095            }
096        }
097    
098        // ------------------------------------------------------------------
099        public ArtifactMetadata( String groupId, String name, String version )
100        {
101            this( groupId, name, version, null );
102        }
103        //------------------------------------------------------------------
104        public ArtifactMetadata( String groupId, String name, String version, String type )
105        {
106            this( groupId, name, version, type, null );
107        }
108    
109        //------------------------------------------------------------------
110        public ArtifactMetadata( String groupId, String name, String version, String type, ArtifactScopeEnum artifactScope )
111        {
112            this( groupId, name, version, type, artifactScope, null );
113        }
114    
115        //------------------------------------------------------------------
116        public ArtifactMetadata( String groupId, String name, String version, String type, ArtifactScopeEnum artifactScope,
117                                 String classifier )
118        {
119            this( groupId, name, version, type, artifactScope, classifier, null );
120        }
121        //------------------------------------------------------------------
122        public ArtifactMetadata( String groupId, String name, String version, String type, ArtifactScopeEnum artifactScope,
123                                 String classifier, String artifactUri )
124        {
125            this( groupId, name, version, type, artifactScope, classifier, artifactUri, null, true, null );
126        }
127        //------------------------------------------------------------------
128        public ArtifactMetadata( String groupId, String name, String version, String type, ArtifactScopeEnum artifactScope,
129                                 String classifier, String artifactUri, String why, boolean resolved, String error )
130        {
131            this.groupId = groupId;
132            this.artifactId = name;
133            this.version = version;
134            this.type = type;
135            this.artifactScope = artifactScope;
136            this.classifier = classifier;
137            this.artifactUri = artifactUri;
138            this.why = why;
139            this.resolved = resolved;
140            this.error = error;
141        }
142        //------------------------------------------------------------------
143        public ArtifactMetadata( String groupId, String name, String version, String type, String scopeString,
144                                 String classifier, String artifactUri, String why, boolean resolved, String error )
145        {
146            this( groupId, name, version, type,
147                  scopeString == null ? ArtifactScopeEnum.DEFAULT_SCOPE : ArtifactScopeEnum.valueOf( scopeString ),
148                  classifier, artifactUri, why, resolved, error );
149        }
150    
151        //------------------------------------------------------------------
152        public ArtifactMetadata( Artifact af )
153        {
154            /*
155            if ( af != null )
156            {
157                init( af );
158            }
159            */
160        }
161        //------------------------------------------------------------------
162    //    public void init( ArtifactMetadata af )
163    //    {
164    //        setGroupId( af.getGroupId() );
165    //        setArtifactId( af.getArtifactId() );
166    //        setVersion( af.getVersion() );
167    //        setType( af.getType() );
168    //        setScope( af.getScope() );
169    //        setClassifier( af.getClassifier() );
170    //        //setUri( af.getDownloadUrl() );
171    //
172    //        this.resolved = af.isResolved();
173    //    }
174    
175        //------------------------------------------------------------------
176        @Override
177        public String toString()
178        {
179            return groupId + ":" + artifactId + ":" + version;
180        }
181    
182        //------------------------------------------------------------------
183        public String toDomainString()
184        {
185            return groupId + ":" + artifactId;
186        }
187    
188        //------------------------------------------------------------------
189        public String getGroupId()
190        {
191            return groupId;
192        }
193    
194        public void setGroupId( String groupId )
195        {
196            this.groupId = groupId;
197        }
198    
199        public String getArtifactId()
200        {
201            return artifactId;
202        }
203    
204        public void setArtifactId( String name )
205        {
206            this.artifactId = name;
207        }
208    
209        public String getVersion()
210        {
211            return version;
212        }
213    
214        public void setVersion( String version )
215        {
216            this.version = version;
217        }
218    
219        public String getType()
220        {
221            return type;
222        }
223    
224        public String getCheckedType()
225        {
226            return type == null ? "jar" : type;
227        }
228    
229        public void setType( String type )
230        {
231            this.type = type;
232        }
233    
234        public ArtifactScopeEnum getArtifactScope()
235        {
236            return artifactScope == null ? ArtifactScopeEnum.DEFAULT_SCOPE : artifactScope;
237        }
238    
239        public void setArtifactScope( ArtifactScopeEnum artifactScope )
240        {
241            this.artifactScope = artifactScope;
242        }
243    
244        public void setScope( String scope )
245        {
246            this.artifactScope = scope == null ? ArtifactScopeEnum.DEFAULT_SCOPE : ArtifactScopeEnum.valueOf( scope );
247        }
248    
249        public String getClassifier()
250        {
251            return classifier;
252        }
253    
254        public void setClassifier( String classifier )
255        {
256            this.classifier = classifier;
257        }
258    
259        public boolean isResolved()
260        {
261            return resolved;
262        }
263    
264        public void setResolved( boolean resolved )
265        {
266            this.resolved = resolved;
267        }
268    
269        public String getUri()
270        {
271            return uri;
272        }
273    
274        public void setUri( String uri )
275        {
276            this.uri = uri;
277        }
278    
279        public String getScope()
280        {
281            return getArtifactScope().getScope();
282        }
283    
284        public ArtifactScopeEnum getScopeAsEnum()
285        {
286            return artifactScope == null ? ArtifactScopeEnum.DEFAULT_SCOPE : artifactScope;
287        }
288    
289        public boolean isArtifactExists()
290        {
291            return artifactExists;
292        }
293    
294        public void setArtifactExists(boolean artifactExists)
295        {
296            this.artifactExists = artifactExists;
297        }
298    
299    
300        public Collection<ArtifactMetadata> getDependencies()
301        {
302            return dependencies;
303        }
304    
305        public void setDependencies(Collection<ArtifactMetadata> dependencies)
306        {
307            this.dependencies = dependencies;
308        }
309    
310        public String getArtifactUri()
311        {
312            return artifactUri;
313        }
314    
315        public void setArtifactUri(String artifactUri)
316        {
317            this.artifactUri = artifactUri;
318        }
319    
320    
321        public String getWhy()
322        {
323            return why;
324        }
325        public void setWhy(String why)
326        {
327            this.why = why;
328        }
329        //-------------------------------------------------------------------
330        public String getError()
331        {
332            return error;
333        }
334        public void setError(String error)
335        {
336            this.error = error;
337        }
338        public boolean isError()
339        {
340            return error == null;
341        }
342        //------------------------------------------------------------------
343        public String getDependencyConflictId()
344        {
345            return groupId + ":" + artifactId;
346        }
347        //------------------------------------------------------------------
348        //------------------------------------------------------------------
349    }