View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.concurrent.TimeUnit;
28  import java.util.function.Supplier;
29  
30  import org.eclipse.aether.ConfigurationProperties;
31  import org.eclipse.aether.impl.NamedLockFactorySelector;
32  import org.eclipse.aether.named.NamedLockFactory;
33  import org.eclipse.aether.spi.io.PathProcessor;
34  import org.eclipse.aether.util.ConfigUtils;
35  
36  /**
37   * Supplies selected instance of {@link TrackingFileManager} implementation.
38   *
39   * @since 2.0.18
40   */
41  @Singleton
42  @Named
43  public class TrackingFileManagerSupplier implements Supplier<TrackingFileManager> {
44      public static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_SYSTEM + "trackingFileManager.";
45  
46      /**
47       * Name of the tracking file manager to use. Supported values are "namedLocks" and "legacy". The latter should be
48       * used if it is known, that local repository is simultaneously accessed by Maven 3.10+ and older Maven versions.
49       * This decision happens early, during boot of the system, hence system properties can be used only as configuration
50       * source.
51       *
52       * @configurationSource {@link System#getProperty(String, String)}
53       * @configurationType {@link String}
54       * @configurationDefaultValue {@link #DEFAULT_TRACKING_FILE_MANAGER_NAME}
55       */
56      public static final String CONFIG_PROP_TRACKING_FILE_MANAGER_NAME = CONFIG_PROPS_PREFIX + "name";
57  
58      public static final String DEFAULT_TRACKING_FILE_MANAGER_NAME = "legacy";
59  
60      private final TrackingFileManager trackingFileManager;
61  
62      /**
63       * Default constructor, to be used in tests; provides "legacy" tracking file manager only.
64       */
65      public TrackingFileManagerSupplier() {
66          this.trackingFileManager = new LegacyTrackingFileManager();
67      }
68  
69      /**
70       * Constructor to be used in production.
71       */
72      public TrackingFileManagerSupplier(NamedLockFactorySelector selector) {
73          this(selector, new DefaultPathProcessor());
74      }
75  
76      @Inject
77      public TrackingFileManagerSupplier(NamedLockFactorySelector selector, PathProcessor pathProcessor) {
78          // this is early construction; no session, hence we must rely on system properties instead
79          Map<String, String> config = new HashMap<>();
80          for (String name : System.getProperties().stringPropertyNames()) {
81              config.put(name, System.getProperty(name));
82          }
83          String tfmName = ConfigUtils.getString(
84                  config, DEFAULT_TRACKING_FILE_MANAGER_NAME, CONFIG_PROP_TRACKING_FILE_MANAGER_NAME);
85          if ("legacy".equals(tfmName)) {
86              this.trackingFileManager = new LegacyTrackingFileManager();
87          } else if ("namedLocks".equals(tfmName)) {
88              NamedLockFactory factory = selector.getNamedLockFactory(config);
89              long time = selector.getLockWaitTime(config);
90              TimeUnit timeUnit = selector.getLockWaitTimeUnit(config);
91              this.trackingFileManager = new NamedLocksTrackingFileManager(factory, pathProcessor, time, timeUnit);
92          } else {
93              throw new IllegalArgumentException("Unknown tracking file manager name: " + tfmName);
94          }
95      }
96  
97      @Override
98      public TrackingFileManager get() {
99          return trackingFileManager;
100     }
101 }