1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import net.sf.beanlib.provider.replicator.BeanReplicator;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.RepositoryCommonValidator;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
31 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
32 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
33 import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
34 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
35 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
36 import org.apache.commons.lang.StringUtils;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Inject;
40 import javax.inject.Named;
42 import java.util.Collections;
43 import java.util.List;
46 * @author Olivier Lamy
49 @Service( "managedRepositoriesService#rest" )
50 public class DefaultManagedRepositoriesService
51 extends AbstractRestService
52 implements ManagedRepositoriesService
56 private ManagedRepositoryAdmin managedRepositoryAdmin;
59 private PlexusSisuBridge plexusSisuBridge;
62 private RepositoryCommonValidator repositoryCommonValidator;
65 private RepositoryStatisticsManager repositoryStatisticsManager;
68 @Named( value = "repositorySessionFactory" )
69 protected RepositorySessionFactory repositorySessionFactory;
72 public List<ManagedRepository> getManagedRepositories()
73 throws ArchivaRestServiceException
77 List<org.apache.archiva.admin.model.beans.ManagedRepository> repos =
78 managedRepositoryAdmin.getManagedRepositories();
79 return repos == null ? Collections.<ManagedRepository>emptyList() : repos;
81 catch ( RepositoryAdminException e )
83 throw new ArchivaRestServiceException( e.getMessage() );
87 public ManagedRepository getManagedRepository( String repositoryId )
88 throws ArchivaRestServiceException
90 List<ManagedRepository> repos = getManagedRepositories();
91 for ( ManagedRepository repo : repos )
93 if ( StringUtils.equals( repo.getId(), repositoryId ) )
102 public Boolean deleteManagedRepository( String repoId, boolean deleteContent )
103 throws ArchivaRestServiceException
108 return managedRepositoryAdmin.deleteManagedRepository( repoId, getAuditInformation(), deleteContent );
110 catch ( RepositoryAdminException e )
112 log.error( e.getMessage(), e );
113 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName() );
117 public ManagedRepository addManagedRepository( ManagedRepository managedRepository )
118 throws ArchivaRestServiceException
124 managedRepositoryAdmin.addManagedRepository( managedRepository, managedRepository.isStageRepoNeeded(),
125 getAuditInformation() );
128 return getManagedRepository( managedRepository.getId() );
130 throw new ArchivaRestServiceException( "fail to created managed Repository" );
132 catch ( RepositoryAdminException e )
134 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName() );
139 public Boolean updateManagedRepository( ManagedRepository managedRepository )
140 throws ArchivaRestServiceException
145 return managedRepositoryAdmin.updateManagedRepository( managedRepository,
146 managedRepository.isStageRepoNeeded(),
147 getAuditInformation(),
148 managedRepository.isResetStats() );
150 catch ( RepositoryAdminException e )
152 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName() );
156 public Boolean fileLocationExists( String fileLocation )
157 throws ArchivaRestServiceException
159 String location = repositoryCommonValidator.removeExpressions( fileLocation );
160 return new File( location ).exists();
163 public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId )
164 throws ArchivaRestServiceException
166 RepositorySession repositorySession = repositorySessionFactory.createSession();
169 MetadataRepository metadataRepository = repositorySession.getRepository();
171 RepositoryStatistics stats = null;
174 stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repositoryId );
176 catch ( MetadataRepositoryException e )
178 log.warn( "Error retrieving repository statistics: " + e.getMessage(), e );
182 ArchivaRepositoryStatistics archivaRepositoryStatistics =
183 new BeanReplicator().replicateBean( stats, ArchivaRepositoryStatistics.class );
184 archivaRepositoryStatistics.setDuration( archivaRepositoryStatistics.getScanEndTime().getTime()
185 - archivaRepositoryStatistics.getScanStartTime().getTime() );
186 return archivaRepositoryStatistics;
192 repositorySession.close();