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