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.event.Event;
33 import org.apache.archiva.event.EventHandler;
34 import org.apache.archiva.event.EventType;
35 import org.apache.archiva.metadata.model.facets.AuditEvent;
36 import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler;
37 import org.apache.archiva.repository.EditableRepositoryGroup;
38 import org.apache.archiva.repository.RepositoryException;
39 import org.apache.archiva.repository.RepositoryRegistry;
40 import org.apache.archiva.repository.event.RepositoryRegistryEvent;
41 import org.apache.archiva.repository.features.IndexCreationFeature;
42 import org.apache.archiva.repository.storage.StorageAsset;
43 import org.apache.commons.lang3.StringUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Service;
48 import javax.annotation.PostConstruct;
49 import javax.inject.Inject;
50 import javax.inject.Named;
51 import java.io.IOException;
52 import java.nio.file.Files;
53 import java.nio.file.Path;
54 import java.nio.file.Paths;
55 import java.util.ArrayList;
56 import java.util.HashMap;
57 import java.util.List;
59 import java.util.regex.Matcher;
60 import java.util.regex.Pattern;
61 import java.util.stream.Collectors;
64 * @author Olivier Lamy
66 @Service("repositoryGroupAdmin#default")
67 public class DefaultRepositoryGroupAdmin
68 extends AbstractRepositoryAdmin
69 implements RepositoryGroupAdmin, EventHandler<Event>
72 private Logger log = LoggerFactory.getLogger( getClass() );
74 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
77 private ManagedRepositoryAdmin managedRepositoryAdmin;
80 @Named("mergedRemoteIndexesScheduler#default")
81 private MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
84 private RepositoryRegistry repositoryRegistry;
86 private Path groupsDirectory;
89 public void baseInit() {
90 this.repositoryRegistry.registerEventHandler( EventType.ROOT, this );
93 public void initialize()
95 String appServerBase = getRegistry().getString( "appserver.base" );
96 groupsDirectory = Paths.get( appServerBase, "groups" );
97 if ( !Files.exists(groupsDirectory) )
99 Files.exists(groupsDirectory);
102 for ( org.apache.archiva.repository.RepositoryGroup repositoryGroup : repositoryRegistry.getRepositoryGroups() )
104 mergedRemoteIndexesScheduler.schedule( repositoryGroup,
105 getMergedIndexDirectory( repositoryGroup.getId() ));
106 // create the directory for each group if not exists
107 Path groupPath = groupsDirectory.resolve(repositoryGroup.getId() );
108 if ( !Files.exists(groupPath) )
111 Files.createDirectories(groupPath);
112 } catch (IOException e) {
113 log.error("Could not create directory {}", groupPath);
122 public StorageAsset getMergedIndexDirectory(String repositoryGroupId )
124 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
126 return group.getFeature(IndexCreationFeature.class).get().getLocalIndexPath();
133 public List<RepositoryGroup> getRepositoriesGroups() {
134 return repositoryRegistry.getRepositoryGroups().stream().map( r -> convertRepositoryGroupObject( r ) ).collect( Collectors.toList());
138 public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws EntityNotFoundException
140 org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
142 throw new EntityNotFoundException( "Repository group does not exist" );
144 return convertRepositoryGroupObject( group );
149 public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
150 throws RepositoryAdminException
152 validateRepositoryGroup( repositoryGroup, false );
153 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
155 RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
156 repositoryGroupConfiguration.setId( repositoryGroup.getId() );
157 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
158 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
159 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
160 repositoryGroupConfiguration.setCronExpression( StringUtils.isEmpty(repositoryGroup.getCronExpression()) ? "0 0 03 ? * MON" : repositoryGroup.getCronExpression() );
163 repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration);
164 } catch (RepositoryException e) {
165 log.error( "Could not add the repository group to the registry: {}", e.getMessage( ), e );
166 throw RepositoryAdminException.ofKey( "repository_group.registry.add_error", e, repositoryGroup.getId(), e.getMessage() );
169 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
170 mergedRemoteIndexesScheduler.schedule( repositoryRegistry.getRepositoryGroup( repositoryGroup.getId()), getMergedIndexDirectory( repositoryGroup.getId() ) );
175 public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
176 throws RepositoryAdminException
179 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
181 repositoryRegistry.removeRepositoryGroup(repositoryGroup);
182 } catch (RepositoryException e) {
183 log.error("Removal of repository group {} failed: {}", repositoryGroup.getId(), e.getMessage(), e);
184 throw new RepositoryAdminException("Removal of repository failed: " + e.getMessage(), e);
186 mergedRemoteIndexesScheduler.unschedule(
188 triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
194 public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
195 throws RepositoryAdminException
197 return updateRepositoryGroup( repositoryGroup, auditInformation, true );
200 private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
201 boolean triggerAuditEvent )
202 throws RepositoryAdminException
204 validateRepositoryGroup( repositoryGroup, true );
205 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
208 Configuration configuration = getArchivaConfiguration().getConfiguration();
210 RepositoryGroupConfiguration repositoryGroupConfiguration =
211 configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
213 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
214 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
215 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
216 repositoryGroupConfiguration.setCronExpression( repositoryGroup.getCronExpression() );
218 repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration);
219 } catch (RepositoryException e) {
220 log.error( "Could not update the repository group in the registry: {}", e.getMessage( ), e );
221 throw RepositoryAdminException.ofKey( "repository_group.registry.update_error", e, repositoryGroup.getId(), e.getMessage() );
224 org.apache.archiva.repository.RepositoryGroup rg = repositoryRegistry.getRepositoryGroup( repositoryGroup.getId( ) );
225 mergedRemoteIndexesScheduler.unschedule( rg );
226 mergedRemoteIndexesScheduler.schedule( rg, getMergedIndexDirectory( repositoryGroup.getId() ) );
227 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
233 public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
234 AuditInformation auditInformation )
235 throws RepositoryAdminException
237 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
238 if ( repositoryGroup == null )
240 throw EntityNotFoundException.ofMessage(
241 "Repository group with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it", repositoryGroupId );
244 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
245 throw RepositoryAdminException.ofKey("repository_group.not_editable",repositoryGroupId);
247 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
248 if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
250 throw new EntityExistsException(
251 "Repository group with id " + repositoryGroupId + " already contain repository with id" + repositoryId );
253 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
254 if (managedRepo==null) {
255 throw EntityNotFoundException.ofMessage("Repository with id "+repositoryId+" does not exist", repositoryId );
258 editableRepositoryGroup.addRepository( managedRepo );
260 repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
261 } catch (RepositoryException e) {
262 throw new RepositoryAdminException("Could not store the repository group "+repositoryGroupId, e);
264 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
269 public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
270 AuditInformation auditInformation )
271 throws RepositoryAdminException
273 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
274 if ( repositoryGroup == null )
276 throw EntityNotFoundException.ofMessage( "Repository group with id " + repositoryGroupId
277 + " doesn't not exists so cannot remove repository from it", repositoryGroupId );
280 if ( !repositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId()) ) )
282 throw EntityNotFoundException.ofMessage(
283 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
284 + repositoryId, repositoryId
288 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
289 throw RepositoryAdminException.ofKey("repository_group.not_editable",repositoryGroupId);
291 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
293 editableRepositoryGroup.removeRepository( repositoryId );
295 repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
296 } catch (RepositoryException e) {
297 throw new RepositoryAdminException("Could not store repository group " + repositoryGroupId, e);
299 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
304 public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
305 throws RepositoryAdminException
307 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
308 Map<String, RepositoryGroup> map = new HashMap<>( repositoriesGroups.size() );
309 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
311 map.put( repositoryGroup.getId(), repositoryGroup );
317 public Map<String, List<String>> getGroupToRepositoryMap()
318 throws RepositoryAdminException
321 Map<String, List<String>> map = new HashMap<>();
323 for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
325 for ( RepositoryGroup group : getRepositoriesGroups() )
327 if ( !group.getRepositories().contains( repo.getId() ) )
329 String groupId = group.getId();
330 List<String> repos = map.get( groupId );
333 repos = new ArrayList<>();
334 map.put( groupId, repos );
336 repos.add( repo.getId() );
344 public Map<String, List<String>> getRepositoryToGroupMap()
345 throws RepositoryAdminException
347 Map<String, List<String>> map = new HashMap<>();
349 for ( RepositoryGroup group : getRepositoriesGroups() )
351 for ( String repositoryId : group.getRepositories() )
353 List<String> groups = map.get( repositoryId );
354 if ( groups == null )
356 groups = new ArrayList<>();
357 map.put( repositoryId, groups );
359 groups.add( group.getId() );
365 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
366 throws RepositoryAdminException
368 String repoGroupId = repositoryGroup.getId();
369 if ( StringUtils.isBlank( repoGroupId ) )
371 throw RepositoryAdminException.ofKey("repository_group.id.empty" );
374 if ( repoGroupId.length() > 100 )
376 throw RepositoryAdminException.ofKey("repository_group.id.max_length",repoGroupId, Integer.toString( 100 ));
380 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
381 if ( !matcher.matches() )
383 throw RepositoryAdminException.ofKey("repository_group.id.invalid_chars","alphanumeric, '.', '-','_'" );
386 if ( repositoryGroup.getMergedIndexTtl() <= 0 )
388 throw RepositoryAdminException.ofKey("repository_group.merged_index_ttl.min","0" );
391 Configuration configuration = getArchivaConfiguration().getConfiguration();
393 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
397 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
398 + "], that id already exists as a repository group." );
401 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
403 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
404 + "], that id already exists as a managed repository." );
406 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
408 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
409 + "], that id already exists as a remote repository." );
415 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
416 throws RepositoryAdminException
418 for ( String id : managedRepositoriesIds )
420 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
422 throw RepositoryAdminException.ofKey("repository_group.repository.not_found",id );
427 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
429 return managedRepositoryAdmin;
432 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
434 this.managedRepositoryAdmin = managedRepositoryAdmin;
437 private RepositoryGroup convertRepositoryGroupObject( org.apache.archiva.repository.RepositoryGroup group ) {
438 RepositoryGroup rg = new RepositoryGroup( group.getId( ), group.getRepositories().stream().map(r -> r.getId()).collect( Collectors.toList()) );
439 if (group.supportsFeature( IndexCreationFeature.class ))
441 IndexCreationFeature indexCreationFeature = group.getFeature( IndexCreationFeature.class ).get();
442 rg.setMergedIndexPath( indexCreationFeature.getIndexPath().getPath() );
444 rg.setCronExpression( group.getSchedulingDefinition() );
445 rg.setMergedIndexTtl( group.getMergedIndexTTL() );
446 rg.setLocation( group.getLocation().toString() );
451 public void handle( Event event )
453 if ( EventType.isInstanceOf( event.getType( ), RepositoryRegistryEvent.INITIALIZED ) )
455 log.debug( "Initializing RepositoryGroupAdmin" );