View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.io.File;
22  
23  import org.eclipse.aether.RepositorySystemSession;
24  import org.eclipse.aether.artifact.Artifact;
25  import org.eclipse.aether.metadata.Metadata;
26  import org.eclipse.aether.repository.LocalArtifactRegistration;
27  import org.eclipse.aether.repository.LocalArtifactRequest;
28  import org.eclipse.aether.repository.LocalArtifactResult;
29  import org.eclipse.aether.repository.LocalMetadataRegistration;
30  import org.eclipse.aether.repository.LocalMetadataRequest;
31  import org.eclipse.aether.repository.LocalMetadataResult;
32  import org.eclipse.aether.repository.LocalRepository;
33  import org.eclipse.aether.repository.LocalRepositoryManager;
34  import org.eclipse.aether.repository.RemoteRepository;
35  
36  /**
37   */
38  public class LegacyLocalRepositoryManager implements LocalRepositoryManager {
39  
40      private final LocalRepository repository;
41  
42      LegacyLocalRepositoryManager(File basedir) {
43          this.repository = new LocalRepository(basedir.getAbsoluteFile(), "legacy");
44      }
45  
46      public LocalRepository getRepository() {
47          return repository;
48      }
49  
50      public String getPathForLocalArtifact(Artifact artifact) {
51          StringBuilder path = new StringBuilder(128);
52  
53          path.append(artifact.getGroupId()).append('/');
54  
55          path.append(artifact.getExtension()).append("s/");
56  
57          path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
58  
59          if (!artifact.getClassifier().isEmpty()) {
60              path.append('-').append(artifact.getClassifier());
61          }
62  
63          path.append('.').append(artifact.getExtension());
64  
65          return path.toString();
66      }
67  
68      public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
69          return getPathForLocalArtifact(artifact);
70      }
71  
72      public String getPathForLocalMetadata(Metadata metadata) {
73          return getPath(metadata, "local");
74      }
75  
76      public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
77          return getPath(metadata, getRepositoryKey(repository, context));
78      }
79  
80      String getRepositoryKey(RemoteRepository repository, String context) {
81          return repository.getId();
82      }
83  
84      private String getPath(Metadata metadata, String repositoryKey) {
85          StringBuilder path = new StringBuilder(128);
86  
87          if (!metadata.getGroupId().isEmpty()) {
88              path.append(metadata.getGroupId().replace('.', '/')).append('/');
89  
90              if (!metadata.getArtifactId().isEmpty()) {
91                  path.append(metadata.getArtifactId()).append('/');
92  
93                  if (!metadata.getVersion().isEmpty()) {
94                      path.append(metadata.getVersion()).append('/');
95                  }
96              }
97          }
98  
99          path.append(insertRepositoryKey(metadata.getType(), repositoryKey));
100 
101         return path.toString();
102     }
103 
104     private String insertRepositoryKey(String filename, String repositoryKey) {
105         String result;
106         int idx = filename.indexOf('.');
107         if (idx < 0) {
108             result = filename + '-' + repositoryKey;
109         } else {
110             result = filename.substring(0, idx) + '-' + repositoryKey + filename.substring(idx);
111         }
112         return result;
113     }
114 
115     public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
116         String path = getPathForLocalArtifact(request.getArtifact());
117         File file = new File(getRepository().getBasedir(), path);
118 
119         LocalArtifactResult result = new LocalArtifactResult(request);
120         if (file.isFile()) {
121             result.setFile(file);
122             result.setAvailable(true);
123         }
124 
125         return result;
126     }
127 
128     public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
129         // noop
130     }
131 
132     public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
133         LocalMetadataResult result = new LocalMetadataResult(request);
134 
135         String path;
136 
137         Metadata metadata = request.getMetadata();
138         String context = request.getContext();
139         RemoteRepository remote = request.getRepository();
140 
141         if (remote != null) {
142             path = getPathForRemoteMetadata(metadata, remote, context);
143         } else {
144             path = getPathForLocalMetadata(metadata);
145         }
146 
147         File file = new File(getRepository().getBasedir(), path);
148         if (file.isFile()) {
149             result.setFile(file);
150         }
151 
152         return result;
153     }
154 
155     public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
156         // noop
157     }
158 
159     public String toString() {
160         return String.valueOf(getRepository());
161     }
162 }