[ https://issues.apache.org/jira/browse/MRESOLVER-269?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17611144#comment-17611144 ]
ASF GitHub Bot commented on MRESOLVER-269: ------------------------------------------ michael-o commented on code in PR #199: URL: https://github.com/apache/maven-resolver/pull/199#discussion_r983824905 ########## maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/checksum/FileTrustedChecksumsSourceSupport.java: ########## @@ -0,0 +1,149 @@ +package org.eclipse.aether.internal.impl.checksum; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; + +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.repository.ArtifactRepository; +import org.eclipse.aether.spi.checksums.TrustedChecksumsSource; +import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; +import org.eclipse.aether.util.ConfigUtils; +import org.eclipse.aether.util.DirectoryUtils; + +import static java.util.Objects.requireNonNull; + +/** + * Support class for implementing {@link TrustedChecksumsSource} backed by local filesystem. It implements basic support + * like bqsedir calculation, "enabled" flag and "originAware" flag. + * <p> + * The configuration keys supported: + * <ul> + * <li><pre>aether.trustedChecksumsSource.${name}.enabled</pre> (boolean) must be explicitly set to "true" + * to become enabled</li> + * <li><pre>aether.trustedChecksumsSource.${name}.basedir</pre> (string, path) directory from where implementation + * can use files. If unset, default value is ".checksums" and is resolved from local repository basedir.</li> + * <li><pre>aether.trustedChecksumsSource.${name}.originAware</pre> (boolean) whether to make implementation + * "originAware", to factor in origin repository ID as well or not.</li> + * </ul> + * <p> + * This implementation ensures that implementations have "name" property, used in configuration properties above. + * + * @since TBD + */ +abstract class FileTrustedChecksumsSourceSupport + implements TrustedChecksumsSource +{ + private static final String CONFIG_PROP_PREFIX = "aether.trustedChecksumsSource."; + + private static final String CONF_NAME_ENABLED = "enabled"; + + private static final String CONF_NAME_BASEDIR = "basedir"; + + private static final String CONF_NAME_ORIGIN_AWARE = "originAware"; + + /** + * Visible for testing. + */ + static final String LOCAL_REPO_PREFIX = ".checksums"; + + private final String name; + + FileTrustedChecksumsSourceSupport( String name ) + { + this.name = requireNonNull( name ); + } + + /** + * The implementation will call into underlying code only if enabled, chosen basedir exists, and requested + * checksum algorithms are not empty. + */ + @Override + public Map<String, String> getTrustedArtifactChecksums( RepositorySystemSession session, + Artifact artifact, + ArtifactRepository artifactRepository, + List<ChecksumAlgorithmFactory> checksumAlgorithmFactories ) + { + boolean enabled = ConfigUtils.getBoolean( session, false, configPropKey( CONF_NAME_ENABLED ) ); + if ( enabled ) + { + Path baseDir = getBasedir( session ); + if ( baseDir != null && !checksumAlgorithmFactories.isEmpty() ) + { + Map<String, String> result = performLookup( + session, baseDir, artifact, artifactRepository, checksumAlgorithmFactories ); + + return result == null || result.isEmpty() ? null : result; + } + } + return null; + } + + protected abstract Map<String, String> performLookup( RepositorySystemSession session, + Path baseDir, + Artifact artifact, + ArtifactRepository artifactRepository, + List<ChecksumAlgorithmFactory> checksumAlgorithmFactories ); + + /** + * To be used by underlying implementations to form configuration property keys properly scoped. + */ + protected String configPropKey( String name ) + { + requireNonNull( name ); + return CONFIG_PROP_PREFIX + this.name + "." + name; + } + + /** + * Returns {@code true} if session configuration contains "originAware" property set to {@code true}. + */ + protected boolean isOriginAware( RepositorySystemSession session ) + { + return ConfigUtils.getBoolean( session, false, configPropKey( CONF_NAME_ORIGIN_AWARE ) ); + } + + /** + * Uses common {@link DirectoryUtils} to calculate (but not) create basedir for this implementation. Returns + * {@code null} if the calculated basedir does not exist. + */ + private Path getBasedir( RepositorySystemSession session ) + { + try + { + Path basedir = DirectoryUtils.resolveDirectory( + session, LOCAL_REPO_PREFIX, configPropKey( CONF_NAME_BASEDIR ), false ); + if ( !Files.isDirectory( basedir ) ) Review Comment: Alright, makes sense. The developer has to take care of that, not us. > Allow more compact storage of provided checksums > ------------------------------------------------ > > Key: MRESOLVER-269 > URL: https://issues.apache.org/jira/browse/MRESOLVER-269 > Project: Maven Resolver > Issue Type: Improvement > Components: Resolver > Reporter: Rafael Winterhalter > Assignee: Tamás Cservenák > Priority: Major > Fix For: resolver-next > > > While the repository layout makes sense for storage outside of a project, it > would be more convenient to store checksums in a single file (per algorithm) > when keeping checksums along when storing these checksums within a project. > This makes the storage easier to version control and avoids the overhead of > storing a lot of files in version control what often creates some overhead. > Ideally, Maven could support such files out of the box by shipping a provider > for such files. -- This message was sent by Atlassian Jira (v8.20.10#820010)