View Javadoc
1   package org.apache.maven.project;
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  
24  import org.eclipse.aether.RepositorySystemSession;
25  import org.eclipse.aether.artifact.Artifact;
26  import org.eclipse.aether.metadata.Metadata;
27  import org.eclipse.aether.repository.LocalArtifactRegistration;
28  import org.eclipse.aether.repository.LocalArtifactRequest;
29  import org.eclipse.aether.repository.LocalArtifactResult;
30  import org.eclipse.aether.repository.LocalMetadataRegistration;
31  import org.eclipse.aether.repository.LocalMetadataRequest;
32  import org.eclipse.aether.repository.LocalMetadataResult;
33  import org.eclipse.aether.repository.LocalRepository;
34  import org.eclipse.aether.repository.LocalRepositoryManager;
35  import org.eclipse.aether.repository.RemoteRepository;
36  
37  /**
38   * @author Benjamin Bentmann
39   */
40  public class LegacyLocalRepositoryManager
41      implements LocalRepositoryManager
42  {
43  
44      private final LocalRepository repository;
45  
46      LegacyLocalRepositoryManager( File basedir )
47      {
48          this.repository = new LocalRepository( basedir.getAbsoluteFile(), "legacy" );
49      }
50  
51      public LocalRepository getRepository()
52      {
53          return repository;
54      }
55  
56      public String getPathForLocalArtifact( Artifact artifact )
57      {
58          StringBuilder path = new StringBuilder( 128 );
59  
60          path.append( artifact.getGroupId() ).append( '/' );
61  
62          path.append( artifact.getExtension() ).append( "s/" );
63  
64          path.append( artifact.getArtifactId() ).append( '-' ).append( artifact.getVersion() );
65  
66          if ( artifact.getClassifier().length() > 0 )
67          {
68              path.append( '-' ).append( artifact.getClassifier() );
69          }
70  
71          path.append( '.' ).append( artifact.getExtension() );
72  
73          return path.toString();
74      }
75  
76      public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
77      {
78          return getPathForLocalArtifact( artifact );
79      }
80  
81      public String getPathForLocalMetadata( Metadata metadata )
82      {
83          return getPath( metadata, "local" );
84      }
85  
86      public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
87      {
88          return getPath( metadata, getRepositoryKey( repository, context ) );
89      }
90  
91      String getRepositoryKey( RemoteRepository repository, String context )
92      {
93          return repository.getId();
94      }
95  
96      private String getPath( Metadata metadata, String repositoryKey )
97      {
98          StringBuilder path = new StringBuilder( 128 );
99  
100         if ( metadata.getGroupId().length() > 0 )
101         {
102             path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
103 
104             if ( metadata.getArtifactId().length() > 0 )
105             {
106                 path.append( metadata.getArtifactId() ).append( '/' );
107 
108                 if ( metadata.getVersion().length() > 0 )
109                 {
110                     path.append( metadata.getVersion() ).append( '/' );
111                 }
112             }
113         }
114 
115         path.append( insertRepositoryKey( metadata.getType(), repositoryKey ) );
116 
117         return path.toString();
118     }
119 
120     private String insertRepositoryKey( String filename, String repositoryKey )
121     {
122         String result;
123         int idx = filename.indexOf( '.' );
124         if ( idx < 0 )
125         {
126             result = filename + '-' + repositoryKey;
127         }
128         else
129         {
130             result = filename.substring( 0, idx ) + '-' + repositoryKey + filename.substring( idx );
131         }
132         return result;
133     }
134 
135     public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
136     {
137         String path = getPathForLocalArtifact( request.getArtifact() );
138         File file = new File( getRepository().getBasedir(), path );
139 
140         LocalArtifactResult result = new LocalArtifactResult( request );
141         if ( file.isFile() )
142         {
143             result.setFile( file );
144             result.setAvailable( true );
145         }
146 
147         return result;
148     }
149 
150     public void add( RepositorySystemSession session, LocalArtifactRegistration request )
151     {
152         // noop
153     }
154 
155     public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
156     {
157         LocalMetadataResult result = new LocalMetadataResult( request );
158 
159         String path;
160 
161         Metadata metadata = request.getMetadata();
162         String context = request.getContext();
163         RemoteRepository remote = request.getRepository();
164 
165         if ( remote != null )
166         {
167             path = getPathForRemoteMetadata( metadata, remote, context );
168         }
169         else
170         {
171             path = getPathForLocalMetadata( metadata );
172         }
173 
174         File file = new File( getRepository().getBasedir(), path );
175         if ( file.isFile() )
176         {
177             result.setFile( file );
178         }
179 
180         return result;
181     }
182 
183     public void add( RepositorySystemSession session, LocalMetadataRegistration request )
184     {
185         // noop
186     }
187 
188     public String toString()
189     {
190         return String.valueOf( getRepository() );
191     }
192 }