1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.utils.VersionUtil;
22 import org.apache.archiva.maven2.model.Artifact;
23 import org.apache.archiva.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.model.facets.AuditEvent;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.filter.Filter;
29 import org.apache.archiva.filter.IncludesFilter;
30 import org.apache.archiva.repository.ReleaseScheme;
31 import org.apache.archiva.repository.Repository;
32 import org.apache.archiva.repository.RepositoryNotFoundException;
33 import org.apache.archiva.repository.RepositoryRegistry;
34 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
35 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
36 import org.apache.archiva.stagerepository.merge.RepositoryMerger;
37 import org.apache.archiva.stagerepository.merge.RepositoryMergerException;
38 import org.springframework.stereotype.Service;
40 import javax.inject.Inject;
41 import java.util.ArrayList;
42 import java.util.List;
45 * @author Olivier Lamy
48 @Service ( "mergeRepositoriesService#rest" )
49 public class DefaultMergeRepositoriesService
50 extends AbstractRestService
51 implements MergeRepositoriesService
54 // FIXME check archiva-merge-repository to sourceRepoId
57 private List<RepositoryMerger> repositoryMerger;
60 private RepositoryRegistry repositoryRegistry;
64 public List<Artifact> getMergeConflictedArtifacts( String sourceRepositoryId, String targetRepositoryId )
65 throws ArchivaRestServiceException
67 RepositorySession repositorySession = null;
70 repositorySession = repositorySessionFactory.createSession();
72 catch ( MetadataRepositoryException e )
78 RepositoryMerger merger = findMerger( sourceRepositoryId );
79 List<ArtifactMetadata> artifactMetadatas =
80 merger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
83 return buildArtifacts( artifactMetadatas, sourceRepositoryId );
85 catch ( RepositoryMergerException | RepositoryNotFoundException e )
87 throw new ArchivaRestServiceException( e.getMessage(), e );
91 repositorySession.close();
95 RepositoryMerger findMerger(String repositoryId) throws RepositoryNotFoundException
97 Repository repo = repositoryRegistry.getRepository( repositoryId );
99 throw new RepositoryNotFoundException( repositoryId );
101 return repositoryMerger.stream( ).filter( m -> m.supportsRepository( repo.getType( ) ) ).findFirst().get();
106 public void mergeRepositories( String sourceRepositoryId, String targetRepositoryId, boolean skipConflicts )
107 throws ArchivaRestServiceException
111 RepositoryMerger merger = findMerger( sourceRepositoryId );
115 mergeBySkippingConflicts( merger, sourceRepositoryId, targetRepositoryId );
119 doMerge( merger, sourceRepositoryId, targetRepositoryId );
123 catch ( RepositoryMergerException | RepositoryNotFoundException e )
125 throw new ArchivaRestServiceException( e.getMessage(), e );
131 protected void doMerge( RepositoryMerger merger, String sourceRepositoryId, String targetRepositoryId )
132 throws RepositoryMergerException, ArchivaRestServiceException
134 RepositorySession repositorySession = null;
137 repositorySession = repositorySessionFactory.createSession();
139 catch ( MetadataRepositoryException e )
141 e.printStackTrace( );
146 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(targetRepositoryId);
147 MetadataRepository metadataRepository = repositorySession.getRepository();
148 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts(repositorySession , sourceRepositoryId );
150 if ( managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) )
152 mergeWithOutSnapshots(merger, metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
156 merger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId );
158 for ( ArtifactMetadata metadata : sourceArtifacts )
160 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
164 doScanRepository( targetRepositoryId, false );
166 catch ( MetadataRepositoryException e )
168 throw new ArchivaRestServiceException( e.getMessage(), e );
171 repositorySession.close();
175 private void mergeBySkippingConflicts( RepositoryMerger merger, String sourceRepositoryId, String targetRepositoryId )
176 throws RepositoryMergerException, ArchivaRestServiceException
179 RepositorySession repositorySession = null;
182 repositorySession = repositorySessionFactory.createSession();
184 catch ( MetadataRepositoryException e )
186 e.printStackTrace( );
190 List<ArtifactMetadata> conflictSourceArtifacts =
191 merger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
192 targetRepositoryId );
193 MetadataRepository metadataRepository = repositorySession.getRepository();
194 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts(repositorySession , sourceRepositoryId );
195 sourceArtifacts.removeAll( conflictSourceArtifacts );
197 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(targetRepositoryId);
199 if ( managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT))
201 mergeWithOutSnapshots( merger, metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
206 Filter<ArtifactMetadata> artifactsWithOutConflicts =
207 new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
208 merger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId,
209 artifactsWithOutConflicts );
210 for ( ArtifactMetadata metadata : sourceArtifacts )
212 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
216 doScanRepository( targetRepositoryId, false );
218 catch ( MetadataRepositoryException e )
220 throw new ArchivaRestServiceException( e.getMessage(), e );
223 repositorySession.close();
227 private void mergeWithOutSnapshots( RepositoryMerger merger, MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts,
228 String sourceRepoId, String repoid )
229 throws RepositoryMergerException
231 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<>();
232 for ( ArtifactMetadata metadata : sourceArtifacts )
234 if ( VersionUtil.isSnapshot( metadata.getProjectVersion() ) )
235 //if ( metadata.getProjectVersion().contains( VersionUtil.SNAPSHOT ) )
237 artifactsWithOutSnapshots.add( metadata );
241 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
245 sourceArtifacts.removeAll( artifactsWithOutSnapshots );
247 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
248 merger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots );