View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
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.locator.Service;
32  import org.eclipse.aether.spi.locator.ServiceLocator;
33  import org.eclipse.aether.util.ConfigUtils;
34  
35  import static java.util.Objects.requireNonNull;
36  
37  /**
38   * Creates enhanced local repository managers for repository types {@code "default"} or {@code "" (automatic)}. Enhanced
39   * local repository manager is built upon the classical Maven 2.0 local repository structure but additionally keeps
40   * track of from what repositories a cached artifact was resolved. Resolution of locally cached artifacts will be
41   * rejected in case the current resolution request does not match the known source repositories of an artifact, thereby
42   * emulating physically separated artifact caches per remote repository.
43   */
44  @Singleton
45  @Named( "enhanced" )
46  public class EnhancedLocalRepositoryManagerFactory
47      implements LocalRepositoryManagerFactory, Service
48  {
49      private static final String CONFIG_PROP_TRACKING_FILENAME = "aether.enhancedLocalRepository.trackingFilename";
50  
51      private static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories";
52  
53      private float priority = 10.0f;
54  
55      private LocalPathComposer localPathComposer;
56  
57      private TrackingFileManager trackingFileManager;
58  
59      private LocalPathPrefixComposerFactory localPathPrefixComposerFactory;
60  
61      public EnhancedLocalRepositoryManagerFactory()
62      {
63          // no arg ctor for ServiceLocator
64      }
65  
66      @Inject
67      public EnhancedLocalRepositoryManagerFactory( final LocalPathComposer localPathComposer,
68                     final TrackingFileManager trackingFileManager,
69                     final LocalPathPrefixComposerFactory localPathPrefixComposerFactory )
70      {
71          this.localPathComposer = requireNonNull( localPathComposer );
72          this.trackingFileManager = requireNonNull( trackingFileManager );
73          this.localPathPrefixComposerFactory = requireNonNull( localPathPrefixComposerFactory );
74      }
75  
76      @Override
77      public void initService( final ServiceLocator locator )
78      {
79          this.localPathComposer = requireNonNull( locator.getService( LocalPathComposer.class ) );
80          this.trackingFileManager = requireNonNull( locator.getService( TrackingFileManager.class ) );
81          this.localPathPrefixComposerFactory = new DefaultLocalPathPrefixComposerFactory();
82      }
83  
84      @Override
85      public LocalRepositoryManager newInstance( RepositorySystemSession session, LocalRepository repository )
86          throws NoLocalRepositoryManagerException
87      {
88          requireNonNull( session, "session cannot be null" );
89          requireNonNull( repository, "repository cannot be null" );
90  
91          String trackingFilename = ConfigUtils.getString( session, "", CONFIG_PROP_TRACKING_FILENAME );
92          if ( trackingFilename.isEmpty() || trackingFilename.contains( "/" ) || trackingFilename.contains( "\\" )
93                  || trackingFilename.contains( ".." ) )
94          {
95              trackingFilename = DEFAULT_TRACKING_FILENAME;
96          }
97  
98          if ( "".equals( repository.getContentType() ) || "default".equals( repository.getContentType() ) )
99          {
100             return new EnhancedLocalRepositoryManager(
101                     repository.getBasedir(),
102                     localPathComposer,
103                     trackingFilename,
104                     trackingFileManager,
105                     localPathPrefixComposerFactory.createComposer( session )
106             );
107         }
108         else
109         {
110             throw new NoLocalRepositoryManagerException( repository );
111         }
112     }
113 
114     @Override
115     public float getPriority()
116     {
117         return priority;
118     }
119 
120     /**
121      * Sets the priority of this component.
122      *
123      * @param priority The priority.
124      * @return This component for chaining, never {@code null}.
125      */
126     public EnhancedLocalRepositoryManagerFactory setPriority( float priority )
127     {
128         this.priority = priority;
129         return this;
130     }
131 
132 }