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 RepositoryContentFactory repoFactory;
75 private ArtifactDAO artifactDAO;
77 private DatabaseCleanupConsumer cleanupArtifacts;
79 private DatabaseCleanupConsumer cleanupProjects;
81 private ArchivaTaskScheduler taskScheduler;
83 public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil,
84 DatabaseConsumers dbConsumersUtil, RepositoryContentFactory repoFactory,
85 ArtifactDAO artifactDAO, DatabaseCleanupConsumer cleanupArtifacts,
86 DatabaseCleanupConsumer cleanupProjects, ArchivaTaskScheduler taskScheduler )
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;
99 * @see AdministrationService#configureDatabaseConsumer(String, boolean)
101 public Boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception
103 List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
104 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
105 dbConsumersUtil.getAvailableUnprocessedConsumers();
107 boolean found = false;
108 boolean isCleanupConsumer = false;
109 for( DatabaseCleanupConsumer consumer : cleanupConsumers )
111 if( consumer.getId().equals( consumerId ) )
114 isCleanupConsumer = true;
121 for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
123 if( consumer.getId().equals( consumerId ) )
133 throw new Exception( "Invalid database consumer." );
136 Configuration config = archivaConfiguration.getConfiguration();
137 DatabaseScanningConfiguration dbScanningConfig = config.getDatabaseScanning();
139 if( isCleanupConsumer )
141 dbScanningConfig.addCleanupConsumer( consumerId );
145 dbScanningConfig.addUnprocessedConsumer( consumerId );
148 config.setDatabaseScanning( dbScanningConfig );
149 saveConfiguration( config );
151 return new Boolean( true );
155 * @see AdministrationService#configureRepositoryConsumer(String, String, boolean)
157 public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable )
160 // TODO use repoId once consumers are configured per repository! (MRM-930)
162 List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
163 List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
165 boolean found = false;
166 boolean isKnownContentConsumer = false;
167 for( KnownRepositoryContentConsumer consumer : knownConsumers )
169 if( consumer.getId().equals( consumerId ) )
172 isKnownContentConsumer = true;
179 for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
181 if( consumer.getId().equals( consumerId ) )
191 throw new Exception( "Invalid repository consumer." );
194 Configuration config = archivaConfiguration.getConfiguration();
195 RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning();
197 if( isKnownContentConsumer )
199 repoScanningConfig.addKnownContentConsumer( consumerId );
203 repoScanningConfig.addInvalidContentConsumer( consumerId );
206 config.setRepositoryScanning( repoScanningConfig );
207 saveConfiguration( config );
209 return new Boolean( true );
213 * @see AdministrationService#deleteArtifact(String, String, String, String)
215 public Boolean deleteArtifact( String repoId, String groupId, String artifactId, String version )
218 Configuration config = archivaConfiguration.getConfiguration();
219 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repoId );
221 if( repoConfig == null )
223 throw new Exception( "Repository does not exist." );
228 ManagedRepositoryContent repoContent = repoFactory.getManagedRepositoryContent( repoId );
229 VersionedReference ref = new VersionedReference();
230 ref.setGroupId( groupId );
231 ref.setArtifactId( artifactId );
232 ref.setVersion( version );
234 // delete from file system
235 repoContent.deleteVersion( ref );
237 ArtifactVersionsConstraint constraint = new ArtifactVersionsConstraint( repoId, groupId, artifactId, false );
238 List<ArchivaArtifact> artifacts = null;
242 artifacts = artifactDAO.queryArtifacts( constraint );
243 if( artifacts == null )
248 catch ( ArchivaDatabaseException e )
250 throw new Exception( "Error occurred while cleaning up database." );
253 // cleanup db manually? or use the cleanup consumers as what is done now?
254 for( ArchivaArtifact artifact : artifacts )
256 if( artifact.getVersion().equals( version ) )
260 cleanupArtifacts.processArchivaArtifact( artifact );
261 cleanupProjects.processArchivaArtifact( artifact );
263 catch ( ConsumerException ce )
271 catch ( ContentNotFoundException e )
273 throw new Exception( "Artifact does not exist." );
275 catch ( RepositoryNotFoundException e )
277 throw new Exception( "Repository does not exist." );
279 catch ( RepositoryException e )
281 throw new Exception( "Repository exception occurred." );
284 return new Boolean( true );
288 * @see AdministrationService#executeDatabaseScanner()
290 public Boolean executeDatabaseScanner() throws Exception
292 if ( taskScheduler.isProcessingDatabaseTask() )
297 DatabaseTask task = new DatabaseTask();
298 task.setName( DefaultArchivaTaskScheduler.DATABASE_JOB + ":user-requested-via-web-service" );
299 task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
301 taskScheduler.queueDatabaseTask( task );
303 return new Boolean( true );
307 * @see AdministrationService#executeRepositoryScanner(String)
309 public Boolean executeRepositoryScanner( String repoId ) throws Exception
311 Configuration config = archivaConfiguration.getConfiguration();
312 if( config.findManagedRepositoryById( repoId ) == null )
314 throw new Exception( "Repository does not exist." );
317 if ( taskScheduler.isProcessingAnyRepositoryTask() )
319 if ( taskScheduler.isProcessingRepositoryTask( repoId ) )
325 RepositoryTask task = new RepositoryTask();
326 task.setRepositoryId( repoId );
327 task.setName( DefaultArchivaTaskScheduler.REPOSITORY_JOB + ":" + repoId );
328 task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
330 taskScheduler.queueRepositoryTask( task );
332 return new Boolean( true );
336 * @see AdministrationService#getAllDatabaseConsumers()
338 public List<String> getAllDatabaseConsumers()
340 List<String> consumers = new ArrayList<String>();
342 List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
343 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers = dbConsumersUtil.getAvailableUnprocessedConsumers();
345 for( DatabaseCleanupConsumer consumer : cleanupConsumers )
347 consumers.add( consumer.getId() );
350 for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
352 consumers.add( consumer.getId() );
359 * @see AdministrationService#getAllRepositoryConsumers()
361 public List<String> getAllRepositoryConsumers()
363 List<String> consumers = new ArrayList<String>();
365 List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
366 List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
368 for( KnownRepositoryContentConsumer consumer : knownConsumers )
370 consumers.add( consumer.getId() );
373 for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
375 consumers.add( consumer.getId() );
382 * @see AdministrationService#getAllManagedRepositories()
384 public List<ManagedRepository> getAllManagedRepositories()
386 List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>();
388 Configuration config = archivaConfiguration.getConfiguration();
389 List<ManagedRepositoryConfiguration> managedRepoConfigs = config.getManagedRepositories();
391 for( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
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 );
404 * @see AdministrationService#getAllRemoteRepositories()
406 public List<RemoteRepository> getAllRemoteRepositories()
408 List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>();
410 Configuration config = archivaConfiguration.getConfiguration();
411 List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
413 for( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
415 RemoteRepository repo =
416 new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
417 repoConfig.getLayout() );
418 remoteRepos.add( repo );
424 private void saveConfiguration( Configuration config )
429 archivaConfiguration.save( config );
431 catch( RegistryException e )
433 throw new Exception( "Error occurred in the registry." );
435 catch ( IndeterminateConfigurationException e )
437 throw new Exception( "Error occurred while saving the configuration." );