View Javadoc
1   package org.eclipse.aether.internal.impl;
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 static java.util.Objects.requireNonNull;
24  import java.util.SortedSet;
25  import java.util.TreeSet;
26  
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.metadata.Metadata;
30  import org.eclipse.aether.repository.LocalArtifactRegistration;
31  import org.eclipse.aether.repository.LocalArtifactRequest;
32  import org.eclipse.aether.repository.LocalArtifactResult;
33  import org.eclipse.aether.repository.LocalMetadataRegistration;
34  import org.eclipse.aether.repository.LocalMetadataRequest;
35  import org.eclipse.aether.repository.LocalMetadataResult;
36  import org.eclipse.aether.repository.LocalRepository;
37  import org.eclipse.aether.repository.LocalRepositoryManager;
38  import org.eclipse.aether.repository.RemoteRepository;
39  
40  /**
41   * A local repository manager that realizes the classical Maven 2.0 local repository.
42   */
43  class SimpleLocalRepositoryManager
44      implements LocalRepositoryManager
45  {
46  
47      private final LocalRepository repository;
48  
49      SimpleLocalRepositoryManager( File basedir )
50      {
51          this( basedir, "simple" );
52      }
53  
54      SimpleLocalRepositoryManager( String basedir )
55      {
56          this( ( basedir != null ) ? new File( basedir ) : null, "simple" );
57      }
58  
59      SimpleLocalRepositoryManager( File basedir, String type )
60      {
61          requireNonNull( basedir, "base directory cannot be null" );
62          repository = new LocalRepository( basedir.getAbsoluteFile(), type );
63      }
64  
65      public LocalRepository getRepository()
66      {
67          return repository;
68      }
69  
70      String getPathForArtifact( Artifact artifact, boolean local )
71      {
72          StringBuilder path = new StringBuilder( 128 );
73  
74          path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
75  
76          path.append( artifact.getArtifactId() ).append( '/' );
77  
78          path.append( artifact.getBaseVersion() ).append( '/' );
79  
80          path.append( artifact.getArtifactId() ).append( '-' );
81          if ( local )
82          {
83              path.append( artifact.getBaseVersion() );
84          }
85          else
86          {
87              path.append( artifact.getVersion() );
88          }
89  
90          if ( artifact.getClassifier().length() > 0 )
91          {
92              path.append( '-' ).append( artifact.getClassifier() );
93          }
94  
95          if ( artifact.getExtension().length() > 0 )
96          {
97              path.append( '.' ).append( artifact.getExtension() );
98          }
99  
100         return path.toString();
101     }
102 
103     public String getPathForLocalArtifact( Artifact artifact )
104     {
105         requireNonNull( artifact, "artifact cannot be null" );
106         return getPathForArtifact( artifact, true );
107     }
108 
109     public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
110     {
111         requireNonNull( artifact, "artifact cannot be null" );
112         requireNonNull( repository, "repository cannot be null" );
113         return getPathForArtifact( artifact, false );
114     }
115 
116     public String getPathForLocalMetadata( Metadata metadata )
117     {
118         requireNonNull( metadata, "metadata cannot be null" );
119         return getPath( metadata, "local" );
120     }
121 
122     public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
123     {
124         requireNonNull( metadata, "metadata cannot be null" );
125         requireNonNull( repository, "repository cannot be null" );
126         return getPath( metadata, getRepositoryKey( repository, context ) );
127     }
128 
129     String getRepositoryKey( RemoteRepository repository, String context )
130     {
131         String key;
132 
133         if ( repository.isRepositoryManager() )
134         {
135             // repository serves dynamic contents, take request parameters into account for key
136 
137             StringBuilder buffer = new StringBuilder( 128 );
138 
139             buffer.append( repository.getId() );
140 
141             buffer.append( '-' );
142 
143             SortedSet<String> subKeys = new TreeSet<>();
144             for ( RemoteRepository mirroredRepo : repository.getMirroredRepositories() )
145             {
146                 subKeys.add( mirroredRepo.getId() );
147             }
148 
149             SimpleDigest digest = new SimpleDigest();
150             digest.update( context );
151             for ( String subKey : subKeys )
152             {
153                 digest.update( subKey );
154             }
155             buffer.append( digest.digest() );
156 
157             key = buffer.toString();
158         }
159         else
160         {
161             // repository serves static contents, its id is sufficient as key
162 
163             key = repository.getId();
164         }
165 
166         return key;
167     }
168 
169     private String getPath( Metadata metadata, String repositoryKey )
170     {
171         StringBuilder path = new StringBuilder( 128 );
172 
173         if ( metadata.getGroupId().length() > 0 )
174         {
175             path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
176 
177             if ( metadata.getArtifactId().length() > 0 )
178             {
179                 path.append( metadata.getArtifactId() ).append( '/' );
180 
181                 if ( metadata.getVersion().length() > 0 )
182                 {
183                     path.append( metadata.getVersion() ).append( '/' );
184                 }
185             }
186         }
187 
188         path.append( insertRepositoryKey( metadata.getType(), repositoryKey ) );
189 
190         return path.toString();
191     }
192 
193     private String insertRepositoryKey( String filename, String repositoryKey )
194     {
195         String result;
196         int idx = filename.indexOf( '.' );
197         if ( idx < 0 )
198         {
199             result = filename + '-' + repositoryKey;
200         }
201         else
202         {
203             result = filename.substring( 0, idx ) + '-' + repositoryKey + filename.substring( idx );
204         }
205         return result;
206     }
207 
208     public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
209     {
210         requireNonNull( session, "session cannot be null" );
211         requireNonNull( request, "request cannot be null" );
212         String path = getPathForArtifact( request.getArtifact(), false );
213         File file = new File( getRepository().getBasedir(), path );
214 
215         LocalArtifactResult result = new LocalArtifactResult( request );
216         if ( file.isFile() )
217         {
218             result.setFile( file );
219             result.setAvailable( true );
220         }
221 
222         return result;
223     }
224 
225     public void add( RepositorySystemSession session, LocalArtifactRegistration request )
226     {
227         requireNonNull( session, "session cannot be null" );
228         requireNonNull( request, "request cannot be null" );
229         // noop
230     }
231 
232     @Override
233     public String toString()
234     {
235         return String.valueOf( getRepository() );
236     }
237 
238     public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
239     {
240         requireNonNull( session, "session cannot be null" );
241         requireNonNull( request, "request cannot be null" );
242         LocalMetadataResult result = new LocalMetadataResult( request );
243 
244         String path;
245 
246         Metadata metadata = request.getMetadata();
247         String context = request.getContext();
248         RemoteRepository remote = request.getRepository();
249 
250         if ( remote != null )
251         {
252             path = getPathForRemoteMetadata( metadata, remote, context );
253         }
254         else
255         {
256             path = getPathForLocalMetadata( metadata );
257         }
258 
259         File file = new File( getRepository().getBasedir(), path );
260         if ( file.isFile() )
261         {
262             result.setFile( file );
263         }
264 
265         return result;
266     }
267 
268     public void add( RepositorySystemSession session, LocalMetadataRegistration request )
269     {
270         requireNonNull( session, "session cannot be null" );
271         requireNonNull( request, "request cannot be null" );
272         // noop
273     }
274 
275 }