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