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.consumers.AbstractMonitoredConsumer; 026import org.apache.archiva.consumers.ConsumerException; 027import org.apache.archiva.consumers.KnownRepositoryContentConsumer; 028import org.apache.archiva.converter.artifact.ArtifactConversionException; 029import org.apache.archiva.converter.artifact.ArtifactConverter; 030import org.apache.archiva.model.ArtifactReference; 031import org.apache.archiva.repository.ManagedRepositoryContent; 032import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent; 033import org.apache.archiva.repository.layout.LayoutException; 034import org.apache.maven.artifact.Artifact; 035import org.apache.maven.artifact.factory.ArtifactFactory; 036import org.apache.maven.artifact.repository.ArtifactRepository; 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039import org.springframework.context.annotation.Scope; 040import org.springframework.stereotype.Service; 041 042import javax.inject.Inject; 043import javax.inject.Named; 044import java.util.ArrayList; 045import java.util.Date; 046import java.util.List; 047 048/** 049 * LegacyConverterArtifactConsumer - convert artifacts as they are found 050 * into the destination repository. 051 * 052 * 053 */ 054@Service( "knownRepositoryContentConsumer#artifact-legacy-to-default-converter" ) 055@Scope( "prototype" ) 056public class LegacyConverterArtifactConsumer 057 extends AbstractMonitoredConsumer 058 implements KnownRepositoryContentConsumer 059{ 060 private Logger log = LoggerFactory.getLogger( LegacyConverterArtifactConsumer.class ); 061 062 @Inject 063 @Named("artifactConverter#legacy-to-default") 064 private ArtifactConverter artifactConverter; 065 066 private ArtifactFactory artifactFactory; 067 068 private ManagedRepositoryContent managedRepository; 069 070 private ArtifactRepository destinationRepository; 071 072 private List<String> includes; 073 074 private List<String> excludes; 075 076 @Inject 077 public LegacyConverterArtifactConsumer( PlexusSisuBridge plexusSisuBridge ) 078 throws PlexusSisuBridgeException 079 { 080 includes = new ArrayList<>( 3 ); 081 includes.add( "**/*.jar" ); 082 includes.add( "**/*.ear" ); 083 includes.add( "**/*.war" ); 084 artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class ); 085 } 086 087 @Override 088 public void beginScan( ManagedRepository repository, Date whenGathered ) 089 throws ConsumerException 090 { 091 this.managedRepository = new ManagedDefaultRepositoryContent(); 092 this.managedRepository.setRepository( repository ); 093 } 094 095 @Override 096 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo ) 097 throws ConsumerException 098 { 099 beginScan( repository, whenGathered ); 100 } 101 102 @Override 103 public void completeScan() 104 { 105 // no op 106 } 107 108 @Override 109 public void completeScan( boolean executeOnEntireRepo ) 110 { 111 completeScan(); 112 } 113 114 @Override 115 public List<String> getExcludes() 116 { 117 return excludes; 118 } 119 120 @Override 121 public List<String> getIncludes() 122 { 123 return includes; 124 } 125 126 @Override 127 public void processFile( String path ) 128 throws ConsumerException 129 { 130 try 131 { 132 ArtifactReference reference = managedRepository.toArtifactReference( path ); 133 Artifact artifact = artifactFactory.createArtifact( reference.getGroupId(), reference.getArtifactId(), 134 reference.getVersion(), reference.getClassifier(), 135 reference.getType() ); 136 artifactConverter.convert( artifact, destinationRepository ); 137 } 138 catch ( LayoutException e ) 139 { 140 log.warn( "Unable to convert artifact: {} : {}",path , e.getMessage(), e ); 141 } 142 catch ( ArtifactConversionException e ) 143 { 144 log.warn( "Unable to convert artifact: {} : {}",path , e.getMessage(), e ); 145 } 146 } 147 148 @Override 149 public void processFile( String path, boolean executeOnEntireRepo ) 150 throws Exception 151 { 152 processFile( path ); 153 } 154 155 @Override 156 public String getDescription() 157 { 158 return "Legacy Artifact to Default Artifact Converter"; 159 } 160 161 @Override 162 public String getId() 163 { 164 return "artifact-legacy-to-default-converter"; 165 } 166 167 public void setExcludes( List<String> excludes ) 168 { 169 this.excludes = excludes; 170 } 171 172 public void setIncludes( List<String> includes ) 173 { 174 this.includes = includes; 175 } 176 177 public ArtifactRepository getDestinationRepository() 178 { 179 return destinationRepository; 180 } 181 182 public void setDestinationRepository( ArtifactRepository destinationRepository ) 183 { 184 this.destinationRepository = destinationRepository; 185 } 186}