View Javadoc

1   package org.apache.maven.artifact.repository;
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.io.File;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.RepositoryUtils;
27  import org.apache.maven.artifact.metadata.ArtifactMetadata;
28  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
30  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
31  import org.apache.maven.repository.Proxy;
32  import org.sonatype.aether.RepositorySystem;
33  import org.sonatype.aether.RepositorySystemSession;
34  import org.sonatype.aether.artifact.Artifact;
35  import org.sonatype.aether.metadata.Metadata;
36  import org.sonatype.aether.repository.LocalArtifactRegistration;
37  import org.sonatype.aether.repository.LocalArtifactRequest;
38  import org.sonatype.aether.repository.LocalArtifactResult;
39  import org.sonatype.aether.repository.LocalMetadataRegistration;
40  import org.sonatype.aether.repository.LocalMetadataRequest;
41  import org.sonatype.aether.repository.LocalMetadataResult;
42  import org.sonatype.aether.repository.LocalRepository;
43  import org.sonatype.aether.repository.LocalRepositoryManager;
44  import org.sonatype.aether.repository.RemoteRepository;
45  import org.sonatype.aether.util.DefaultRepositorySystemSession;
46  import org.sonatype.aether.util.FilterRepositorySystemSession;
47  
48  /**
49   * <strong>Warning:</strong> This is an internal utility class that is only public for technical reasons, it is not part
50   * of the public API. In particular, this class can be changed or deleted without prior notice.
51   * 
52   * @author Benjamin Bentmann
53   */
54  public class LegacyLocalRepositoryManager
55      implements LocalRepositoryManager
56  {
57  
58      private final ArtifactRepository delegate;
59  
60      private final LocalRepository repo;
61  
62      private final boolean realLocalRepo;
63  
64      public static RepositorySystemSession overlay( ArtifactRepository repository, RepositorySystemSession session,
65                                                     RepositorySystem system )
66      {
67          if ( repository == null || repository.getBasedir() == null )
68          {
69              return session;
70          }
71  
72          if ( session != null )
73          {
74              LocalRepositoryManager lrm = session.getLocalRepositoryManager();
75              if ( lrm != null && lrm.getRepository().getBasedir().equals( new File( repository.getBasedir() ) ) )
76              {
77                  return session;
78              }
79          }
80          else
81          {
82              session = new DefaultRepositorySystemSession();
83          }
84  
85          final LocalRepositoryManager llrm = new LegacyLocalRepositoryManager( repository );
86  
87          return new FilterRepositorySystemSession( session )
88          {
89              @Override
90              public LocalRepositoryManager getLocalRepositoryManager()
91              {
92                  return llrm;
93              }
94          };
95      }
96  
97      private LegacyLocalRepositoryManager( ArtifactRepository delegate )
98      {
99          if ( delegate == null )
100         {
101             throw new IllegalArgumentException( "local repository delegate missing" );
102         }
103         this.delegate = delegate;
104 
105         ArtifactRepositoryLayout layout = delegate.getLayout();
106         repo =
107             new LocalRepository( new File( delegate.getBasedir() ),
108                                  ( layout != null ) ? layout.getClass().getSimpleName() : "legacy" );
109 
110         /*
111          * NOTE: "invoker:install" vs "appassembler:assemble": Both mojos use the artifact installer to put an artifact
112          * into a repository. In the first case, the result needs to be a proper local repository that one can use for
113          * local artifact resolution. In the second case, the result needs to precisely obey the path information of the
114          * repository's layout to allow pointing at artifacts within the repository. Unfortunately,
115          * DefaultRepositoryLayout does not correctly describe the layout of a local repository which unlike a remote
116          * repository never uses timestamps in the filename of a snapshot artifact. The discrepancy gets notable when a
117          * remotely resolved snapshot artifact gets passed into pathOf(). So producing a proper local artifact path
118          * using DefaultRepositoryLayout requires us to enforce usage of the artifact's base version. This
119          * transformation however contradicts the other use case of precisely obeying the repository's layout. The below
120          * flag tries to detect which use case applies to make both plugins happy.
121          */
122         realLocalRepo = ( layout instanceof DefaultRepositoryLayout ) && "local".equals( delegate.getId() );
123     }
124 
125     public LocalRepository getRepository()
126     {
127         return repo;
128     }
129 
130     public String getPathForLocalArtifact( Artifact artifact )
131     {
132         if ( realLocalRepo )
133         {
134             return delegate.pathOf( RepositoryUtils.toArtifact( artifact.setVersion( artifact.getBaseVersion() ) ) );
135         }
136         return delegate.pathOf( RepositoryUtils.toArtifact( artifact ) );
137     }
138 
139     public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
140     {
141         return delegate.pathOf( RepositoryUtils.toArtifact( artifact ) );
142     }
143 
144     public String getPathForLocalMetadata( Metadata metadata )
145     {
146         return delegate.pathOfLocalRepositoryMetadata( new ArtifactMetadataAdapter( metadata ), delegate );
147     }
148 
149     public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
150     {
151         return delegate.pathOfLocalRepositoryMetadata( new ArtifactMetadataAdapter( metadata ),
152                                                        new ArtifactRepositoryAdapter( repository ) );
153     }
154 
155     public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
156     {
157         String path = getPathForLocalArtifact( request.getArtifact() );
158         File file = new File( getRepository().getBasedir(), path );
159 
160         LocalArtifactResult result = new LocalArtifactResult( request );
161         if ( file.isFile() )
162         {
163             result.setFile( file );
164             result.setAvailable( true );
165         }
166 
167         return result;
168     }
169 
170     public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
171     {
172         Metadata metadata = request.getMetadata();
173 
174         String path;
175         if ( request.getRepository() == null )
176         {
177             path = getPathForLocalMetadata( metadata );
178         }
179         else
180         {
181             path = getPathForRemoteMetadata( metadata, request.getRepository(), request.getContext() );
182         }
183 
184         File file = new File( getRepository().getBasedir(), path );
185 
186         LocalMetadataResult result = new LocalMetadataResult( request );
187         if ( file.isFile() )
188         {
189             result.setFile( file );
190         }
191 
192         return result;
193     }
194 
195     public void add( RepositorySystemSession session, LocalArtifactRegistration request )
196     {
197         // noop
198     }
199 
200     public void add( RepositorySystemSession session, LocalMetadataRegistration request )
201     {
202         // noop
203     }
204 
205     static class ArtifactMetadataAdapter
206         implements ArtifactMetadata
207     {
208 
209         private final Metadata metadata;
210 
211         public ArtifactMetadataAdapter( Metadata metadata )
212         {
213             this.metadata = metadata;
214         }
215 
216         public boolean storedInArtifactVersionDirectory()
217         {
218             return metadata.getVersion().length() > 0;
219         }
220 
221         public boolean storedInGroupDirectory()
222         {
223             return metadata.getArtifactId().length() <= 0;
224         }
225 
226         public String getGroupId()
227         {
228             return nullify( metadata.getGroupId() );
229         }
230 
231         public String getArtifactId()
232         {
233             return nullify( metadata.getArtifactId() );
234         }
235 
236         public String getBaseVersion()
237         {
238             return nullify( metadata.getVersion() );
239         }
240 
241         private String nullify( String str )
242         {
243             return ( str == null || str.length() <= 0 ) ? null : str;
244         }
245 
246         public Object getKey()
247         {
248             return metadata.toString();
249         }
250 
251         public String getRemoteFilename()
252         {
253             return metadata.getType();
254         }
255 
256         public String getLocalFilename( ArtifactRepository repository )
257         {
258             return insertRepositoryKey( getRemoteFilename(), repository.getKey() );
259         }
260 
261         private String insertRepositoryKey( String filename, String repositoryKey )
262         {
263             String result;
264             int idx = filename.indexOf( '.' );
265             if ( idx < 0 )
266             {
267                 result = filename + '-' + repositoryKey;
268             }
269             else
270             {
271                 result = filename.substring( 0, idx ) + '-' + repositoryKey + filename.substring( idx );
272             }
273             return result;
274         }
275 
276         public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
277         {
278             // not used
279         }
280 
281         public void merge( ArtifactMetadata metadata )
282         {
283             // not used
284         }
285 
286         public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
287             throws RepositoryMetadataStoreException
288         {
289             // not used
290         }
291 
292         public String extendedToString()
293         {
294             return metadata.toString();
295         }
296 
297     }
298 
299     static class ArtifactRepositoryAdapter
300         implements ArtifactRepository
301     {
302 
303         private final RemoteRepository repository;
304 
305         public ArtifactRepositoryAdapter( RemoteRepository repository )
306         {
307             this.repository = repository;
308         }
309 
310         public String pathOf( org.apache.maven.artifact.Artifact artifact )
311         {
312             return null;
313         }
314 
315         public String pathOfRemoteRepositoryMetadata( ArtifactMetadata artifactMetadata )
316         {
317             return null;
318         }
319 
320         public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
321         {
322             return null;
323         }
324 
325         public String getUrl()
326         {
327             return repository.getUrl();
328         }
329 
330         public void setUrl( String url )
331         {
332         }
333 
334         public String getBasedir()
335         {
336             return null;
337         }
338 
339         public String getProtocol()
340         {
341             return repository.getProtocol();
342         }
343 
344         public String getId()
345         {
346             return repository.getId();
347         }
348 
349         public void setId( String id )
350         {
351         }
352 
353         public ArtifactRepositoryPolicy getSnapshots()
354         {
355             return null;
356         }
357 
358         public void setSnapshotUpdatePolicy( ArtifactRepositoryPolicy policy )
359         {
360         }
361 
362         public ArtifactRepositoryPolicy getReleases()
363         {
364             return null;
365         }
366 
367         public void setReleaseUpdatePolicy( ArtifactRepositoryPolicy policy )
368         {
369         }
370 
371         public ArtifactRepositoryLayout getLayout()
372         {
373             return null;
374         }
375 
376         public void setLayout( ArtifactRepositoryLayout layout )
377         {
378         }
379 
380         public String getKey()
381         {
382             return getId();
383         }
384 
385         public boolean isUniqueVersion()
386         {
387             return true;
388         }
389 
390         public boolean isBlacklisted()
391         {
392             return false;
393         }
394 
395         public void setBlacklisted( boolean blackListed )
396         {
397         }
398 
399         public org.apache.maven.artifact.Artifact find( org.apache.maven.artifact.Artifact artifact )
400         {
401             return null;
402         }
403 
404         public List<String> findVersions( org.apache.maven.artifact.Artifact artifact )
405         {
406             return Collections.emptyList();
407         }
408 
409         public boolean isProjectAware()
410         {
411             return false;
412         }
413 
414         public void setAuthentication( Authentication authentication )
415         {
416         }
417 
418         public Authentication getAuthentication()
419         {
420             return null;
421         }
422 
423         public void setProxy( Proxy proxy )
424         {
425         }
426 
427         public Proxy getProxy()
428         {
429             return null;
430         }
431 
432         public List<ArtifactRepository> getMirroredRepositories()
433         {
434             return Collections.emptyList();
435         }
436 
437         public void setMirroredRepositories( List<ArtifactRepository> mirroredRepositories )
438         {
439         }
440 
441     }
442 
443 }