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  
46  /**
47   * <strong>Warning:</strong> This is an internal utility class that is only public for technical reasons, it is not part
48   * of the public API. In particular, this class can be changed or deleted without prior notice.
49   * 
50   * @author Benjamin Bentmann
51   */
52  public class LegacyLocalRepositoryManager
53      implements LocalRepositoryManager
54  {
55  
56      private final ArtifactRepository delegate;
57  
58      private final LocalRepository repo;
59  
60      public static LocalRepositoryManager wrap( ArtifactRepository repository, RepositorySystem system )
61      {
62          ArtifactRepositoryLayout layout = repository.getLayout();
63          if ( layout != null && layout.getClass().equals( DefaultRepositoryLayout.class ) )
64          {
65              // map the default layout to the default impl of the repo system
66              return system.newLocalRepositoryManager( new LocalRepository( repository.getBasedir() ) );
67          }
68  
69          return new LegacyLocalRepositoryManager( repository );
70      }
71  
72      private LegacyLocalRepositoryManager( ArtifactRepository delegate )
73      {
74          if ( delegate == null )
75          {
76              throw new IllegalArgumentException( "local repository delegate missing" );
77          }
78          this.delegate = delegate;
79  
80          ArtifactRepositoryLayout layout = delegate.getLayout();
81          repo =
82              new LocalRepository( new File( delegate.getBasedir() ),
83                                   ( layout != null ) ? layout.getClass().getSimpleName() : "legacy" );
84      }
85  
86      public LocalRepository getRepository()
87      {
88          return repo;
89      }
90  
91      public String getPathForLocalArtifact( Artifact artifact )
92      {
93          return delegate.pathOf( RepositoryUtils.toArtifact( artifact ) );
94      }
95  
96      public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
97      {
98          return getPathForLocalArtifact( artifact );
99      }
100 
101     public String getPathForLocalMetadata( Metadata metadata )
102     {
103         return delegate.pathOfLocalRepositoryMetadata( new ArtifactMetadataAdapter( metadata ), delegate );
104     }
105 
106     public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
107     {
108         return delegate.pathOfLocalRepositoryMetadata( new ArtifactMetadataAdapter( metadata ),
109                                                        new ArtifactRepositoryAdapter( repository ) );
110     }
111 
112     public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
113     {
114         String path = getPathForLocalArtifact( request.getArtifact() );
115         File file = new File( getRepository().getBasedir(), path );
116 
117         LocalArtifactResult result = new LocalArtifactResult( request );
118         if ( file.isFile() )
119         {
120             result.setFile( file );
121             result.setAvailable( true );
122         }
123 
124         return result;
125     }
126 
127     public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
128     {
129         Metadata metadata = request.getMetadata();
130 
131         String path;
132         if ( request.getRepository() == null )
133         {
134             path = getPathForLocalMetadata( metadata );
135         }
136         else
137         {
138             path = getPathForRemoteMetadata( metadata, request.getRepository(), request.getContext() );
139         }
140 
141         File file = new File( getRepository().getBasedir(), path );
142 
143         LocalMetadataResult result = new LocalMetadataResult( request );
144         if ( file.isFile() )
145         {
146             result.setFile( file );
147         }
148 
149         return result;
150     }
151 
152     public void add( RepositorySystemSession session, LocalArtifactRegistration request )
153     {
154         // noop
155     }
156 
157     public void add( RepositorySystemSession session, LocalMetadataRegistration request )
158     {
159         // noop
160     }
161 
162     static class ArtifactMetadataAdapter
163         implements ArtifactMetadata
164     {
165 
166         private final Metadata metadata;
167 
168         public ArtifactMetadataAdapter( Metadata metadata )
169         {
170             this.metadata = metadata;
171         }
172 
173         public boolean storedInArtifactVersionDirectory()
174         {
175             return metadata.getVersion().length() > 0;
176         }
177 
178         public boolean storedInGroupDirectory()
179         {
180             return metadata.getArtifactId().length() <= 0;
181         }
182 
183         public String getGroupId()
184         {
185             return nullify( metadata.getGroupId() );
186         }
187 
188         public String getArtifactId()
189         {
190             return nullify( metadata.getArtifactId() );
191         }
192 
193         public String getBaseVersion()
194         {
195             return nullify( metadata.getVersion() );
196         }
197 
198         private String nullify( String str )
199         {
200             return ( str == null || str.length() <= 0 ) ? null : str;
201         }
202 
203         public Object getKey()
204         {
205             return metadata.toString();
206         }
207 
208         public String getRemoteFilename()
209         {
210             return metadata.getType();
211         }
212 
213         public String getLocalFilename( ArtifactRepository repository )
214         {
215             return insertRepositoryKey( getRemoteFilename(), repository.getKey() );
216         }
217 
218         private String insertRepositoryKey( String filename, String repositoryKey )
219         {
220             String result;
221             int idx = filename.indexOf( '.' );
222             if ( idx < 0 )
223             {
224                 result = filename + '-' + repositoryKey;
225             }
226             else
227             {
228                 result = filename.substring( 0, idx ) + '-' + repositoryKey + filename.substring( idx );
229             }
230             return result;
231         }
232 
233         public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
234         {
235             // not used
236         }
237 
238         public void merge( ArtifactMetadata metadata )
239         {
240             // not used
241         }
242 
243         public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
244             throws RepositoryMetadataStoreException
245         {
246             // not used
247         }
248 
249         public String extendedToString()
250         {
251             return metadata.toString();
252         }
253 
254     }
255 
256     static class ArtifactRepositoryAdapter
257         implements ArtifactRepository
258     {
259 
260         private final RemoteRepository repository;
261 
262         public ArtifactRepositoryAdapter( RemoteRepository repository )
263         {
264             this.repository = repository;
265         }
266 
267         public String pathOf( org.apache.maven.artifact.Artifact artifact )
268         {
269             return null;
270         }
271 
272         public String pathOfRemoteRepositoryMetadata( ArtifactMetadata artifactMetadata )
273         {
274             return null;
275         }
276 
277         public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
278         {
279             return null;
280         }
281 
282         public String getUrl()
283         {
284             return repository.getUrl();
285         }
286 
287         public void setUrl( String url )
288         {
289         }
290 
291         public String getBasedir()
292         {
293             return null;
294         }
295 
296         public String getProtocol()
297         {
298             return repository.getProtocol();
299         }
300 
301         public String getId()
302         {
303             return repository.getId();
304         }
305 
306         public void setId( String id )
307         {
308         }
309 
310         public ArtifactRepositoryPolicy getSnapshots()
311         {
312             return null;
313         }
314 
315         public void setSnapshotUpdatePolicy( ArtifactRepositoryPolicy policy )
316         {
317         }
318 
319         public ArtifactRepositoryPolicy getReleases()
320         {
321             return null;
322         }
323 
324         public void setReleaseUpdatePolicy( ArtifactRepositoryPolicy policy )
325         {
326         }
327 
328         public ArtifactRepositoryLayout getLayout()
329         {
330             return null;
331         }
332 
333         public void setLayout( ArtifactRepositoryLayout layout )
334         {
335         }
336 
337         public String getKey()
338         {
339             return getId();
340         }
341 
342         public boolean isUniqueVersion()
343         {
344             return true;
345         }
346 
347         public boolean isBlacklisted()
348         {
349             return false;
350         }
351 
352         public void setBlacklisted( boolean blackListed )
353         {
354         }
355 
356         public org.apache.maven.artifact.Artifact find( org.apache.maven.artifact.Artifact artifact )
357         {
358             return null;
359         }
360 
361         public List<String> findVersions( org.apache.maven.artifact.Artifact artifact )
362         {
363             return Collections.emptyList();
364         }
365 
366         public boolean isProjectAware()
367         {
368             return false;
369         }
370 
371         public void setAuthentication( Authentication authentication )
372         {
373         }
374 
375         public Authentication getAuthentication()
376         {
377             return null;
378         }
379 
380         public void setProxy( Proxy proxy )
381         {
382         }
383 
384         public Proxy getProxy()
385         {
386             return null;
387         }
388     }
389 
390 }