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, if needed. If not present, it is auto-calculated.
049     *
050     * @since 1.7.0
051     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
052     * @configurationType {@link java.lang.String}
053     */
054    public static final String CONFIG_PROP_DISCRIMINATOR =
055            NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "discriminating.discriminator";
056
057    /**
058     * Configuration property to pass in hostname, if needed. If not present, hostname as reported by system will be
059     * used.
060     *
061     * @since 1.7.0
062     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
063     * @configurationType {@link java.lang.String}
064     */
065    public static final String CONFIG_PROP_HOSTNAME =
066            NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "discriminating.hostname";
067
068    private static final String DEFAULT_DISCRIMINATOR_DIGEST = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
069
070    private static final String DEFAULT_HOSTNAME = "localhost";
071
072    private static final Logger LOGGER = LoggerFactory.getLogger(DiscriminatingNameMapper.class);
073
074    private final NameMapper delegate;
075
076    private final String hostname;
077
078    public DiscriminatingNameMapper(final NameMapper delegate) {
079        this.delegate = requireNonNull(delegate);
080        this.hostname = getHostname();
081    }
082
083    @Override
084    public boolean isFileSystemFriendly() {
085        return false; // uses ":" in produced lock names
086    }
087
088    @Override
089    public Collection<String> nameLocks(
090            final RepositorySystemSession session,
091            final Collection<? extends Artifact> artifacts,
092            final Collection<? extends Metadata> metadatas) {
093        String discriminator = createDiscriminator(session);
094        return delegate.nameLocks(session, artifacts, metadatas).stream()
095                .map(s -> discriminator + ":" + s)
096                .collect(toList());
097    }
098
099    private String getHostname() {
100        try {
101            return InetAddress.getLocalHost().getHostName();
102        } catch (UnknownHostException e) {
103            LOGGER.warn("Failed to get hostname, using '{}'", DEFAULT_HOSTNAME, e);
104            return DEFAULT_HOSTNAME;
105        }
106    }
107
108    private String createDiscriminator(final RepositorySystemSession session) {
109        String discriminator = ConfigUtils.getString(session, null, CONFIG_PROP_DISCRIMINATOR);
110
111        if (discriminator == null || discriminator.isEmpty()) {
112            String hostname = ConfigUtils.getString(session, this.hostname, CONFIG_PROP_HOSTNAME);
113            File basedir = session.getLocalRepository().getBasedir();
114            discriminator = hostname + ":" + basedir;
115            try {
116                return StringDigestUtil.sha1(discriminator);
117            } catch (Exception e) {
118                LOGGER.warn("Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e);
119                return DEFAULT_DISCRIMINATOR_DIGEST;
120            }
121        }
122        return discriminator;
123    }
124}