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.eclipse.aether.internal.impl;
20  
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.util.Objects;
24  import java.util.SortedSet;
25  import java.util.TreeSet;
26  import java.util.function.Function;
27  
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.metadata.Metadata;
31  import org.eclipse.aether.repository.ArtifactRepository;
32  import org.eclipse.aether.repository.LocalArtifactRegistration;
33  import org.eclipse.aether.repository.LocalArtifactRequest;
34  import org.eclipse.aether.repository.LocalArtifactResult;
35  import org.eclipse.aether.repository.LocalMetadataRegistration;
36  import org.eclipse.aether.repository.LocalMetadataRequest;
37  import org.eclipse.aether.repository.LocalMetadataResult;
38  import org.eclipse.aether.repository.LocalRepository;
39  import org.eclipse.aether.repository.LocalRepositoryManager;
40  import org.eclipse.aether.repository.RemoteRepository;
41  import org.eclipse.aether.util.StringDigestUtil;
42  
43  import static java.util.Objects.requireNonNull;
44  
45  /**
46   * A local repository manager that realizes the classical Maven 2.0 local repository.
47   */
48  class SimpleLocalRepositoryManager implements LocalRepositoryManager {
49  
50      private final LocalRepository repository;
51  
52      private final LocalPathComposer localPathComposer;
53  
54      private final Function<ArtifactRepository, String> idToPathSegmentFunction;
55  
56      SimpleLocalRepositoryManager(
57              Path basePath,
58              String type,
59              LocalPathComposer localPathComposer,
60              Function<ArtifactRepository, String> idToPathSegmentFunction) {
61          requireNonNull(basePath, "base directory cannot be null");
62          repository = new LocalRepository(basePath.toAbsolutePath(), type);
63          this.localPathComposer = requireNonNull(localPathComposer);
64          this.idToPathSegmentFunction = requireNonNull(idToPathSegmentFunction);
65      }
66  
67      @Override
68      public LocalRepository getRepository() {
69          return repository;
70      }
71  
72      @Override
73      public String getPathForLocalArtifact(Artifact artifact) {
74          requireNonNull(artifact, "artifact cannot be null");
75          return localPathComposer.getPathForArtifact(artifact, true);
76      }
77  
78      @Override
79      public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
80          requireNonNull(artifact, "artifact cannot be null");
81          requireNonNull(repository, "repository cannot be null");
82          return localPathComposer.getPathForArtifact(artifact, false);
83      }
84  
85      @Override
86      public String getPathForLocalMetadata(Metadata metadata) {
87          requireNonNull(metadata, "metadata cannot be null");
88          return localPathComposer.getPathForMetadata(metadata, "local");
89      }
90  
91      @Override
92      public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
93          requireNonNull(metadata, "metadata cannot be null");
94          requireNonNull(repository, "repository cannot be null");
95          return localPathComposer.getPathForMetadata(metadata, getRepositoryKey(repository, context));
96      }
97  
98      /**
99       * Returns {@link RemoteRepository#getId()}, unless {@link RemoteRepository#isRepositoryManager()} returns
100      * {@code true}, in which case this method creates unique identifier based on ID and current configuration
101      * of the remote repository (as it may change).
102      */
103     protected String getRepositoryKey(RemoteRepository repository, String context) {
104         String key;
105 
106         if (repository.isRepositoryManager()) {
107             // repository serves dynamic contents, take request parameters into account for key
108 
109             StringBuilder buffer = new StringBuilder(128);
110 
111             buffer.append(idToPathSegmentFunction.apply(repository));
112 
113             buffer.append('-');
114 
115             SortedSet<String> subKeys = new TreeSet<>();
116             for (RemoteRepository mirroredRepo : repository.getMirroredRepositories()) {
117                 subKeys.add(mirroredRepo.getId());
118             }
119 
120             StringDigestUtil sha1 = StringDigestUtil.sha1();
121             sha1.update(context);
122             for (String subKey : subKeys) {
123                 sha1.update(subKey);
124             }
125             buffer.append(sha1.digest());
126 
127             key = buffer.toString();
128         } else {
129             key = idToPathSegmentFunction.apply(repository);
130         }
131 
132         return key;
133     }
134 
135     @Override
136     public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
137         requireNonNull(session, "session cannot be null");
138         requireNonNull(request, "request cannot be null");
139         Artifact artifact = request.getArtifact();
140         LocalArtifactResult result = new LocalArtifactResult(request);
141 
142         Path filePath;
143 
144         // Local repository CANNOT have timestamped installed, they are created only during deploy
145         if (Objects.equals(artifact.getVersion(), artifact.getBaseVersion())) {
146             filePath = getAbsolutePathForLocalArtifact(artifact);
147             if (Files.isRegularFile(filePath)) {
148                 result.setPath(filePath);
149                 result.setAvailable(true);
150             }
151         }
152 
153         if (!result.isAvailable()) {
154             for (RemoteRepository repository : request.getRepositories()) {
155                 filePath = getAbsolutePathForRemoteArtifact(artifact, repository, request.getContext());
156                 if (Files.isRegularFile(filePath)) {
157                     result.setPath(filePath);
158                     result.setAvailable(true);
159                     break;
160                 }
161             }
162         }
163 
164         return result;
165     }
166 
167     @Override
168     public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
169         requireNonNull(session, "session cannot be null");
170         requireNonNull(request, "request cannot be null");
171         // noop
172     }
173 
174     @Override
175     public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
176         requireNonNull(session, "session cannot be null");
177         requireNonNull(request, "request cannot be null");
178         LocalMetadataResult result = new LocalMetadataResult(request);
179 
180         String path;
181 
182         Metadata metadata = request.getMetadata();
183         String context = request.getContext();
184         RemoteRepository remote = request.getRepository();
185 
186         if (remote != null) {
187             path = getPathForRemoteMetadata(metadata, remote, context);
188         } else {
189             path = getPathForLocalMetadata(metadata);
190         }
191 
192         Path filePath = getRepository().getBasePath().resolve(path);
193         if (Files.isRegularFile(filePath)) {
194             result.setPath(filePath);
195         }
196 
197         return result;
198     }
199 
200     @Override
201     public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
202         requireNonNull(session, "session cannot be null");
203         requireNonNull(request, "request cannot be null");
204         // noop
205     }
206 
207     @Override
208     public String toString() {
209         return String.valueOf(getRepository());
210     }
211 }