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