1 package org.apache.archiva.admin.repository.group;
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 org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.EntityExistsException;
23 import org.apache.archiva.admin.model.EntityNotFoundException;
24 import org.apache.archiva.admin.model.RepositoryAdminException;
25 import org.apache.archiva.admin.model.beans.ManagedRepository;
26 import org.apache.archiva.admin.model.beans.RepositoryGroup;
27 import org.apache.archiva.admin.model.group.RepositoryGroupAdmin;
28 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
29 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
30 import org.apache.archiva.configuration.Configuration;
31 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
32 import org.apache.archiva.metadata.model.facets.AuditEvent;
33 import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler;
34 import org.apache.archiva.repository.EditableRepositoryGroup;
35 import org.apache.archiva.repository.RepositoryException;
36 import org.apache.archiva.repository.RepositoryRegistry;
37 import org.apache.archiva.repository.features.IndexCreationFeature;
38 import org.apache.archiva.repository.storage.StorageAsset;
39 import org.apache.commons.lang3.StringUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Service;
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
47 import java.io.IOException;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.List;
55 import java.util.regex.Matcher;
56 import java.util.regex.Pattern;
57 import java.util.stream.Collectors;
60 * @author Olivier Lamy
62 @Service("repositoryGroupAdmin#default")
63 public class DefaultRepositoryGroupAdmin
64 extends AbstractRepositoryAdmin
65 implements RepositoryGroupAdmin
68 private Logger log = LoggerFactory.getLogger( getClass() );
70 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
73 private ManagedRepositoryAdmin managedRepositoryAdmin;
76 @Named("mergedRemoteIndexesScheduler#default")
77 private MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
80 private RepositoryRegistry repositoryRegistry;
82 private Path groupsDirectory;
85 public void initialize()
87 String appServerBase = getRegistry().getString( "appserver.base" );
88 groupsDirectory = Paths.get( appServerBase, "groups" );
89 if ( !Files.exists(groupsDirectory) )
91 Files.exists(groupsDirectory);
94 for ( org.apache.archiva.repository.RepositoryGroup repositoryGroup : repositoryRegistry.getRepositoryGroups() )
96 mergedRemoteIndexesScheduler.schedule( repositoryGroup,
97 getMergedIndexDirectory( repositoryGroup.getId() ));
98 // create the directory for each group if not exists
99 Path groupPath = groupsDirectory.resolve(repositoryGroup.getId() );
100 if ( !Files.exists(groupPath) )
103 Files.createDirectories(groupPath);
104 } catch (IOException e) {
105 log.error("Could not create directory {}", groupPath);
114 public StorageAsset getMergedIndexDirectory(String repositoryGroupId )
116 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
118 return group.getFeature(IndexCreationFeature.class).get().getLocalIndexPath();
125 public List<RepositoryGroup> getRepositoriesGroups() {
126 return repositoryRegistry.getRepositoryGroups().stream().map( r -> convertRepositoryGroupObject( r ) ).collect( Collectors.toList());
130 public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws EntityNotFoundException
132 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
134 throw new EntityNotFoundException( "Repository group does not exist" );
136 return convertRepositoryGroupObject( group );
141 public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
142 throws RepositoryAdminException
144 validateRepositoryGroup( repositoryGroup, false );
145 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
147 RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
148 repositoryGroupConfiguration.setId( repositoryGroup.getId() );
149 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
150 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
151 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
152 repositoryGroupConfiguration.setCronExpression( StringUtils.isEmpty(repositoryGroup.getCronExpression()) ? "0 0 03 ? * MON" : repositoryGroup.getCronExpression() );
155 repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration);
156 } catch (RepositoryException e) {
157 log.error( "Could not add the repository group to the registry: {}", e.getMessage( ), e );
158 throw RepositoryAdminException.ofKey( "repository_group.registry.add_error", e, repositoryGroup.getId(), e.getMessage() );
161 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
162 mergedRemoteIndexesScheduler.schedule( repositoryRegistry.getRepositoryGroup( repositoryGroup.getId()), getMergedIndexDirectory( repositoryGroup.getId() ) );
167 public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
168 throws RepositoryAdminException
171 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
173 repositoryRegistry.removeRepositoryGroup(repositoryGroup);
174 } catch (RepositoryException e) {
175 log.error("Removal of repository group {} failed: {}", repositoryGroup.getId(), e.getMessage(), e);
176 throw new RepositoryAdminException("Removal of repository failed: " + e.getMessage(), e);
178 mergedRemoteIndexesScheduler.unschedule(
180 triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
186 public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
187 throws RepositoryAdminException
189 return updateRepositoryGroup( repositoryGroup, auditInformation, true );
192 private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
193 boolean triggerAuditEvent )
194 throws RepositoryAdminException
196 validateRepositoryGroup( repositoryGroup, true );
197 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
200 Configuration configuration = getArchivaConfiguration().getConfiguration();
202 RepositoryGroupConfiguration repositoryGroupConfiguration =
203 configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
205 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
206 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
207 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
208 repositoryGroupConfiguration.setCronExpression( repositoryGroup.getCronExpression() );
210 repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration);
211 } catch (RepositoryException e) {
212 log.error( "Could not update the repository group in the registry: {}", e.getMessage( ), e );
213 throw RepositoryAdminException.ofKey( "repository_group.registry.update_error", e, repositoryGroup.getId(), e.getMessage() );
216 org.apache.archiva.repository.RepositoryGroup rg = repositoryRegistry.getRepositoryGroup( repositoryGroup.getId( ) );
217 mergedRemoteIndexesScheduler.unschedule( rg );
218 mergedRemoteIndexesScheduler.schedule( rg, getMergedIndexDirectory( repositoryGroup.getId() ) );
219 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
225 public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
226 AuditInformation auditInformation )
227 throws RepositoryAdminException
229 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
230 if ( repositoryGroup == null )
232 throw EntityNotFoundException.ofMessage(
233 "Repository group with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it", repositoryGroupId );
236 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
237 throw RepositoryAdminException.ofKey("repository_group.not_editable",repositoryGroupId);
239 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
240 if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
242 throw new EntityExistsException(
243 "Repository group with id " + repositoryGroupId + " already contain repository with id" + repositoryId );
245 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
246 if (managedRepo==null) {
247 throw EntityNotFoundException.ofMessage("Repository with id "+repositoryId+" does not exist", repositoryId );
250 editableRepositoryGroup.addRepository( managedRepo );
252 repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
253 } catch (RepositoryException e) {
254 throw new RepositoryAdminException("Could not store the repository group "+repositoryGroupId, e);
256 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
261 public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
262 AuditInformation auditInformation )
263 throws RepositoryAdminException
265 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
266 if ( repositoryGroup == null )
268 throw EntityNotFoundException.ofMessage( "Repository group with id " + repositoryGroupId
269 + " doesn't not exists so cannot remove repository from it", repositoryGroupId );
272 if ( !repositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId()) ) )
274 throw EntityNotFoundException.ofMessage(
275 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
276 + repositoryId, repositoryId
280 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
281 throw RepositoryAdminException.ofKey("repository_group.not_editable",repositoryGroupId);
283 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
285 editableRepositoryGroup.removeRepository( repositoryId );
287 repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
288 } catch (RepositoryException e) {
289 throw new RepositoryAdminException("Could not store repository group " + repositoryGroupId, e);
291 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
296 public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
297 throws RepositoryAdminException
299 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
300 Map<String, RepositoryGroup> map = new HashMap<>( repositoriesGroups.size() );
301 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
303 map.put( repositoryGroup.getId(), repositoryGroup );
309 public Map<String, List<String>> getGroupToRepositoryMap()
310 throws RepositoryAdminException
313 Map<String, List<String>> map = new HashMap<>();
315 for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
317 for ( RepositoryGroup group : getRepositoriesGroups() )
319 if ( !group.getRepositories().contains( repo.getId() ) )
321 String groupId = group.getId();
322 List<String> repos = map.get( groupId );
325 repos = new ArrayList<>();
326 map.put( groupId, repos );
328 repos.add( repo.getId() );
336 public Map<String, List<String>> getRepositoryToGroupMap()
337 throws RepositoryAdminException
339 Map<String, List<String>> map = new HashMap<>();
341 for ( RepositoryGroup group : getRepositoriesGroups() )
343 for ( String repositoryId : group.getRepositories() )
345 List<String> groups = map.get( repositoryId );
346 if ( groups == null )
348 groups = new ArrayList<>();
349 map.put( repositoryId, groups );
351 groups.add( group.getId() );
357 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
358 throws RepositoryAdminException
360 String repoGroupId = repositoryGroup.getId();
361 if ( StringUtils.isBlank( repoGroupId ) )
363 throw RepositoryAdminException.ofKey("repository_group.id.empty" );
366 if ( repoGroupId.length() > 100 )
368 throw RepositoryAdminException.ofKey("repository_group.id.max_length",repoGroupId, Integer.toString( 100 ));
372 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
373 if ( !matcher.matches() )
375 throw RepositoryAdminException.ofKey("repository_group.id.invalid_chars","alphanumeric, '.', '-','_'" );
378 if ( repositoryGroup.getMergedIndexTtl() <= 0 )
380 throw RepositoryAdminException.ofKey("repository_group.merged_index_ttl.min","0" );
383 Configuration configuration = getArchivaConfiguration().getConfiguration();
385 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
389 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
390 + "], that id already exists as a repository group." );
393 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
395 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
396 + "], that id already exists as a managed repository." );
398 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
400 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
401 + "], that id already exists as a remote repository." );
407 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
408 throws RepositoryAdminException
410 for ( String id : managedRepositoriesIds )
412 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
414 throw RepositoryAdminException.ofKey("repository_group.repository.not_found",id );
419 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
421 return managedRepositoryAdmin;
424 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
426 this.managedRepositoryAdmin = managedRepositoryAdmin;
429 private RepositoryGroup convertRepositoryGroupObject( org.apache.archiva.repository.RepositoryGroup group ) {
430 RepositoryGroup rg = new RepositoryGroup( group.getId( ), group.getRepositories().stream().map(r -> r.getId()).collect( Collectors.toList()) );
431 if (group.supportsFeature( IndexCreationFeature.class ))
433 IndexCreationFeature indexCreationFeature = group.getFeature( IndexCreationFeature.class ).get();
434 rg.setMergedIndexPath( indexCreationFeature.getIndexPath().getPath() );
436 rg.setCronExpression( group.getSchedulingDefinition() );
437 rg.setMergedIndexTtl( group.getMergedIndexTTL() );
438 rg.setLocation( group.getLocation().toString() );