View Javadoc

1   package org.apache.maven.shared.io.location;
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.List;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.factory.ArtifactFactory;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
28  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
29  import org.apache.maven.artifact.resolver.ArtifactResolver;
30  import org.apache.maven.shared.io.logging.MessageHolder;
31  
32  public class ArtifactLocatorStrategy
33      implements LocatorStrategy
34  {
35      private final ArtifactFactory factory;
36  
37      private final ArtifactResolver resolver;
38  
39      private String defaultArtifactType = "jar";
40  
41      private final ArtifactRepository localRepository;
42  
43      private final List remoteRepositories;
44  
45      private String defaultClassifier = null;
46  
47      public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
48                                      ArtifactRepository localRepository, List remoteRepositories )
49      {
50          this.factory = factory;
51          this.resolver = resolver;
52          this.localRepository = localRepository;
53          this.remoteRepositories = remoteRepositories;
54      }
55  
56      public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
57                                      ArtifactRepository localRepository, List remoteRepositories,
58                                      String defaultArtifactType )
59      {
60          this.factory = factory;
61          this.resolver = resolver;
62          this.localRepository = localRepository;
63          this.remoteRepositories = remoteRepositories;
64          this.defaultArtifactType = defaultArtifactType;
65      }
66  
67      public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
68                                      ArtifactRepository localRepository, List remoteRepositories,
69                                      String defaultArtifactType, String defaultClassifier )
70      {
71          this.factory = factory;
72          this.resolver = resolver;
73          this.localRepository = localRepository;
74          this.remoteRepositories = remoteRepositories;
75          this.defaultArtifactType = defaultArtifactType;
76          this.defaultClassifier = defaultClassifier;
77      }
78  
79      /**
80       * Assumes artifact identity is given in a set of comma-delimited tokens of
81       * the form: <code>groupId:artifactId:version:type:classifier</code>, where
82       * type and classifier are optional.
83       */
84      public Location resolve( String locationSpecification, MessageHolder messageHolder )
85      {
86          String[] parts = locationSpecification.split( ":" );
87  
88          Location location = null;
89  
90          if ( parts.length > 2 )
91          {
92              String groupId = parts[0];
93              String artifactId = parts[1];
94              String version = parts[2];
95  
96              String type = defaultArtifactType;
97              if ( parts.length > 3 )
98              {
99                  if ( parts[3].trim().length() > 0 )
100                 {
101                     type = parts[3];
102                 }
103             }
104 
105             String classifier = defaultClassifier;
106             if ( parts.length > 4 )
107             {
108                 classifier = parts[4];
109             }
110 
111             if ( parts.length > 5 )
112             {
113                 messageHolder.newMessage().append( "Location specification has unused tokens: \'" );
114 
115                 for ( int i = 5; i < parts.length; i++ )
116                 {
117                     messageHolder.append( ":" + parts[i] );
118                 }
119             }
120 
121             Artifact artifact;
122             if ( classifier == null )
123             {
124                 artifact = factory.createArtifact( groupId, artifactId, version, null, type );
125             }
126             else
127             {
128                 artifact = factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
129             }
130 
131             try
132             {
133                 resolver.resolve( artifact, remoteRepositories, localRepository );
134 
135                 if ( artifact.getFile() != null )
136                 {
137                     location = new ArtifactLocation( artifact, locationSpecification );
138                 }
139                 else
140                 {
141                     messageHolder.addMessage( "Supposedly resolved artifact: " + artifact.getId()
142                         + " does not have an associated file." );
143                 }
144             }
145             catch ( ArtifactResolutionException e )
146             {
147                 messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
148                     + locationSpecification, e );
149             }
150             catch ( ArtifactNotFoundException e )
151             {
152                 messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
153                     + locationSpecification, e );
154             }
155         }
156         else
157         {
158             messageHolder.addMessage( "Invalid artifact specification: \'" + locationSpecification
159                 + "\'. Must contain at least three fields, separated by ':'." );
160         }
161 
162         return location;
163     }
164 
165 }