]> source.dussan.org Git - archiva.git/blob
9dc984d0de532c4065d172c2c6d1095e35c057e9
[archiva.git] /
1 package org.apache.maven.archiva.web.action;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
35
36 import java.util.List;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.Iterator;
40
41 /**
42  * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="mergeAction" instantiation-strategy="per-lookup"
43  */
44 public class
45     MergeAction
46     extends PlexusActionSupport
47     implements Validateable, Preparable, Auditable
48
49 {
50     /**
51      * @plexus.requirement role="org.apache.archiva.stagerepository.merge.RepositoryMerger" role-hint="maven2"
52      */
53     private Maven2RepositoryMerger repositoryMerger;
54
55     /**
56      * @plexus.requirement
57      */
58     protected ArchivaConfiguration archivaConfiguration;
59
60     /**
61      * @plexus.requirement role-hint="default"
62      */
63     private MetadataRepository metadataRepository;
64
65     /**
66      * @plexus.requirement role="com.opensymphony.xwork2.Action" role-hint="schedulerAction"
67      */
68     private SchedulerAction scheduler;
69
70     private ManagedRepositoryConfiguration repository;
71
72     private String repoid;
73
74     private String sourceRepoId;
75
76     private final String action = "merge";
77
78     private final String hasConflicts = "CONFLICTS";
79
80     private List<ArtifactMetadata> conflictSourceArtifacts;
81
82     private List<ArtifactMetadata> conflictSourceArtifactsToBeDisplayed;
83
84     public String getConflicts()
85     {
86         sourceRepoId = repoid + "-stage";
87         Configuration config = archivaConfiguration.getConfiguration();
88         ManagedRepositoryConfiguration targetRepoConfig = config.findManagedRepositoryById( sourceRepoId );
89
90         if ( targetRepoConfig != null )
91         {
92             return hasConflicts;
93
94         }
95         else
96         {
97             return ERROR;
98         }
99     }
100
101     public String doMerge()
102         throws Exception
103     {
104         try
105         {
106             List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
107
108             if ( repository.isReleases() && !repository.isSnapshots() )
109             {
110                 mergeWithOutSnapshots( sourceArtifacts, sourceRepoId, repoid );
111             }
112             else
113             {
114                 repositoryMerger.merge( sourceRepoId, repoid );
115
116                 for ( ArtifactMetadata metadata : sourceArtifacts )
117                 {
118                     triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
119                 }
120
121             }
122             scheduler.scanRepository();
123             addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
124
125             return SUCCESS;
126         }
127         catch ( Exception ex )
128         {
129             ex.printStackTrace();
130             addActionError( "Error occurred while merging the repositories." );
131             return ERROR;
132         }
133     }
134
135     public String mergeBySkippingConflicts()
136     {
137         try
138         {
139             List<ArtifactMetadata> sourceArtifacts = metadataRepository.getArtifacts( sourceRepoId );
140             sourceArtifacts.removeAll( conflictSourceArtifacts );
141
142             if ( repository.isReleases() && !repository.isSnapshots() )
143             {
144                 mergeWithOutSnapshots( sourceArtifacts, sourceRepoId, repoid );
145             }
146             else
147             {
148
149                 Filter<ArtifactMetadata> artifactsWithOutConflicts =
150                     new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
151                 repositoryMerger.merge( sourceRepoId, repoid, artifactsWithOutConflicts );
152                 for ( ArtifactMetadata metadata : sourceArtifacts )
153                 {
154                     triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
155                 }
156             }
157             scheduler.scanRepository();
158             addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
159
160             return SUCCESS;
161         }
162         catch ( Exception ex )
163         {
164             ex.printStackTrace();
165             addActionError( "Error occurred while merging the repositories." );
166             return ERROR;
167         }
168     }
169
170     public String mergeWithOutConlficts()        
171     {
172
173         sourceRepoId = repoid + "-stage";
174
175         try
176         {
177             conflictSourceArtifacts = repositoryMerger.getConflictingArtifacts( sourceRepoId, repoid );
178         }
179         catch ( Exception e )
180         {
181             addActionError( "Error occurred while merging the repositories." );
182             return ERROR;
183         }
184
185         addActionMessage( "Repository '" + sourceRepoId + "' successfully merged to '" + repoid + "'." );
186
187         return SUCCESS;
188     }
189
190     public ManagedRepositoryConfiguration getRepository()
191     {
192         return repository;
193     }
194
195     public void setRepository( ManagedRepositoryConfiguration repository )
196     {
197         this.repository = repository;
198     }
199
200     public void prepare()
201         throws Exception
202     {
203         sourceRepoId = repoid + "-stage";
204         conflictSourceArtifacts = repositoryMerger.getConflictingArtifacts( sourceRepoId, repoid );
205         this.scheduler.setRepoid( repoid );
206         
207         Configuration config = archivaConfiguration.getConfiguration();
208         this.repository = config.findManagedRepositoryById( repoid );
209         setConflictSourceArtifactsToBeDisplayed( conflictSourceArtifacts );
210     }
211
212     public String getSourceRepoId()
213     {
214         return sourceRepoId;
215     }
216
217     public void setSourceRepoId( String sourceRepoId )
218     {
219         this.sourceRepoId = sourceRepoId;
220     }
221
222     public String getRepoid()
223     {
224         return repoid;
225     }
226
227     public void setRepoid( String repoid )
228     {
229         this.repoid = repoid;
230     }
231
232     public List<ArtifactMetadata> getConflictSourceArtifacts()
233     {
234         return conflictSourceArtifacts;
235     }
236
237     public void setConflictSourceArtifacts( List<ArtifactMetadata> conflictSourceArtifacts )
238     {
239         this.conflictSourceArtifacts = conflictSourceArtifacts;
240     }
241
242     public List<ArtifactMetadata> getConflictSourceArtifactsToBeDisplayed()
243     {
244         return conflictSourceArtifactsToBeDisplayed;
245     }
246
247     public void setConflictSourceArtifactsToBeDisplayed( List<ArtifactMetadata> conflictSourceArtifacts )
248         throws Exception
249     {
250         this.conflictSourceArtifactsToBeDisplayed = new ArrayList<ArtifactMetadata>();
251         HashMap<String, ArtifactMetadata> map = new HashMap<String, ArtifactMetadata>();
252         for ( ArtifactMetadata metadata : conflictSourceArtifacts )
253         {
254             String metadataId =
255                 metadata.getNamespace() + metadata.getProject() + metadata.getProjectVersion() + metadata.getVersion();
256             map.put( metadataId, metadata );
257         }
258         Iterator iterator = map.keySet().iterator();
259
260         while ( iterator.hasNext() )
261         {
262             conflictSourceArtifactsToBeDisplayed.add( map.get( iterator.next() ) );
263         }
264     }
265
266     private void mergeWithOutSnapshots( List<ArtifactMetadata> sourceArtifacts, String sourceRepoId, String repoid )
267         throws Exception
268     {
269         List<ArtifactMetadata> artifactsWithOutSnapshots = new ArrayList<ArtifactMetadata>();
270         for ( ArtifactMetadata metadata : sourceArtifacts )
271         {
272
273             if ( metadata.getProjectVersion().contains( "SNAPSHOT" ) )
274             {
275                 artifactsWithOutSnapshots.add( metadata );
276             }
277             else
278             {
279                 triggerAuditEvent( repoid, metadata.getId(), AuditEvent.MERGING_REPOSITORIES );
280             }
281
282         }
283         sourceArtifacts.removeAll( artifactsWithOutSnapshots );
284
285         Filter<ArtifactMetadata> artifactListWithOutSnapShots = new IncludesFilter<ArtifactMetadata>( sourceArtifacts );
286         repositoryMerger.merge( sourceRepoId, repoid, artifactListWithOutSnapShots );
287     }
288 }
289