]> source.dussan.org Git - archiva.git/blob
33bea017b9bd5984953b82424357671b4dac836b
[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 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;
55
56 import java.io.File;
57 import java.io.IOException;
58 import java.util.ArrayList;
59 import java.util.Collection;
60 import java.util.List;
61 import java.util.Map;
62
63 /**
64  * AdministrationServiceImpl
65  * 
66  * @version $Id: AdministrationServiceImpl.java
67  */
68 public class AdministrationServiceImpl
69     implements AdministrationService
70 {
71     protected Logger log = LoggerFactory.getLogger( getClass() );
72
73     private ArchivaConfiguration archivaConfiguration;
74
75     private RepositoryContentConsumers repoConsumersUtil;
76
77     private RepositoryContentFactory repoFactory;
78
79     private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
80
81     private Collection<RepositoryListener> listeners;
82
83     private MetadataRepository metadataRepository;
84
85     private RepositoryStatisticsManager repositoryStatisticsManager;
86
87     public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil,
88                                       RepositoryContentFactory repoFactory, MetadataRepository metadataRepository,
89                                       RepositoryArchivaTaskScheduler repositoryTaskScheduler,
90                                       Collection<RepositoryListener> listeners,
91                                       RepositoryStatisticsManager repositoryStatisticsManager )
92     {
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;
100     }
101
102     /**
103      * @see AdministrationService#configureRepositoryConsumer(String, String, boolean)
104      */
105     public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable )
106         throws Exception
107     {
108         // TODO use repoId once consumers are configured per repository! (MRM-930)
109
110         List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
111         List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
112
113         boolean found = false;
114         boolean isKnownContentConsumer = false;
115         for ( KnownRepositoryContentConsumer consumer : knownConsumers )
116         {
117             if ( consumer.getId().equals( consumerId ) )
118             {
119                 found = true;
120                 isKnownContentConsumer = true;
121                 break;
122             }
123         }
124
125         if ( !found )
126         {
127             for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
128             {
129                 if ( consumer.getId().equals( consumerId ) )
130                 {
131                     found = true;
132                     break;
133                 }
134             }
135         }
136
137         if ( !found )
138         {
139             throw new Exception( "Invalid repository consumer." );
140         }
141
142         Configuration config = archivaConfiguration.getConfiguration();
143         RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning();
144
145         if ( isKnownContentConsumer )
146         {
147             repoScanningConfig.addKnownContentConsumer( consumerId );
148         }
149         else
150         {
151             repoScanningConfig.addInvalidContentConsumer( consumerId );
152         }
153
154         config.setRepositoryScanning( repoScanningConfig );
155         saveConfiguration( config );
156
157         return true;
158     }
159
160     /**
161      * @see AdministrationService#deleteArtifact(String, String, String, String)
162      */
163     public Boolean deleteArtifact( String repoId, String groupId, String artifactId, String version )
164         throws Exception
165     {
166         // TODO: remove duplication with web
167
168         Configuration config = archivaConfiguration.getConfiguration();
169         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repoId );
170
171         if ( repoConfig == null )
172         {
173             throw new Exception( "Repository does not exist." );
174         }
175
176         try
177         {
178             ManagedRepositoryContent repoContent = repoFactory.getManagedRepositoryContent( repoId );
179             VersionedReference ref = new VersionedReference();
180             ref.setGroupId( groupId );
181             ref.setArtifactId( artifactId );
182             ref.setVersion( version );
183
184             // delete from file system
185             repoContent.deleteVersion( ref );
186
187             Collection<ArtifactMetadata> artifacts =
188                 metadataRepository.getArtifacts( repoId, groupId, artifactId, version );
189
190             for ( ArtifactMetadata artifact : artifacts )
191             {
192                 // TODO: mismatch between artifact (snapshot) version and project (base) version here
193                 if ( artifact.getVersion().equals( version ) )
194                 {
195                     metadataRepository.deleteArtifact( artifact.getRepositoryId(), artifact.getNamespace(),
196                                                        artifact.getProject(), artifact.getVersion(), artifact.getId() );
197
198                     // TODO: move into the metadata repository proper - need to differentiate attachment of
199                     // repository metadata to an artifact
200                     for ( RepositoryListener listener : listeners )
201                     {
202                         listener.deleteArtifact( repoId, artifact.getNamespace(), artifact.getProject(),
203                                                  artifact.getVersion(), artifact.getId() );
204                     }
205                 }
206             }
207         }
208         catch ( ContentNotFoundException e )
209         {
210             throw new Exception( "Artifact does not exist." );
211         }
212         catch ( RepositoryNotFoundException e )
213         {
214             throw new Exception( "Repository does not exist." );
215         }
216         catch ( RepositoryException e )
217         {
218             throw new Exception( "Repository exception occurred." );
219         }
220
221         return true;
222     }
223
224     /**
225      * @see AdministrationService#executeRepositoryScanner(String)
226      */
227     public Boolean executeRepositoryScanner( String repoId )
228         throws Exception
229     {
230         Configuration config = archivaConfiguration.getConfiguration();
231         if ( config.findManagedRepositoryById( repoId ) == null )
232         {
233             throw new Exception( "Repository does not exist." );
234         }
235
236         if ( repositoryTaskScheduler.isProcessingRepositoryTask( repoId ) )
237         {
238             return false;
239         }
240
241         RepositoryTask task = new RepositoryTask();
242         task.setRepositoryId( repoId );
243
244         repositoryTaskScheduler.queueTask( task );
245
246         return true;
247     }
248
249     /**
250      * @see AdministrationService#getAllRepositoryConsumers()
251      */
252     public List<String> getAllRepositoryConsumers()
253     {
254         List<String> consumers = new ArrayList<String>();
255
256         List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
257         List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
258
259         for ( KnownRepositoryContentConsumer consumer : knownConsumers )
260         {
261             consumers.add( consumer.getId() );
262         }
263
264         for ( InvalidRepositoryContentConsumer consumer : invalidConsumers )
265         {
266             consumers.add( consumer.getId() );
267         }
268
269         return consumers;
270     }
271
272     /**
273      * @see AdministrationService#getAllManagedRepositories()
274      */
275     public List<ManagedRepository> getAllManagedRepositories()
276     {
277         List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>();
278
279         Configuration config = archivaConfiguration.getConfiguration();
280         List<ManagedRepositoryConfiguration> managedRepoConfigs = config.getManagedRepositories();
281
282         for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
283         {
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 );
289         }
290
291         return managedRepos;
292     }
293
294     /**
295      * @see AdministrationService#getAllRemoteRepositories()
296      */
297     public List<RemoteRepository> getAllRemoteRepositories()
298     {
299         List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>();
300
301         Configuration config = archivaConfiguration.getConfiguration();
302         List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
303
304         for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
305         {
306             RemoteRepository repo =
307                 new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
308                                       repoConfig.getLayout() );
309             remoteRepos.add( repo );
310         }
311
312         return remoteRepos;
313     }
314
315     private void saveConfiguration( Configuration config )
316         throws Exception
317     {
318         try
319         {
320             archivaConfiguration.save( config );
321         }
322         catch ( RegistryException e )
323         {
324             throw new Exception( "Error occurred in the registry." );
325         }
326         catch ( IndeterminateConfigurationException e )
327         {
328             throw new Exception( "Error occurred while saving the configuration." );
329         }
330     }
331
332     public Boolean addManagedRepository( String repoId, String layout, String name, String location,
333                                          boolean blockRedeployments, boolean releasesIncluded,
334                                          boolean snapshotsIncluded, String cronExpression )
335         throws Exception
336     {
337
338         Configuration config = archivaConfiguration.getConfiguration();
339
340         CronExpressionValidator validator = new CronExpressionValidator();
341
342         if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
343         {
344             throw new Exception( "Unable to add new repository with id [" + repoId
345                 + "], that id already exists as a managed repository." );
346         }
347         else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) )
348         {
349             throw new Exception( "Unable to add new repository with id [" + repoId
350                 + "], that id already exists as a remote repository." );
351         }
352         else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
353         {
354             throw new Exception( "Unable to add new repository with id [" + repoId
355                 + "], that id already exists as a repository group." );
356         }
357
358         if ( !validator.validate( cronExpression ) )
359         {
360             throw new Exception( "Invalid cron expression." );
361         }
362
363         ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
364
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 );
373
374         File file = new File( repository.getLocation() );
375         repository.setLocation( file.getCanonicalPath() );
376
377         if ( !file.exists() )
378         {
379             file.mkdirs();
380         }
381
382         if ( !file.exists() || !file.isDirectory() )
383         {
384             throw new IOException( "Unable to add repository - no write access, can not create the root directory: "
385                 + file );
386         }
387         config.addManagedRepository( repository );
388         saveConfiguration( config );
389         return Boolean.TRUE;
390     }
391
392     public Boolean deleteManagedRepository( String repoId )
393         throws Exception
394     {
395         Configuration config = archivaConfiguration.getConfiguration();
396
397         ManagedRepositoryConfiguration repository = config.findManagedRepositoryById( repoId );
398
399         if ( repository == null )
400         {
401             throw new Exception( "A repository with that id does not exist" );
402         }
403
404         metadataRepository.deleteRepository( repository.getId() );
405         repositoryStatisticsManager.deleteStatistics( repository.getId() );
406         config.removeManagedRepository( repository );
407
408         try
409         {
410             saveConfiguration( config );
411         }
412         catch ( Exception e )
413         {
414             throw new Exception( "Error saving configuration for delete action" + e.getMessage() );
415         }
416
417         FileUtils.deleteDirectory( new File( repository.getLocation() ) );
418
419         List<ProxyConnectorConfiguration> proxyConnectors = config.getProxyConnectors();
420         for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
421         {
422             if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) )
423             {
424                 archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
425             }
426         }
427
428         Map<String, List<String>> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap();
429         if ( repoToGroupMap != null )
430         {
431             if ( repoToGroupMap.containsKey( repository.getId() ) )
432             {
433                 List<String> repoGroups = repoToGroupMap.get( repository.getId() );
434                 for ( String repoGroup : repoGroups )
435                 {
436                     archivaConfiguration.getConfiguration().findRepositoryGroupById( repoGroup ).removeRepository(
437                                                                                                                    repository.getId() );
438                 }
439             }
440         }
441
442         return Boolean.TRUE;
443     }
444
445     public ManagedRepository getManagedRepository( String repoId )
446         throws Exception
447     {
448         Configuration config = archivaConfiguration.getConfiguration();
449         ManagedRepositoryConfiguration managedRepository = config.findManagedRepositoryById( repoId );
450         if ( managedRepository == null )
451         {
452             throw new Exception( "A repository with that id does not exist" );
453         }
454         ManagedRepository repo =
455             new ManagedRepository( managedRepository.getId(), managedRepository.getName(), "URL",
456                                    managedRepository.getLayout(), managedRepository.isSnapshots(),
457                                    managedRepository.isReleases() );
458
459         return repo;
460     }
461
462 }