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.admin.model.RepositoryAdminException;
25 import org.apache.archiva.admin.model.managed.ManagedRepository;
26 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
27 import org.apache.archiva.audit.AuditEvent;
28 import org.apache.archiva.audit.Auditable;
29 import org.apache.archiva.metadata.model.ArtifactMetadata;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.archiva.metadata.repository.RepositorySession;
32 import org.apache.archiva.metadata.repository.filter.Filter;
33 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
34 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
35 import org.apache.archiva.scheduler.repository.RepositoryTask;
36 import org.apache.archiva.stagerepository.merge.Maven2RepositoryMerger;
37 import org.apache.maven.archiva.configuration.Configuration;
38 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
39 import org.codehaus.plexus.taskqueue.TaskQueueException;
40 import org.springframework.context.annotation.Scope;
41 import org.springframework.stereotype.Controller;
43 import javax.inject.Inject;
44 import javax.inject.Named;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
52 @Controller( "mergeAction" )
54 public class MergeAction
55 extends AbstractActionSupport
56 implements Validateable, Preparable, Auditable
61 @Named( value = "repositoryMerger#maven2" )
62 private Maven2RepositoryMerger repositoryMerger;
65 protected ManagedRepositoryAdmin managedRepositoryAdmin;
68 @Named( value = "archivaTaskScheduler#repository" )
69 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
71 private ManagedRepository repository;
73 private String repoid;
75 private String sourceRepoId;
77 private final String action = "merge";
79 private final String hasConflicts = "CONFLICTS";
81 private List<ArtifactMetadata> conflictSourceArtifacts;
83 private List<ArtifactMetadata> conflictSourceArtifactsToBeDisplayed;
85 public String getConflicts()
89 sourceRepoId = repoid + "-stage";
90 ManagedRepository targetRepoConfig = managedRepositoryAdmin.getManagedRepository( sourceRepoId );
92 if ( targetRepoConfig != null )
101 catch ( RepositoryAdminException e )
103 addActionError( "RepositoryAdminException " + e.getMessage() );
108 public String doMerge()
110 RepositorySession repositorySession = repositorySessionFactory.createSession();
113 MetadataRepository metadataRepository = repositorySession.getRepository();
114 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
116 if ( repository.isReleases() && !repository.isSnapshots() )
118 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepoId, repoid );
122 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid );
124 for ( ArtifactMetadata metadata : sourceArtifacts )
126 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
132 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
136 catch ( Exception e )
138 log.error( e.getMessage(), e );
139 addActionError( "Error occurred while merging the repositories: " + e.getMessage() );
144 repositorySession.close();
148 public String mergeBySkippingConflicts()
150 RepositorySession repositorySession = repositorySessionFactory.createSession();
153 MetadataRepository metadataRepository = repositorySession.getRepository();
154 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
155 sourceArtifacts.removeAll( conflictSourceArtifacts );
157 if ( repository.isReleases() && !repository.isSnapshots() )
159 mergeWithOutSnapshots( metadataRepository, sourceArtifacts, sourceRepoId, repoid );
164 Filter<ArtifactMetadata> artifactsWithOutConflicts =
165 new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
166 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactsWithOutConflicts );
167 for ( ArtifactMetadata metadata : sourceArtifacts )
169 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
175 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
179 catch ( Exception e )
181 log.error( e.getMessage(), e );
182 addActionError( "Error occurred while merging the repositories: " + e.getMessage() );
187 repositorySession.close();
191 public String mergeWithOutConlficts()
193 sourceRepoId = repoid + "-stage";
195 RepositorySession repositorySession = repositorySessionFactory.createSession();
198 conflictSourceArtifacts =
199 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepoId, repoid );
201 catch ( Exception e )
203 addActionError( "Error occurred while merging the repositories." );
208 repositorySession.close();
211 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
216 public ManagedRepository getRepository()
221 public void setRepository( ManagedRepository repository )
223 this.repository = repository;
226 public void prepare()
229 sourceRepoId = repoid + "-stage";
230 RepositorySession repositorySession = repositorySessionFactory.createSession();
233 conflictSourceArtifacts =
234 repositoryMerger.getConflictingArtifacts( repositorySession.getRepository(), sourceRepoId, repoid );
238 repositorySession.close();
241 this.repository = managedRepositoryAdmin.getManagedRepository( repoid );
242 setConflictSourceArtifactsToBeDisplayed( conflictSourceArtifacts );
245 public String getSourceRepoId()
250 public void setSourceRepoId( String sourceRepoId )
252 this.sourceRepoId = sourceRepoId;
255 public String getRepoid()
260 public void setRepoid( String repoid )
262 this.repoid = repoid;
265 public List<ArtifactMetadata> getConflictSourceArtifacts()
267 return conflictSourceArtifacts;
270 public void setConflictSourceArtifacts( List<ArtifactMetadata> conflictSourceArtifacts )
272 this.conflictSourceArtifacts = conflictSourceArtifacts;
275 public List<ArtifactMetadata> getConflictSourceArtifactsToBeDisplayed()
277 return conflictSourceArtifactsToBeDisplayed;
280 public void setConflictSourceArtifactsToBeDisplayed( List<ArtifactMetadata> conflictSourceArtifacts )
283 this.conflictSourceArtifactsToBeDisplayed = new ArrayList<ArtifactMetadata>();
284 HashMap<String, ArtifactMetadata> map = new HashMap<String, ArtifactMetadata>();
285 for ( ArtifactMetadata metadata : conflictSourceArtifacts )
288 metadata.getNamespace() + metadata.getProject() + metadata.getProjectVersion() + metadata.getVersion();
289 map.put( metadataId, metadata );
291 conflictSourceArtifactsToBeDisplayed.addAll( map.values() );
294 private void mergeWithOutSnapshots( MetadataRepository metadataRepository, List<ArtifactMetadata> sourceArtifacts,
295 String sourceRepoId, String repoid )
298 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<ArtifactMetadata>();
299 for ( ArtifactMetadata metadata : sourceArtifacts )
302 if ( metadata.getProjectVersion().contains( "SNAPSHOT" ) )
304 artifactsWithOutSnapshots.add( metadata );
308 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
312 sourceArtifacts.removeAll( artifactsWithOutSnapshots );
314 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
315 repositoryMerger.merge( metadataRepository, sourceRepoId, repoid, artifactListWithOutSnapShots );
318 private void scanRepository()
320 RepositoryTask task = new RepositoryTask();
321 task.setRepositoryId( repoid );
322 task.setScanAll( true );
324 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repoid ) )
326 log.info( "Repository [" + repoid + "] task was already queued." );
332 log.info( "Your request to have repository [" + repoid + "] be indexed has been queued." );
333 repositoryTaskScheduler.queueTask( task );
335 catch ( TaskQueueException e )
338 "Unable to queue your request to have repository [" + repoid + "] be indexed: " + e.getMessage() );
343 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
345 return managedRepositoryAdmin;
348 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
350 this.managedRepositoryAdmin = managedRepositoryAdmin;