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.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.audit.AuditEvent;
24 import org.apache.archiva.common.utils.VersionUtil;
25 import org.apache.archiva.maven2.model.Artifact;
26 import org.apache.archiva.metadata.model.ArtifactMetadata;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.metadata.repository.filter.Filter;
31 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
32 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
33 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
34 import org.apache.archiva.stagerepository.merge.Maven2RepositoryMerger;
35 import org.apache.archiva.stagerepository.merge.RepositoryMergerException;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import java.util.ArrayList;
41 import java.util.List;
44 * @author Olivier Lamy
47 @Service ( "mergeRepositoriesService#rest" )
48 public class DefaultMergeRepositoriesService
49 extends AbstractRestService
50 implements MergeRepositoriesService
54 @Named ( value = "repositoryMerger#maven2" )
55 private Maven2RepositoryMerger repositoryMerger;
58 public List<Artifact> getMergeConflictedArtifacts( String sourceRepositoryId, String targetRepositoryId )
59 throws ArchivaRestServiceException
61 RepositorySession repositorySession = repositorySessionFactory.createSession();
64 List<ArtifactMetadata> artifactMetadatas =
65 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
68 return buildArtifacts( artifactMetadatas, sourceRepositoryId );
70 catch ( RepositoryMergerException e )
72 throw new ArchivaRestServiceException( e.getMessage(), e );
76 repositorySession.close();
80 public void mergeRepositories( String sourceRepositoryId, String targetRepositoryId, boolean skipConflicts )
81 throws ArchivaRestServiceException
87 mergeBySkippingConflicts( sourceRepositoryId, targetRepositoryId );
91 doMerge( sourceRepositoryId, targetRepositoryId );
95 catch ( RepositoryMergerException e )
97 throw new ArchivaRestServiceException( e.getMessage(), e );
103 protected void doMerge( String sourceRepositoryId, String targetRepositoryId )
104 throws RepositoryMergerException, ArchivaRestServiceException
106 RepositorySession repositorySession = repositorySessionFactory.createSession();
110 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( targetRepositoryId );
111 MetadataRepository metadataRepository = repositorySession.getRepository();
112 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId );
114 if ( repository.isReleases() && !repository.isSnapshots() )
116 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
120 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId );
122 for ( ArtifactMetadata metadata : sourceArtifacts )
124 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
128 doScanRepository( targetRepositoryId, false );
130 catch ( MetadataRepositoryException e )
132 throw new ArchivaRestServiceException( e.getMessage(), e );
134 catch ( RepositoryAdminException e )
136 throw new ArchivaRestServiceException( e.getMessage(), e );
140 repositorySession.close();
144 public void mergeBySkippingConflicts( String sourceRepositoryId, String targetRepositoryId )
145 throws RepositoryMergerException, ArchivaRestServiceException
148 RepositorySession repositorySession = repositorySessionFactory.createSession();
151 List<ArtifactMetadata> conflictSourceArtifacts =
152 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
153 targetRepositoryId );
154 MetadataRepository metadataRepository = repositorySession.getRepository();
155 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId );
156 sourceArtifacts.removeAll( conflictSourceArtifacts );
158 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( targetRepositoryId );
160 if ( repository.isReleases() && !repository.isSnapshots() )
162 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
167 Filter<ArtifactMetadata> artifactsWithOutConflicts =
168 new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
169 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId,
170 artifactsWithOutConflicts );
171 for ( ArtifactMetadata metadata : sourceArtifacts )
173 triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
177 doScanRepository( targetRepositoryId, false );
179 catch ( MetadataRepositoryException e )
181 throw new ArchivaRestServiceException( e.getMessage(), e );
183 catch ( RepositoryAdminException e )
185 throw new ArchivaRestServiceException( e.getMessage(), e );
190 repositorySession.close();
194 private void mergeWithOutSnapshots( MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts,
195 String sourceRepoId, String repoid )
196 throws RepositoryMergerException
198 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<ArtifactMetadata>();
199 for ( ArtifactMetadata metadata : sourceArtifacts )
201 if ( VersionUtil.isSnapshot( metadata.getProjectVersion() ) )
202 //if ( metadata.getProjectVersion().contains( VersionUtil.SNAPSHOT ) )
204 artifactsWithOutSnapshots.add( metadata );
208 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
212 sourceArtifacts.removeAll( artifactsWithOutSnapshots );
214 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
215 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots );