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 org.eclipse.aether.ConfigurationProperties;
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.repository.LocalRepository;
28  import org.eclipse.aether.repository.LocalRepositoryManager;
29  import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
30  import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
31  import org.eclipse.aether.spi.remoterepo.RepositoryKeyFunctionFactory;
32  import org.eclipse.aether.util.ConfigUtils;
33  
34  import static java.util.Objects.requireNonNull;
35  
36  /**
37   * Creates enhanced local repository managers for repository types {@code "default"} or {@code "" (automatic)}. Enhanced
38   * local repository manager is built upon the classical Maven 2.0 local repository structure but additionally keeps
39   * track of from what repositories a cached artifact was resolved. Resolution of locally cached artifacts will be
40   * rejected in case the current resolution request does not match the known source repositories of an artifact, thereby
41   * emulating physically separated artifact caches per remote repository.
42   */
43  @Singleton
44  @Named(EnhancedLocalRepositoryManagerFactory.NAME)
45  public class EnhancedLocalRepositoryManagerFactory implements LocalRepositoryManagerFactory {
46      public static final String NAME = "enhanced";
47  
48      static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_LRM + NAME + ".";
49  
50      /**
51       * Filename of the file in which to track the remote repositories.
52       *
53       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
54       * @configurationType {@link java.lang.String}
55       * @configurationDefaultValue {@link #DEFAULT_TRACKING_FILENAME}
56       */
57      public static final String CONFIG_PROP_TRACKING_FILENAME = CONFIG_PROPS_PREFIX + "trackingFilename";
58  
59      public static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories";
60  
61      private float priority = 10.0f;
62  
63      private final LocalPathComposer localPathComposer;
64  
65      private final TrackingFileManager trackingFileManager;
66  
67      private final LocalPathPrefixComposerFactory localPathPrefixComposerFactory;
68  
69      private final RepositoryKeyFunctionFactory repositoryKeyFunctionFactory;
70  
71      @Inject
72      public EnhancedLocalRepositoryManagerFactory(
73              final LocalPathComposer localPathComposer,
74              final TrackingFileManager trackingFileManager,
75              final LocalPathPrefixComposerFactory localPathPrefixComposerFactory,
76              final RepositoryKeyFunctionFactory repositoryKeyFunctionFactory) {
77          this.localPathComposer = requireNonNull(localPathComposer);
78          this.trackingFileManager = requireNonNull(trackingFileManager);
79          this.localPathPrefixComposerFactory = requireNonNull(localPathPrefixComposerFactory);
80          this.repositoryKeyFunctionFactory = requireNonNull(repositoryKeyFunctionFactory);
81      }
82  
83      @Override
84      public LocalRepositoryManager newInstance(RepositorySystemSession session, LocalRepository repository)
85              throws NoLocalRepositoryManagerException {
86          requireNonNull(session, "session cannot be null");
87          requireNonNull(repository, "repository cannot be null");
88  
89          String trackingFilename = ConfigUtils.getString(session, "", CONFIG_PROP_TRACKING_FILENAME);
90          if (trackingFilename.isEmpty()
91                  || trackingFilename.contains("/")
92                  || trackingFilename.contains("\\")
93                  || trackingFilename.contains("..")) {
94              trackingFilename = DEFAULT_TRACKING_FILENAME;
95          }
96  
97          if ("".equals(repository.getContentType()) || "default".equals(repository.getContentType())) {
98              return new EnhancedLocalRepositoryManager(
99                      repository.getBasePath(),
100                     localPathComposer,
101                     repositoryKeyFunctionFactory.systemRepositoryKeyFunction(session),
102                     trackingFilename,
103                     trackingFileManager,
104                     localPathPrefixComposerFactory.createComposer(session));
105         } else {
106             throw new NoLocalRepositoryManagerException(repository);
107         }
108     }
109 
110     @Override
111     public float getPriority() {
112         return priority;
113     }
114 
115     /**
116      * Sets the priority of this component.
117      *
118      * @param priority The priority.
119      * @return This component for chaining, never {@code null}.
120      */
121     public EnhancedLocalRepositoryManagerFactory setPriority(float priority) {
122         this.priority = priority;
123         return this;
124     }
125 }