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 new RepositoryAdminException(
233 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
236 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
237 throw new RepositoryAdminException("The repository group is not editable "+repositoryGroupId);
239 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
240 if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
242 throw new RepositoryAdminException(
243 "repositoryGroup 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 new RepositoryAdminException("Repository with id "+repositoryId+" does not exist" );
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 new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
269 + " doesn't not exists so cannot remove repository from it" );
272 if ( !repositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId()) ) )
274 throw new RepositoryAdminException(
275 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
279 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
280 throw new RepositoryAdminException("Repository group is not editable " + repositoryGroupId);
282 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
284 editableRepositoryGroup.removeRepository( repositoryId );
286 repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
287 } catch (RepositoryException e) {
288 throw new RepositoryAdminException("Could not store repository group " + repositoryGroupId, e);
290 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
295 public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
296 throws RepositoryAdminException
298 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
299 Map<String, RepositoryGroup> map = new HashMap<>( repositoriesGroups.size() );
300 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
302 map.put( repositoryGroup.getId(), repositoryGroup );
308 public Map<String, List<String>> getGroupToRepositoryMap()
309 throws RepositoryAdminException
312 Map<String, List<String>> map = new HashMap<>();
314 for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
316 for ( RepositoryGroup group : getRepositoriesGroups() )
318 if ( !group.getRepositories().contains( repo.getId() ) )
320 String groupId = group.getId();
321 List<String> repos = map.get( groupId );
324 repos = new ArrayList<>();
325 map.put( groupId, repos );
327 repos.add( repo.getId() );
335 public Map<String, List<String>> getRepositoryToGroupMap()
336 throws RepositoryAdminException
338 Map<String, List<String>> map = new HashMap<>();
340 for ( RepositoryGroup group : getRepositoriesGroups() )
342 for ( String repositoryId : group.getRepositories() )
344 List<String> groups = map.get( repositoryId );
345 if ( groups == null )
347 groups = new ArrayList<>();
348 map.put( repositoryId, groups );
350 groups.add( group.getId() );
356 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
357 throws RepositoryAdminException
359 String repoGroupId = repositoryGroup.getId();
360 if ( StringUtils.isBlank( repoGroupId ) )
362 throw RepositoryAdminException.ofKey("repository_group.id.empty" );
365 if ( repoGroupId.length() > 100 )
367 throw RepositoryAdminException.ofKey("repository_group.id.max_length",repoGroupId, Integer.toString( 100 ));
371 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
372 if ( !matcher.matches() )
374 throw RepositoryAdminException.ofKey("repository_group.id.invalid_chars","alphanumeric, '.', '-','_'" );
377 if ( repositoryGroup.getMergedIndexTtl() <= 0 )
379 throw RepositoryAdminException.ofKey("repository_group.merged_index_ttl.min","0" );
382 Configuration configuration = getArchivaConfiguration().getConfiguration();
384 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
388 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
389 + "], that id already exists as a repository group." );
392 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
394 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
395 + "], that id already exists as a managed repository." );
397 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
399 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
400 + "], that id already exists as a remote repository." );
406 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
407 throws RepositoryAdminException
409 for ( String id : managedRepositoriesIds )
411 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
413 throw RepositoryAdminException.ofKey("repository_group.repository.not_found",id );
418 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
420 return managedRepositoryAdmin;
423 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
425 this.managedRepositoryAdmin = managedRepositoryAdmin;
428 private RepositoryGroup convertRepositoryGroupObject( org.apache.archiva.repository.RepositoryGroup group ) {
429 RepositoryGroup rg = new RepositoryGroup( group.getId( ), group.getRepositories().stream().map(r -> r.getId()).collect( Collectors.toList()) );
430 if (group.supportsFeature( IndexCreationFeature.class ))
432 IndexCreationFeature indexCreationFeature = group.getFeature( IndexCreationFeature.class ).get();
433 rg.setMergedIndexPath( indexCreationFeature.getIndexPath().getPath() );
435 rg.setCronExpression( group.getSchedulingDefinition() );
436 rg.setMergedIndexTtl( group.getMergedIndexTTL() );
437 rg.setLocation( group.getLocation().toString() );