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.eclipse.aether.util.ConfigUtils;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  import static java.util.Objects.requireNonNull;
44  
45  /**
46   * Sparse file {@link FileTrustedChecksumsSourceSupport} implementation that use specified directory as base
47   * directory, where it expects artifacts checksums on standard Maven2 "local" layout. This implementation uses Artifact
48   * coordinates solely to form path from basedir, pretty much as Maven local repository does.
49   * <p>
50   * The source by default is "origin aware", it will factor in origin repository ID as well into base directory name
51   * (for example ".checksums/central/...").
52   * <p>
53   * The checksums files are directly loaded from disk, so in-flight file changes during lifecycle of session are picked
54   * up. This implementation can be simultaneously used to lookup and also write checksums. The written checksums
55   * will become visible across all sessions right after the moment they were written.
56   * <p>
57   * The name of this implementation is "sparseDirectory".
58   *
59   * @see LocalPathComposer
60   * @since 1.9.0
61   */
62  @Singleton
63  @Named(SparseDirectoryTrustedChecksumsSource.NAME)
64  public final class SparseDirectoryTrustedChecksumsSource extends FileTrustedChecksumsSourceSupport {
65      public static final String NAME = "sparseDirectory";
66  
67      private static final String CONFIG_PROPS_PREFIX =
68              FileTrustedChecksumsSourceSupport.CONFIG_PROPS_PREFIX + NAME + ".";
69  
70      /**
71       * Is checksum source enabled?
72       *
73       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
74       * @configurationType {@link java.lang.Boolean}
75       * @configurationDefaultValue false
76       */
77      public static final String CONFIG_PROP_ENABLED = FileTrustedChecksumsSourceSupport.CONFIG_PROPS_PREFIX + NAME;
78  
79      /**
80       * The basedir where checksums are. If relative, is resolved from local repository root.
81       *
82       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
83       * @configurationType {@link java.lang.String}
84       * @configurationDefaultValue {@link #LOCAL_REPO_PREFIX_DIR}
85       */
86      public static final String CONFIG_PROP_BASEDIR = CONFIG_PROPS_PREFIX + "basedir";
87  
88      public static final String LOCAL_REPO_PREFIX_DIR = ".checksums";
89  
90      /**
91       * Is source origin aware?
92       *
93       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
94       * @configurationType {@link java.lang.Boolean}
95       * @configurationDefaultValue true
96       */
97      public static final String CONFIG_PROP_ORIGIN_AWARE = CONFIG_PROPS_PREFIX + "originAware";
98  
99      private static final Logger LOGGER = LoggerFactory.getLogger(SparseDirectoryTrustedChecksumsSource.class);
100 
101     private final FileProcessor fileProcessor;
102 
103     private final LocalPathComposer localPathComposer;
104 
105     @Inject
106     public SparseDirectoryTrustedChecksumsSource(FileProcessor fileProcessor, LocalPathComposer localPathComposer) {
107         this.fileProcessor = requireNonNull(fileProcessor);
108         this.localPathComposer = requireNonNull(localPathComposer);
109     }
110 
111     @Override
112     protected boolean isEnabled(RepositorySystemSession session) {
113         return ConfigUtils.getBoolean(session, false, CONFIG_PROP_ENABLED);
114     }
115 
116     private boolean isOriginAware(RepositorySystemSession session) {
117         return ConfigUtils.getBoolean(session, true, CONFIG_PROP_ORIGIN_AWARE);
118     }
119 
120     @Override
121     protected Map<String, String> doGetTrustedArtifactChecksums(
122             RepositorySystemSession session,
123             Artifact artifact,
124             ArtifactRepository artifactRepository,
125             List<ChecksumAlgorithmFactory> checksumAlgorithmFactories) {
126         final boolean originAware = isOriginAware(session);
127         final HashMap<String, String> checksums = new HashMap<>();
128         Path basedir = getBasedir(session, LOCAL_REPO_PREFIX_DIR, CONFIG_PROP_BASEDIR, false);
129         if (Files.isDirectory(basedir)) {
130             for (ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories) {
131                 Path checksumPath = basedir.resolve(
132                         calculateArtifactPath(originAware, artifact, artifactRepository, checksumAlgorithmFactory));
133 
134                 if (!Files.isRegularFile(checksumPath)) {
135                     LOGGER.debug(
136                             "Artifact '{}' trusted checksum '{}' not found on path '{}'",
137                             artifact,
138                             checksumAlgorithmFactory.getName(),
139                             checksumPath);
140                     continue;
141                 }
142 
143                 try {
144                     String checksum = fileProcessor.readChecksum(checksumPath.toFile());
145                     if (checksum != null) {
146                         checksums.put(checksumAlgorithmFactory.getName(), checksum);
147                     }
148                 } catch (IOException e) {
149                     // unexpected, log
150                     LOGGER.warn(
151                             "Could not read artifact '{}' trusted checksum on path '{}'", artifact, checksumPath, e);
152                     throw new UncheckedIOException(e);
153                 }
154             }
155         }
156         return checksums;
157     }
158 
159     @Override
160     protected Writer doGetTrustedArtifactChecksumsWriter(RepositorySystemSession session) {
161         return new SparseDirectoryWriter(
162                 getBasedir(session, LOCAL_REPO_PREFIX_DIR, CONFIG_PROP_BASEDIR, true), isOriginAware(session));
163     }
164 
165     private String calculateArtifactPath(
166             boolean originAware,
167             Artifact artifact,
168             ArtifactRepository artifactRepository,
169             ChecksumAlgorithmFactory checksumAlgorithmFactory) {
170         String path = localPathComposer.getPathForArtifact(artifact, false) + "."
171                 + checksumAlgorithmFactory.getFileExtension();
172         if (originAware) {
173             path = artifactRepository.getId() + "/" + path;
174         }
175         return path;
176     }
177 
178     private class SparseDirectoryWriter implements Writer {
179         private final Path basedir;
180 
181         private final boolean originAware;
182 
183         private SparseDirectoryWriter(Path basedir, boolean originAware) {
184             this.basedir = basedir;
185             this.originAware = originAware;
186         }
187 
188         @Override
189         public void addTrustedArtifactChecksums(
190                 Artifact artifact,
191                 ArtifactRepository artifactRepository,
192                 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories,
193                 Map<String, String> trustedArtifactChecksums)
194                 throws IOException {
195             for (ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories) {
196                 Path checksumPath = basedir.resolve(
197                         calculateArtifactPath(originAware, artifact, artifactRepository, checksumAlgorithmFactory));
198                 String checksum = requireNonNull(trustedArtifactChecksums.get(checksumAlgorithmFactory.getName()));
199                 fileProcessor.writeChecksum(checksumPath.toFile(), checksum);
200             }
201         }
202     }
203 }