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.checksum;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.IOException;
26  import java.io.UncheckedIOException;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.eclipse.aether.RepositorySystemSession;
34  import org.eclipse.aether.artifact.Artifact;
35  import org.eclipse.aether.internal.impl.LocalPathComposer;
36  import org.eclipse.aether.repository.ArtifactRepository;
37  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
38  import org.eclipse.aether.spi.io.FileProcessor;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  import static java.util.Objects.requireNonNull;
43  
44  /**
45   * Sparse file {@link FileTrustedChecksumsSourceSupport} implementation that use specified directory as base
46   * directory, where it expects artifacts checksums on standard Maven2 "local" layout. This implementation uses Artifact
47   * coordinates solely to form path from basedir, pretty much as Maven local repository does.
48   * <p>
49   * The source by default is "origin aware", it will factor in origin repository ID as well into base directory name
50   * (for example ".checksums/central/...").
51   * <p>
52   * The checksums files are directly loaded from disk, so in-flight file changes during lifecycle of session are picked
53   * up. This implementation can be simultaneously used to lookup and also write checksums. The written checksums
54   * will become visible across all sessions right after the moment they were written.
55   * <p>
56   * The name of this implementation is "sparseDirectory".
57   *
58   * @see LocalPathComposer
59   * @since 1.9.0
60   */
61  @Singleton
62  @Named(SparseDirectoryTrustedChecksumsSource.NAME)
63  public final class SparseDirectoryTrustedChecksumsSource extends FileTrustedChecksumsSourceSupport {
64      public static final String NAME = "sparseDirectory";
65  
66      private static final Logger LOGGER = LoggerFactory.getLogger(SparseDirectoryTrustedChecksumsSource.class);
67  
68      private final FileProcessor fileProcessor;
69  
70      private final LocalPathComposer localPathComposer;
71  
72      @Inject
73      public SparseDirectoryTrustedChecksumsSource(FileProcessor fileProcessor, LocalPathComposer localPathComposer) {
74          super(NAME);
75          this.fileProcessor = requireNonNull(fileProcessor);
76          this.localPathComposer = requireNonNull(localPathComposer);
77      }
78  
79      @Override
80      protected Map<String, String> doGetTrustedArtifactChecksums(
81              RepositorySystemSession session,
82              Artifact artifact,
83              ArtifactRepository artifactRepository,
84              List<ChecksumAlgorithmFactory> checksumAlgorithmFactories) {
85          final boolean originAware = isOriginAware(session);
86          final HashMap<String, String> checksums = new HashMap<>();
87          Path basedir = getBasedir(session, false);
88          if (Files.isDirectory(basedir)) {
89              for (ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories) {
90                  Path checksumPath = basedir.resolve(
91                          calculateArtifactPath(originAware, artifact, artifactRepository, checksumAlgorithmFactory));
92  
93                  if (!Files.isRegularFile(checksumPath)) {
94                      LOGGER.debug(
95                              "Artifact '{}' trusted checksum '{}' not found on path '{}'",
96                              artifact,
97                              checksumAlgorithmFactory.getName(),
98                              checksumPath);
99                      continue;
100                 }
101 
102                 try {
103                     String checksum = fileProcessor.readChecksum(checksumPath.toFile());
104                     if (checksum != null) {
105                         checksums.put(checksumAlgorithmFactory.getName(), checksum);
106                     }
107                 } catch (IOException e) {
108                     // unexpected, log
109                     LOGGER.warn(
110                             "Could not read artifact '{}' trusted checksum on path '{}'", artifact, checksumPath, e);
111                     throw new UncheckedIOException(e);
112                 }
113             }
114         }
115         return checksums;
116     }
117 
118     @Override
119     protected SparseDirectoryWriter doGetTrustedArtifactChecksumsWriter(RepositorySystemSession session) {
120         return new SparseDirectoryWriter(getBasedir(session, true), isOriginAware(session));
121     }
122 
123     private String calculateArtifactPath(
124             boolean originAware,
125             Artifact artifact,
126             ArtifactRepository artifactRepository,
127             ChecksumAlgorithmFactory checksumAlgorithmFactory) {
128         String path = localPathComposer.getPathForArtifact(artifact, false) + "."
129                 + checksumAlgorithmFactory.getFileExtension();
130         if (originAware) {
131             path = artifactRepository.getId() + "/" + path;
132         }
133         return path;
134     }
135 
136     private class SparseDirectoryWriter implements Writer {
137         private final Path basedir;
138 
139         private final boolean originAware;
140 
141         private SparseDirectoryWriter(Path basedir, boolean originAware) {
142             this.basedir = basedir;
143             this.originAware = originAware;
144         }
145 
146         @Override
147         public void addTrustedArtifactChecksums(
148                 Artifact artifact,
149                 ArtifactRepository artifactRepository,
150                 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories,
151                 Map<String, String> trustedArtifactChecksums)
152                 throws IOException {
153             for (ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories) {
154                 Path checksumPath = basedir.resolve(
155                         calculateArtifactPath(originAware, artifact, artifactRepository, checksumAlgorithmFactory));
156                 String checksum = requireNonNull(trustedArtifactChecksums.get(checksumAlgorithmFactory.getName()));
157                 fileProcessor.writeChecksum(checksumPath.toFile(), checksum);
158             }
159         }
160     }
161 }