michael-o commented on a change in pull request #141: URL: https://github.com/apache/maven-resolver/pull/141#discussion_r795242905
########## File path: maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/FileProvidedChecksumsSource.java ########## @@ -0,0 +1,193 @@ +package org.eclipse.aether.internal.impl; + +/* + * 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 org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.spi.connector.ArtifactDownload; +import org.eclipse.aether.spi.connector.MetadataDownload; +import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; +import org.eclipse.aether.spi.connector.checksum.ProvidedChecksumsSource; +import org.eclipse.aether.spi.io.FileProcessor; +import org.eclipse.aether.util.ConfigUtils; +import org.eclipse.aether.util.artifact.ArtifactIdUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static java.util.Objects.requireNonNull; + +/** + * Local filesystem backed {@link ProvidedChecksumsSource} implementation that use specified directory as base + * directory, where it expects artifacts checksums on standard Maven2 "local" layout. This implementation uses Artifact + * (and Metadata) coordinates solely to form path from baseDir (for Metadata file name is + * {@code maven-metadata-local.xml.sha1} in case of SHA-1 checksum). + * + * @since TBD + */ +@Singleton +@Named( FileProvidedChecksumsSource.NAME ) +public final class FileProvidedChecksumsSource + implements ProvidedChecksumsSource +{ + public static final String NAME = "file"; + + static final String CONFIG_PROP_BASE_DIR = "aether.artifactResolver.providedChecksumsSource.file.baseDir"; + + static final String LOCAL_REPO_PREFIX = ".checksums"; + + private static final Logger LOGGER = LoggerFactory.getLogger( FileProvidedChecksumsSource.class ); + + private final FileProcessor fileProcessor; + + private final SimpleLocalRepositoryManager simpleLocalRepositoryManager; + + @Inject + public FileProvidedChecksumsSource( FileProcessor fileProcessor ) + { + this.fileProcessor = requireNonNull( fileProcessor ); + // we really needs just "local layout" from it (relative paths), so baseDir here is irrelevant + this.simpleLocalRepositoryManager = new SimpleLocalRepositoryManager( new File( "" ) ); + } + + @Override + public Map<String, String> getProvidedMetadataChecksums( RepositorySystemSession session, + MetadataDownload transfer, + List<ChecksumAlgorithmFactory> checksumAlgorithmFactories ) + { + Path baseDir = getBaseDir( session ); + if ( baseDir == null ) + { + return null; + } + ArrayList<ChecksumFilePath> checksumFilePaths = new ArrayList<>( checksumAlgorithmFactories.size() ); + for ( ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories ) + { + checksumFilePaths.add( new ChecksumFilePath( + simpleLocalRepositoryManager.getPathForLocalMetadata( transfer.getMetadata() ) + '.' + + checksumAlgorithmFactory.getFileExtension(), checksumAlgorithmFactory ) ); + } + return getProvidedChecksums( baseDir, checksumFilePaths, String.valueOf( transfer.getMetadata() ) ); + } + + @Override + public Map<String, String> getProvidedArtifactChecksums( RepositorySystemSession session, + ArtifactDownload transfer, + List<ChecksumAlgorithmFactory> checksumAlgorithmFactories ) + { + Path baseDir = getBaseDir( session ); + if ( baseDir == null ) + { + return null; + } + ArrayList<ChecksumFilePath> checksumFilePaths = new ArrayList<>( checksumAlgorithmFactories.size() ); + for ( ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories ) + { + checksumFilePaths.add( new ChecksumFilePath( + simpleLocalRepositoryManager.getPathForArtifact( transfer.getArtifact(), false ) + '.' + + checksumAlgorithmFactory.getFileExtension(), checksumAlgorithmFactory ) ); + } + return getProvidedChecksums( baseDir, checksumFilePaths, ArtifactIdUtils.toId( transfer.getArtifact() ) ); + } + + /** + * May return {@code null}. + */ + private Map<String, String> getProvidedChecksums( Path baseDir, + List<ChecksumFilePath> checksumFilePaths, + String subjectId ) + { + HashMap<String, String> checksums = new HashMap<>(); + for ( ChecksumFilePath checksumFilePath : checksumFilePaths ) + { + Path checksumPath = baseDir.resolve( checksumFilePath.path ); + if ( Files.isReadable( checksumPath ) ) + { + try + { + String checksum = fileProcessor.readChecksum( checksumPath.toFile() ); + if ( checksum != null ) + { + if ( LOGGER.isDebugEnabled() ) Review comment: This is redudant with SLF4J. ########## File path: maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/FileProvidedChecksumsSource.java ########## @@ -0,0 +1,193 @@ +package org.eclipse.aether.internal.impl; + +/* + * 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 org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.spi.connector.ArtifactDownload; +import org.eclipse.aether.spi.connector.MetadataDownload; +import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; +import org.eclipse.aether.spi.connector.checksum.ProvidedChecksumsSource; +import org.eclipse.aether.spi.io.FileProcessor; +import org.eclipse.aether.util.ConfigUtils; +import org.eclipse.aether.util.artifact.ArtifactIdUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static java.util.Objects.requireNonNull; + +/** + * Local filesystem backed {@link ProvidedChecksumsSource} implementation that use specified directory as base + * directory, where it expects artifacts checksums on standard Maven2 "local" layout. This implementation uses Artifact + * (and Metadata) coordinates solely to form path from baseDir (for Metadata file name is + * {@code maven-metadata-local.xml.sha1} in case of SHA-1 checksum). + * + * @since TBD + */ +@Singleton +@Named( FileProvidedChecksumsSource.NAME ) +public final class FileProvidedChecksumsSource + implements ProvidedChecksumsSource +{ + public static final String NAME = "file"; + + static final String CONFIG_PROP_BASE_DIR = "aether.artifactResolver.providedChecksumsSource.file.baseDir"; + + static final String LOCAL_REPO_PREFIX = ".checksums"; + + private static final Logger LOGGER = LoggerFactory.getLogger( FileProvidedChecksumsSource.class ); + + private final FileProcessor fileProcessor; + + private final SimpleLocalRepositoryManager simpleLocalRepositoryManager; + + @Inject + public FileProvidedChecksumsSource( FileProcessor fileProcessor ) + { + this.fileProcessor = requireNonNull( fileProcessor ); + // we really needs just "local layout" from it (relative paths), so baseDir here is irrelevant + this.simpleLocalRepositoryManager = new SimpleLocalRepositoryManager( new File( "" ) ); + } + + @Override + public Map<String, String> getProvidedMetadataChecksums( RepositorySystemSession session, + MetadataDownload transfer, + List<ChecksumAlgorithmFactory> checksumAlgorithmFactories ) + { + Path baseDir = getBaseDir( session ); + if ( baseDir == null ) + { + return null; + } + ArrayList<ChecksumFilePath> checksumFilePaths = new ArrayList<>( checksumAlgorithmFactories.size() ); + for ( ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories ) + { + checksumFilePaths.add( new ChecksumFilePath( + simpleLocalRepositoryManager.getPathForLocalMetadata( transfer.getMetadata() ) + '.' + + checksumAlgorithmFactory.getFileExtension(), checksumAlgorithmFactory ) ); + } + return getProvidedChecksums( baseDir, checksumFilePaths, String.valueOf( transfer.getMetadata() ) ); + } + + @Override + public Map<String, String> getProvidedArtifactChecksums( RepositorySystemSession session, + ArtifactDownload transfer, + List<ChecksumAlgorithmFactory> checksumAlgorithmFactories ) + { + Path baseDir = getBaseDir( session ); + if ( baseDir == null ) + { + return null; + } + ArrayList<ChecksumFilePath> checksumFilePaths = new ArrayList<>( checksumAlgorithmFactories.size() ); + for ( ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories ) + { + checksumFilePaths.add( new ChecksumFilePath( + simpleLocalRepositoryManager.getPathForArtifact( transfer.getArtifact(), false ) + '.' + + checksumAlgorithmFactory.getFileExtension(), checksumAlgorithmFactory ) ); + } + return getProvidedChecksums( baseDir, checksumFilePaths, ArtifactIdUtils.toId( transfer.getArtifact() ) ); + } + + /** + * May return {@code null}. + */ + private Map<String, String> getProvidedChecksums( Path baseDir, + List<ChecksumFilePath> checksumFilePaths, + String subjectId ) + { + HashMap<String, String> checksums = new HashMap<>(); + for ( ChecksumFilePath checksumFilePath : checksumFilePaths ) + { + Path checksumPath = baseDir.resolve( checksumFilePath.path ); + if ( Files.isReadable( checksumPath ) ) + { + try + { + String checksum = fileProcessor.readChecksum( checksumPath.toFile() ); + if ( checksum != null ) + { + if ( LOGGER.isDebugEnabled() ) + { + LOGGER.debug( "{} -> {}:{}", subjectId, + checksumFilePath.checksumAlgorithmFactory.getName(), checksum ); Review comment: I think even for debugging purposes this needs more text. E.g.: > "Resolved provided checksum '{}:{}' for '{}'" -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
