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.annotation.PostConstruct;
37 import javax.inject.Inject;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.List;
44 import java.util.regex.Matcher;
45 import java.util.regex.Pattern;
48 * @author Olivier Lamy
50 @Service("repositoryGroupAdmin#default")
51 public class DefaultRepositoryGroupAdmin
52 extends AbstractRepositoryAdmin
53 implements RepositoryGroupAdmin
56 private Logger log = LoggerFactory.getLogger( getClass() );
58 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
61 private ManagedRepositoryAdmin managedRepositoryAdmin;
63 private File groupsDirectory;
66 public void initialize()
68 String appServerBase = getRegistry().getString( "appserver.base" );
69 groupsDirectory = new File( appServerBase + File.separatorChar + "groups" );
70 if ( !groupsDirectory.exists() )
72 groupsDirectory.mkdirs();
77 public File getMergedIndexDirectory( String repositoryGroupId )
79 return new File( groupsDirectory, repositoryGroupId );
82 public List<RepositoryGroup> getRepositoriesGroups()
83 throws RepositoryAdminException
85 List<RepositoryGroup> repositoriesGroups =
86 new ArrayList<RepositoryGroup>( getArchivaConfiguration().getConfiguration().getRepositoryGroups().size() );
88 for ( RepositoryGroupConfiguration repositoryGroupConfiguration : getArchivaConfiguration().getConfiguration().getRepositoryGroups() )
90 repositoriesGroups.add( new RepositoryGroup( repositoryGroupConfiguration.getId(), new ArrayList<String>(
91 repositoryGroupConfiguration.getRepositories() ) ).mergedIndexPath(
92 repositoryGroupConfiguration.getMergedIndexPath() ).mergedIndexTtl(
93 repositoryGroupConfiguration.getMergedIndexTtl() ) );
96 return repositoriesGroups;
99 public RepositoryGroup getRepositoryGroup( String repositoryGroupId )
100 throws RepositoryAdminException
102 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
103 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
105 if ( StringUtils.equals( repositoryGroupId, repositoryGroup.getId() ) )
107 return repositoryGroup;
113 public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
114 throws RepositoryAdminException
116 validateRepositoryGroup( repositoryGroup, false );
117 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
119 RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
120 repositoryGroupConfiguration.setId( repositoryGroup.getId() );
121 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
122 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
123 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
124 Configuration configuration = getArchivaConfiguration().getConfiguration();
125 configuration.addRepositoryGroup( repositoryGroupConfiguration );
126 saveConfiguration( configuration );
127 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
131 public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
132 throws RepositoryAdminException
134 Configuration configuration = getArchivaConfiguration().getConfiguration();
135 RepositoryGroupConfiguration repositoryGroupConfiguration =
136 configuration.getRepositoryGroupsAsMap().get( repositoryGroupId );
137 if ( repositoryGroupConfiguration == null )
139 throw new RepositoryAdminException(
140 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot remove" );
142 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
143 triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
147 public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
148 throws RepositoryAdminException
150 return updateRepositoryGroup( repositoryGroup, auditInformation, true );
153 private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
154 boolean triggerAuditEvent )
155 throws RepositoryAdminException
157 validateRepositoryGroup( repositoryGroup, true );
158 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
159 Configuration configuration = getArchivaConfiguration().getConfiguration();
161 RepositoryGroupConfiguration repositoryGroupConfiguration =
162 configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
164 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
166 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
167 repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
168 repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
169 configuration.addRepositoryGroup( repositoryGroupConfiguration );
171 saveConfiguration( configuration );
172 if ( triggerAuditEvent )
174 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
180 public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
181 AuditInformation auditInformation )
182 throws RepositoryAdminException
184 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
185 if ( repositoryGroup == null )
187 throw new RepositoryAdminException(
188 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
191 if ( repositoryGroup.getRepositories().contains( repositoryId ) )
193 throw new RepositoryAdminException(
194 "repositoryGroup with id " + repositoryGroupId + " already contain repository with id" + repositoryId );
196 validateManagedRepositoriesExists( Arrays.asList( repositoryId ) );
198 repositoryGroup.addRepository( repositoryId );
199 updateRepositoryGroup( repositoryGroup, auditInformation, false );
200 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
204 public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
205 AuditInformation auditInformation )
206 throws RepositoryAdminException
208 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
209 if ( repositoryGroup == null )
211 throw new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
212 + " doesn't not exists so cannot remove repository from it" );
215 if ( !repositoryGroup.getRepositories().contains( repositoryId ) )
217 throw new RepositoryAdminException(
218 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
222 repositoryGroup.removeRepository( repositoryId );
223 updateRepositoryGroup( repositoryGroup, auditInformation, false );
224 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
228 public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
229 throws RepositoryAdminException
231 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
232 Map<String, RepositoryGroup> map = new HashMap<String, RepositoryGroup>( repositoriesGroups.size() );
233 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
235 map.put( repositoryGroup.getId(), repositoryGroup );
240 public Map<String, List<String>> getGroupToRepositoryMap()
241 throws RepositoryAdminException
244 java.util.Map<String, java.util.List<String>> map = new java.util.HashMap<String, java.util.List<String>>();
246 for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
248 for ( RepositoryGroup group : getRepositoriesGroups() )
250 if ( !group.getRepositories().contains( repo.getId() ) )
252 String groupId = group.getId();
253 java.util.List<String> repos = map.get( groupId );
256 repos = new ArrayList<String>();
257 map.put( groupId, repos );
259 repos.add( repo.getId() );
266 public Map<String, List<String>> getRepositoryToGroupMap()
267 throws RepositoryAdminException
269 java.util.Map<String, java.util.List<String>> map = new java.util.HashMap<String, java.util.List<String>>();
271 for ( RepositoryGroup group : getRepositoriesGroups() )
273 for ( String repositoryId : group.getRepositories() )
275 java.util.List<String> groups = map.get( repositoryId );
276 if ( groups == null )
278 groups = new ArrayList<String>();
279 map.put( repositoryId, groups );
281 groups.add( group.getId() );
287 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
288 throws RepositoryAdminException
290 String repoGroupId = repositoryGroup.getId();
291 if ( StringUtils.isBlank( repoGroupId ) )
293 throw new RepositoryAdminException( "repositoryGroup id cannot be empty" );
296 if ( repoGroupId.length() > 100 )
298 throw new RepositoryAdminException(
299 "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
303 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
304 if ( !matcher.matches() )
306 throw new RepositoryAdminException(
307 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
310 if ( repositoryGroup.getMergedIndexTtl() <= 0 )
312 throw new RepositoryAdminException( "Merged Index TTL must be greater than 0." );
315 Configuration configuration = getArchivaConfiguration().getConfiguration();
317 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
321 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
322 + "], that id already exists as a repository group." );
325 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
327 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
328 + "], that id already exists as a managed repository." );
330 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
332 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
333 + "], that id already exists as a remote repository." );
339 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
340 throws RepositoryAdminException
342 for ( String id : managedRepositoriesIds )
344 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
346 throw new RepositoryAdminException(
347 "managedRepository with id " + id + " not exists so cannot be used in a repositoryGroup" );
352 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
354 return managedRepositoryAdmin;
357 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
359 this.managedRepositoryAdmin = managedRepositoryAdmin;