]> source.dussan.org Git - archiva.git/blob
256d73a11f9851095f78f88162328425217c9b1e
[archiva.git] /
1 package org.apache.archiva.rest.services;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.VersionUtil;
24 import org.apache.archiva.maven2.model.Artifact;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.metadata.model.facets.AuditEvent;
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.repository.ReleaseScheme;
33 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
34 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
35 import org.apache.archiva.stagerepository.merge.Maven2RepositoryMerger;
36 import org.apache.archiva.stagerepository.merge.RepositoryMergerException;
37 import org.springframework.stereotype.Service;
38
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import java.util.ArrayList;
42 import java.util.List;
43
44 /**
45  * @author Olivier Lamy
46  * @since 1.4-M3
47  */
48 @Service ( "mergeRepositoriesService#rest" )
49 public class DefaultMergeRepositoriesService
50     extends AbstractRestService
51     implements MergeRepositoriesService
52 {
53
54     // FIXME check archiva-merge-repository to sourceRepoId
55
56     @Inject
57     @Named ( value = "repositoryMerger#maven2" )
58     private Maven2RepositoryMerger repositoryMerger;
59
60
61     @Override
62     public List<Artifact> getMergeConflictedArtifacts( String sourceRepositoryId, String targetRepositoryId )
63         throws ArchivaRestServiceException
64     {
65         RepositorySession repositorySession = repositorySessionFactory.createSession();
66         try
67         {
68             List<ArtifactMetadata> artifactMetadatas =
69                 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
70                                                           targetRepositoryId );
71
72             return buildArtifacts( artifactMetadatas, sourceRepositoryId );
73         }
74         catch ( RepositoryMergerException e )
75         {
76             throw new ArchivaRestServiceException( e.getMessage(), e );
77         }
78         finally
79         {
80             repositorySession.close();
81         }
82     }
83
84     @Override
85     public void mergeRepositories( String sourceRepositoryId, String targetRepositoryId, boolean skipConflicts )
86         throws ArchivaRestServiceException
87     {
88         try
89         {
90             if ( skipConflicts )
91             {
92                 mergeBySkippingConflicts( sourceRepositoryId, targetRepositoryId );
93             }
94             else
95             {
96                 doMerge( sourceRepositoryId, targetRepositoryId );
97             }
98
99         }
100         catch ( RepositoryMergerException e )
101         {
102             throw new ArchivaRestServiceException( e.getMessage(), e );
103         }
104
105     }
106
107
108     protected void doMerge( String sourceRepositoryId, String targetRepositoryId )
109         throws RepositoryMergerException, ArchivaRestServiceException
110     {
111         RepositorySession repositorySession = repositorySessionFactory.createSession();
112
113         try
114         {
115             org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(targetRepositoryId);
116             MetadataRepository metadataRepository = repositorySession.getRepository();
117             List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepositoryId );
118
119             if ( managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) )
120             {
121                 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
122             }
123             else
124             {
125                 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId );
126
127                 for ( ArtifactMetadata metadata : sourceArtifacts )
128                 {
129                     triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
130                 }
131             }
132
133             doScanRepository( targetRepositoryId, false );
134         }
135         catch ( MetadataRepositoryException e )
136         {
137             throw new ArchivaRestServiceException( e.getMessage(), e );
138         } finally
139         {
140             repositorySession.close();
141         }
142     }
143
144     public void mergeBySkippingConflicts( String sourceRepositoryId, String targetRepositoryId )
145         throws RepositoryMergerException, ArchivaRestServiceException
146     {
147
148         RepositorySession repositorySession = repositorySessionFactory.createSession();
149         try
150         {
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 );
157
158             org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(targetRepositoryId);
159
160             if ( managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT))
161             {
162                 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepositoryId, targetRepositoryId );
163             }
164             else
165             {
166
167                 Filter<ArtifactMetadata> artifactsWithOutConflicts =
168                     new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
169                 repositoryMerger.merge( metadataRepository, sourceRepositoryId, targetRepositoryId,
170                                         artifactsWithOutConflicts );
171                 for ( ArtifactMetadata metadata : sourceArtifacts )
172                 {
173                     triggerAuditEvent( targetRepositoryId, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
174                 }
175             }
176
177             doScanRepository( targetRepositoryId, false );
178         }
179         catch ( MetadataRepositoryException e )
180         {
181             throw new ArchivaRestServiceException( e.getMessage(), e );
182         } finally
183         {
184             repositorySession.close();
185         }
186     }
187
188     private void mergeWithOutSnapshots( MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts,
189                                         String sourceRepoId, String repoid )
190         throws RepositoryMergerException
191     {
192         List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<>();
193         for ( ArtifactMetadata metadata : sourceArtifacts )
194         {
195             if ( VersionUtil.isSnapshot( metadata.getProjectVersion() ) )
196             //if ( metadata.getProjectVersion().contains( VersionUtil.SNAPSHOT ) )
197             {
198                 artifactsWithOutSnapshots.add( metadata );
199             }
200             else
201             {
202                 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
203             }
204
205         }
206         sourceArtifacts.removeAll( artifactsWithOutSnapshots );
207
208         Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
209         repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots );
210     }
211 }