001package org.apache.archiva.rest.services; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.admin.model.RepositoryAdminException; 022import org.apache.archiva.admin.model.beans.ManagedRepository; 023import org.apache.archiva.metadata.model.facets.AuditEvent; 024import org.apache.archiva.common.utils.VersionUtil; 025import org.apache.archiva.maven2.model.Artifact; 026import org.apache.archiva.metadata.model.ArtifactMetadata; 027import org.apache.archiva.metadata.repository.MetadataRepository; 028import org.apache.archiva.metadata.repository.MetadataRepositoryException; 029import org.apache.archiva.metadata.repository.RepositorySession; 030import org.apache.archiva.metadata.repository.filter.Filter; 031import org.apache.archiva.metadata.repository.filter.IncludesFilter; 032import org.apache.archiva.rest.api.services.ArchivaRestServiceException; 033import org.apache.archiva.rest.api.services.MergeRepositoriesService; 034import org.apache.archiva.stagerepository.merge.Maven2RepositoryMerger; 035import org.apache.archiva.stagerepository.merge.RepositoryMergerException; 036import org.springframework.stereotype.Service; 037 038import javax.inject.Inject; 039import javax.inject.Named; 040import java.util.ArrayList; 041import java.util.List; 042 043/** 044 * @author Olivier Lamy 045 * @since 1.4-M3 046 */ 047@Service ( "mergeRepositoriesService#rest" ) 048public class DefaultMergeRepositoriesService 049 extends AbstractRestService 050 implements MergeRepositoriesService 051{ 052 053 // FIXME check archiva-merge-repository to sourceRepoId 054 055 @Inject 056 @Named ( value = "repositoryMerger#maven2" ) 057 private Maven2RepositoryMerger repositoryMerger; 058 059 060 @Override 061 public List<Artifact> getMergeConflictedArtifacts( String sourceRepositoryId, String targetRepositoryId ) 062 throws ArchivaRestServiceException 063 { 064 RepositorySession repositorySession = repositorySessionFactory.createSession(); 065 try 066 { 067 List<ArtifactMetadata> artifactMetadatas = 068 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId, 069 targetRepositoryId ); 070 071 return buildArtifacts( artifactMetadatas, sourceRepositoryId ); 072 } 073 catch ( RepositoryMergerException e ) 074 { 075 throw new ArchivaRestServiceException( e.getMessage(), e ); 076 } 077 finally 078 { 079 repositorySession.close(); 080 } 081 } 082 083 @Override 084 public void mergeRepositories( String sourceRepositoryId, String targetRepositoryId, boolean skipConflicts ) 085 throws ArchivaRestServiceException 086 { 087 try 088 { 089 if ( skipConflicts ) 090 { 091 mergeBySkippingConflicts( sourceRepositoryId, targetRepositoryId ); 092 } 093 else 094 { 095 doMerge( sourceRepositoryId, targetRepositoryId ); 096 } 097 098 } 099 catch ( RepositoryMergerException e ) 100 { 101 throw new ArchivaRestServiceException( e.getMessage(), e ); 102 } 103 104 } 105 106 107 protected void doMerge( String sourceRepositoryId, String targetRepositoryId ) 108 throws RepositoryMergerException, ArchivaRestServiceException 109 { 110 RepositorySession repositorySession = repositorySessionFactory.createSession(); 111 112 try 113 { 114 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( targetRepositoryId ); 115 MetadataRepository metadataRepository = repositorySession.getRepository(); 116 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId ); 117 118 if ( repository.isReleases() && !repository.isSnapshots() ) 119 { 120 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId ); 121 } 122 else 123 { 124 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId ); 125 126 for ( ArtifactMetadata metadata : sourceArtifacts ) 127 { 128 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES ); 129 } 130 } 131 132 doScanRepository( targetRepositoryId, false ); 133 } 134 catch ( MetadataRepositoryException e ) 135 { 136 throw new ArchivaRestServiceException( e.getMessage(), e ); 137 } 138 catch ( RepositoryAdminException e ) 139 { 140 throw new ArchivaRestServiceException( e.getMessage(), e ); 141 } 142 finally 143 { 144 repositorySession.close(); 145 } 146 } 147 148 public void mergeBySkippingConflicts( String sourceRepositoryId, String targetRepositoryId ) 149 throws RepositoryMergerException, ArchivaRestServiceException 150 { 151 152 RepositorySession repositorySession = repositorySessionFactory.createSession(); 153 try 154 { 155 List<ArtifactMetadata> conflictSourceArtifacts = 156 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId, 157 targetRepositoryId ); 158 MetadataRepository metadataRepository = repositorySession.getRepository(); 159 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId ); 160 sourceArtifacts.removeAll( conflictSourceArtifacts ); 161 162 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( targetRepositoryId ); 163 164 if ( repository.isReleases() && !repository.isSnapshots() ) 165 { 166 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId ); 167 } 168 else 169 { 170 171 Filter<ArtifactMetadata> artifactsWithOutConflicts = 172 new IncludesFilter<ArtifactMetadata>( sourceArtifacts ); 173 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId, 174 artifactsWithOutConflicts ); 175 for ( ArtifactMetadata metadata : sourceArtifacts ) 176 { 177 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES ); 178 } 179 } 180 181 doScanRepository( targetRepositoryId, false ); 182 } 183 catch ( MetadataRepositoryException e ) 184 { 185 throw new ArchivaRestServiceException( e.getMessage(), e ); 186 } 187 catch ( RepositoryAdminException e ) 188 { 189 throw new ArchivaRestServiceException( e.getMessage(), e ); 190 191 } 192 finally 193 { 194 repositorySession.close(); 195 } 196 } 197 198 private void mergeWithOutSnapshots( MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts, 199 String sourceRepoId, String repoid ) 200 throws RepositoryMergerException 201 { 202 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<>(); 203 for ( ArtifactMetadata metadata : sourceArtifacts ) 204 { 205 if ( VersionUtil.isSnapshot( metadata.getProjectVersion() ) ) 206 //if ( metadata.getProjectVersion().contains( VersionUtil.SNAPSHOT ) ) 207 { 208 artifactsWithOutSnapshots.add( metadata ); 209 } 210 else 211 { 212 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES ); 213 } 214 215 } 216 sourceArtifacts.removeAll( artifactsWithOutSnapshots ); 217 218 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts ); 219 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots ); 220 } 221}