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;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.HashMap;
026import java.util.Map;
027import java.util.concurrent.TimeUnit;
028import java.util.function.Supplier;
029
030import org.eclipse.aether.ConfigurationProperties;
031import org.eclipse.aether.impl.NamedLockFactorySelector;
032import org.eclipse.aether.named.NamedLockFactory;
033import org.eclipse.aether.spi.io.PathProcessor;
034import org.eclipse.aether.util.ConfigUtils;
035
036/**
037 * Supplies selected instance of {@link TrackingFileManager} implementation.
038 *
039 * @since 2.0.18
040 */
041@Singleton
042@Named
043public class TrackingFileManagerSupplier implements Supplier<TrackingFileManager> {
044    public static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_SYSTEM + "trackingFileManager.";
045
046    /**
047     * Name of the tracking file manager to use. Supported values are "namedLocks" and "legacy". The latter should be
048     * used if it is known, that local repository is simultaneously accessed by Maven 3.10+ and older Maven versions.
049     * This decision happens early, during boot of the system, hence system properties can be used only as configuration
050     * source.
051     *
052     * @configurationSource {@link System#getProperty(String, String)}
053     * @configurationType {@link String}
054     * @configurationDefaultValue {@link #DEFAULT_TRACKING_FILE_MANAGER_NAME}
055     */
056    public static final String CONFIG_PROP_TRACKING_FILE_MANAGER_NAME = CONFIG_PROPS_PREFIX + "name";
057
058    public static final String DEFAULT_TRACKING_FILE_MANAGER_NAME = "legacy";
059
060    private final TrackingFileManager trackingFileManager;
061
062    /**
063     * Default constructor, to be used in tests; provides "legacy" tracking file manager only.
064     */
065    public TrackingFileManagerSupplier() {
066        this.trackingFileManager = new LegacyTrackingFileManager();
067    }
068
069    /**
070     * Constructor to be used in production.
071     */
072    public TrackingFileManagerSupplier(NamedLockFactorySelector selector) {
073        this(selector, new DefaultPathProcessor());
074    }
075
076    @Inject
077    public TrackingFileManagerSupplier(NamedLockFactorySelector selector, PathProcessor pathProcessor) {
078        // this is early construction; no session, hence we must rely on system properties instead
079        Map<String, String> config = new HashMap<>();
080        for (String name : System.getProperties().stringPropertyNames()) {
081            config.put(name, System.getProperty(name));
082        }
083        String tfmName = ConfigUtils.getString(
084                config, DEFAULT_TRACKING_FILE_MANAGER_NAME, CONFIG_PROP_TRACKING_FILE_MANAGER_NAME);
085        if ("legacy".equals(tfmName)) {
086            this.trackingFileManager = new LegacyTrackingFileManager();
087        } else if ("namedLocks".equals(tfmName)) {
088            NamedLockFactory factory = selector.getNamedLockFactory(config);
089            long time = selector.getLockWaitTime(config);
090            TimeUnit timeUnit = selector.getLockWaitTimeUnit(config);
091            this.trackingFileManager = new NamedLocksTrackingFileManager(factory, pathProcessor, time, timeUnit);
092        } else {
093            throw new IllegalArgumentException("Unknown tracking file manager name: " + tfmName);
094        }
095    }
096
097    @Override
098    public TrackingFileManager get() {
099        return trackingFileManager;
100    }
101}