001package org.eclipse.aether.internal.impl.synccontext.named; 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 org.eclipse.aether.RepositorySystemSession; 023import org.eclipse.aether.artifact.Artifact; 024import org.eclipse.aether.metadata.Metadata; 025import org.eclipse.aether.util.ChecksumUtils; 026import org.eclipse.aether.util.ConfigUtils; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030import javax.inject.Inject; 031import javax.inject.Named; 032import javax.inject.Singleton; 033import java.io.File; 034import java.net.InetAddress; 035import java.net.UnknownHostException; 036import java.nio.charset.StandardCharsets; 037import java.util.Collection; 038import java.util.Collections; 039import java.util.Map; 040import java.util.Objects; 041 042import static java.util.stream.Collectors.toList; 043 044/** 045 * Discriminating {@link NameMapper}, that wraps another {@link NameMapper} and adds a "discriminator" as prefix, that 046 * makes lock names unique including the hostname and local repository (by default). The discriminator may be passed 047 * in via {@link RepositorySystemSession} or is automatically calculated based on the local hostname and repository 048 * path. The implementation retains order of collection elements as it got it from 049 * {@link NameMapper#nameLocks(RepositorySystemSession, Collection, Collection)} method. 050 * <p> 051 * The default setup wraps {@link GAVNameMapper}, but manually may be created any instance needed. 052 */ 053@Singleton 054@Named( DiscriminatingNameMapper.NAME ) 055public class DiscriminatingNameMapper implements NameMapper 056{ 057 public static final String NAME = "discriminating"; 058 059 /** 060 * Configuration property to pass in discriminator 061 */ 062 private static final String CONFIG_PROP_DISCRIMINATOR = "aether.syncContext.named.discriminating.discriminator"; 063 064 /** 065 * Configuration property to pass in hostname 066 */ 067 private static final String CONFIG_PROP_HOSTNAME = "aether.syncContext.named.discriminating.hostname"; 068 069 private static final String DEFAULT_DISCRIMINATOR_DIGEST = "da39a3ee5e6b4b0d3255bfef95601890afd80709"; 070 071 private static final String DEFAULT_HOSTNAME = "localhost"; 072 073 private static final Logger LOGGER = LoggerFactory.getLogger( DiscriminatingNameMapper.class ); 074 075 private final NameMapper nameMapper; 076 077 private final String hostname; 078 079 @Inject 080 public DiscriminatingNameMapper( @Named( GAVNameMapper.NAME ) final NameMapper nameMapper ) 081 { 082 this.nameMapper = Objects.requireNonNull( nameMapper ); 083 this.hostname = getHostname(); 084 } 085 086 @Override 087 public Collection<String> nameLocks( final RepositorySystemSession session, 088 final Collection<? extends Artifact> artifacts, 089 final Collection<? extends Metadata> metadatas ) 090 { 091 String discriminator = createDiscriminator( session ); 092 return nameMapper.nameLocks( session, artifacts, metadatas ).stream().map( s -> discriminator + ":" + s ) 093 .collect( toList() ); 094 } 095 096 private String getHostname() 097 { 098 try 099 { 100 return InetAddress.getLocalHost().getHostName(); 101 } 102 catch ( UnknownHostException e ) 103 { 104 LOGGER.warn( "Failed to get hostname, using '{}'", DEFAULT_HOSTNAME, e ); 105 return DEFAULT_HOSTNAME; 106 } 107 } 108 109 private String createDiscriminator( final RepositorySystemSession session ) 110 { 111 String discriminator = ConfigUtils.getString( session, null, CONFIG_PROP_DISCRIMINATOR ); 112 113 if ( discriminator == null || discriminator.isEmpty() ) 114 { 115 String hostname = ConfigUtils.getString( session, this.hostname, CONFIG_PROP_HOSTNAME ); 116 File basedir = session.getLocalRepository().getBasedir(); 117 discriminator = hostname + ":" + basedir; 118 try 119 { 120 Map<String, Object> checksums = ChecksumUtils 121 .calc( discriminator.getBytes( StandardCharsets.UTF_8 ), Collections.singletonList( "SHA-1" ) ); 122 Object checksum = checksums.get( "SHA-1" ); 123 124 if ( checksum instanceof Exception ) 125 { 126 throw (Exception) checksum; 127 } 128 129 return String.valueOf( checksum ); 130 } 131 catch ( Exception e ) 132 { 133 LOGGER.warn( "Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e ); 134 return DEFAULT_DISCRIMINATOR_DIGEST; 135 } 136 } 137 return discriminator; 138 } 139}