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() ) ).mergedIndexPath(
71 repositoryGroupConfiguration.getMergedIndexPath() ).mergedIndexTtl( repositoryGroupConfiguration.getMergedIndexTtl() ) );
74 return repositoriesGroups;
77 public RepositoryGroup getRepositoryGroup( String repositoryGroupId )
78 throws RepositoryAdminException
80 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
81 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
83 if ( StringUtils.equals( repositoryGroupId, repositoryGroup.getId() ) )
85 return repositoryGroup;
91 public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
92 throws RepositoryAdminException
94 validateRepositoryGroup( repositoryGroup, false );
95 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
97 RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
98 repositoryGroupConfiguration.setId( repositoryGroup.getId() );
99 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
100 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
101 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
102 Configuration configuration = getArchivaConfiguration().getConfiguration();
103 configuration.addRepositoryGroup( repositoryGroupConfiguration );
104 saveConfiguration( configuration );
105 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
109 public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
110 throws RepositoryAdminException
112 Configuration configuration = getArchivaConfiguration().getConfiguration();
113 RepositoryGroupConfiguration repositoryGroupConfiguration =
114 configuration.getRepositoryGroupsAsMap().get( repositoryGroupId );
115 if ( repositoryGroupConfiguration == null )
117 throw new RepositoryAdminException(
118 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot remove" );
120 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
121 triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
125 public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
126 throws RepositoryAdminException
128 return updateRepositoryGroup( repositoryGroup, auditInformation, true );
131 private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
132 boolean triggerAuditEvent )
133 throws RepositoryAdminException
135 validateRepositoryGroup( repositoryGroup, true );
136 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
137 Configuration configuration = getArchivaConfiguration().getConfiguration();
139 RepositoryGroupConfiguration repositoryGroupConfiguration =
140 configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
142 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
144 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
145 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
146 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
147 configuration.addRepositoryGroup( repositoryGroupConfiguration );
149 saveConfiguration( configuration );
150 if ( triggerAuditEvent )
152 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
158 public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
159 AuditInformation auditInformation )
160 throws RepositoryAdminException
162 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
163 if ( repositoryGroup == null )
165 throw new RepositoryAdminException(
166 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
169 if ( repositoryGroup.getRepositories().contains( repositoryId ) )
171 throw new RepositoryAdminException(
172 "repositoryGroup with id " + repositoryGroupId + " already contain repository with id" + repositoryId );
174 validateManagedRepositoriesExists( Arrays.asList( repositoryId ) );
176 repositoryGroup.addRepository( repositoryId );
177 updateRepositoryGroup( repositoryGroup, auditInformation, false );
178 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
182 public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
183 AuditInformation auditInformation )
184 throws RepositoryAdminException
186 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
187 if ( repositoryGroup == null )
189 throw new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
190 + " doesn't not exists so cannot remove repository from it" );
193 if ( !repositoryGroup.getRepositories().contains( repositoryId ) )
195 throw new RepositoryAdminException(
196 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
200 repositoryGroup.removeRepository( repositoryId );
201 updateRepositoryGroup( repositoryGroup, auditInformation, false );
202 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
206 public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
207 throws RepositoryAdminException
209 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
210 Map<String, RepositoryGroup> map = new HashMap<String, RepositoryGroup>( repositoriesGroups.size() );
211 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
213 map.put( repositoryGroup.getId(), repositoryGroup );
218 public Map<String, List<String>> getGroupToRepositoryMap()
219 throws RepositoryAdminException
222 java.util.Map<String, java.util.List<String>> map = new java.util.HashMap<String, java.util.List<String>>();
224 for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
226 for ( RepositoryGroup group : getRepositoriesGroups() )
228 if ( !group.getRepositories().contains( repo.getId() ) )
230 String groupId = group.getId();
231 java.util.List<String> repos = map.get( groupId );
234 repos = new ArrayList<String>();
235 map.put( groupId, repos );
237 repos.add( repo.getId() );
244 public Map<String, List<String>> getRepositoryToGroupMap()
245 throws RepositoryAdminException
247 java.util.Map<String, java.util.List<String>> map = new java.util.HashMap<String, java.util.List<String>>();
249 for ( RepositoryGroup group : getRepositoriesGroups() )
251 for ( String repositoryId : group.getRepositories() )
253 java.util.List<String> groups = map.get( repositoryId );
254 if ( groups == null )
256 groups = new ArrayList<String>();
257 map.put( repositoryId, groups );
259 groups.add( group.getId() );
265 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
266 throws RepositoryAdminException
268 String repoGroupId = repositoryGroup.getId();
269 if ( StringUtils.isBlank( repoGroupId ) )
271 throw new RepositoryAdminException( "repositoryGroup id cannot be empty" );
274 if ( repoGroupId.length() > 100 )
276 throw new RepositoryAdminException(
277 "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
281 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
282 if ( !matcher.matches() )
284 throw new RepositoryAdminException(
285 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
288 if ( repositoryGroup.getMergedIndexTtl() <= 0)
290 throw new RepositoryAdminException( "Merged Index TTL must be greater than 0." );
293 Configuration configuration = getArchivaConfiguration().getConfiguration();
295 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
299 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
300 + "], that id already exists as a repository group." );
303 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
305 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
306 + "], that id already exists as a managed repository." );
308 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
310 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
311 + "], that id already exists as a remote repository." );
317 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
318 throws RepositoryAdminException
320 for ( String id : managedRepositoriesIds )
322 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
324 throw new RepositoryAdminException(
325 "managedRepository with id " + id + " not exists so cannot be used in a repositoryGroup" );
330 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
332 return managedRepositoryAdmin;
335 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
337 this.managedRepositoryAdmin = managedRepositoryAdmin;