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.audit.AuditEvent;
29 import org.apache.archiva.configuration.Configuration;
30 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
31 import org.apache.commons.lang.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.stereotype.Service;
36 import javax.inject.Inject;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
46 * @author Olivier Lamy
48 @Service( "repositoryGroupAdmin#default" )
49 public class DefaultRepositoryGroupAdmin
50 extends AbstractRepositoryAdmin
51 implements RepositoryGroupAdmin
54 private Logger log = LoggerFactory.getLogger( getClass() );
56 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
59 private ManagedRepositoryAdmin managedRepositoryAdmin;
61 public List<RepositoryGroup> getRepositoriesGroups()
62 throws RepositoryAdminException
64 List<RepositoryGroup> repositoriesGroups =
65 new ArrayList<RepositoryGroup>( getArchivaConfiguration().getConfiguration().getRepositoryGroups().size() );
67 for ( RepositoryGroupConfiguration repositoryGroupConfiguration : getArchivaConfiguration().getConfiguration().getRepositoryGroups() )
69 repositoriesGroups.add( new RepositoryGroup( repositoryGroupConfiguration.getId(), new ArrayList<String>(
70 repositoryGroupConfiguration.getRepositories() ) ) );
73 return repositoriesGroups;
76 public RepositoryGroup getRepositoryGroup( String repositoryGroupId )
77 throws RepositoryAdminException
79 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
80 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
82 if ( StringUtils.equals( repositoryGroupId, repositoryGroup.getId() ) )
84 return repositoryGroup;
90 public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
91 throws RepositoryAdminException
93 validateRepositoryGroup( repositoryGroup, false );
94 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
95 RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
96 repositoryGroupConfiguration.setId( repositoryGroup.getId() );
97 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
98 Configuration configuration = getArchivaConfiguration().getConfiguration();
99 configuration.addRepositoryGroup( repositoryGroupConfiguration );
100 saveConfiguration( configuration );
101 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
105 public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
106 throws RepositoryAdminException
108 Configuration configuration = getArchivaConfiguration().getConfiguration();
109 RepositoryGroupConfiguration repositoryGroupConfiguration =
110 configuration.getRepositoryGroupsAsMap().get( repositoryGroupId );
111 if ( repositoryGroupConfiguration == null )
113 throw new RepositoryAdminException(
114 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot remove" );
116 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
117 triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
121 public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
122 throws RepositoryAdminException
124 return updateRepositoryGroup( repositoryGroup, auditInformation, true );
127 private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
128 boolean triggerAuditEvent )
129 throws RepositoryAdminException
131 validateRepositoryGroup( repositoryGroup, true );
132 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
133 Configuration configuration = getArchivaConfiguration().getConfiguration();
135 RepositoryGroupConfiguration repositoryGroupConfiguration =
136 configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
138 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
140 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
141 configuration.addRepositoryGroup( repositoryGroupConfiguration );
143 saveConfiguration( configuration );
144 if ( triggerAuditEvent )
146 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
152 public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
153 AuditInformation auditInformation )
154 throws RepositoryAdminException
156 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
157 if ( repositoryGroup == null )
159 throw new RepositoryAdminException(
160 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
163 if ( repositoryGroup.getRepositories().contains( repositoryId ) )
165 throw new RepositoryAdminException(
166 "repositoryGroup with id " + repositoryGroupId + " already contain repository with id" + repositoryId );
168 validateManagedRepositoriesExists( Arrays.asList( repositoryId ) );
170 repositoryGroup.addRepository( repositoryId );
171 updateRepositoryGroup( repositoryGroup, auditInformation, false );
172 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
176 public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
177 AuditInformation auditInformation )
178 throws RepositoryAdminException
180 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
181 if ( repositoryGroup == null )
183 throw new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
184 + " doesn't not exists so cannot remove repository from it" );
187 if ( !repositoryGroup.getRepositories().contains( repositoryId ) )
189 throw new RepositoryAdminException(
190 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
194 repositoryGroup.removeRepository( repositoryId );
195 updateRepositoryGroup( repositoryGroup, auditInformation, false );
196 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
200 public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
201 throws RepositoryAdminException
203 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
204 Map<String, RepositoryGroup> map = new HashMap<String, RepositoryGroup>( repositoriesGroups.size() );
205 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
207 map.put( repositoryGroup.getId(), repositoryGroup );
212 public Map<String, List<String>> getGroupToRepositoryMap()
213 throws RepositoryAdminException
216 java.util.Map<String, java.util.List<String>> map = new java.util.HashMap<String, java.util.List<String>>();
218 for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
220 for ( RepositoryGroup group : getRepositoriesGroups() )
222 if ( !group.getRepositories().contains( repo.getId() ) )
224 String groupId = group.getId();
225 java.util.List<String> repos = map.get( groupId );
228 repos = new java.util.ArrayList<String>();
229 map.put( groupId, repos );
231 repos.add( repo.getId() );
238 public Map<String, List<String>> getRepositoryToGroupMap()
239 throws RepositoryAdminException
241 java.util.Map<String, java.util.List<String>> map = new java.util.HashMap<String, java.util.List<String>>();
243 for ( RepositoryGroup group : getRepositoriesGroups() )
245 for ( String repositoryId : group.getRepositories() )
247 java.util.List<String> groups = map.get( repositoryId );
248 if ( groups == null )
250 groups = new java.util.ArrayList<String>();
251 map.put( repositoryId, groups );
253 groups.add( group.getId() );
259 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
260 throws RepositoryAdminException
262 String repoGroupId = repositoryGroup.getId();
263 if ( StringUtils.isBlank( repoGroupId ) )
265 throw new RepositoryAdminException( "repositoryGroup id cannot be empty" );
268 if ( repoGroupId.length() > 100 )
270 throw new RepositoryAdminException(
271 "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
275 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
276 if ( !matcher.matches() )
278 throw new RepositoryAdminException(
279 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
282 Configuration configuration = getArchivaConfiguration().getConfiguration();
284 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
288 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
289 + "], that id already exists as a repository group." );
292 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
294 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
295 + "], that id already exists as a managed repository." );
297 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
299 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
300 + "], that id already exists as a remote repository." );
306 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
307 throws RepositoryAdminException
309 for ( String id : managedRepositoriesIds )
311 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
313 throw new RepositoryAdminException(
314 "managedRepository with id " + id + " not exists so cannot be used in a repositoryGroup" );
319 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
321 return managedRepositoryAdmin;
324 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
326 this.managedRepositoryAdmin = managedRepositoryAdmin;