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.ManagedRepositoryAdmin;
25 import org.apache.archiva.audit.AuditEvent;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Inject;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
41 * @author Olivier Lamy
43 @Service( "repositoryGroupAdmin#default" )
44 public class DefaultRepositoryGroupAdmin
45 extends AbstractRepositoryAdmin
46 implements RepositoryGroupAdmin
49 private Logger log = LoggerFactory.getLogger( getClass() );
51 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
54 private ManagedRepositoryAdmin managedRepositoryAdmin;
56 public List<RepositoryGroup> getRepositoriesGroups()
57 throws RepositoryAdminException
59 List<RepositoryGroup> repositoriesGroups = new ArrayList<RepositoryGroup>();
61 for ( RepositoryGroupConfiguration repositoryGroupConfiguration : getArchivaConfiguration().getConfiguration().getRepositoryGroups() )
63 repositoriesGroups.add( new RepositoryGroup( repositoryGroupConfiguration.getId(), new ArrayList<String>(
64 repositoryGroupConfiguration.getRepositories() ) ) );
67 return repositoriesGroups;
70 public RepositoryGroup getRepositoryGroup( String repositoryGroupId )
71 throws RepositoryAdminException
73 List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
74 for ( RepositoryGroup repositoryGroup : repositoriesGroups )
76 if ( StringUtils.equals( repositoryGroupId, repositoryGroup.getId() ) )
78 return repositoryGroup;
84 public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
85 throws RepositoryAdminException
87 validateRepositoryGroup( repositoryGroup, false );
88 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
89 RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
90 repositoryGroupConfiguration.setId( repositoryGroup.getId() );
91 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
92 Configuration configuration = getArchivaConfiguration().getConfiguration();
93 configuration.addRepositoryGroup( repositoryGroupConfiguration );
94 saveConfiguration( configuration );
95 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
99 public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
100 throws RepositoryAdminException
102 Configuration configuration = getArchivaConfiguration().getConfiguration();
103 RepositoryGroupConfiguration repositoryGroupConfiguration =
104 configuration.getRepositoryGroupsAsMap().get( repositoryGroupId );
105 if ( repositoryGroupConfiguration == null )
107 throw new RepositoryAdminException(
108 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot remove" );
110 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
111 triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
115 public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
116 throws RepositoryAdminException
118 return updateRepositoryGroup( repositoryGroup, auditInformation, true );
121 private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
122 boolean triggerAuditEvent )
123 throws RepositoryAdminException
125 validateRepositoryGroup( repositoryGroup, true );
126 validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
127 Configuration configuration = getArchivaConfiguration().getConfiguration();
129 RepositoryGroupConfiguration repositoryGroupConfiguration =
130 configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
132 configuration.removeRepositoryGroup( repositoryGroupConfiguration );
134 repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
135 configuration.addRepositoryGroup( repositoryGroupConfiguration );
137 saveConfiguration( configuration );
138 if ( triggerAuditEvent )
140 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
146 public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
147 AuditInformation auditInformation )
148 throws RepositoryAdminException
150 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
151 if ( repositoryGroup == null )
153 throw new RepositoryAdminException(
154 "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
157 if ( repositoryGroup.getRepositories().contains( repositoryId ) )
159 log.info( "repositoryGroup {} already contains repository {} so skip adding it" );
162 validateManagedRepositoriesExists( Arrays.asList( repositoryId ) );
164 repositoryGroup.addRepository( repositoryId );
165 updateRepositoryGroup( repositoryGroup, auditInformation, false );
166 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
170 public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
171 AuditInformation auditInformation )
172 throws RepositoryAdminException
174 RepositoryGroup repositoryGroup = getRepositoryGroup( repositoryGroupId );
175 if ( repositoryGroup == null )
177 throw new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
178 + " doesn't not exists so cannot remove repository from it" );
181 if ( !repositoryGroup.getRepositories().contains( repositoryId ) )
183 log.info( "repositoryGroup {} already contains repository {} so skip removing it" );
187 repositoryGroup.removeRepository( repositoryId );
188 updateRepositoryGroup( repositoryGroup, auditInformation, false );
189 triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
193 public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
194 throws RepositoryAdminException
196 String repoGroupId = repositoryGroup.getId();
197 if ( StringUtils.isBlank( repoGroupId ) )
199 throw new RepositoryAdminException( "repositoryGroup id cannot be empty" );
202 if ( repoGroupId.length() > 100 )
204 throw new RepositoryAdminException(
205 "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
209 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
210 if ( !matcher.matches() )
212 throw new RepositoryAdminException(
213 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
216 Configuration configuration = getArchivaConfiguration().getConfiguration();
218 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
222 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
223 + "], that id already exists as a repository group." );
226 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
228 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
229 + "], that id already exists as a managed repository." );
231 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
233 throw new RepositoryAdminException( "Unable to add new repository group with id [" + repoGroupId
234 + "], that id already exists as a remote repository." );
240 private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
241 throws RepositoryAdminException
243 for ( String id : managedRepositoriesIds )
245 if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
247 throw new RepositoryAdminException(
248 "managedRepository with id " + id + " not exists so cannot be used in a repositoryGroup" );
253 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
255 return managedRepositoryAdmin;
258 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
260 this.managedRepositoryAdmin = managedRepositoryAdmin;