1 package org.apache.archiva.web.xmlrpc.services;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.ArrayList;
23 import java.util.List;
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;
60 * AdministrationServiceImpl
62 * @version $Id: AdministrationServiceImpl.java
64 public class AdministrationServiceImpl
65 implements AdministrationService
67 private ArchivaConfiguration archivaConfiguration;
69 private RepositoryContentConsumers repoConsumersUtil;
71 private DatabaseConsumers dbConsumersUtil;
73 //private ArchivaTaskScheduler taskScheduler;
75 private RepositoryContentFactory repoFactory;
77 private ArtifactDAO artifactDAO;
79 private DatabaseCleanupConsumer cleanupArtifacts;
81 private DatabaseCleanupConsumer cleanupProjects;
83 public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil,
84 DatabaseConsumers dbConsumersUtil, RepositoryContentFactory repoFactory,
85 ArtifactDAO artifactDAO, DatabaseCleanupConsumer cleanupArtifacts,
86 DatabaseCleanupConsumer cleanupProjects )
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;
98 * @see AdministrationService#configureDatabaseConsumer(String, boolean)
100 public Boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception
102 List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
103 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
104 dbConsumersUtil.getAvailableUnprocessedConsumers();
106 boolean found = false;
107 boolean isCleanupConsumer = false;
108 for( DatabaseCleanupConsumer consumer : cleanupConsumers )
110 if( consumer.getId().equals( consumerId ) )
113 isCleanupConsumer = true;
120 for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
122 if( consumer.getId().equals( consumerId ) )
132 throw new Exception( "Invalid database consumer." );
135 Configuration config = archivaConfiguration.getConfiguration();
136 DatabaseScanningConfiguration dbScanningConfig = config.getDatabaseScanning();
138 if( isCleanupConsumer )
140 dbScanningConfig.addCleanupConsumer( consumerId );
144 dbScanningConfig.addUnprocessedConsumer( consumerId );
147 config.setDatabaseScanning( dbScanningConfig );
148 saveConfiguration( config );
150 return new Boolean( true );
154 * @see AdministrationService#configureRepositoryConsumer(String, String, boolean)
156 public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable )
159 // TODO use repoId once consumers are configured per repository! (MRM-930)
161 List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
162 List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
164 boolean found = false;
165 boolean isKnownContentConsumer = false;
166 for( KnownRepositoryContentConsumer consumer : knownConsumers )
168 if( consumer.getId().equals( consumerId ) )
171 isKnownContentConsumer = true;
178 for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
180 if( consumer.getId().equals( consumerId ) )
190 throw new Exception( "Invalid repository consumer." );
193 Configuration config = archivaConfiguration.getConfiguration();
194 RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning();
196 if( isKnownContentConsumer )
198 repoScanningConfig.addKnownContentConsumer( consumerId );
202 repoScanningConfig.addInvalidContentConsumer( consumerId );
205 config.setRepositoryScanning( repoScanningConfig );
206 saveConfiguration( config );
208 return new Boolean( true );
212 * @see AdministrationService#deleteArtifact(String, String, String, String)
214 public Boolean deleteArtifact( String repoId, String groupId, String artifactId, String version )
217 Configuration config = archivaConfiguration.getConfiguration();
218 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repoId );
220 if( repoConfig == null )
222 throw new Exception( "Repository does not exist." );
227 ManagedRepositoryContent repoContent = repoFactory.getManagedRepositoryContent( repoId );
228 VersionedReference ref = new VersionedReference();
229 ref.setGroupId( groupId );
230 ref.setArtifactId( artifactId );
231 ref.setVersion( version );
233 // delete from file system
234 repoContent.deleteVersion( ref );
236 ArtifactVersionsConstraint constraint = new ArtifactVersionsConstraint( repoId, groupId, artifactId, false );
237 List<ArchivaArtifact> artifacts = null;
241 artifacts = artifactDAO.queryArtifacts( constraint );
242 if( artifacts == null )
247 catch ( ArchivaDatabaseException e )
249 throw new Exception( "Error occurred while cleaning up database." );
252 // cleanup db manually? or use the cleanup consumers as what is done now?
253 for( ArchivaArtifact artifact : artifacts )
255 if( artifact.getVersion().equals( version ) )
259 cleanupArtifacts.processArchivaArtifact( artifact );
260 cleanupProjects.processArchivaArtifact( artifact );
262 catch ( ConsumerException ce )
270 catch ( ContentNotFoundException e )
272 throw new Exception( "Artifact does not exist." );
274 catch ( RepositoryNotFoundException e )
276 throw new Exception( "Repository does not exist." );
278 catch ( RepositoryException e )
280 throw new Exception( "Repository exception occurred." );
283 return new Boolean( true );
287 * @see AdministrationService#executeDatabaseScanner()
289 public Boolean executeDatabaseScanner() throws Exception
291 /*if ( taskScheduler.isProcessingDatabaseTask() )
296 DatabaseTask task = new DatabaseTask();
297 task.setName( DefaultArchivaTaskScheduler.DATABASE_JOB + ":user-requested-via-web-service" );
298 task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
300 taskScheduler.queueDatabaseTask( task ); */
302 return new Boolean( true );
306 * @see AdministrationService#executeRepositoryScanner(String)
308 public Boolean executeRepositoryScanner( String repoId ) throws Exception
310 /* Configuration config = archivaConfiguration.getConfiguration();
311 if( config.findManagedRepositoryById( repoId ) == null )
313 throw new Exception( "Repository does not exist." );
316 if ( taskScheduler.isProcessingAnyRepositoryTask() )
318 if ( taskScheduler.isProcessingRepositoryTask( repoId ) )
324 RepositoryTask task = new RepositoryTask();
325 task.setRepositoryId( repoId );
326 task.setName( DefaultArchivaTaskScheduler.REPOSITORY_JOB + ":" + repoId );
327 task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
329 taskScheduler.queueRepositoryTask( task ); */
331 return new Boolean( true );
335 * @see AdministrationService#getAllDatabaseConsumers()
337 public List<String> getAllDatabaseConsumers()
339 List<String> consumers = new ArrayList<String>();
341 List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
342 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers = dbConsumersUtil.getAvailableUnprocessedConsumers();
344 for( DatabaseCleanupConsumer consumer : cleanupConsumers )
346 consumers.add( consumer.getId() );
349 for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
351 consumers.add( consumer.getId() );
358 * @see AdministrationService#getAllRepositoryConsumers()
360 public List<String> getAllRepositoryConsumers()
362 List<String> consumers = new ArrayList<String>();
364 List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
365 List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
367 for( KnownRepositoryContentConsumer consumer : knownConsumers )
369 consumers.add( consumer.getId() );
372 for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
374 consumers.add( consumer.getId() );
381 * @see AdministrationService#getAllManagedRepositories()
383 public List<ManagedRepository> getAllManagedRepositories()
385 List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>();
387 Configuration config = archivaConfiguration.getConfiguration();
388 List<ManagedRepositoryConfiguration> managedRepoConfigs = config.getManagedRepositories();
390 for( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
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 );
399 System.out.println( "\n++++++MANAGED REPOS --> " + managedRepos );
405 * @see AdministrationService#getAllRemoteRepositories()
407 public List<RemoteRepository> getAllRemoteRepositories()
409 List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>();
411 Configuration config = archivaConfiguration.getConfiguration();
412 List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
414 for( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
416 RemoteRepository repo =
417 new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
418 repoConfig.getLayout() );
419 remoteRepos.add( repo );
425 private void saveConfiguration( Configuration config )
430 archivaConfiguration.save( config );
432 catch( RegistryException e )
434 throw new Exception( "Error occurred in the registry." );
436 catch ( IndeterminateConfigurationException e )
438 throw new Exception( "Error occurred while saving the configuration." );