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 org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
25 import org.apache.archiva.repository.events.RepositoryListener;
26 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
27 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
28 import org.apache.archiva.scheduler.repository.RepositoryTask;
29 import org.apache.archiva.web.xmlrpc.api.AdministrationService;
30 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
31 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
35 import org.apache.maven.archiva.configuration.Configuration;
36 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
37 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
38 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
39 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
40 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
41 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
42 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
43 import org.apache.maven.archiva.model.VersionedReference;
44 import org.apache.maven.archiva.repository.ContentNotFoundException;
45 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
46 import org.apache.maven.archiva.repository.RepositoryContentFactory;
47 import org.apache.maven.archiva.repository.RepositoryException;
48 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
49 import org.apache.maven.archiva.security.ArchivaRoleConstants;
50 import org.codehaus.plexus.redback.role.RoleManager;
51 import org.codehaus.plexus.registry.RegistryException;
52 import org.codehaus.plexus.scheduler.CronExpressionValidator;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
57 import java.io.IOException;
58 import java.util.ArrayList;
59 import java.util.Collection;
60 import java.util.List;
64 * AdministrationServiceImpl
66 * @version $Id: AdministrationServiceImpl.java
68 public class AdministrationServiceImpl
69 implements AdministrationService
71 protected Logger log = LoggerFactory.getLogger( getClass() );
73 private ArchivaConfiguration archivaConfiguration;
75 private RepositoryContentConsumers repoConsumersUtil;
77 private RepositoryContentFactory repoFactory;
79 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
81 private Collection<RepositoryListener> listeners;
83 private MetadataRepository metadataRepository;
85 private RepositoryStatisticsManager repositoryStatisticsManager;
87 public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil,
88 RepositoryContentFactory repoFactory, MetadataRepository metadataRepository,
89 RepositoryArchivaTaskScheduler repositoryTaskScheduler,
90 Collection<RepositoryListener> listeners,
91 RepositoryStatisticsManager repositoryStatisticsManager )
93 this.archivaConfiguration = archivaConfig;
94 this.repoConsumersUtil = repoConsumersUtil;
95 this.repoFactory = repoFactory;
96 this.repositoryTaskScheduler = repositoryTaskScheduler;
97 this.listeners = listeners;
98 this.metadataRepository = metadataRepository;
99 this.repositoryStatisticsManager = repositoryStatisticsManager;
103 * @see AdministrationService#configureRepositoryConsumer(String, String, boolean)
105 public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable )
108 // TODO use repoId once consumers are configured per repository! (MRM-930)
110 List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
111 List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
113 boolean found = false;
114 boolean isKnownContentConsumer = false;
115 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
117 if ( consumer.getId().equals( consumerId ) )
120 isKnownContentConsumer = true;
127 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
129 if ( consumer.getId().equals( consumerId ) )
139 throw new Exception( "Invalid repository consumer." );
142 Configuration config = archivaConfiguration.getConfiguration();
143 RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning();
145 if ( isKnownContentConsumer )
147 repoScanningConfig.addKnownContentConsumer( consumerId );
151 repoScanningConfig.addInvalidContentConsumer( consumerId );
154 config.setRepositoryScanning( repoScanningConfig );
155 saveConfiguration( config );
161 * @see AdministrationService#deleteArtifact(String, String, String, String)
163 public Boolean deleteArtifact( String repoId, String groupId, String artifactId, String version )
166 // TODO: remove duplication with web
168 Configuration config = archivaConfiguration.getConfiguration();
169 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repoId );
171 if ( repoConfig == null )
173 throw new Exception( "Repository does not exist." );
178 ManagedRepositoryContent repoContent = repoFactory.getManagedRepositoryContent( repoId );
179 VersionedReference ref = new VersionedReference();
180 ref.setGroupId( groupId );
181 ref.setArtifactId( artifactId );
182 ref.setVersion( version );
184 // delete from file system
185 repoContent.deleteVersion( ref );
187 Collection<ArtifactMetadata> artifacts =
188 metadataRepository.getArtifacts( repoId, groupId, artifactId, version );
190 for ( ArtifactMetadata artifact : artifacts )
192 // TODO: mismatch between artifact (snapshot) version and project (base) version here
193 if ( artifact.getVersion().equals( version ) )
195 metadataRepository.deleteArtifact( artifact.getRepositoryId(), artifact.getNamespace(),
196 artifact.getProject(), artifact.getVersion(), artifact.getId() );
198 // TODO: move into the metadata repository proper - need to differentiate attachment of
199 // repository metadata to an artifact
200 for ( RepositoryListener listener : listeners )
202 listener.deleteArtifact( repoId, artifact.getNamespace(), artifact.getProject(),
203 artifact.getVersion(), artifact.getId() );
208 catch ( ContentNotFoundException e )
210 throw new Exception( "Artifact does not exist." );
212 catch ( RepositoryNotFoundException e )
214 throw new Exception( "Repository does not exist." );
216 catch ( RepositoryException e )
218 throw new Exception( "Repository exception occurred." );
225 * @see AdministrationService#executeRepositoryScanner(String)
227 public Boolean executeRepositoryScanner( String repoId )
230 Configuration config = archivaConfiguration.getConfiguration();
231 if ( config.findManagedRepositoryById( repoId ) == null )
233 throw new Exception( "Repository does not exist." );
236 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repoId ) )
241 RepositoryTask task = new RepositoryTask();
242 task.setRepositoryId( repoId );
244 repositoryTaskScheduler.queueTask( task );
250 * @see AdministrationService#getAllRepositoryConsumers()
252 public List<String> getAllRepositoryConsumers()
254 List<String> consumers = new ArrayList<String>();
256 List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
257 List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
259 for ( KnownRepositoryContentConsumer consumer : knownConsumers )
261 consumers.add( consumer.getId() );
264 for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
266 consumers.add( consumer.getId() );
273 * @see AdministrationService#getAllManagedRepositories()
275 public List<ManagedRepository> getAllManagedRepositories()
277 List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>();
279 Configuration config = archivaConfiguration.getConfiguration();
280 List<ManagedRepositoryConfiguration> managedRepoConfigs = config.getManagedRepositories();
282 for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
284 // TODO fix resolution of repo url!
285 ManagedRepository repo =
286 new ManagedRepository( repoConfig.getId(), repoConfig.getName(), "URL", repoConfig.getLayout(),
287 repoConfig.isSnapshots(), repoConfig.isReleases() );
288 managedRepos.add( repo );
295 * @see AdministrationService#getAllRemoteRepositories()
297 public List<RemoteRepository> getAllRemoteRepositories()
299 List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>();
301 Configuration config = archivaConfiguration.getConfiguration();
302 List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
304 for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
306 RemoteRepository repo =
307 new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
308 repoConfig.getLayout() );
309 remoteRepos.add( repo );
315 private void saveConfiguration( Configuration config )
320 archivaConfiguration.save( config );
322 catch ( RegistryException e )
324 throw new Exception( "Error occurred in the registry." );
326 catch ( IndeterminateConfigurationException e )
328 throw new Exception( "Error occurred while saving the configuration." );
332 public Boolean addManagedRepository( String repoId, String layout, String name, String location,
333 boolean blockRedeployments, boolean releasesIncluded,
334 boolean snapshotsIncluded, String cronExpression )
338 Configuration config = archivaConfiguration.getConfiguration();
340 CronExpressionValidator validator = new CronExpressionValidator();
342 if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
344 throw new Exception( "Unable to add new repository with id [" + repoId
345 + "], that id already exists as a managed repository." );
347 else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) )
349 throw new Exception( "Unable to add new repository with id [" + repoId
350 + "], that id already exists as a remote repository." );
352 else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
354 throw new Exception( "Unable to add new repository with id [" + repoId
355 + "], that id already exists as a repository group." );
358 if ( !validator.validate( cronExpression ) )
360 throw new Exception( "Invalid cron expression." );
363 ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
365 repository.setId( repoId );
366 repository.setBlockRedeployments( blockRedeployments );
367 repository.setReleases( releasesIncluded );
368 repository.setSnapshots( snapshotsIncluded );
369 repository.setName( name );
370 repository.setLocation( location );
371 repository.setLayout( layout );
372 repository.setRefreshCronExpression( cronExpression );
374 File file = new File( repository.getLocation() );
375 repository.setLocation( file.getCanonicalPath() );
377 if ( !file.exists() )
382 if ( !file.exists() || !file.isDirectory() )
384 throw new IOException( "Unable to add repository - no write access, can not create the root directory: "
387 config.addManagedRepository( repository );
388 saveConfiguration( config );
392 public Boolean deleteManagedRepository( String repoId )
395 Configuration config = archivaConfiguration.getConfiguration();
397 ManagedRepositoryConfiguration repository = config.findManagedRepositoryById( repoId );
399 if ( repository == null )
401 throw new Exception( "A repository with that id does not exist" );
404 metadataRepository.deleteRepository( repository.getId() );
405 repositoryStatisticsManager.deleteStatistics( repository.getId() );
406 config.removeManagedRepository( repository );
410 saveConfiguration( config );
412 catch ( Exception e )
414 throw new Exception( "Error saving configuration for delete action" + e.getMessage() );
417 FileUtils.deleteDirectory( new File( repository.getLocation() ) );
419 List<ProxyConnectorConfiguration> proxyConnectors = config.getProxyConnectors();
420 for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
422 if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) )
424 archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
428 Map<String, List<String>> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap();
429 if ( repoToGroupMap != null )
431 if ( repoToGroupMap.containsKey( repository.getId() ) )
433 List<String> repoGroups = repoToGroupMap.get( repository.getId() );
434 for ( String repoGroup : repoGroups )
436 archivaConfiguration.getConfiguration().findRepositoryGroupById( repoGroup ).removeRepository(
437 repository.getId() );
445 public ManagedRepository getManagedRepository( String repoId )
448 Configuration config = archivaConfiguration.getConfiguration();
449 ManagedRepositoryConfiguration managedRepository = config.findManagedRepositoryById( repoId );
450 if ( managedRepository == null )
452 throw new Exception( "A repository with that id does not exist" );
454 ManagedRepository repo =
455 new ManagedRepository( managedRepository.getId(), managedRepository.getName(), "URL",
456 managedRepository.getLayout(), managedRepository.isSnapshots(),
457 managedRepository.isReleases() );