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;
50 @Controller( "mergeAction" )
52 public class MergeAction
53 extends AbstractActionSupport
54 implements Validateable, Preparable, Auditable
59 @Named( value = "repositoryMerger#maven2" )
60 private Maven2RepositoryMerger repositoryMerger;
63 protected ArchivaConfiguration archivaConfiguration;
66 @Named( value = "archivaTaskScheduler#repository" )
67 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
69 private ManagedRepositoryConfiguration repository;
71 private String repoid;
73 private String sourceRepoId;
75 private final String action = "merge";
77 private final String hasConflicts = "CONFLICTS";
79 private List<ArtifactMetadata> conflictSourceArtifacts;
81 private List<ArtifactMetadata> conflictSourceArtifactsToBeDisplayed;
83 public String getConflicts()
85 sourceRepoId = repoid + "-stage";
86 Configuration config = archivaConfiguration.getConfiguration();
87 ManagedRepositoryConfiguration targetRepoConfig = config.findManagedRepositoryById( sourceRepoId );
89 if ( targetRepoConfig != null )
100 public String doMerge()
102 RepositorySession repositorySession = repositorySessionFactory.createSession();
105 MetadataRepository metadataRepository = repositorySession.getRepository();
106 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
108 if ( repository.isReleases() && !repository.isSnapshots() )
110 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepoId, repoid );
114 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid );
116 for ( ArtifactMetadata metadata : sourceArtifacts )
118 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
124 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
128 catch ( Exception e )
130 log.error( e.getMessage(), e );
131 addActionError( "Error occurred while merging the repositories: " + e.getMessage() );
136 repositorySession.close();
140 public String mergeBySkippingConflicts()
142 RepositorySession repositorySession = repositorySessionFactory.createSession();
145 MetadataRepository metadataRepository = repositorySession.getRepository();
146 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
147 sourceArtifacts.removeAll( conflictSourceArtifacts );
149 if ( repository.isReleases() && !repository.isSnapshots() )
151 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepoId, repoid );
156 Filter<ArtifactMetadata> artifactsWithOutConflicts =
157 new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
158 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactsWithOutConflicts );
159 for ( ArtifactMetadata metadata : sourceArtifacts )
161 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
167 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
171 catch ( Exception e )
173 log.error( e.getMessage(), e );
174 addActionError( "Error occurred while merging the repositories: " + e.getMessage() );
179 repositorySession.close();
183 public String mergeWithOutConlficts()
185 sourceRepoId = repoid + "-stage";
187 RepositorySession repositorySession = repositorySessionFactory.createSession();
190 conflictSourceArtifacts =
191 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepoId, repoid );
193 catch ( Exception e )
195 addActionError( "Error occurred while merging the repositories." );
200 repositorySession.close();
203 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
208 public ManagedRepositoryConfiguration getRepository()
213 public void setRepository( ManagedRepositoryConfiguration repository )
215 this.repository = repository;
218 public void prepare()
221 sourceRepoId = repoid + "-stage";
222 RepositorySession repositorySession = repositorySessionFactory.createSession();
225 conflictSourceArtifacts =
226 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepoId, repoid );
230 repositorySession.close();
233 Configuration config = archivaConfiguration.getConfiguration();
234 this.repository = config.findManagedRepositoryById( repoid );
235 setConflictSourceArtifactsToBeDisplayed( conflictSourceArtifacts );
238 public String getSourceRepoId()
243 public void setSourceRepoId( String sourceRepoId )
245 this.sourceRepoId = sourceRepoId;
248 public String getRepoid()
253 public void setRepoid( String repoid )
255 this.repoid = repoid;
258 public List<ArtifactMetadata> getConflictSourceArtifacts()
260 return conflictSourceArtifacts;
263 public void setConflictSourceArtifacts( List<ArtifactMetadata> conflictSourceArtifacts )
265 this.conflictSourceArtifacts = conflictSourceArtifacts;
268 public List<ArtifactMetadata> getConflictSourceArtifactsToBeDisplayed()
270 return conflictSourceArtifactsToBeDisplayed;
273 public void setConflictSourceArtifactsToBeDisplayed( List<ArtifactMetadata> conflictSourceArtifacts )
276 this.conflictSourceArtifactsToBeDisplayed = new ArrayList<ArtifactMetadata>();
277 HashMap<String, ArtifactMetadata> map = new HashMap<String, ArtifactMetadata>();
278 for ( ArtifactMetadata metadata : conflictSourceArtifacts )
281 metadata.getNamespace() + metadata.getProject() + metadata.getProjectVersion() + metadata.getVersion();
282 map.put( metadataId, metadata );
284 conflictSourceArtifactsToBeDisplayed.addAll( map.values() );
287 private void mergeWithOutSnapshots( MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts,
288 String sourceRepoId, String repoid )
291 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<ArtifactMetadata>();
292 for ( ArtifactMetadata metadata : sourceArtifacts )
295 if ( metadata.getProjectVersion().contains( "SNAPSHOT" ) )
297 artifactsWithOutSnapshots.add( metadata );
301 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
305 sourceArtifacts.removeAll( artifactsWithOutSnapshots );
307 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
308 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots );
311 private void scanRepository()
313 RepositoryTask task = new RepositoryTask();
314 task.setRepositoryId( repoid );
315 task.setScanAll( true );
317 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repoid ) )
319 log.info( "Repository [" + repoid + "] task was already queued." );
325 log.info( "Your request to have repository [" + repoid + "] be indexed has been queued." );
326 repositoryTaskScheduler.queueTask( task );
328 catch ( TaskQueueException e )
331 "Unable to queue your request to have repository [" + repoid + "] be indexed: " + e.getMessage() );