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