]> source.dussan.org Git - archiva.git/blob
3b810c7c9971b0f9a6dcb2b9f19b94c7331f8d9c
[archiva.git] /
1 package org.apache.archiva.admin.repository.group;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.EntityExistsException;
23 import org.apache.archiva.admin.model.EntityNotFoundException;
24 import org.apache.archiva.admin.model.RepositoryAdminException;
25 import org.apache.archiva.admin.model.beans.ManagedRepository;
26 import org.apache.archiva.admin.model.beans.RepositoryGroup;
27 import org.apache.archiva.admin.model.group.RepositoryGroupAdmin;
28 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
29 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
30 import org.apache.archiva.configuration.Configuration;
31 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
32 import org.apache.archiva.metadata.model.facets.AuditEvent;
33 import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler;
34 import org.apache.archiva.repository.EditableRepositoryGroup;
35 import org.apache.archiva.repository.RepositoryException;
36 import org.apache.archiva.repository.RepositoryRegistry;
37 import org.apache.archiva.repository.features.IndexCreationFeature;
38 import org.apache.archiva.repository.storage.StorageAsset;
39 import org.apache.commons.lang3.StringUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Service;
43
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
47 import java.io.IOException;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.regex.Matcher;
56 import java.util.regex.Pattern;
57 import java.util.stream.Collectors;
58
59 /**
60  * @author Olivier Lamy
61  */
62 @Service("repositoryGroupAdmin#default")
63 public class DefaultRepositoryGroupAdmin
64     extends AbstractRepositoryAdmin
65     implements RepositoryGroupAdmin
66 {
67
68     private Logger log = LoggerFactory.getLogger( getClass() );
69
70     private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
71
72     @Inject
73     private ManagedRepositoryAdmin managedRepositoryAdmin;
74
75     @Inject
76     @Named("mergedRemoteIndexesScheduler#default")
77     private MergedRemoteIndexesScheduler mergedRemoteIndexesScheduler;
78
79     @Inject
80     private RepositoryRegistry repositoryRegistry;
81
82     private Path groupsDirectory;
83
84     @PostConstruct
85     public void initialize()
86     {
87         String appServerBase = getRegistry().getString( "appserver.base" );
88         groupsDirectory = Paths.get( appServerBase, "groups" );
89         if ( !Files.exists(groupsDirectory) )
90         {
91             Files.exists(groupsDirectory);
92         }
93
94         for ( org.apache.archiva.repository.RepositoryGroup repositoryGroup : repositoryRegistry.getRepositoryGroups() )
95         {
96             mergedRemoteIndexesScheduler.schedule( repositoryGroup,
97                                                    getMergedIndexDirectory( repositoryGroup.getId() ));
98             // create the directory for each group if not exists
99             Path groupPath = groupsDirectory.resolve(repositoryGroup.getId() );
100             if ( !Files.exists(groupPath) )
101             {
102                 try {
103                     Files.createDirectories(groupPath);
104                 } catch (IOException e) {
105                     log.error("Could not create directory {}", groupPath);
106                 }
107             }
108         }
109
110     }
111
112
113     @Override
114     public StorageAsset getMergedIndexDirectory(String repositoryGroupId )
115     {
116         org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
117         if (group!=null) {
118             return group.getFeature(IndexCreationFeature.class).get().getLocalIndexPath();
119         } else {
120             return null;
121         }
122     }
123
124     @Override
125     public List<RepositoryGroup> getRepositoriesGroups() {
126         return repositoryRegistry.getRepositoryGroups().stream().map( r -> convertRepositoryGroupObject( r ) ).collect( Collectors.toList());
127     }
128
129     @Override
130     public RepositoryGroup getRepositoryGroup( String repositoryGroupId ) throws EntityNotFoundException
131     {
132         org.apache.archiva.repository.RepositoryGroup group = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
133         if (group==null) {
134             throw new EntityNotFoundException( "Repository group does not exist" );
135         } else {
136             return convertRepositoryGroupObject( group );
137         }
138     }
139
140     @Override
141     public Boolean addRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
142         throws RepositoryAdminException
143     {
144         validateRepositoryGroup( repositoryGroup, false );
145         validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
146
147         RepositoryGroupConfiguration repositoryGroupConfiguration = new RepositoryGroupConfiguration();
148         repositoryGroupConfiguration.setId( repositoryGroup.getId() );
149         repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
150         repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
151         repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
152         repositoryGroupConfiguration.setCronExpression( StringUtils.isEmpty(repositoryGroup.getCronExpression()) ? "0 0 03 ? * MON" : repositoryGroup.getCronExpression() );
153
154         try {
155             repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration);
156         } catch (RepositoryException e) {
157             log.error( "Could not add the repository group to the registry: {}", e.getMessage( ), e );
158             throw RepositoryAdminException.ofKey( "repository_group.registry.add_error", e, repositoryGroup.getId(), e.getMessage() );
159         }
160
161         triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_GROUP, auditInformation );
162         mergedRemoteIndexesScheduler.schedule( repositoryRegistry.getRepositoryGroup( repositoryGroup.getId()), getMergedIndexDirectory( repositoryGroup.getId() ) );
163         return Boolean.TRUE;
164     }
165
166     @Override
167     public Boolean deleteRepositoryGroup( String repositoryGroupId, AuditInformation auditInformation )
168         throws RepositoryAdminException
169     {
170
171         org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup(repositoryGroupId);
172         try {
173             repositoryRegistry.removeRepositoryGroup(repositoryGroup);
174         } catch (RepositoryException e) {
175             log.error("Removal of repository group {} failed: {}", repositoryGroup.getId(), e.getMessage(), e);
176             throw new RepositoryAdminException("Removal of repository failed: " + e.getMessage(), e);
177         }
178         mergedRemoteIndexesScheduler.unschedule(
179             repositoryGroup );
180         triggerAuditEvent( repositoryGroupId, null, AuditEvent.DELETE_REPO_GROUP, auditInformation );
181
182         return Boolean.TRUE;
183     }
184
185     @Override
186     public Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation )
187         throws RepositoryAdminException
188     {
189         return updateRepositoryGroup( repositoryGroup, auditInformation, true );
190     }
191
192     private Boolean updateRepositoryGroup( RepositoryGroup repositoryGroup, AuditInformation auditInformation,
193                                            boolean triggerAuditEvent )
194         throws RepositoryAdminException
195     {
196         validateRepositoryGroup( repositoryGroup, true );
197         validateManagedRepositoriesExists( repositoryGroup.getRepositories() );
198
199
200         Configuration configuration = getArchivaConfiguration().getConfiguration();
201
202         RepositoryGroupConfiguration repositoryGroupConfiguration =
203             configuration.getRepositoryGroupsAsMap().get( repositoryGroup.getId() );
204
205         repositoryGroupConfiguration.setRepositories( repositoryGroup.getRepositories() );
206         repositoryGroupConfiguration.setMergedIndexPath( repositoryGroup.getMergedIndexPath() );
207         repositoryGroupConfiguration.setMergedIndexTtl( repositoryGroup.getMergedIndexTtl() );
208         repositoryGroupConfiguration.setCronExpression( repositoryGroup.getCronExpression() );
209         try {
210             repositoryRegistry.putRepositoryGroup(repositoryGroupConfiguration);
211         } catch (RepositoryException e) {
212             log.error( "Could not update the repository group in the registry: {}", e.getMessage( ), e );
213             throw RepositoryAdminException.ofKey( "repository_group.registry.update_error", e, repositoryGroup.getId(), e.getMessage() );
214         }
215
216         org.apache.archiva.repository.RepositoryGroup rg = repositoryRegistry.getRepositoryGroup( repositoryGroup.getId( ) );
217         mergedRemoteIndexesScheduler.unschedule( rg );
218         mergedRemoteIndexesScheduler.schedule( rg, getMergedIndexDirectory( repositoryGroup.getId() ) );
219         triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.MODIFY_REPO_GROUP, auditInformation );
220         return Boolean.TRUE;
221     }
222
223
224     @Override
225     public Boolean addRepositoryToGroup( String repositoryGroupId, String repositoryId,
226                                          AuditInformation auditInformation )
227         throws RepositoryAdminException
228     {
229         org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
230         if ( repositoryGroup == null )
231         {
232             throw new RepositoryAdminException(
233                     "repositoryGroup with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it" );
234         }
235
236         if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
237             throw new RepositoryAdminException("The repository group is not editable "+repositoryGroupId);
238         }
239         EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
240         if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
241         {
242             throw new RepositoryAdminException(
243                 "repositoryGroup with id " + repositoryGroupId + " already contain repository with id" + repositoryId );
244         }
245         org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repositoryId);
246         if (managedRepo==null) {
247             throw new RepositoryAdminException("Repository with id "+repositoryId+" does not exist" );
248         }
249
250         editableRepositoryGroup.addRepository( managedRepo );
251         try {
252             repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
253         } catch (RepositoryException e) {
254             throw new RepositoryAdminException("Could not store the repository group "+repositoryGroupId, e);
255         }
256         triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.ADD_REPO_TO_GROUP, auditInformation );
257         return Boolean.TRUE;
258     }
259
260     @Override
261     public Boolean deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId,
262                                               AuditInformation auditInformation )
263         throws RepositoryAdminException
264     {
265         org.apache.archiva.repository.RepositoryGroup repositoryGroup = repositoryRegistry.getRepositoryGroup( repositoryGroupId );
266         if ( repositoryGroup == null )
267         {
268             throw new RepositoryAdminException( "repositoryGroup with id " + repositoryGroupId
269                                                     + " doesn't not exists so cannot remove repository from it" );
270         }
271
272         if ( !repositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId()) ) )
273         {
274             throw new RepositoryAdminException(
275                 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
276                     + repositoryId
277             );
278         }
279         if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
280             throw new RepositoryAdminException("Repository group is not editable " + repositoryGroupId);
281         }
282         EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
283
284         editableRepositoryGroup.removeRepository( repositoryId );
285         try {
286             repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
287         } catch (RepositoryException e) {
288             throw new RepositoryAdminException("Could not store repository group " + repositoryGroupId, e);
289         }
290         triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
291         return Boolean.TRUE;
292     }
293
294     @Override
295     public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
296         throws RepositoryAdminException
297     {
298         List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
299         Map<String, RepositoryGroup> map = new HashMap<>( repositoriesGroups.size() );
300         for ( RepositoryGroup repositoryGroup : repositoriesGroups )
301         {
302             map.put( repositoryGroup.getId(), repositoryGroup );
303         }
304         return map;
305     }
306
307     @Override
308     public Map<String, List<String>> getGroupToRepositoryMap()
309         throws RepositoryAdminException
310     {
311
312         Map<String, List<String>> map = new HashMap<>();
313
314         for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
315         {
316             for ( RepositoryGroup group : getRepositoriesGroups() )
317             {
318                 if ( !group.getRepositories().contains( repo.getId() ) )
319                 {
320                     String groupId = group.getId();
321                     List<String> repos = map.get( groupId );
322                     if ( repos == null )
323                     {
324                         repos = new ArrayList<>();
325                         map.put( groupId, repos );
326                     }
327                     repos.add( repo.getId() );
328                 }
329             }
330         }
331         return map;
332     }
333
334     @Override
335     public Map<String, List<String>> getRepositoryToGroupMap()
336         throws RepositoryAdminException
337     {
338         Map<String, List<String>> map = new HashMap<>();
339
340         for ( RepositoryGroup group : getRepositoriesGroups() )
341         {
342             for ( String repositoryId : group.getRepositories() )
343             {
344                 List<String> groups = map.get( repositoryId );
345                 if ( groups == null )
346                 {
347                     groups = new ArrayList<>();
348                     map.put( repositoryId, groups );
349                 }
350                 groups.add( group.getId() );
351             }
352         }
353         return map;
354     }
355
356     public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
357         throws RepositoryAdminException
358     {
359         String repoGroupId = repositoryGroup.getId();
360         if ( StringUtils.isBlank( repoGroupId ) )
361         {
362             throw RepositoryAdminException.ofKey("repository_group.id.empty" );
363         }
364
365         if ( repoGroupId.length() > 100 )
366         {
367             throw RepositoryAdminException.ofKey("repository_group.id.max_length",repoGroupId, Integer.toString( 100 ));
368
369         }
370
371         Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
372         if ( !matcher.matches() )
373         {
374             throw RepositoryAdminException.ofKey("repository_group.id.invalid_chars","alphanumeric, '.', '-','_'" );
375         }
376
377         if ( repositoryGroup.getMergedIndexTtl() <= 0 )
378         {
379             throw RepositoryAdminException.ofKey("repository_group.merged_index_ttl.min","0" );
380         }
381
382         Configuration configuration = getArchivaConfiguration().getConfiguration();
383
384         if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
385         {
386             if ( !updateMode )
387             {
388                 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
389                                                         + "], that id already exists as a repository group." );
390             }
391         }
392         else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
393         {
394             throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
395                                                     + "], that id already exists as a managed repository." );
396         }
397         else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
398         {
399             throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
400                                                     + "], that id already exists as a remote repository." );
401         }
402
403         return Boolean.TRUE;
404     }
405
406     private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
407         throws RepositoryAdminException
408     {
409         for ( String id : managedRepositoriesIds )
410         {
411             if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
412             {
413                 throw RepositoryAdminException.ofKey("repository_group.repository.not_found",id );
414             }
415         }
416     }
417
418     public ManagedRepositoryAdmin getManagedRepositoryAdmin()
419     {
420         return managedRepositoryAdmin;
421     }
422
423     public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
424     {
425         this.managedRepositoryAdmin = managedRepositoryAdmin;
426     }
427
428     private RepositoryGroup convertRepositoryGroupObject( org.apache.archiva.repository.RepositoryGroup group ) {
429         RepositoryGroup rg = new RepositoryGroup( group.getId( ), group.getRepositories().stream().map(r -> r.getId()).collect( Collectors.toList()) );
430         if (group.supportsFeature( IndexCreationFeature.class ))
431         {
432             IndexCreationFeature indexCreationFeature = group.getFeature( IndexCreationFeature.class ).get();
433             rg.setMergedIndexPath( indexCreationFeature.getIndexPath().getPath() );
434         }
435         rg.setCronExpression( group.getSchedulingDefinition() );
436         rg.setMergedIndexTtl( group.getMergedIndexTTL() );
437         rg.setLocation( group.getLocation().toString() );
438         return rg;
439     }
440 }