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.net.InetAddress;
022import java.net.UnknownHostException;
023import java.nio.file.Path;
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.named.NamedLockKey;
030import org.eclipse.aether.util.ConfigUtils;
031import org.eclipse.aether.util.StringDigestUtil;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035import static java.util.Objects.requireNonNull;
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     * Configuration property to pass in discriminator, if needed. If not present, it is auto-calculated.
050     *
051     * @since 1.7.0
052     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
053     * @configurationType {@link java.lang.String}
054     */
055    public static final String CONFIG_PROP_DISCRIMINATOR =
056            NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "discriminating.discriminator";
057
058    /**
059     * Configuration property to pass in hostname, if needed. If not present, hostname as reported by system will be
060     * used.
061     *
062     * @since 1.7.0
063     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
064     * @configurationType {@link java.lang.String}
065     */
066    public static final String CONFIG_PROP_HOSTNAME =
067            NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "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 delegate;
076
077    private final String hostname;
078
079    public DiscriminatingNameMapper(final NameMapper delegate) {
080        this.delegate = requireNonNull(delegate);
081        this.hostname = getHostname();
082    }
083
084    @Override
085    public boolean isFileSystemFriendly() {
086        return false; // uses ":" in produced lock names
087    }
088
089    @Override
090    public Collection<NamedLockKey> nameLocks(
091            final RepositorySystemSession session,
092            final Collection<? extends Artifact> artifacts,
093            final Collection<? extends Metadata> metadatas) {
094        String discriminator = createDiscriminator(session);
095        return delegate.nameLocks(session, artifacts, metadatas).stream()
096                .map(k -> NamedLockKey.of(discriminator + ":" + k.name(), k.resources()))
097                .collect(toList());
098    }
099
100    private String getHostname() {
101        try {
102            return InetAddress.getLocalHost().getHostName();
103        } catch (UnknownHostException e) {
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        String discriminator = ConfigUtils.getString(session, null, CONFIG_PROP_DISCRIMINATOR);
111
112        if (discriminator == null || discriminator.isEmpty()) {
113            String hostname = ConfigUtils.getString(session, this.hostname, CONFIG_PROP_HOSTNAME);
114            Path basedir = session.getLocalRepository().getBasePath();
115            discriminator = hostname + ":" + basedir;
116            try {
117                return StringDigestUtil.sha1(discriminator);
118            } catch (Exception e) {
119                LOGGER.warn("Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e);
120                return DEFAULT_DISCRIMINATOR_DIGEST;
121            }
122        }
123        return discriminator;
124    }
125}