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