This project has retired. For details please refer to its Attic page.
DefaultMergeRepositoriesService xref
View Javadoc
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.metadata.model.facets.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;
37  
38  import javax.inject.Inject;
39  import javax.inject.Named;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * @author Olivier Lamy
45   * @since 1.4-M3
46   */
47  @Service ( "mergeRepositoriesService#rest" )
48  public class DefaultMergeRepositoriesService
49      extends AbstractRestService
50      implements MergeRepositoriesService
51  {
52  
53      // FIXME check archiva-merge-repository to sourceRepoId
54  
55      @Inject
56      @Named ( value = "repositoryMerger#maven2" )
57      private Maven2RepositoryMerger repositoryMerger;
58  
59  
60      @Override
61      public List<Artifact> getMergeConflictedArtifacts( String sourceRepositoryId, String targetRepositoryId )
62          throws ArchivaRestServiceException
63      {
64          RepositorySession repositorySession = repositorySessionFactory.createSession();
65          try
66          {
67              List<ArtifactMetadata> artifactMetadatas =
68                  repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepositoryId,
69                                                            targetRepositoryId );
70  
71              return buildArtifacts( artifactMetadatas, sourceRepositoryId );
72          }
73          catch ( RepositoryMergerException e )
74          {
75              throw new ArchivaRestServiceException( e.getMessage(), e );
76          }
77          finally
78          {
79              repositorySession.close();
80          }
81      }
82  
83      @Override
84      public void mergeRepositories( String sourceRepositoryId, String targetRepositoryId, boolean skipConflicts )
85          throws ArchivaRestServiceException
86      {
87          try
88          {
89              if ( skipConflicts )
90              {
91                  mergeBySkippingConflicts( sourceRepositoryId, targetRepositoryId );
92              }
93              else
94              {
95                  doMerge( sourceRepositoryId, targetRepositoryId );
96              }
97  
98          }
99          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 }