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         return getPathForArtifact( artifact, true );
106     }
107 
108     public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
109     {
110         return getPathForArtifact( artifact, false );
111     }
112 
113     public String getPathForLocalMetadata( Metadata metadata )
114     {
115         return getPath( metadata, "local" );
116     }
117 
118     public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
119     {
120         return getPath( metadata, getRepositoryKey( repository, context ) );
121     }
122 
123     String getRepositoryKey( RemoteRepository repository, String context )
124     {
125         String key;
126 
127         if ( repository.isRepositoryManager() )
128         {
129             // repository serves dynamic contents, take request parameters into account for key
130 
131             StringBuilder buffer = new StringBuilder( 128 );
132 
133             buffer.append( repository.getId() );
134 
135             buffer.append( '-' );
136 
137             SortedSet<String> subKeys = new TreeSet<>();
138             for ( RemoteRepository mirroredRepo : repository.getMirroredRepositories() )
139             {
140                 subKeys.add( mirroredRepo.getId() );
141             }
142 
143             SimpleDigest digest = new SimpleDigest();
144             digest.update( context );
145             for ( String subKey : subKeys )
146             {
147                 digest.update( subKey );
148             }
149             buffer.append( digest.digest() );
150 
151             key = buffer.toString();
152         }
153         else
154         {
155             // repository serves static contents, its id is sufficient as key
156 
157             key = repository.getId();
158         }
159 
160         return key;
161     }
162 
163     private String getPath( Metadata metadata, String repositoryKey )
164     {
165         StringBuilder path = new StringBuilder( 128 );
166 
167         if ( metadata.getGroupId().length() > 0 )
168         {
169             path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
170 
171             if ( metadata.getArtifactId().length() > 0 )
172             {
173                 path.append( metadata.getArtifactId() ).append( '/' );
174 
175                 if ( metadata.getVersion().length() > 0 )
176                 {
177                     path.append( metadata.getVersion() ).append( '/' );
178                 }
179             }
180         }
181 
182         path.append( insertRepositoryKey( metadata.getType(), repositoryKey ) );
183 
184         return path.toString();
185     }
186 
187     private String insertRepositoryKey( String filename, String repositoryKey )
188     {
189         String result;
190         int idx = filename.indexOf( '.' );
191         if ( idx < 0 )
192         {
193             result = filename + '-' + repositoryKey;
194         }
195         else
196         {
197             result = filename.substring( 0, idx ) + '-' + repositoryKey + filename.substring( idx );
198         }
199         return result;
200     }
201 
202     public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
203     {
204         String path = getPathForArtifact( request.getArtifact(), false );
205         File file = new File( getRepository().getBasedir(), path );
206 
207         LocalArtifactResult result = new LocalArtifactResult( request );
208         if ( file.isFile() )
209         {
210             result.setFile( file );
211             result.setAvailable( true );
212         }
213 
214         return result;
215     }
216 
217     public void add( RepositorySystemSession session, LocalArtifactRegistration request )
218     {
219         // noop
220     }
221 
222     @Override
223     public String toString()
224     {
225         return String.valueOf( getRepository() );
226     }
227 
228     public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
229     {
230         LocalMetadataResult result = new LocalMetadataResult( request );
231 
232         String path;
233 
234         Metadata metadata = request.getMetadata();
235         String context = request.getContext();
236         RemoteRepository remote = request.getRepository();
237 
238         if ( remote != null )
239         {
240             path = getPathForRemoteMetadata( metadata, remote, context );
241         }
242         else
243         {
244             path = getPathForLocalMetadata( metadata );
245         }
246 
247         File file = new File( getRepository().getBasedir(), path );
248         if ( file.isFile() )
249         {
250             result.setFile( file );
251         }
252 
253         return result;
254     }
255 
256     public void add( RepositorySystemSession session, LocalMetadataRegistration request )
257     {
258         // noop
259     }
260 
261 }