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.Provider;
24  import javax.inject.Singleton;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.concurrent.TimeUnit;
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.util.ConfigUtils;
34  
35  /**
36   * Provides selected instance of {@link TrackingFileManager} implementation.
37   *
38   * @since 2.0.17
39   */
40  @Singleton
41  @Named
42  public class TrackingFileManagerProvider implements Provider<TrackingFileManager> {
43      public static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_SYSTEM + "trackingFileManager.";
44  
45      /**
46       * Name of the tracking file manager to use. Supported values are "namedLocks" and "legacy". The latter should be
47       * used if it is known, that local repository is simultaneously accessed by Maven 3.10+ and older Maven versions.
48       * This decision happens early, during boot of the system, hence system properties can be used only as configuration
49       * source.
50       *
51       * @configurationSource {@link System#getProperty(String, String)}
52       * @configurationType {@link java.lang.String}
53       * @configurationDefaultValue {@link #DEFAULT_TRACKING_FILE_MANAGER_NAME}
54       */
55      public static final String CONFIG_PROP_TRACKING_FILE_MANAGER_NAME = CONFIG_PROPS_PREFIX + "name";
56  
57      public static final String DEFAULT_TRACKING_FILE_MANAGER_NAME = "legacy";
58  
59      private final TrackingFileManager trackingFileManager;
60  
61      /**
62       * Default constructor, to be used in tests; provides "legacy" tracking file manager only.
63       */
64      public TrackingFileManagerProvider() {
65          this.trackingFileManager = new LegacyTrackingFileManager();
66      }
67  
68      /**
69       * Constructor to be used in production.
70       */
71      @Inject
72      public TrackingFileManagerProvider(NamedLockFactorySelector selector) {
73          // this is early construction; no session, hence we must rely on system properties instead
74          Map<String, String> config = new HashMap<>();
75          for (String name : System.getProperties().stringPropertyNames()) {
76              config.put(name, System.getProperty(name));
77          }
78          String tfmName = ConfigUtils.getString(
79                  config, DEFAULT_TRACKING_FILE_MANAGER_NAME, CONFIG_PROP_TRACKING_FILE_MANAGER_NAME);
80          if ("legacy".equals(tfmName)) {
81              this.trackingFileManager = new LegacyTrackingFileManager();
82          } else if ("namedLocks".equals(tfmName)) {
83              NamedLockFactory factory = selector.getNamedLockFactory(config);
84              long time = selector.getLockWaitTime(config);
85              TimeUnit timeUnit = selector.getLockWaitTimeUnit(config);
86              this.trackingFileManager = new NamedLocksTrackingFileManager(factory, time, timeUnit);
87          } else {
88              throw new IllegalArgumentException("Unknown tracking file manager name: " + tfmName);
89          }
90      }
91  
92      @Override
93      public TrackingFileManager get() {
94          return trackingFileManager;
95      }
96  }