]> source.dussan.org Git - archiva.git/blob
35ff650a1847dd7b180497d9b2b653cfbd6c74b4
[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.DatabaseCleanupConsumer;
36 import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
37 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
38 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
39 import org.apache.maven.archiva.database.updater.DatabaseConsumers;
40 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
41 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
42 import org.apache.maven.archiva.scheduled.DefaultArchivaTaskScheduler;
43 import org.apache.maven.archiva.scheduled.tasks.ArchivaTask;
44 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
45 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
46 import org.codehaus.plexus.registry.RegistryException;
47
48 /**
49  * AdministrationServiceImpl
50  * 
51  * @version $Id: AdministrationServiceImpl.java
52  */
53 public class AdministrationServiceImpl
54     implements AdministrationService
55 {
56     /**
57      * @plexus.requirement
58      */
59     private ArchivaConfiguration archivaConfiguration;
60     
61     /**
62      * @plexus.requirement
63      */
64     private RepositoryContentConsumers repoConsumersUtil;
65     
66     /**
67      * @plexus.requirement
68      */
69     private DatabaseConsumers dbConsumersUtil;
70     
71     /**
72      * @plexus.requirement
73      */
74     private ArchivaTaskScheduler taskScheduler;
75     
76     /**
77      * @see AdministrationService#configureDatabaseConsumer(String, boolean)
78      */
79     public boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception
80     {
81         List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
82         List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
83             dbConsumersUtil.getAvailableUnprocessedConsumers();
84         
85         boolean found = false;
86         boolean isCleanupConsumer = false;        
87         for( DatabaseCleanupConsumer consumer : cleanupConsumers )
88         {
89             if( consumer.getId().equals( consumerId ) )
90             {
91                 found = true;
92                 isCleanupConsumer = true;
93                 break;
94             }
95         }
96         
97         if( !found )
98         {
99             for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
100             {
101                 if( consumer.getId().equals( consumerId ) )
102                 {
103                     found = true;
104                     break;
105                 }
106             }
107         }
108         
109         if( !found )
110         {
111             throw new Exception( "Invalid database consumer." );
112         }
113         
114         Configuration config = archivaConfiguration.getConfiguration();
115         DatabaseScanningConfiguration dbScanningConfig = config.getDatabaseScanning();
116         
117         if( isCleanupConsumer )
118         {
119             dbScanningConfig.addCleanupConsumer( consumerId );            
120         }
121         else
122         {
123             dbScanningConfig.addUnprocessedConsumer( consumerId );
124         }
125         
126         config.setDatabaseScanning( dbScanningConfig );        
127         saveConfiguration( config );
128         
129         return true;
130     }
131
132     /**
133      * @see AdministrationService#configureRepositoryConsumer(String, String, boolean)
134      */
135     public boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable )
136         throws Exception
137     {
138         // TODO use repoId once consumers are configured per repository! (MRM-930)
139         
140         List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
141         List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
142         
143         boolean found = false;
144         boolean isKnownContentConsumer = false;
145         for( KnownRepositoryContentConsumer consumer : knownConsumers )
146         {
147             if( consumer.getId().equals( consumerId ) )
148             {
149                 found = true;
150                 isKnownContentConsumer = true;
151                 break;
152             }
153         }
154         
155         if( !found )
156         {
157             for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
158             {
159                 if( consumer.getId().equals( consumerId ) )
160                 {
161                     found = true;
162                     break;
163                 }
164             }
165         }
166         
167         if( !found )
168         {
169             throw new Exception( "Invalid repository consumer." );
170         }
171         
172         Configuration config = archivaConfiguration.getConfiguration();
173         RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning();
174         
175         if( isKnownContentConsumer )
176         {
177             repoScanningConfig.addKnownContentConsumer( consumerId );
178         }
179         else
180         {
181             repoScanningConfig.addInvalidContentConsumer( consumerId );
182         }
183         
184         config.setRepositoryScanning( repoScanningConfig );        
185         saveConfiguration( config );
186         
187         return true;
188     }
189     
190     /**
191      * @see AdministrationService#deleteArtifact(String, String, String, String)
192      */
193     public boolean deleteArtifact( String repoId, String groupId, String artifactId, String version ) throws Exception
194     {
195         // TODO implement delete artifact in Archiva
196         
197         // TODO Auto-generated method stub
198         return false;
199     }
200
201     /**
202      * @see AdministrationService#executeDatabaseScanner()
203      */
204     public boolean executeDatabaseScanner() throws Exception
205     {
206         if ( taskScheduler.isProcessingDatabaseTask() )
207         {
208             return false;
209         }
210
211         DatabaseTask task = new DatabaseTask();
212         task.setName( DefaultArchivaTaskScheduler.DATABASE_JOB + ":user-requested-via-web-service" );
213         task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
214         
215         taskScheduler.queueDatabaseTask( task );            
216         
217         return true;
218     }
219
220     /**
221      * @see AdministrationService#executeRepositoryScanner(String)
222      */
223     public boolean executeRepositoryScanner( String repoId ) throws Exception
224     {
225         Configuration config = archivaConfiguration.getConfiguration();
226         if( config.findManagedRepositoryById( repoId ) == null )
227         {
228             throw new Exception( "Repository does not exist." );
229         }
230         
231         if ( taskScheduler.isProcessingAnyRepositoryTask() )
232         {
233             if ( taskScheduler.isProcessingRepositoryTask( repoId ) )
234             {
235                 return false;
236             }
237         }
238
239         RepositoryTask task = new RepositoryTask();
240         task.setRepositoryId( repoId );
241         task.setName( DefaultArchivaTaskScheduler.REPOSITORY_JOB + ":" + repoId );
242         task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
243
244         taskScheduler.queueRepositoryTask( task );            
245         
246         return true;
247     }
248
249     /**
250      * @see AdministrationService#getAllDatabaseConsumers()
251      */
252     public List<String> getAllDatabaseConsumers()
253     {
254         List<String> consumers = new ArrayList<String>();
255         
256         List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
257         List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers = dbConsumersUtil.getAvailableUnprocessedConsumers();
258         
259         for( DatabaseCleanupConsumer consumer : cleanupConsumers )
260         {
261             consumers.add( consumer.getId() );
262         }  
263         
264         for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
265         {
266             consumers.add( consumer.getId() );
267         } 
268         
269         return consumers;
270     }
271
272     /**
273      * @see AdministrationService#getAllRepositoryConsumers()
274      */
275     public List<String> getAllRepositoryConsumers()
276     {
277         List<String> consumers = new ArrayList<String>();
278                 
279         List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
280         List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
281         
282         for( KnownRepositoryContentConsumer consumer : knownConsumers )
283         {
284             consumers.add( consumer.getId() );
285         }
286         
287         for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
288         {
289             consumers.add( consumer.getId() );
290         }
291
292         return consumers;
293     }
294
295     /**
296      * @see AdministrationService#getAllManagedRepositories()
297      */
298     public List<ManagedRepository> getAllManagedRepositories()
299     {
300         List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>();
301         
302         Configuration config = archivaConfiguration.getConfiguration();
303         List<ManagedRepositoryConfiguration> managedRepoConfigs = config.getManagedRepositories();
304         
305         for( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
306         {
307             // TODO fix resolution of repo url!            
308             ManagedRepository repo =
309                 new ManagedRepository( repoConfig.getId(), repoConfig.getName(), "URL", repoConfig.getLayout(),
310                                        repoConfig.isSnapshots(), repoConfig.isReleases() );  
311             managedRepos.add( repo );
312         }
313         
314         return managedRepos;
315     }
316
317     /**
318      * @see AdministrationService#getAllRemoteRepositories()
319      */
320     public List<RemoteRepository> getAllRemoteRepositories()
321     {
322         List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>();
323         
324         Configuration config = archivaConfiguration.getConfiguration();
325         List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
326         
327         for( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
328         {
329             RemoteRepository repo =
330                 new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
331                                       repoConfig.getLayout() );
332             remoteRepos.add( repo );
333         }
334         
335         return remoteRepos;
336     }
337
338     private void saveConfiguration( Configuration config )
339         throws Exception
340     {
341         try
342         {
343             archivaConfiguration.save( config );
344         }
345         catch(  RegistryException e )
346         {
347             throw new Exception( "Error occurred in the registry." );
348         }
349         catch ( IndeterminateConfigurationException e )
350         {
351             throw new Exception( "Error occurred while saving the configuration." );    
352         }
353     }
354     
355     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
356     {
357         this.archivaConfiguration = archivaConfiguration;
358     }
359
360     public void setRepoConsumersUtil( RepositoryContentConsumers consumerUtil )
361     {
362         this.repoConsumersUtil = consumerUtil;
363     }    
364     
365     public void setDbConsumersUtil( DatabaseConsumers consumerUtil )
366     {
367         this.dbConsumersUtil = consumerUtil;
368     }
369
370     public void setTaskScheduler( ArchivaTaskScheduler taskScheduler )
371     {
372         this.taskScheduler = taskScheduler;
373     }   
374 }