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  import org.eclipse.aether.spi.log.Logger;
40  
41  /**
42   * A local repository manager that realizes the classical Maven 2.0 local repository.
43   */
44  class SimpleLocalRepositoryManager
45      implements LocalRepositoryManager
46  {
47  
48      private final LocalRepository repository;
49  
50      SimpleLocalRepositoryManager( File basedir )
51      {
52          this( basedir, "simple" );
53      }
54  
55      SimpleLocalRepositoryManager( String basedir )
56      {
57          this( ( basedir != null ) ? new File( basedir ) : null, "simple" );
58      }
59  
60      SimpleLocalRepositoryManager( File basedir, String type )
61      {
62          requireNonNull( basedir, "base directory cannot be null" );
63          repository = new LocalRepository( basedir.getAbsoluteFile(), type );
64      }
65  
66      public SimpleLocalRepositoryManager setLogger( Logger logger )
67      {
68          return this;
69      }
70  
71      public LocalRepository getRepository()
72      {
73          return repository;
74      }
75  
76      String getPathForArtifact( Artifact artifact, boolean local )
77      {
78          StringBuilder path = new StringBuilder( 128 );
79  
80          path.append( artifact.getGroupId().replace( '.', '/' ) ).append( '/' );
81  
82          path.append( artifact.getArtifactId() ).append( '/' );
83  
84          path.append( artifact.getBaseVersion() ).append( '/' );
85  
86          path.append( artifact.getArtifactId() ).append( '-' );
87          if ( local )
88          {
89              path.append( artifact.getBaseVersion() );
90          }
91          else
92          {
93              path.append( artifact.getVersion() );
94          }
95  
96          if ( artifact.getClassifier().length() > 0 )
97          {
98              path.append( '-' ).append( artifact.getClassifier() );
99          }
100 
101         if ( artifact.getExtension().length() > 0 )
102         {
103             path.append( '.' ).append( artifact.getExtension() );
104         }
105 
106         return path.toString();
107     }
108 
109     public String getPathForLocalArtifact( Artifact artifact )
110     {
111         return getPathForArtifact( artifact, true );
112     }
113 
114     public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
115     {
116         return getPathForArtifact( artifact, false );
117     }
118 
119     public String getPathForLocalMetadata( Metadata metadata )
120     {
121         return getPath( metadata, "local" );
122     }
123 
124     public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
125     {
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<String>();
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         String path = getPathForArtifact( request.getArtifact(), false );
211         File file = new File( getRepository().getBasedir(), path );
212 
213         LocalArtifactResult result = new LocalArtifactResult( request );
214         if ( file.isFile() )
215         {
216             result.setFile( file );
217             result.setAvailable( true );
218         }
219 
220         return result;
221     }
222 
223     public void add( RepositorySystemSession session, LocalArtifactRegistration request )
224     {
225         // noop
226     }
227 
228     @Override
229     public String toString()
230     {
231         return String.valueOf( getRepository() );
232     }
233 
234     public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
235     {
236         LocalMetadataResult result = new LocalMetadataResult( request );
237 
238         String path;
239 
240         Metadata metadata = request.getMetadata();
241         String context = request.getContext();
242         RemoteRepository remote = request.getRepository();
243 
244         if ( remote != null )
245         {
246             path = getPathForRemoteMetadata( metadata, remote, context );
247         }
248         else
249         {
250             path = getPathForLocalMetadata( metadata );
251         }
252 
253         File file = new File( getRepository().getBasedir(), path );
254         if ( file.isFile() )
255         {
256             result.setFile( file );
257         }
258 
259         return result;
260     }
261 
262     public void add( RepositorySystemSession session, LocalMetadataRegistration request )
263     {
264         // noop
265     }
266 
267 }