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.Collection;
24 import java.util.List;
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;
61 * AdministrationServiceImpl
63 * @version $Id: AdministrationServiceImpl.java
65 public class AdministrationServiceImpl
66 implements AdministrationService
68 private ArchivaConfiguration archivaConfiguration;
70 private RepositoryContentConsumers repoConsumersUtil;
72 private DatabaseConsumers dbConsumersUtil;
74 private RepositoryContentFactory repoFactory;
76 private ArtifactDAO artifactDAO;
78 private ArchivaTaskScheduler taskScheduler;
80 private Collection<RepositoryListener> listeners;
82 public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil,
83 DatabaseConsumers dbConsumersUtil, RepositoryContentFactory repoFactory,
84 ArtifactDAO artifactDAO, ArchivaTaskScheduler taskScheduler,
85 Collection<RepositoryListener> listeners )
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;
97 * @see AdministrationService#configureDatabaseConsumer(String, boolean)
99 public Boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception
101 List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
102 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
103 dbConsumersUtil.getAvailableUnprocessedConsumers();
105 boolean found = false;
106 boolean isCleanupConsumer = false;
107 for( DatabaseCleanupConsumer consumer : cleanupConsumers )
109 if( consumer.getId().equals( consumerId ) )
112 isCleanupConsumer = true;
119 for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
121 if( consumer.getId().equals( consumerId ) )
131 throw new Exception( "Invalid database consumer." );
134 Configuration config = archivaConfiguration.getConfiguration();
135 DatabaseScanningConfiguration dbScanningConfig = config.getDatabaseScanning();
137 if( isCleanupConsumer )
139 dbScanningConfig.addCleanupConsumer( consumerId );
143 dbScanningConfig.addUnprocessedConsumer( consumerId );
146 config.setDatabaseScanning( dbScanningConfig );
147 saveConfiguration( config );
149 return new Boolean( true );
153 * @see AdministrationService#configureRepositoryConsumer(String, String, boolean)
155 public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable )
158 // TODO use repoId once consumers are configured per repository! (MRM-930)
160 List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
161 List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
163 boolean found = false;
164 boolean isKnownContentConsumer = false;
165 for( KnownRepositoryContentConsumer consumer : knownConsumers )
167 if( consumer.getId().equals( consumerId ) )
170 isKnownContentConsumer = true;
177 for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
179 if( consumer.getId().equals( consumerId ) )
189 throw new Exception( "Invalid repository consumer." );
192 Configuration config = archivaConfiguration.getConfiguration();
193 RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning();
195 if( isKnownContentConsumer )
197 repoScanningConfig.addKnownContentConsumer( consumerId );
201 repoScanningConfig.addInvalidContentConsumer( consumerId );
204 config.setRepositoryScanning( repoScanningConfig );
205 saveConfiguration( config );
207 return new Boolean( true );
211 * @see AdministrationService#deleteArtifact(String, String, String, String)
213 public Boolean deleteArtifact( String repoId, String groupId, String artifactId, String version )
216 Configuration config = archivaConfiguration.getConfiguration();
217 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repoId );
219 if( repoConfig == null )
221 throw new Exception( "Repository does not exist." );
226 ManagedRepositoryContent repoContent = repoFactory.getManagedRepositoryContent( repoId );
227 VersionedReference ref = new VersionedReference();
228 ref.setGroupId( groupId );
229 ref.setArtifactId( artifactId );
230 ref.setVersion( version );
232 // delete from file system
233 repoContent.deleteVersion( ref );
235 ArtifactVersionsConstraint constraint = new ArtifactVersionsConstraint( repoId, groupId, artifactId, false );
236 List<ArchivaArtifact> artifacts = null;
240 artifacts = artifactDAO.queryArtifacts( constraint );
242 catch ( ArchivaDatabaseException e )
244 throw new Exception( "Error occurred while cleaning up database." );
247 if ( artifacts != null )
249 for ( ArchivaArtifact artifact : artifacts )
251 if ( artifact.getVersion().equals( version ) )
253 for ( RepositoryListener listener : listeners )
255 listener.deleteArtifact( repoContent, artifact );
261 catch ( ContentNotFoundException e )
263 throw new Exception( "Artifact does not exist." );
265 catch ( RepositoryNotFoundException e )
267 throw new Exception( "Repository does not exist." );
269 catch ( RepositoryException e )
271 throw new Exception( "Repository exception occurred." );
274 return new Boolean( true );
278 * @see AdministrationService#executeDatabaseScanner()
280 public Boolean executeDatabaseScanner() throws Exception
282 if ( taskScheduler.isProcessingDatabaseTask() )
287 DatabaseTask task = new DatabaseTask();
288 task.setName( DefaultArchivaTaskScheduler.DATABASE_JOB + ":user-requested-via-web-service" );
289 task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
291 taskScheduler.queueDatabaseTask( task );
293 return new Boolean( true );
297 * @see AdministrationService#executeRepositoryScanner(String)
299 public Boolean executeRepositoryScanner( String repoId ) throws Exception
301 Configuration config = archivaConfiguration.getConfiguration();
302 if( config.findManagedRepositoryById( repoId ) == null )
304 throw new Exception( "Repository does not exist." );
307 if ( taskScheduler.isProcessingAnyRepositoryTask() )
309 if ( taskScheduler.isProcessingRepositoryTask( repoId ) )
315 RepositoryTask task = new RepositoryTask();
316 task.setRepositoryId( repoId );
317 task.setName( DefaultArchivaTaskScheduler.REPOSITORY_JOB + ":" + repoId );
318 task.setQueuePolicy( ArchivaTask.QUEUE_POLICY_WAIT );
320 taskScheduler.queueRepositoryTask( task );
322 return new Boolean( true );
326 * @see AdministrationService#getAllDatabaseConsumers()
328 public List<String> getAllDatabaseConsumers()
330 List<String> consumers = new ArrayList<String>();
332 List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
333 List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers = dbConsumersUtil.getAvailableUnprocessedConsumers();
335 for( DatabaseCleanupConsumer consumer : cleanupConsumers )
337 consumers.add( consumer.getId() );
340 for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
342 consumers.add( consumer.getId() );
349 * @see AdministrationService#getAllRepositoryConsumers()
351 public List<String> getAllRepositoryConsumers()
353 List<String> consumers = new ArrayList<String>();
355 List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
356 List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
358 for( KnownRepositoryContentConsumer consumer : knownConsumers )
360 consumers.add( consumer.getId() );
363 for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
365 consumers.add( consumer.getId() );
372 * @see AdministrationService#getAllManagedRepositories()
374 public List<ManagedRepository> getAllManagedRepositories()
376 List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>();
378 Configuration config = archivaConfiguration.getConfiguration();
379 List<ManagedRepositoryConfiguration> managedRepoConfigs = config.getManagedRepositories();
381 for( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
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 );
394 * @see AdministrationService#getAllRemoteRepositories()
396 public List<RemoteRepository> getAllRemoteRepositories()
398 List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>();
400 Configuration config = archivaConfiguration.getConfiguration();
401 List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
403 for( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
405 RemoteRepository repo =
406 new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
407 repoConfig.getLayout() );
408 remoteRepos.add( repo );
414 private void saveConfiguration( Configuration config )
419 archivaConfiguration.save( config );
421 catch( RegistryException e )
423 throw new Exception( "Error occurred in the registry." );
425 catch ( IndeterminateConfigurationException e )
427 throw new Exception( "Error occurred while saving the configuration." );