001package org.eclipse.aether.internal.impl.checksum; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import javax.inject.Inject; 023import javax.inject.Named; 024import javax.inject.Singleton; 025 026import java.io.IOException; 027import java.nio.file.Files; 028import java.nio.file.Path; 029import java.util.HashMap; 030import java.util.List; 031import java.util.Map; 032 033import org.eclipse.aether.RepositorySystemSession; 034import org.eclipse.aether.artifact.Artifact; 035import org.eclipse.aether.internal.impl.LocalPathComposer; 036import org.eclipse.aether.repository.ArtifactRepository; 037import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory; 038import org.eclipse.aether.spi.io.FileProcessor; 039import org.slf4j.Logger; 040import org.slf4j.LoggerFactory; 041 042import static java.util.Objects.requireNonNull; 043 044/** 045 * Sparse file {@link FileTrustedChecksumsSourceSupport} implementation that use specified directory as base 046 * directory, where it expects artifacts checksums on standard Maven2 "local" layout. This implementation uses Artifact 047 * coordinates solely to form path from basedir, pretty much as Maven local repository does. 048 * <p> 049 * The source by default is "origin aware", it will factor in origin repository ID as well into base directory name 050 * (for example ".checksums/central/..."). 051 * <p> 052 * The checksums files are directly loaded from disk, so in-flight file changes during lifecycle of session are picked 053 * up. This implementation can be simultaneously used to lookup and also write checksums. The written checksums 054 * will become visible across all sessions right after the moment they were written. 055 * <p> 056 * The name of this implementation is "sparseDirectory". 057 * 058 * @see LocalPathComposer 059 * @since 1.9.0 060 */ 061@Singleton 062@Named( SparseDirectoryTrustedChecksumsSource.NAME ) 063public final class SparseDirectoryTrustedChecksumsSource 064 extends FileTrustedChecksumsSourceSupport 065{ 066 public static final String NAME = "sparseDirectory"; 067 068 private static final Logger LOGGER = LoggerFactory.getLogger( SparseDirectoryTrustedChecksumsSource.class ); 069 070 private final FileProcessor fileProcessor; 071 072 private final LocalPathComposer localPathComposer; 073 074 @Inject 075 public SparseDirectoryTrustedChecksumsSource( FileProcessor fileProcessor, LocalPathComposer localPathComposer ) 076 { 077 super( NAME ); 078 this.fileProcessor = requireNonNull( fileProcessor ); 079 this.localPathComposer = requireNonNull( localPathComposer ); 080 } 081 082 @Override 083 protected Map<String, String> doGetTrustedArtifactChecksums( 084 RepositorySystemSession session, Artifact artifact, ArtifactRepository artifactRepository, 085 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories ) 086 { 087 final boolean originAware = isOriginAware( session ); 088 final HashMap<String, String> checksums = new HashMap<>(); 089 Path basedir = getBasedir( session, false ); 090 if ( Files.isDirectory( basedir ) ) 091 { 092 for ( ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories ) 093 { 094 Path checksumPath = basedir.resolve( 095 calculateArtifactPath( originAware, artifact, artifactRepository, checksumAlgorithmFactory ) ); 096 097 if ( !Files.isRegularFile( checksumPath ) ) 098 { 099 LOGGER.debug( "Artifact '{}' trusted checksum '{}' not found on path '{}'", 100 artifact, checksumAlgorithmFactory.getName(), checksumPath ); 101 continue; 102 } 103 104 try 105 { 106 String checksum = fileProcessor.readChecksum( checksumPath.toFile() ); 107 if ( checksum != null ) 108 { 109 checksums.put( checksumAlgorithmFactory.getName(), checksum ); 110 } 111 } 112 catch ( IOException e ) 113 { 114 // unexpected, log, skip 115 LOGGER.warn( "Could not read artifact '{}' trusted checksum on path '{}'", artifact, checksumPath, 116 e ); 117 } 118 } 119 } 120 return checksums; 121 } 122 123 @Override 124 protected SparseDirectoryWriter doGetTrustedArtifactChecksumsWriter( RepositorySystemSession session ) 125 { 126 return new SparseDirectoryWriter( getBasedir( session, true ), isOriginAware( session ) ); 127 } 128 129 private String calculateArtifactPath( boolean originAware, 130 Artifact artifact, 131 ArtifactRepository artifactRepository, 132 ChecksumAlgorithmFactory checksumAlgorithmFactory ) 133 { 134 String path = localPathComposer.getPathForArtifact( artifact, false ) 135 + "." + checksumAlgorithmFactory.getFileExtension(); 136 if ( originAware ) 137 { 138 path = artifactRepository.getId() + "/" + path; 139 } 140 return path; 141 } 142 143 private class SparseDirectoryWriter implements Writer 144 { 145 private final Path basedir; 146 147 private final boolean originAware; 148 149 private SparseDirectoryWriter( Path basedir, boolean originAware ) 150 { 151 this.basedir = basedir; 152 this.originAware = originAware; 153 } 154 155 @Override 156 public void addTrustedArtifactChecksums( Artifact artifact, 157 ArtifactRepository artifactRepository, 158 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories, 159 Map<String, String> trustedArtifactChecksums ) throws IOException 160 { 161 for ( ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories ) 162 { 163 Path checksumPath = basedir.resolve( calculateArtifactPath( 164 originAware, artifact, artifactRepository, checksumAlgorithmFactory ) ); 165 String checksum = requireNonNull( 166 trustedArtifactChecksums.get( checksumAlgorithmFactory.getName() ) ); 167 fileProcessor.writeChecksum( checksumPath.toFile(), checksum ); 168 } 169 } 170 } 171}