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.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.beans.RepositoryGroup;
25 import org.apache.archiva.admin.model.group.RepositoryGroupAdmin;
26 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
27 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
30 import org.apache.archiva.metadata.model.facets.AuditEvent;
31 import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler;
32 import org.apache.archiva.repository.EditableRepository;
33 import org.apache.archiva.repository.EditableRepositoryGroup;
34 import org.apache.archiva.repository.RepositoryException;
35 import org.apache.archiva.repository.RepositoryRegistry;
36 import org.apache.archiva.repository.features.IndexCreationFeature;
37 import org.apache.commons.lang.StringUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Service;
42 import javax.annotation.PostConstruct;
43 import javax.inject.Inject;
44 import javax.inject.Named;
45 import java.io.IOException;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.HashMap;
54 import java.util.List;
56 import java.util.regex.Matcher;
57 import java.util.regex.Pattern;
58 import java.util.stream.Collectors;
61 * @author Olivier Lamy
63 @Service("repositoryGroupAdmin#default")
64 public class DefaultRepositoryGroupAdmin
65 extends AbstractRepositoryAdmin
66 implements RepositoryGroupAdmin
69 private Logger log = LoggerFactory.getLogger( getClass() );
71 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
74 private ManagedRepositoryAdmin managedRepositoryAdmin;
77 @Named("mergedRemoteIndexesScheduler#default")
78 private MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
81 private RepositoryRegistry repositoryRegistry;
83 private Path groupsDirectory;
86 public void initialize()
88 String appServerBase = getRegistry().getString( "appserver.base" );
89 groupsDirectory = Paths.get( appServerBase, "groups" );
90 if ( !Files.exists(groupsDirectory) )
92 Files.exists(groupsDirectory);
95 for ( org.apache.archiva.repository.RepositoryGroup repositoryGroup : repositoryRegistry.getRepositoryGroups() )
97 mergedRemoteIndexesScheduler.schedule( repositoryGroup,
98 getMergedIndexDirectory( repositoryGroup.getId() ));
99 // create the directory for each group if not exists
100 Path groupPath = groupsDirectory.resolve(repositoryGroup.getId() );
101 if ( !Files.exists(groupPath) )
104 Files.createDirectories(groupPath);
105 } catch (IOException e) {
106 log.error("Could not create directory {}", groupPath);
115 public Path getMergedIndexDirectory( String repositoryGroupId )
117 return groupsDirectory.resolve( repositoryGroupId );
121 public List<RepositoryGroup> getRepositoriesGroups() {
122 return repositoryRegistry.getRepositoryGroups().stream().map( r -> convertRepositoryGroupObject( r ) ).collect( Collectors.toList());
126 public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) {
127 return convertRepositoryGroupObject( repositoryRegistry.getRepositoryGroup( repositoryGroupId ) );
131 public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
132 throws RepositoryAdminException
134 validateRepositoryGroup( repositoryGroup, false );
135 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
137 RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
138 repositoryGroupConfiguration.setId( repositoryGroup.getId() );
139 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
140 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
141 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
142 repositoryGroupConfiguration.setCronExpression( StringUtils.isEmpty(repositoryGroup.getCronExpression()) ? "0 0 03 ? * MON" : repositoryGroup.getCronExpression() );
145 repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration);
146 } catch (RepositoryException e) {
150 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
151 mergedRemoteIndexesScheduler.schedule( repositoryRegistry.getRepositoryGroup( repositoryGroup.getId()), getMergedIndexDirectory( repositoryGroup.getId() ) );
156 public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
157 throws RepositoryAdminException
160 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
162 repositoryRegistry.removeRepositoryGroup(repositoryGroup);
163 } catch (RepositoryException e) {
164 log.error("Removal of repository group {} failed: {}", repositoryGroup.getId(), e.getMessage(), e);
165 throw new RepositoryAdminException("Removal of repository failed: " + e.getMessage(), e);
167 mergedRemoteIndexesScheduler.unschedule(
169 triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
175 public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
176 throws RepositoryAdminException
178 return updateRepositoryGroup( repositoryGroup, auditInformation, true );
181 private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
182 boolean triggerAuditEvent )
183 throws RepositoryAdminException
185 validateRepositoryGroup( repositoryGroup, true );
186 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
189 Configuration configuration = getArchivaConfiguration().getConfiguration();
191 RepositoryGroupConfiguration repositoryGroupConfiguration =
192 configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
194 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
195 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
196 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
197 repositoryGroupConfiguration.setCronExpression( repositoryGroup.getCronExpression() );
199 repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration);
200 } catch (RepositoryException e) {
204 org.apache.archiva.repository.RepositoryGroup rg = repositoryRegistry.getRepositoryGroup( repositoryGroup.getId( ) );
205 mergedRemoteIndexesScheduler.unschedule( rg );
206 mergedRemoteIndexesScheduler.schedule( rg, getMergedIndexDirectory( repositoryGroup.getId() ) );
207 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
213 public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
214 AuditInformation auditInformation )
215 throws RepositoryAdminException
217 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
218 if ( repositoryGroup == null )
220 throw new RepositoryAdminException(
221 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
224 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
225 throw new RepositoryAdminException("The repository group is not editable "+repositoryGroupId);
227 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
228 if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
230 throw new RepositoryAdminException(
231 "repositoryGroup with id " + repositoryGroupId + " already contain repository with id" + repositoryId );
233 org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
234 if (managedRepo==null) {
235 throw new RepositoryAdminException("Repository with id "+repositoryId+" does not exist" );
238 editableRepositoryGroup.addRepository( managedRepo );
240 repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
241 } catch (RepositoryException e) {
242 throw new RepositoryAdminException("Could not store the repository group "+repositoryGroupId, e);
244 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
249 public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
250 AuditInformation auditInformation )
251 throws RepositoryAdminException
253 org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
254 if ( repositoryGroup == null )
256 throw new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
257 + " doesn't not exists so cannot remove repository from it" );
260 if ( !repositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId()) ) )
262 throw new RepositoryAdminException(
263 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
267 if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
268 throw new RepositoryAdminException("Repository group is not editable " + repositoryGroupId);
270 EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
272 editableRepositoryGroup.removeRepository( repositoryId );
274 repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
275 } catch (RepositoryException e) {
276 throw new RepositoryAdminException("Could not store repository group " + repositoryGroupId, e);
278 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
283 public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
284 throws RepositoryAdminException
286 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
287 Map<String, RepositoryGroup> map = new HashMap<>( repositoriesGroups.size() );
288 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
290 map.put( repositoryGroup.getId(), repositoryGroup );
296 public Map<String, List<String>> getGroupToRepositoryMap()
297 throws RepositoryAdminException
300 Map<String, List<String>> map = new HashMap<>();
302 for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
304 for ( RepositoryGroup group : getRepositoriesGroups() )
306 if ( !group.getRepositories().contains( repo.getId() ) )
308 String groupId = group.getId();
309 List<String> repos = map.get( groupId );
312 repos = new ArrayList<>();
313 map.put( groupId, repos );
315 repos.add( repo.getId() );
323 public Map<String, List<String>> getRepositoryToGroupMap()
324 throws RepositoryAdminException
326 Map<String, List<String>> map = new HashMap<>();
328 for ( RepositoryGroup group : getRepositoriesGroups() )
330 for ( String repositoryId : group.getRepositories() )
332 List<String> groups = map.get( repositoryId );
333 if ( groups == null )
335 groups = new ArrayList<>();
336 map.put( repositoryId, groups );
338 groups.add( group.getId() );
344 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
345 throws RepositoryAdminException
347 String repoGroupId = repositoryGroup.getId();
348 if ( StringUtils.isBlank( repoGroupId ) )
350 throw new RepositoryAdminException( "repositoryGroup id cannot be empty" );
353 if ( repoGroupId.length() > 100 )
355 throw new RepositoryAdminException(
356 "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
360 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
361 if ( !matcher.matches() )
363 throw new RepositoryAdminException(
364 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
367 if ( repositoryGroup.getMergedIndexTtl() <= 0 )
369 throw new RepositoryAdminException( "Merged Index TTL must be greater than 0." );
372 Configuration configuration = getArchivaConfiguration().getConfiguration();
374 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
378 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
379 + "], that id already exists as a repository group." );
382 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
384 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
385 + "], that id already exists as a managed repository." );
387 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
389 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
390 + "], that id already exists as a remote repository." );
396 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
397 throws RepositoryAdminException
399 for ( String id : managedRepositoriesIds )
401 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
403 throw new RepositoryAdminException(
404 "managedRepository with id " + id + " not exists so cannot be used in a repositoryGroup" );
409 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
411 return managedRepositoryAdmin;
414 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
416 this.managedRepositoryAdmin = managedRepositoryAdmin;
419 private RepositoryGroup convertRepositoryGroupObject( org.apache.archiva.repository.RepositoryGroup group ) {
420 RepositoryGroup rg = new RepositoryGroup( group.getId( ), group.getRepositories().stream().map(r -> r.getId()).collect( Collectors.toList()) );
421 if (group.supportsFeature( IndexCreationFeature.class ))
423 IndexCreationFeature indexCreationFeature = group.getFeature( IndexCreationFeature.class ).get();
424 rg.setMergedIndexPath( indexCreationFeature.getIndexPath().getPath() );
426 rg.setCronExpression( group.getSchedulingDefinition() );
427 rg.setMergedIndexTtl( group.getMergedIndexTTL() );