1 package org.apache.maven.archiva.web.action;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.Preparable;
23 import com.opensymphony.xwork2.Validateable;
24 import org.apache.archiva.audit.AuditEvent;
25 import org.apache.archiva.audit.Auditable;
26 import org.apache.archiva.metadata.model.ArtifactMetadata;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.archiva.metadata.repository.filter.Filter;
30 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
31 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
32 import org.apache.archiva.scheduler.repository.RepositoryTask;
33 import org.apache.archiva.stagerepository.merge.Maven2RepositoryMerger;
34 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
35 import org.apache.maven.archiva.configuration.Configuration;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.codehaus.plexus.taskqueue.TaskQueueException;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Controller;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44 import javax.inject.Inject;
45 import javax.inject.Named;
48 * plexus.component role="com.opensymphony.xwork2.Action" role-hint="mergeAction" instantiation-strategy="per-lookup"
50 @Controller( "mergeAction" )
52 public class MergeAction
53 extends AbstractActionSupport
54 implements Validateable, Preparable, Auditable
58 * plexus.requirement role="org.apache.archiva.stagerepository.merge.RepositoryMerger" role-hint="maven2"
61 @Named( value = "repositoryMerger#maven2" )
62 private Maven2RepositoryMerger repositoryMerger;
68 protected ArchivaConfiguration archivaConfiguration;
71 @Named( value = "archivaTaskScheduler#repository" )
72 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
74 private ManagedRepositoryConfiguration repository;
76 private String repoid;
78 private String sourceRepoId;
80 private final String action = "merge";
82 private final String hasConflicts = "CONFLICTS";
84 private List<ArtifactMetadata> conflictSourceArtifacts;
86 private List<ArtifactMetadata> conflictSourceArtifactsToBeDisplayed;
88 public String getConflicts()
90 sourceRepoId = repoid + "-stage";
91 Configuration config = archivaConfiguration.getConfiguration();
92 ManagedRepositoryConfiguration targetRepoConfig = config.findManagedRepositoryById( sourceRepoId );
94 if ( targetRepoConfig != null )
105 public String doMerge()
107 RepositorySession repositorySession = repositorySessionFactory.createSession();
110 MetadataRepository metadataRepository = repositorySession.getRepository();
111 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
113 if ( repository.isReleases() && !repository.isSnapshots() )
115 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepoId, repoid );
119 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid );
121 for ( ArtifactMetadata metadata : sourceArtifacts )
123 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
129 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
133 catch ( Exception e )
135 log.error( e.getMessage(), e );
136 addActionError( "Error occurred while merging the repositories: " + e.getMessage() );
141 repositorySession.close();
145 public String mergeBySkippingConflicts()
147 RepositorySession repositorySession = repositorySessionFactory.createSession();
150 MetadataRepository metadataRepository = repositorySession.getRepository();
151 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
152 sourceArtifacts.removeAll( conflictSourceArtifacts );
154 if ( repository.isReleases() && !repository.isSnapshots() )
156 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepoId, repoid );
161 Filter<ArtifactMetadata> artifactsWithOutConflicts =
162 new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
163 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactsWithOutConflicts );
164 for ( ArtifactMetadata metadata : sourceArtifacts )
166 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
172 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
176 catch ( Exception e )
178 log.error( e.getMessage(), e );
179 addActionError( "Error occurred while merging the repositories: " + e.getMessage() );
184 repositorySession.close();
188 public String mergeWithOutConlficts()
190 sourceRepoId = repoid + "-stage";
192 RepositorySession repositorySession = repositorySessionFactory.createSession();
195 conflictSourceArtifacts =
196 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepoId, repoid );
198 catch ( Exception e )
200 addActionError( "Error occurred while merging the repositories." );
205 repositorySession.close();
208 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
213 public ManagedRepositoryConfiguration getRepository()
218 public void setRepository( ManagedRepositoryConfiguration repository )
220 this.repository = repository;
223 public void prepare()
226 sourceRepoId = repoid + "-stage";
227 RepositorySession repositorySession = repositorySessionFactory.createSession();
230 conflictSourceArtifacts =
231 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepoId, repoid );
235 repositorySession.close();
238 Configuration config = archivaConfiguration.getConfiguration();
239 this.repository = config.findManagedRepositoryById( repoid );
240 setConflictSourceArtifactsToBeDisplayed( conflictSourceArtifacts );
243 public String getSourceRepoId()
248 public void setSourceRepoId( String sourceRepoId )
250 this.sourceRepoId = sourceRepoId;
253 public String getRepoid()
258 public void setRepoid( String repoid )
260 this.repoid = repoid;
263 public List<ArtifactMetadata> getConflictSourceArtifacts()
265 return conflictSourceArtifacts;
268 public void setConflictSourceArtifacts( List<ArtifactMetadata> conflictSourceArtifacts )
270 this.conflictSourceArtifacts = conflictSourceArtifacts;
273 public List<ArtifactMetadata> getConflictSourceArtifactsToBeDisplayed()
275 return conflictSourceArtifactsToBeDisplayed;
278 public void setConflictSourceArtifactsToBeDisplayed( List<ArtifactMetadata> conflictSourceArtifacts )
281 this.conflictSourceArtifactsToBeDisplayed = new ArrayList<ArtifactMetadata>();
282 HashMap<String, ArtifactMetadata> map = new HashMap<String, ArtifactMetadata>();
283 for ( ArtifactMetadata metadata : conflictSourceArtifacts )
286 metadata.getNamespace() + metadata.getProject() + metadata.getProjectVersion() + metadata.getVersion();
287 map.put( metadataId, metadata );
289 conflictSourceArtifactsToBeDisplayed.addAll( map.values() );
292 private void mergeWithOutSnapshots( MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts,
293 String sourceRepoId, String repoid )
296 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<ArtifactMetadata>();
297 for ( ArtifactMetadata metadata : sourceArtifacts )
300 if ( metadata.getProjectVersion().contains( "SNAPSHOT" ) )
302 artifactsWithOutSnapshots.add( metadata );
306 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
310 sourceArtifacts.removeAll( artifactsWithOutSnapshots );
312 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
313 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots );
316 private void scanRepository()
318 RepositoryTask task = new RepositoryTask();
319 task.setRepositoryId( repoid );
320 task.setScanAll( true );
322 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repoid ) )
324 log.info( "Repository [" + repoid + "] task was already queued." );
330 log.info( "Your request to have repository [" + repoid + "] be indexed has been queued." );
331 repositoryTaskScheduler.queueTask( task );
333 catch ( TaskQueueException e )
336 "Unable to queue your request to have repository [" + repoid + "] be indexed: " + e.getMessage() );