use a better exception mechanism for that

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1387387 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-09-18 22:02:33 +00:00
parent 65768e8719
commit 5a3bf2a3af
3 changed files with 101 additions and 33 deletions

View File

@ -27,8 +27,8 @@ import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.archiva.maven2.metadata.MavenMetadataReader;
import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataRepositoryException;
import org.apache.archiva.metadata.repository.filter.Filter;
import org.apache.archiva.metadata.repository.filter.IncludesFilter;
import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.repository.RepositoryException;
@ -59,7 +59,7 @@ import java.util.regex.Pattern;
/**
*
*/
@Service( "repositoryMerger#maven2" )
@Service ("repositoryMerger#maven2")
public class Maven2RepositoryMerger
implements RepositoryMerger
{
@ -80,8 +80,8 @@ public class Maven2RepositoryMerger
@Inject
public Maven2RepositoryMerger(
@Named( value = "archivaConfiguration#default" ) ArchivaConfiguration archivaConfiguration,
@Named( value = "repositoryPathTranslator#maven2" ) RepositoryPathTranslator repositoryPathTranslator )
@Named (value = "archivaConfiguration#default") ArchivaConfiguration archivaConfiguration,
@Named (value = "repositoryPathTranslator#maven2") RepositoryPathTranslator repositoryPathTranslator )
{
this.configuration = archivaConfiguration;
this.pathTranslator = repositoryPathTranslator;
@ -93,30 +93,60 @@ public class Maven2RepositoryMerger
}
public void merge( MetadataRepository metadataRepository, String sourceRepoId, String targetRepoId )
throws Exception
throws RepositoryMergerException
{
List<ArtifactMetadata> artifactsInSourceRepo = metadataRepository.getArtifacts( sourceRepoId );
for ( ArtifactMetadata artifactMetadata : artifactsInSourceRepo )
try
{
artifactMetadata.setRepositoryId( targetRepoId );
createFolderStructure( sourceRepoId, targetRepoId, artifactMetadata );
List<ArtifactMetadata> artifactsInSourceRepo = metadataRepository.getArtifacts( sourceRepoId );
for ( ArtifactMetadata artifactMetadata : artifactsInSourceRepo )
{
artifactMetadata.setRepositoryId( targetRepoId );
createFolderStructure( sourceRepoId, targetRepoId, artifactMetadata );
}
}
catch ( MetadataRepositoryException e )
{
throw new RepositoryMergerException( e.getMessage(), e );
}
catch ( IOException e )
{
throw new RepositoryMergerException( e.getMessage(), e );
}
catch ( RepositoryException e )
{
throw new RepositoryMergerException( e.getMessage(), e );
}
}
// TODO when UI needs a subset to merge
public void merge( MetadataRepository metadataRepository, String sourceRepoId, String targetRepoId,
Filter<ArtifactMetadata> filter )
throws Exception
throws RepositoryMergerException
{
List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
for ( ArtifactMetadata metadata : sourceArtifacts )
try
{
if ( filter.accept( metadata ) )
List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
for ( ArtifactMetadata metadata : sourceArtifacts )
{
createFolderStructure( sourceRepoId, targetRepoId, metadata );
if ( filter.accept( metadata ) )
{
createFolderStructure( sourceRepoId, targetRepoId, metadata );
}
}
}
catch ( MetadataRepositoryException e )
{
throw new RepositoryMergerException( e.getMessage(), e );
}
catch ( IOException e )
{
throw new RepositoryMergerException( e.getMessage(), e );
}
catch ( RepositoryException e )
{
throw new RepositoryMergerException( e.getMessage(), e );
}
}
private void createFolderStructure( String sourceRepoId, String targetRepoId, ArtifactMetadata artifactMetadata )
@ -206,7 +236,7 @@ public class Maven2RepositoryMerger
if ( versionMetaDataFileInSourceRepo.exists() )
{//Pattern quote for windows path
String relativePathToVersionMetadataFile =
versionMetaDataFileInSourceRepo.getAbsolutePath().split( Pattern.quote( sourceRepoPath ) )[1];
versionMetaDataFileInSourceRepo.getAbsolutePath().split( Pattern.quote( sourceRepoPath ) )[1];
File versionMetaDataFileInTargetRepo = new File( targetRepoPath, relativePathToVersionMetadataFile );
if ( !versionMetaDataFileInTargetRepo.exists() )
@ -352,30 +382,36 @@ public class Maven2RepositoryMerger
public List<ArtifactMetadata> getConflictingArtifacts( MetadataRepository metadataRepository, String sourceRepo,
String targetRepo )
throws Exception
throws RepositoryMergerException
{
List<ArtifactMetadata> targetArtifacts = metadataRepository.getArtifacts( targetRepo );
List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepo );
List<ArtifactMetadata> conflictsArtifacts = new ArrayList<ArtifactMetadata>();
for ( ArtifactMetadata targetArtifact : targetArtifacts )
try
{
for ( ArtifactMetadata sourceArtifact : sourceArtifacts )
List<ArtifactMetadata> targetArtifacts = metadataRepository.getArtifacts( targetRepo );
List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepo );
List<ArtifactMetadata> conflictsArtifacts = new ArrayList<ArtifactMetadata>();
for ( ArtifactMetadata targetArtifact : targetArtifacts )
{
if ( isEquals( targetArtifact, sourceArtifact ) )
for ( ArtifactMetadata sourceArtifact : sourceArtifacts )
{
if ( !conflictsArtifacts.contains( sourceArtifact ) )
if ( isEquals( targetArtifact, sourceArtifact ) )
{
conflictsArtifacts.add( sourceArtifact );
if ( !conflictsArtifacts.contains( sourceArtifact ) )
{
conflictsArtifacts.add( sourceArtifact );
}
}
}
}
}
sourceArtifacts.removeAll( conflictsArtifacts );
Filter<ArtifactMetadata> artifactsWithOutConflicts = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
// merge( sourceRepo, targetRepo, artifactsWithOutConflicts );
return conflictsArtifacts;
sourceArtifacts.removeAll( conflictsArtifacts );
return conflictsArtifacts;
}
catch ( MetadataRepositoryException e )
{
throw new RepositoryMergerException( e.getMessage(), e );
}
}
private boolean isEquals( ArtifactMetadata sourceArtifact, ArtifactMetadata targetArtifact )

View File

@ -28,13 +28,13 @@ import java.util.List;
public interface RepositoryMerger
{
void merge( MetadataRepository metadataRepository, String sourceRepoId, String targetRepoId )
throws Exception;
throws RepositoryMergerException;
void merge( MetadataRepository metadataRepository, String sourceRepoId, String targetRepoId,
Filter<ArtifactMetadata> filter )
throws Exception;
throws RepositoryMergerException;
List<ArtifactMetadata> getConflictingArtifacts( MetadataRepository metadataRepository, String sourceRepo,
String targetRepo )
throws Exception;
throws RepositoryMergerException;
}

View File

@ -0,0 +1,32 @@
package org.apache.archiva.stagerepository.merge;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* @author Olivier Lamy
* @since 1.4-M3
*/
public class RepositoryMergerException
extends Exception
{
public RepositoryMergerException( String msg, Exception e )
{
super( msg, e );
}
}