001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl.synccontext.named;
020
021import java.io.File;
022import java.net.InetAddress;
023import java.net.UnknownHostException;
024import java.util.Collection;
025
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.artifact.Artifact;
028import org.eclipse.aether.metadata.Metadata;
029import org.eclipse.aether.util.ConfigUtils;
030import org.eclipse.aether.util.StringDigestUtil;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import static java.util.Objects.requireNonNull;
035import static java.util.stream.Collectors.toList;
036
037/**
038 * Wrapping {@link NameMapper}, that wraps another {@link NameMapper} and adds a "discriminator" as prefix, that
039 * makes lock names unique including the hostname and local repository (by default). The discriminator may be passed
040 * in via {@link RepositorySystemSession} or is automatically calculated based on the local hostname and repository
041 * path. The implementation retains order of collection elements as it got it from
042 * {@link NameMapper#nameLocks(RepositorySystemSession, Collection, Collection)} method.
043 * <p>
044 * The default setup wraps {@link GAVNameMapper}, but manually may be created any instance needed.
045 */
046public class DiscriminatingNameMapper implements NameMapper {
047    /**
048     * Configuration property to pass in discriminator
049     */
050    private static final String CONFIG_PROP_DISCRIMINATOR = "aether.syncContext.named.discriminating.discriminator";
051
052    /**
053     * Configuration property to pass in hostname
054     */
055    private static final String CONFIG_PROP_HOSTNAME = "aether.syncContext.named.discriminating.hostname";
056
057    private static final String DEFAULT_DISCRIMINATOR_DIGEST = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
058
059    private static final String DEFAULT_HOSTNAME = "localhost";
060
061    private static final Logger LOGGER = LoggerFactory.getLogger(DiscriminatingNameMapper.class);
062
063    private final NameMapper delegate;
064
065    private final String hostname;
066
067    public DiscriminatingNameMapper(final NameMapper delegate) {
068        this.delegate = requireNonNull(delegate);
069        this.hostname = getHostname();
070    }
071
072    @Override
073    public boolean isFileSystemFriendly() {
074        return false; // uses ":" in produced lock names
075    }
076
077    @Override
078    public Collection<String> nameLocks(
079            final RepositorySystemSession session,
080            final Collection<? extends Artifact> artifacts,
081            final Collection<? extends Metadata> metadatas) {
082        String discriminator = createDiscriminator(session);
083        return delegate.nameLocks(session, artifacts, metadatas).stream()
084                .map(s -> discriminator + ":" + s)
085                .collect(toList());
086    }
087
088    private String getHostname() {
089        try {
090            return InetAddress.getLocalHost().getHostName();
091        } catch (UnknownHostException e) {
092            LOGGER.warn("Failed to get hostname, using '{}'", DEFAULT_HOSTNAME, e);
093            return DEFAULT_HOSTNAME;
094        }
095    }
096
097    private String createDiscriminator(final RepositorySystemSession session) {
098        String discriminator = ConfigUtils.getString(session, null, CONFIG_PROP_DISCRIMINATOR);
099
100        if (discriminator == null || discriminator.isEmpty()) {
101            String hostname = ConfigUtils.getString(session, this.hostname, CONFIG_PROP_HOSTNAME);
102            File basedir = session.getLocalRepository().getBasedir();
103            discriminator = hostname + ":" + basedir;
104            try {
105                return StringDigestUtil.sha1(discriminator);
106            } catch (Exception e) {
107                LOGGER.warn("Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e);
108                return DEFAULT_DISCRIMINATOR_DIGEST;
109            }
110        }
111        return discriminator;
112    }
113}