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