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.AuditInformation;
22 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
23 import org.apache.archiva.admin.repository.RepositoryAdminException;
24 import org.apache.archiva.admin.repository.managed.ManagedRepository;
25 import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.audit.AuditEvent;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Service;
34 import javax.inject.Inject;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
44 * @author Olivier Lamy
46 @Service( "repositoryGroupAdmin#default" )
47 public class DefaultRepositoryGroupAdmin
48 extends AbstractRepositoryAdmin
49 implements RepositoryGroupAdmin
52 private Logger log = LoggerFactory.getLogger( getClass() );
54 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
57 private ManagedRepositoryAdmin managedRepositoryAdmin;
59 public List<RepositoryGroup> getRepositoriesGroups()
60 throws RepositoryAdminException
62 List<RepositoryGroup> repositoriesGroups = new ArrayList<RepositoryGroup>();
64 for ( RepositoryGroupConfiguration repositoryGroupConfiguration : getArchivaConfiguration().getConfiguration().getRepositoryGroups() )
66 repositoriesGroups.add( new RepositoryGroup( repositoryGroupConfiguration.getId(), new ArrayList<String>(
67 repositoryGroupConfiguration.getRepositories() ) ) );
70 return repositoriesGroups;
73 public RepositoryGroup getRepositoryGroup( String repositoryGroupId )
74 throws RepositoryAdminException
76 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
77 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
79 if ( StringUtils.equals( repositoryGroupId, repositoryGroup.getId() ) )
81 return repositoryGroup;
87 public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
88 throws RepositoryAdminException
90 validateRepositoryGroup( repositoryGroup, false );
91 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
92 RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
93 repositoryGroupConfiguration.setId( repositoryGroup.getId() );
94 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
95 Configuration configuration = getArchivaConfiguration().getConfiguration();
96 configuration.addRepositoryGroup( repositoryGroupConfiguration );
97 saveConfiguration( configuration );
98 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
102 public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
103 throws RepositoryAdminException
105 Configuration configuration = getArchivaConfiguration().getConfiguration();
106 RepositoryGroupConfiguration repositoryGroupConfiguration =
107 configuration.getRepositoryGroupsAsMap().get( repositoryGroupId );
108 if ( repositoryGroupConfiguration == null )
110 throw new RepositoryAdminException(
111 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot remove" );
113 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
114 triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
118 public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
119 throws RepositoryAdminException
121 return updateRepositoryGroup( repositoryGroup, auditInformation, true );
124 private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
125 boolean triggerAuditEvent )
126 throws RepositoryAdminException
128 validateRepositoryGroup( repositoryGroup, true );
129 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
130 Configuration configuration = getArchivaConfiguration().getConfiguration();
132 RepositoryGroupConfiguration repositoryGroupConfiguration =
133 configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
135 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
137 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
138 configuration.addRepositoryGroup( repositoryGroupConfiguration );
140 saveConfiguration( configuration );
141 if ( triggerAuditEvent )
143 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
149 public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
150 AuditInformation auditInformation )
151 throws RepositoryAdminException
153 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
154 if ( repositoryGroup == null )
156 throw new RepositoryAdminException(
157 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
160 if ( repositoryGroup.getRepositories().contains( repositoryId ) )
162 throw new RepositoryAdminException(
163 "repositoryGroup with id " + repositoryGroupId + " already contain repository with id" + repositoryId );
165 validateManagedRepositoriesExists( Arrays.asList( repositoryId ) );
167 repositoryGroup.addRepository( repositoryId );
168 updateRepositoryGroup( repositoryGroup, auditInformation, false );
169 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
173 public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
174 AuditInformation auditInformation )
175 throws RepositoryAdminException
177 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
178 if ( repositoryGroup == null )
180 throw new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
181 + " doesn't not exists so cannot remove repository from it" );
184 if ( !repositoryGroup.getRepositories().contains( repositoryId ) )
186 throw new RepositoryAdminException(
187 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
191 repositoryGroup.removeRepository( repositoryId );
192 updateRepositoryGroup( repositoryGroup, auditInformation, false );
193 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
197 public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
198 throws RepositoryAdminException
200 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
201 Map<String, RepositoryGroup> map = new HashMap<String, RepositoryGroup>( repositoriesGroups.size() );
202 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
204 map.put( repositoryGroup.getId(), repositoryGroup );
209 public Map<String, List<String>> getGroupToRepositoryMap()
210 throws RepositoryAdminException
213 java.util.Map<String, java.util.List<String>> map = new java.util.HashMap<String, java.util.List<String>>();
215 for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
217 for ( RepositoryGroup group : getRepositoriesGroups() )
219 if ( !group.getRepositories().contains( repo.getId() ) )
221 String groupId = group.getId();
222 java.util.List<String> repos = map.get( groupId );
225 repos = new java.util.ArrayList<String>();
226 map.put( groupId, repos );
228 repos.add( repo.getId() );
235 public Map<String, List<String>> getRepositoryToGroupMap()
236 throws RepositoryAdminException
238 java.util.Map<String, java.util.List<String>> map = new java.util.HashMap<String, java.util.List<String>>();
240 for ( RepositoryGroup group : getRepositoriesGroups() )
242 for ( String repositoryId : group.getRepositories() )
244 java.util.List<String> groups = map.get( repositoryId );
245 if ( groups == null )
247 groups = new java.util.ArrayList<String>();
248 map.put( repositoryId, groups );
250 groups.add( group.getId() );
256 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
257 throws RepositoryAdminException
259 String repoGroupId = repositoryGroup.getId();
260 if ( StringUtils.isBlank( repoGroupId ) )
262 throw new RepositoryAdminException( "repositoryGroup id cannot be empty" );
265 if ( repoGroupId.length() > 100 )
267 throw new RepositoryAdminException(
268 "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
272 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
273 if ( !matcher.matches() )
275 throw new RepositoryAdminException(
276 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
279 Configuration configuration = getArchivaConfiguration().getConfiguration();
281 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
285 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
286 + "], that id already exists as a repository group." );
289 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
291 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
292 + "], that id already exists as a managed repository." );
294 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
296 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
297 + "], that id already exists as a remote repository." );
303 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
304 throws RepositoryAdminException
306 for ( String id : managedRepositoriesIds )
308 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
310 throw new RepositoryAdminException(
311 "managedRepository with id " + id + " not exists so cannot be used in a repositoryGroup" );
316 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
318 return managedRepositoryAdmin;
321 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
323 this.managedRepositoryAdmin = managedRepositoryAdmin;