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