001package org.apache.archiva.converter.legacy; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.admin.model.beans.ManagedRepository; 023import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; 024import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException; 025import org.apache.archiva.common.utils.PathUtil; 026import org.apache.archiva.consumers.InvalidRepositoryContentConsumer; 027import org.apache.archiva.consumers.KnownRepositoryContentConsumer; 028import org.apache.archiva.converter.RepositoryConversionException; 029import org.apache.archiva.repository.scanner.RepositoryScanner; 030import org.apache.archiva.repository.scanner.RepositoryScannerException; 031import org.apache.maven.artifact.repository.ArtifactRepository; 032import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; 033import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; 034import org.springframework.stereotype.Service; 035 036import javax.inject.Inject; 037import javax.inject.Named; 038import java.io.File; 039import java.util.ArrayList; 040import java.util.Arrays; 041import java.util.Collections; 042import java.util.List; 043 044/** 045 * DefaultLegacyRepositoryConverter 046 * 047 * 048 */ 049@Service( "legacyRepositoryConverter#default" ) 050public class DefaultLegacyRepositoryConverter 051 implements LegacyRepositoryConverter 052{ 053 /** 054 * 055 */ 056 private ArtifactRepositoryFactory artifactRepositoryFactory; 057 058 /** 059 * 060 */ 061 private ArtifactRepositoryLayout defaultLayout; 062 063 /** 064 * 065 */ 066 @Inject 067 @Named( value = "knownRepositoryContentConsumer#artifact-legacy-to-default-converter" ) 068 private LegacyConverterArtifactConsumer legacyConverterConsumer; 069 070 /** 071 * 072 */ 073 @Inject 074 private RepositoryScanner repoScanner; 075 076 @Inject 077 public DefaultLegacyRepositoryConverter( PlexusSisuBridge plexusSisuBridge ) 078 throws PlexusSisuBridgeException 079 { 080 artifactRepositoryFactory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class ); 081 defaultLayout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" ); 082 } 083 084 @Override 085 public void convertLegacyRepository( File legacyRepositoryDirectory, File repositoryDirectory, 086 List<String> fileExclusionPatterns ) 087 throws RepositoryConversionException 088 { 089 try 090 { 091 String defaultRepositoryUrl = PathUtil.toUrl( repositoryDirectory ); 092 093 ManagedRepository legacyRepository = new ManagedRepository(); 094 legacyRepository.setId( "legacy" ); 095 legacyRepository.setName( "Legacy Repository" ); 096 legacyRepository.setLocation( legacyRepositoryDirectory.getAbsolutePath() ); 097 legacyRepository.setLayout( "legacy" ); 098 099 ArtifactRepository repository = 100 artifactRepositoryFactory.createArtifactRepository( "default", defaultRepositoryUrl, defaultLayout, 101 null, null ); 102 legacyConverterConsumer.setExcludes( fileExclusionPatterns ); 103 legacyConverterConsumer.setDestinationRepository( repository ); 104 105 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>( 1 ); 106 knownConsumers.add( legacyConverterConsumer ); 107 108 List<InvalidRepositoryContentConsumer> invalidConsumers = Collections.emptyList(); 109 List<String> ignoredContent = new ArrayList<String>( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) ); 110 111 repoScanner.scan( legacyRepository, knownConsumers, invalidConsumers, ignoredContent, 112 RepositoryScanner.FRESH_SCAN ); 113 } 114 catch ( RepositoryScannerException e ) 115 { 116 throw new RepositoryConversionException( "Error convering legacy repository.", e ); 117 } 118 } 119}