]> source.dussan.org Git - archiva.git/blob
61204b891f4cbb671bf32ff2ebf0e80329fd1499
[archiva.git] /
1 package org.apache.archiva.web.xmlrpc.services;
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 java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.archiva.web.xmlrpc.api.AdministrationService;
26 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
27 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration;
31 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
34 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
35 import org.apache.maven.archiva.consumers.ConsumerException;
36 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
37 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
38 import org.apache.maven.archiva.database.ArchivaDatabaseException;
39 import org.apache.maven.archiva.database.ArtifactDAO;
40 import org.apache.maven.archiva.database.constraints.ArtifactVersionsConstraint;
41 import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer;
42 import org.apache.maven.archiva.database.updater.DatabaseConsumers;
43 import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer;
44 import org.apache.maven.archiva.model.ArchivaArtifact;
45 import org.apache.maven.archiva.model.VersionedReference;
46 import org.apache.maven.archiva.repository.ContentNotFoundException;
47 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
48 import org.apache.maven.archiva.repository.RepositoryContentFactory;
49 import org.apache.maven.archiva.repository.RepositoryException;
50 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
51 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
52 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
53 import org.apache.maven.archiva.scheduled.DefaultArchivaTaskScheduler;
54 import org.apache.maven.archiva.scheduled.tasks.ArchivaTask;
55 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
56 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
57 import org.codehaus.plexus.registry.RegistryException;
58
59 /**
60  * AdministrationServiceImpl
61  * 
62  * @version $Id: AdministrationServiceImpl.java
63  */
64 public class AdministrationServiceImpl
65     implements AdministrationService
66 {    
67     private ArchivaConfiguration archivaConfiguration;
68         
69     private RepositoryContentConsumers repoConsumersUtil;
70         
71     private DatabaseConsumers dbConsumersUtil;
72             
73     private RepositoryContentFactory repoFactory;
74     
75     private ArtifactDAO artifactDAO;
76     
77     private DatabaseCleanupConsumer cleanupArtifacts;
78    
79     private DatabaseCleanupConsumer cleanupProjects;
80     
81     private ArchivaTaskScheduler taskScheduler;
82     
83     public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil,
84                                       DatabaseConsumers dbConsumersUtil, RepositoryContentFactory repoFactory,
85                                       ArtifactDAO artifactDAO, DatabaseCleanupConsumer cleanupArtifacts,
86                                       DatabaseCleanupConsumer cleanupProjects, ArchivaTaskScheduler taskScheduler )
87     {   
88         this.archivaConfiguration = archivaConfig;
89         this.repoConsumersUtil = repoConsumersUtil;
90         this.dbConsumersUtil = dbConsumersUtil;
91         this.repoFactory = repoFactory;
92         this.artifactDAO = artifactDAO;
93         this.cleanupArtifacts = cleanupArtifacts;
94         this.cleanupProjects = cleanupProjects;             
95         this.taskScheduler = taskScheduler;
96     }
97         
98     /**
99      * @see AdministrationService#configureDatabaseConsumer(String, boolean)
100      */
101     public Boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception
102     {
103         List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
104         List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
105             dbConsumersUtil.getAvailableUnprocessedConsumers();
106         
107         boolean found = false;
108         boolean isCleanupConsumer = false;        
109         for( DatabaseCleanupConsumer consumer : cleanupConsumers )
110         {
111             if( consumer.getId().equals( consumerId ) )
112             {
113                 found = true;
114                 isCleanupConsumer = true;
115                 break;
116             }
117         }
118         
119         if( !found )
120         {
121             for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
122             {
123                 if( consumer.getId().equals( consumerId ) )
124                 {
125                     found = true;
126                     break;
127                 }
128             }
129         }
130         
131         if( !found )
132         {
133             throw new Exception( "Invalid database consumer." );
134         }
135         
136         Configuration config = archivaConfiguration.getConfiguration();
137         DatabaseScanningConfiguration dbScanningConfig = config.getDatabaseScanning();
138         
139         if( isCleanupConsumer )
140         {
141             dbScanningConfig.addCleanupConsumer( consumerId );            
142         }
143         else
144         {
145             dbScanningConfig.addUnprocessedConsumer( consumerId );
146         }
147         
148         config.setDatabaseScanning( dbScanningConfig );        
149         saveConfiguration( config );
150         
151         return new Boolean( true );
152     }
153
154     /**
155      * @see AdministrationService#configureRepositoryConsumer(String, String, boolean)
156      */
157     public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable )
158         throws Exception
159     {
160         // TODO use repoId once consumers are configured per repository! (MRM-930)
161         
162         List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
163         List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
164         
165         boolean found = false;
166         boolean isKnownContentConsumer = false;
167         for( KnownRepositoryContentConsumer consumer : knownConsumers )
168         {
169             if( consumer.getId().equals( consumerId ) )
170             {
171                 found = true;
172                 isKnownContentConsumer = true;
173                 break;
174             }
175         }
176         
177         if( !found )
178         {
179             for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
180             {
181                 if( consumer.getId().equals( consumerId ) )
182                 {
183                     found = true;
184                     break;
185                 }
186             }
187         }
188         
189         if( !found )
190         {
191             throw new Exception( "Invalid repository consumer." );
192         }
193         
194         Configuration config = archivaConfiguration.getConfiguration();
195         RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning();
196         
197         if( isKnownContentConsumer )
198         {
199             repoScanningConfig.addKnownContentConsumer( consumerId );
200         }
201         else
202         {
203             repoScanningConfig.addInvalidContentConsumer( consumerId );
204         }
205         
206         config.setRepositoryScanning( repoScanningConfig );        
207         saveConfiguration( config );
208         
209         return new Boolean( true );
210     }
211     
212     /**
213      * @see AdministrationService#deleteArtifact(String, String, String, String)
214      */
215     public Boolean deleteArtifact( String repoId, String groupId, String artifactId, String version )
216         throws Exception
217     {
218         Configuration config = archivaConfiguration.getConfiguration();
219         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repoId );
220         
221         if( repoConfig == null )
222         {
223             throw new Exception( "Repository does not exist." );
224         }
225             
226         try
227         {
228             ManagedRepositoryContent repoContent = repoFactory.getManagedRepositoryContent( repoId );            
229             VersionedReference ref = new VersionedReference();
230             ref.setGroupId( groupId );
231             ref.setArtifactId( artifactId );
232             ref.setVersion( version );
233                    
234             // delete from file system
235             repoContent.deleteVersion( ref );
236             
237             ArtifactVersionsConstraint constraint = new ArtifactVersionsConstraint( repoId, groupId, artifactId, false );
238             List<ArchivaArtifact> artifacts = null;
239             
240             try
241             {
242                 artifacts = artifactDAO.queryArtifacts( constraint );
243                 if( artifacts == null )
244                 {
245                     return true;
246                 }
247             }
248             catch ( ArchivaDatabaseException e )
249             {
250                 throw new Exception( "Error occurred while cleaning up database." );
251             }            
252                
253             // cleanup db manually? or use the cleanup consumers as what is done now?
254             for( ArchivaArtifact artifact : artifacts )
255             {
256                 if( artifact.getVersion().equals( version ) )
257                 {
258                     try
259                     {
260                         cleanupArtifacts.processArchivaArtifact( artifact );
261                         cleanupProjects.processArchivaArtifact( artifact );
262                     }
263                     catch ( ConsumerException ce )
264                     {
265                         // log error
266                         continue;
267                     }                   
268                 }
269             }
270         }
271         catch ( ContentNotFoundException e )
272         {
273             throw new Exception( "Artifact does not exist." );
274         }
275         catch ( RepositoryNotFoundException e )
276         {
277             throw new Exception( "Repository does not exist." );
278         }
279         catch ( RepositoryException e )
280         {
281             throw new Exception( "Repository exception occurred." );
282         }
283         
284         return new Boolean( true );
285     }
286
287     /**
288      * @see AdministrationService#executeDatabaseScanner()
289      */
290     public Boolean executeDatabaseScanner() throws Exception
291     {
292         if ( taskScheduler.isProcessingDatabaseTask() )
293         {
294             return false;
295         }
296
297         DatabaseTask task = new DatabaseTask();
298         task.setName( DefaultArchivaTaskScheduler.DATABASE_JOB + ":user-requested-via-web-service" );
299         task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
300         
301         taskScheduler.queueDatabaseTask( task );           
302         
303         return new Boolean( true );
304     }
305
306     /**
307      * @see AdministrationService#executeRepositoryScanner(String)
308      */
309     public Boolean executeRepositoryScanner( String repoId ) throws Exception
310     {
311         Configuration config = archivaConfiguration.getConfiguration();
312         if( config.findManagedRepositoryById( repoId ) == null )
313         {
314             throw new Exception( "Repository does not exist." );
315         }
316         
317         if ( taskScheduler.isProcessingAnyRepositoryTask() )
318         {
319             if ( taskScheduler.isProcessingRepositoryTask( repoId ) )
320             {
321                 return false;
322             }
323         }
324
325         RepositoryTask task = new RepositoryTask();
326         task.setRepositoryId( repoId );
327         task.setName( DefaultArchivaTaskScheduler.REPOSITORY_JOB + ":" + repoId );
328         task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
329
330         taskScheduler.queueRepositoryTask( task );          
331         
332         return new Boolean( true );
333     }
334
335     /**
336      * @see AdministrationService#getAllDatabaseConsumers()
337      */
338     public List<String> getAllDatabaseConsumers()
339     {
340         List<String> consumers = new ArrayList<String>();
341         
342         List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
343         List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers = dbConsumersUtil.getAvailableUnprocessedConsumers();
344         
345         for( DatabaseCleanupConsumer consumer : cleanupConsumers )
346         {
347             consumers.add( consumer.getId() );
348         }  
349         
350         for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
351         {
352             consumers.add( consumer.getId() );
353         } 
354         
355         return consumers;
356     }
357
358     /**
359      * @see AdministrationService#getAllRepositoryConsumers()
360      */
361     public List<String> getAllRepositoryConsumers()
362     {
363         List<String> consumers = new ArrayList<String>();
364                 
365         List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
366         List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
367         
368         for( KnownRepositoryContentConsumer consumer : knownConsumers )
369         {
370             consumers.add( consumer.getId() );
371         }
372         
373         for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
374         {
375             consumers.add( consumer.getId() );
376         }
377
378         return consumers;
379     }
380
381     /**
382      * @see AdministrationService#getAllManagedRepositories()
383      */
384     public List<ManagedRepository> getAllManagedRepositories()
385     {
386         List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>();
387         
388         Configuration config = archivaConfiguration.getConfiguration();
389         List<ManagedRepositoryConfiguration> managedRepoConfigs = config.getManagedRepositories();
390         
391         for( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
392         {
393             // TODO fix resolution of repo url!            
394             ManagedRepository repo =
395                 new ManagedRepository( repoConfig.getId(), repoConfig.getName(), "URL", repoConfig.getLayout(),
396                                        repoConfig.isSnapshots(), repoConfig.isReleases() );  
397             managedRepos.add( repo );
398         }
399         
400         return managedRepos;
401     }
402
403     /**
404      * @see AdministrationService#getAllRemoteRepositories()
405      */
406     public List<RemoteRepository> getAllRemoteRepositories()
407     {
408         List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>();
409         
410         Configuration config = archivaConfiguration.getConfiguration();
411         List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
412         
413         for( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
414         {
415             RemoteRepository repo =
416                 new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
417                                       repoConfig.getLayout() );
418             remoteRepos.add( repo );
419         }
420         
421         return remoteRepos;
422     }
423
424     private void saveConfiguration( Configuration config )
425         throws Exception
426     {
427         try
428         {
429             archivaConfiguration.save( config );
430         }
431         catch(  RegistryException e )
432         {
433             throw new Exception( "Error occurred in the registry." );
434         }
435         catch ( IndeterminateConfigurationException e )
436         {
437             throw new Exception( "Error occurred while saving the configuration." );    
438         }
439     }    
440 }