001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.impl; 020 021import javax.inject.Inject; 022import javax.inject.Named; 023import javax.inject.Singleton; 024 025import org.eclipse.aether.ConfigurationProperties; 026import org.eclipse.aether.RepositorySystemSession; 027import org.eclipse.aether.repository.LocalRepository; 028import org.eclipse.aether.repository.LocalRepositoryManager; 029import org.eclipse.aether.repository.NoLocalRepositoryManagerException; 030import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory; 031import org.eclipse.aether.spi.remoterepo.RepositoryKeyFunctionFactory; 032import org.eclipse.aether.util.ConfigUtils; 033 034import static java.util.Objects.requireNonNull; 035 036/** 037 * Creates enhanced local repository managers for repository types {@code "default"} or {@code "" (automatic)}. Enhanced 038 * local repository manager is built upon the classical Maven 2.0 local repository structure but additionally keeps 039 * track of from what repositories a cached artifact was resolved. Resolution of locally cached artifacts will be 040 * rejected in case the current resolution request does not match the known source repositories of an artifact, thereby 041 * emulating physically separated artifact caches per remote repository. 042 */ 043@Singleton 044@Named(EnhancedLocalRepositoryManagerFactory.NAME) 045public class EnhancedLocalRepositoryManagerFactory implements LocalRepositoryManagerFactory { 046 public static final String NAME = "enhanced"; 047 048 static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_LRM + NAME + "."; 049 050 /** 051 * Filename of the file in which to track the remote repositories. 052 * 053 * @configurationSource {@link RepositorySystemSession#getConfigProperties()} 054 * @configurationType {@link java.lang.String} 055 * @configurationDefaultValue {@link #DEFAULT_TRACKING_FILENAME} 056 */ 057 public static final String CONFIG_PROP_TRACKING_FILENAME = CONFIG_PROPS_PREFIX + "trackingFilename"; 058 059 public static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories"; 060 061 private float priority = 10.0f; 062 063 private final LocalPathComposer localPathComposer; 064 065 private final TrackingFileManager trackingFileManager; 066 067 private final LocalPathPrefixComposerFactory localPathPrefixComposerFactory; 068 069 private final RepositoryKeyFunctionFactory repositoryKeyFunctionFactory; 070 071 @Inject 072 public EnhancedLocalRepositoryManagerFactory( 073 final LocalPathComposer localPathComposer, 074 final TrackingFileManager trackingFileManager, 075 final LocalPathPrefixComposerFactory localPathPrefixComposerFactory, 076 final RepositoryKeyFunctionFactory repositoryKeyFunctionFactory) { 077 this.localPathComposer = requireNonNull(localPathComposer); 078 this.trackingFileManager = requireNonNull(trackingFileManager); 079 this.localPathPrefixComposerFactory = requireNonNull(localPathPrefixComposerFactory); 080 this.repositoryKeyFunctionFactory = requireNonNull(repositoryKeyFunctionFactory); 081 } 082 083 @Override 084 public LocalRepositoryManager newInstance(RepositorySystemSession session, LocalRepository repository) 085 throws NoLocalRepositoryManagerException { 086 requireNonNull(session, "session cannot be null"); 087 requireNonNull(repository, "repository cannot be null"); 088 089 String trackingFilename = ConfigUtils.getString(session, "", CONFIG_PROP_TRACKING_FILENAME); 090 if (trackingFilename.isEmpty() 091 || trackingFilename.contains("/") 092 || trackingFilename.contains("\\") 093 || trackingFilename.contains("..")) { 094 trackingFilename = DEFAULT_TRACKING_FILENAME; 095 } 096 097 if ("".equals(repository.getContentType()) || "default".equals(repository.getContentType())) { 098 return new EnhancedLocalRepositoryManager( 099 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}