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.RepositorySystemSession; 026import org.eclipse.aether.repository.LocalRepository; 027import org.eclipse.aether.repository.LocalRepositoryManager; 028import org.eclipse.aether.repository.NoLocalRepositoryManagerException; 029import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory; 030import org.eclipse.aether.spi.locator.Service; 031import org.eclipse.aether.spi.locator.ServiceLocator; 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("enhanced") 045public class EnhancedLocalRepositoryManagerFactory implements LocalRepositoryManagerFactory, Service { 046 private static final String CONFIG_PROP_TRACKING_FILENAME = "aether.enhancedLocalRepository.trackingFilename"; 047 048 private static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories"; 049 050 private float priority = 10.0f; 051 052 private LocalPathComposer localPathComposer; 053 054 private TrackingFileManager trackingFileManager; 055 056 private LocalPathPrefixComposerFactory localPathPrefixComposerFactory; 057 058 public EnhancedLocalRepositoryManagerFactory() { 059 // no arg ctor for ServiceLocator 060 } 061 062 @Inject 063 public EnhancedLocalRepositoryManagerFactory( 064 final LocalPathComposer localPathComposer, 065 final TrackingFileManager trackingFileManager, 066 final LocalPathPrefixComposerFactory localPathPrefixComposerFactory) { 067 this.localPathComposer = requireNonNull(localPathComposer); 068 this.trackingFileManager = requireNonNull(trackingFileManager); 069 this.localPathPrefixComposerFactory = requireNonNull(localPathPrefixComposerFactory); 070 } 071 072 @Override 073 public void initService(final ServiceLocator locator) { 074 this.localPathComposer = requireNonNull(locator.getService(LocalPathComposer.class)); 075 this.trackingFileManager = requireNonNull(locator.getService(TrackingFileManager.class)); 076 this.localPathPrefixComposerFactory = new DefaultLocalPathPrefixComposerFactory(); 077 } 078 079 @Override 080 public LocalRepositoryManager newInstance(RepositorySystemSession session, LocalRepository repository) 081 throws NoLocalRepositoryManagerException { 082 requireNonNull(session, "session cannot be null"); 083 requireNonNull(repository, "repository cannot be null"); 084 085 String trackingFilename = ConfigUtils.getString(session, "", CONFIG_PROP_TRACKING_FILENAME); 086 if (trackingFilename.isEmpty() 087 || trackingFilename.contains("/") 088 || trackingFilename.contains("\\") 089 || trackingFilename.contains("..")) { 090 trackingFilename = DEFAULT_TRACKING_FILENAME; 091 } 092 093 if ("".equals(repository.getContentType()) || "default".equals(repository.getContentType())) { 094 return new EnhancedLocalRepositoryManager( 095 repository.getBasedir(), 096 localPathComposer, 097 trackingFilename, 098 trackingFileManager, 099 localPathPrefixComposerFactory.createComposer(session)); 100 } else { 101 throw new NoLocalRepositoryManagerException(repository); 102 } 103 } 104 105 @Override 106 public float getPriority() { 107 return priority; 108 } 109 110 /** 111 * Sets the priority of this component. 112 * 113 * @param priority The priority. 114 * @return This component for chaining, never {@code null}. 115 */ 116 public EnhancedLocalRepositoryManagerFactory setPriority(float priority) { 117 this.priority = priority; 118 return this; 119 } 120}