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