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