]> source.dussan.org Git - archiva.git/blob
ae6c4fd3b94c1f9eb71bb7a7875c22b4552f3e9f
[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 EntityNotFoundException.ofMessage(
233                     "Repository group with id " + repositoryGroupId + " doesn't not exists so cannot add repository to it", repositoryGroupId );
234         }
235
236         if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
237             throw RepositoryAdminException.ofKey("repository_group.not_editable",repositoryGroupId);
238         }
239         EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
240         if ( editableRepositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId())) )
241         {
242             throw new EntityExistsException(
243                 "Repository group 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 EntityNotFoundException.ofMessage("Repository with id "+repositoryId+" does not exist", repositoryId );
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 EntityNotFoundException.ofMessage( "Repository group with id " + repositoryGroupId
269                                                     + " doesn't not exists so cannot remove repository from it", repositoryGroupId );
270         }
271
272         if ( !repositoryGroup.getRepositories().stream().anyMatch( repo -> repositoryId.equals(repo.getId()) ) )
273         {
274             throw EntityNotFoundException.ofMessage(
275                 "repositoryGroup with id " + repositoryGroupId + " doesn't not contains repository with id"
276                     + repositoryId, repositoryId
277             );
278         }
279
280         if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
281             throw RepositoryAdminException.ofKey("repository_group.not_editable",repositoryGroupId);
282         }
283         EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
284
285         editableRepositoryGroup.removeRepository( repositoryId );
286         try {
287             repositoryRegistry.putRepositoryGroup(editableRepositoryGroup);
288         } catch (RepositoryException e) {
289             throw new RepositoryAdminException("Could not store repository group " + repositoryGroupId, e);
290         }
291         triggerAuditEvent( repositoryGroup.getId(), null, AuditEvent.DELETE_REPO_FROM_GROUP, auditInformation );
292         return Boolean.TRUE;
293     }
294
295     @Override
296     public Map<String, RepositoryGroup> getRepositoryGroupsAsMap()
297         throws RepositoryAdminException
298     {
299         List<RepositoryGroup> repositoriesGroups = getRepositoriesGroups();
300         Map<String, RepositoryGroup> map = new HashMap<>( repositoriesGroups.size() );
301         for ( RepositoryGroup repositoryGroup : repositoriesGroups )
302         {
303             map.put( repositoryGroup.getId(), repositoryGroup );
304         }
305         return map;
306     }
307
308     @Override
309     public Map<String, List<String>> getGroupToRepositoryMap()
310         throws RepositoryAdminException
311     {
312
313         Map<String, List<String>> map = new HashMap<>();
314
315         for ( ManagedRepository repo : getManagedRepositoryAdmin().getManagedRepositories() )
316         {
317             for ( RepositoryGroup group : getRepositoriesGroups() )
318             {
319                 if ( !group.getRepositories().contains( repo.getId() ) )
320                 {
321                     String groupId = group.getId();
322                     List<String> repos = map.get( groupId );
323                     if ( repos == null )
324                     {
325                         repos = new ArrayList<>();
326                         map.put( groupId, repos );
327                     }
328                     repos.add( repo.getId() );
329                 }
330             }
331         }
332         return map;
333     }
334
335     @Override
336     public Map<String, List<String>> getRepositoryToGroupMap()
337         throws RepositoryAdminException
338     {
339         Map<String, List<String>> map = new HashMap<>();
340
341         for ( RepositoryGroup group : getRepositoriesGroups() )
342         {
343             for ( String repositoryId : group.getRepositories() )
344             {
345                 List<String> groups = map.get( repositoryId );
346                 if ( groups == null )
347                 {
348                     groups = new ArrayList<>();
349                     map.put( repositoryId, groups );
350                 }
351                 groups.add( group.getId() );
352             }
353         }
354         return map;
355     }
356
357     public Boolean validateRepositoryGroup( RepositoryGroup repositoryGroup, boolean updateMode )
358         throws RepositoryAdminException
359     {
360         String repoGroupId = repositoryGroup.getId();
361         if ( StringUtils.isBlank( repoGroupId ) )
362         {
363             throw RepositoryAdminException.ofKey("repository_group.id.empty" );
364         }
365
366         if ( repoGroupId.length() > 100 )
367         {
368             throw RepositoryAdminException.ofKey("repository_group.id.max_length",repoGroupId, Integer.toString( 100 ));
369
370         }
371
372         Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
373         if ( !matcher.matches() )
374         {
375             throw RepositoryAdminException.ofKey("repository_group.id.invalid_chars","alphanumeric, '.', '-','_'" );
376         }
377
378         if ( repositoryGroup.getMergedIndexTtl() <= 0 )
379         {
380             throw RepositoryAdminException.ofKey("repository_group.merged_index_ttl.min","0" );
381         }
382
383         Configuration configuration = getArchivaConfiguration().getConfiguration();
384
385         if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
386         {
387             if ( !updateMode )
388             {
389                 throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
390                                                         + "], that id already exists as a repository group." );
391             }
392         }
393         else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
394         {
395             throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
396                                                     + "], that id already exists as a managed repository." );
397         }
398         else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
399         {
400             throw new EntityExistsException( "Unable to add new repository group with id [" + repoGroupId
401                                                     + "], that id already exists as a remote repository." );
402         }
403
404         return Boolean.TRUE;
405     }
406
407     private void validateManagedRepositoriesExists( List<String> managedRepositoriesIds )
408         throws RepositoryAdminException
409     {
410         for ( String id : managedRepositoriesIds )
411         {
412             if ( getManagedRepositoryAdmin().getManagedRepository( id ) == null )
413             {
414                 throw RepositoryAdminException.ofKey("repository_group.repository.not_found",id );
415             }
416         }
417     }
418
419     public ManagedRepositoryAdmin getManagedRepositoryAdmin()
420     {
421         return managedRepositoryAdmin;
422     }
423
424     public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
425     {
426         this.managedRepositoryAdmin = managedRepositoryAdmin;
427     }
428
429     private RepositoryGroup convertRepositoryGroupObject( org.apache.archiva.repository.RepositoryGroup group ) {
430         RepositoryGroup rg = new RepositoryGroup( group.getId( ), group.getRepositories().stream().map(r -> r.getId()).collect( Collectors.toList()) );
431         if (group.supportsFeature( IndexCreationFeature.class ))
432         {
433             IndexCreationFeature indexCreationFeature = group.getFeature( IndexCreationFeature.class ).get();
434             rg.setMergedIndexPath( indexCreationFeature.getIndexPath().getPath() );
435         }
436         rg.setCronExpression( group.getSchedulingDefinition() );
437         rg.setMergedIndexTtl( group.getMergedIndexTTL() );
438         rg.setLocation( group.getLocation().toString() );
439         return rg;
440     }
441 }