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.Validateable;
23 import com.opensymphony.xwork2.Preparable;
24 import org.apache.archiva.audit.Auditable;
25 import org.apache.archiva.audit.AuditEvent;
26 import org.apache.archiva.stagerepository.merge.Maven2RepositoryMerger;
27 import org.apache.archiva.metadata.model.ArtifactMetadata;
28 import org.apache.archiva.metadata.repository.filter.Filter;
29 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.web.action.admin.SchedulerAction;
36 import java.util.List;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.Iterator;
42 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="mergeAction" instantiation-strategy="per-lookup"
46 extends PlexusActionSupport
47 implements Validateable, Preparable, Auditable
51 * @plexus.requirement role="org.apache.archiva.stagerepository.merge.RepositoryMerger" role-hint="maven2"
53 private Maven2RepositoryMerger repositoryMerger;
58 protected ArchivaConfiguration archivaConfiguration;
61 * @plexus.requirement role-hint="default"
63 private MetadataRepository metadataRepository;
66 * @plexus.requirement role="com.opensymphony.xwork2.Action" role-hint="schedulerAction"
68 private SchedulerAction scheduler;
70 private ManagedRepositoryConfiguration repository;
72 private String repoid;
74 private String sourceRepoId;
76 private final String action = "merge";
78 private final String hasConflicts = "CONFLICTS";
80 private List<ArtifactMetadata> conflictSourceArtifacts;
82 private List<ArtifactMetadata> conflictSourceArtifactsToBeDisplayed;
84 public String getConflicts()
86 sourceRepoId = repoid + "-stage";
87 Configuration config = archivaConfiguration.getConfiguration();
88 ManagedRepositoryConfiguration targetRepoConfig = config.findManagedRepositoryById( sourceRepoId );
90 if ( targetRepoConfig != null )
101 public String doMerge()
106 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
108 if ( repository.isReleases() && !repository.isSnapshots() )
110 mergeWithOutSnapshots( sourceArtifacts, sourceRepoId, repoid );
114 repositoryMerger.merge( sourceRepoId, repoid );
116 for ( ArtifactMetadata metadata : sourceArtifacts )
118 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
122 scheduler.scanRepository();
123 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
127 catch ( Exception ex )
129 ex.printStackTrace();
130 addActionError( "Error occurred while merging the repositories." );
135 public String mergeBySkippingConflicts()
139 List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
140 sourceArtifacts.removeAll( conflictSourceArtifacts );
142 if ( repository.isReleases() && !repository.isSnapshots() )
144 mergeWithOutSnapshots( sourceArtifacts, sourceRepoId, repoid );
149 Filter<ArtifactMetadata> artifactsWithOutConflicts =
150 new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
151 repositoryMerger.merge( sourceRepoId, repoid, artifactsWithOutConflicts );
152 for ( ArtifactMetadata metadata : sourceArtifacts )
154 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
157 scheduler.scanRepository();
158 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
162 catch ( Exception ex )
164 ex.printStackTrace();
165 addActionError( "Error occurred while merging the repositories." );
170 public String mergeWithOutConlficts()
173 sourceRepoId = repoid + "-stage";
177 conflictSourceArtifacts = repositoryMerger.getConflictingArtifacts( sourceRepoId, repoid );
179 catch ( Exception e )
181 addActionError( "Error occurred while merging the repositories." );
185 addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
190 public ManagedRepositoryConfiguration getRepository()
195 public void setRepository( ManagedRepositoryConfiguration repository )
197 this.repository = repository;
200 public void prepare()
203 sourceRepoId = repoid + "-stage";
204 conflictSourceArtifacts = repositoryMerger.getConflictingArtifacts( sourceRepoId, repoid );
205 this.scheduler.setRepoid( repoid );
207 Configuration config = archivaConfiguration.getConfiguration();
208 this.repository = config.findManagedRepositoryById( repoid );
209 setConflictSourceArtifactsToBeDisplayed( conflictSourceArtifacts );
212 public String getSourceRepoId()
217 public void setSourceRepoId( String sourceRepoId )
219 this.sourceRepoId = sourceRepoId;
222 public String getRepoid()
227 public void setRepoid( String repoid )
229 this.repoid = repoid;
232 public List<ArtifactMetadata> getConflictSourceArtifacts()
234 return conflictSourceArtifacts;
237 public void setConflictSourceArtifacts( List<ArtifactMetadata> conflictSourceArtifacts )
239 this.conflictSourceArtifacts = conflictSourceArtifacts;
242 public List<ArtifactMetadata> getConflictSourceArtifactsToBeDisplayed()
244 return conflictSourceArtifactsToBeDisplayed;
247 public void setConflictSourceArtifactsToBeDisplayed( List<ArtifactMetadata> conflictSourceArtifacts )
250 this.conflictSourceArtifactsToBeDisplayed = new ArrayList<ArtifactMetadata>();
251 HashMap<String, ArtifactMetadata> map = new HashMap<String, ArtifactMetadata>();
252 for ( ArtifactMetadata metadata : conflictSourceArtifacts )
255 metadata.getNamespace() + metadata.getProject() + metadata.getProjectVersion() + metadata.getVersion();
256 map.put( metadataId, metadata );
258 Iterator iterator = map.keySet().iterator();
260 while ( iterator.hasNext() )
262 conflictSourceArtifactsToBeDisplayed.add( map.get( iterator.next() ) );
266 private void mergeWithOutSnapshots( List<ArtifactMetadata> sourceArtifacts, String sourceRepoId, String repoid )
269 List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<ArtifactMetadata>();
270 for ( ArtifactMetadata metadata : sourceArtifacts )
273 if ( metadata.getProjectVersion().contains( "SNAPSHOT" ) )
275 artifactsWithOutSnapshots.add( metadata );
279 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
283 sourceArtifacts.removeAll( artifactsWithOutSnapshots );
285 Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
286 repositoryMerger.merge( sourceRepoId, repoid, artifactListWithOutSnapShots );