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