View Javadoc

1   package org.apache.archiva.admin.repository.managed;
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.RepositoryAdminException;
23  import org.apache.archiva.admin.model.beans.ManagedRepository;
24  import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25  import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
26  import org.apache.archiva.audit.AuditEvent;
27  import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
28  import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
29  import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
30  import org.apache.archiva.configuration.Configuration;
31  import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
32  import org.apache.archiva.configuration.ProxyConnectorConfiguration;
33  import org.apache.archiva.configuration.RepositoryGroupConfiguration;
34  import org.apache.archiva.metadata.repository.MetadataRepository;
35  import org.apache.archiva.metadata.repository.MetadataRepositoryException;
36  import org.apache.archiva.metadata.repository.RepositorySession;
37  import org.apache.archiva.metadata.repository.RepositorySessionFactory;
38  import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
39  import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
40  import org.apache.archiva.redback.role.RoleManager;
41  import org.apache.archiva.redback.role.RoleManagerException;
42  import org.apache.archiva.scheduler.repository.model.RepositoryArchivaTaskScheduler;
43  import org.apache.archiva.scheduler.repository.model.RepositoryTask;
44  import org.apache.archiva.security.common.ArchivaRoleConstants;
45  import org.apache.commons.io.FileUtils;
46  import org.apache.commons.lang.StringUtils;
47  import org.apache.maven.index.NexusIndexer;
48  import org.apache.maven.index.context.IndexCreator;
49  import org.apache.maven.index.context.IndexingContext;
50  import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  import org.springframework.stereotype.Service;
54  
55  import javax.annotation.PostConstruct;
56  import javax.annotation.PreDestroy;
57  import javax.inject.Inject;
58  import javax.inject.Named;
59  import java.io.File;
60  import java.io.IOException;
61  import java.net.MalformedURLException;
62  import java.util.ArrayList;
63  import java.util.Collections;
64  import java.util.HashMap;
65  import java.util.List;
66  import java.util.Map;
67  
68  /**
69   * FIXME review the staging mechanism to have a per user session one
70   *
71   * @author Olivier Lamy
72   */
73  @Service( "managedRepositoryAdmin#default" )
74  public class DefaultManagedRepositoryAdmin
75      extends AbstractRepositoryAdmin
76      implements ManagedRepositoryAdmin
77  {
78  
79      private Logger log = LoggerFactory.getLogger( getClass() );
80  
81      public static final String STAGE_REPO_ID_END = "-stage";
82  
83      @Inject
84      @Named( value = "archivaTaskScheduler#repository" )
85      private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
86  
87      /**
88       * FIXME: this could be multiple implementations and needs to be configured.
89       */
90      @Inject
91      private RepositorySessionFactory repositorySessionFactory;
92  
93      @Inject
94      private RepositoryStatisticsManager repositoryStatisticsManager;
95  
96      @Inject
97      private PlexusSisuBridge plexusSisuBridge;
98  
99      @Inject
100     private MavenIndexerUtils mavenIndexerUtils;
101 
102     @Inject
103     protected RoleManager roleManager;
104 
105     // fields
106     List<? extends IndexCreator> indexCreators;
107 
108     NexusIndexer indexer;
109 
110     @PostConstruct
111     public void initialize()
112         throws RepositoryAdminException, RoleManagerException
113     {
114         try
115         {
116             indexCreators = mavenIndexerUtils.getAllIndexCreators();
117             indexer = plexusSisuBridge.lookup( NexusIndexer.class );
118         }
119         catch ( PlexusSisuBridgeException e )
120         {
121             throw new RepositoryAdminException( e.getMessage(), e );
122         }
123         // initialize index context on start and check roles here
124         for ( ManagedRepository managedRepository : getManagedRepositories() )
125         {
126             createIndexContext( managedRepository );
127             addRepositoryRoles( managedRepository.getId() );
128 
129         }
130     }
131 
132     @PreDestroy
133     public void shutdown()
134         throws RepositoryAdminException
135     {
136         try
137         {
138             // close index on shutdown
139             for ( ManagedRepository managedRepository : getManagedRepositories() )
140             {
141                 IndexingContext context = indexer.getIndexingContexts().get( managedRepository.getId() );
142                 if ( context != null )
143                 {
144                     indexer.removeIndexingContext( context, false );
145                 }
146             }
147         }
148         catch ( IOException e )
149         {
150             throw new RepositoryAdminException( e.getMessage(), e );
151         }
152     }
153 
154     public List<ManagedRepository> getManagedRepositories()
155         throws RepositoryAdminException
156     {
157         List<ManagedRepositoryConfiguration> managedRepoConfigs =
158             getArchivaConfiguration().getConfiguration().getManagedRepositories();
159 
160         if ( managedRepoConfigs == null )
161         {
162             return Collections.emptyList();
163         }
164 
165         List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>( managedRepoConfigs.size() );
166 
167         for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
168         {
169             ManagedRepository repo =
170                 new ManagedRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getLocation(),
171                                        repoConfig.getLayout(), repoConfig.isSnapshots(), repoConfig.isReleases(),
172                                        repoConfig.isBlockRedeployments(), repoConfig.getRefreshCronExpression(),
173                                        repoConfig.getIndexDir(), repoConfig.isScanned(), repoConfig.getDaysOlder(),
174                                        repoConfig.getRetentionCount(), repoConfig.isDeleteReleasedSnapshots(),
175                                        repoConfig.isStageRepoNeeded() );
176             repo.setDescription( repoConfig.getDescription() );
177             repo.setSkipPackedIndexCreation( repoConfig.isSkipPackedIndexCreation() );
178             managedRepos.add( repo );
179         }
180 
181         return managedRepos;
182     }
183 
184     public Map<String, ManagedRepository> getManagedRepositoriesAsMap()
185         throws RepositoryAdminException
186     {
187         List<ManagedRepository> managedRepositories = getManagedRepositories();
188         Map<String, ManagedRepository> repositoriesMap =
189             new HashMap<String, ManagedRepository>( managedRepositories.size() );
190         for ( ManagedRepository managedRepository : managedRepositories )
191         {
192             repositoriesMap.put( managedRepository.getId(), managedRepository );
193         }
194         return repositoriesMap;
195     }
196 
197     public ManagedRepository getManagedRepository( String repositoryId )
198         throws RepositoryAdminException
199     {
200         List<ManagedRepository> repos = getManagedRepositories();
201         for ( ManagedRepository repo : repos )
202         {
203             if ( StringUtils.equals( repo.getId(), repositoryId ) )
204             {
205                 return repo;
206             }
207         }
208         return null;
209     }
210 
211     public Boolean addManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
212                                          AuditInformation auditInformation )
213         throws RepositoryAdminException
214     {
215 
216         getRepositoryCommonValidator().basicValidation( managedRepository, false );
217         getRepositoryCommonValidator().validateManagedRepository( managedRepository );
218         triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
219         Boolean res =
220             addManagedRepository( managedRepository.getId(), managedRepository.getLayout(), managedRepository.getName(),
221                                   managedRepository.getLocation(), managedRepository.isBlockRedeployments(),
222                                   managedRepository.isReleases(), managedRepository.isSnapshots(), needStageRepo,
223                                   managedRepository.getCronExpression(), managedRepository.getIndexDirectory(),
224                                   managedRepository.getDaysOlder(), managedRepository.getRetentionCount(),
225                                   managedRepository.isDeleteReleasedSnapshots(), managedRepository.getDescription(),
226                                   managedRepository.isSkipPackedIndexCreation(), managedRepository.isScanned(),
227                                   auditInformation, getArchivaConfiguration().getConfiguration() ) != null;
228 
229         createIndexContext( managedRepository );
230         return res;
231 
232     }
233 
234     private ManagedRepositoryConfiguration addManagedRepository( String repoId, String layout, String name,
235                                                                  String location, boolean blockRedeployments,
236                                                                  boolean releasesIncluded, boolean snapshotsIncluded,
237                                                                  boolean stageRepoNeeded, String cronExpression,
238                                                                  String indexDir, int daysOlder, int retentionCount,
239                                                                  boolean deteleReleasedSnapshots, String description,
240                                                                  boolean skipPackedIndexCreation, boolean scanned,
241                                                                  AuditInformation auditInformation,
242                                                                  Configuration config )
243         throws RepositoryAdminException
244     {
245 
246         ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
247 
248         repository.setId( repoId );
249         repository.setBlockRedeployments( blockRedeployments );
250         repository.setReleases( releasesIncluded );
251         repository.setSnapshots( snapshotsIncluded );
252         repository.setScanned( scanned );
253         repository.setName( name );
254         repository.setLocation( getRepositoryCommonValidator().removeExpressions( location ) );
255         repository.setLayout( layout );
256         repository.setRefreshCronExpression( cronExpression );
257         repository.setIndexDir( indexDir );
258         repository.setDaysOlder( daysOlder );
259         repository.setRetentionCount( retentionCount );
260         repository.setDeleteReleasedSnapshots( deteleReleasedSnapshots );
261         repository.setIndexDir( indexDir );
262         repository.setDescription( description );
263         repository.setSkipPackedIndexCreation( skipPackedIndexCreation );
264         repository.setStageRepoNeeded( stageRepoNeeded );
265 
266         try
267         {
268             addRepository( repository, config );
269             addRepositoryRoles( repository.getId() );
270 
271             if ( stageRepoNeeded )
272             {
273                 ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig( repository );
274                 addRepository( stagingRepository, config );
275                 addRepositoryRoles( stagingRepository.getId() );
276                 triggerAuditEvent( stagingRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
277             }
278         }
279         catch ( RoleManagerException e )
280         {
281             throw new RepositoryAdminException( "failed to add repository roles " + e.getMessage(), e );
282         }
283         catch ( IOException e )
284         {
285             throw new RepositoryAdminException( "failed to add repository " + e.getMessage(), e );
286         }
287 
288         saveConfiguration( config );
289 
290         //MRM-1342 Repository statistics report doesn't appear to be working correctly
291         //scan repository when adding of repository is successful
292         try
293         {
294             if ( scanned )
295             {
296                 scanRepository( repoId, true );
297             }
298 
299             // TODO need a better to define scanning or not for staged repo
300             if ( stageRepoNeeded && scanned )
301             {
302                 ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig( repository );
303                 scanRepository( stagingRepository.getId(), true );
304             }
305         }
306         catch ( Exception e )
307         {
308             log.warn( new StringBuilder( "Unable to scan repository [" ).append( repoId ).append( "]: " ).append(
309                 e.getMessage() ).toString(), e );
310         }
311 
312         return repository;
313     }
314 
315     public Boolean deleteManagedRepository( String repositoryId, AuditInformation auditInformation,
316                                             boolean deleteContent )
317         throws RepositoryAdminException
318     {
319         Configuration config = getArchivaConfiguration().getConfiguration();
320 
321         ManagedRepositoryConfiguration repository = config.findManagedRepositoryById( repositoryId );
322 
323         if ( repository == null )
324         {
325             throw new RepositoryAdminException( "A repository with that id does not exist" );
326         }
327 
328         triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_MANAGED_REPO, auditInformation );
329 
330         deleteManagedRepository( repository, deleteContent, config, false );
331 
332         // stage repo exists ?
333         ManagedRepositoryConfiguration stagingRepository =
334             getArchivaConfiguration().getConfiguration().findManagedRepositoryById( repositoryId + STAGE_REPO_ID_END );
335         if ( stagingRepository != null )
336         {
337             // do not trigger event when deleting the staged one
338             deleteManagedRepository( stagingRepository, deleteContent, config, true );
339         }
340 
341         try
342         {
343             saveConfiguration( config );
344         }
345         catch ( Exception e )
346         {
347             throw new RepositoryAdminException( "Error saving configuration for delete action" + e.getMessage(), e );
348         }
349 
350         return Boolean.TRUE;
351     }
352 
353     private Boolean deleteManagedRepository( ManagedRepositoryConfiguration repository, boolean deleteContent,
354                                              Configuration config, boolean stagedOne )
355         throws RepositoryAdminException
356     {
357 
358         try
359         {
360             NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
361 
362             IndexingContext context = nexusIndexer.getIndexingContexts().get( repository.getId() );
363             if ( context != null )
364             {
365                 // delete content only if directory exists
366                 nexusIndexer.removeIndexingContext( context,
367                                                     deleteContent && context.getIndexDirectoryFile().exists() );
368             }
369         }
370         catch ( PlexusSisuBridgeException e )
371         {
372             throw new RepositoryAdminException( e.getMessage(), e );
373         }
374         catch ( IOException e )
375         {
376             throw new RepositoryAdminException( e.getMessage(), e );
377         }
378         if ( !stagedOne )
379         {
380             RepositorySession repositorySession = getRepositorySessionFactory().createSession();
381             try
382             {
383                 MetadataRepository metadataRepository = repositorySession.getRepository();
384                 metadataRepository.removeRepository( repository.getId() );
385                 log.debug( "call repositoryStatisticsManager.deleteStatistics" );
386                 getRepositoryStatisticsManager().deleteStatistics( metadataRepository, repository.getId() );
387                 repositorySession.save();
388             }
389             catch ( MetadataRepositoryException e )
390             {
391                 //throw new RepositoryAdminException( e.getMessage(), e );
392                 log.warn( "skip error during removing repository from MetadatRepository:" + e.getMessage(), e );
393             }
394             finally
395             {
396                 repositorySession.close();
397             }
398         }
399         config.removeManagedRepository( repository );
400 
401         if ( deleteContent )
402         {
403             // TODO could be async ? as directory can be huge
404             File dir = new File( repository.getLocation() );
405             if ( !FileUtils.deleteQuietly( dir ) )
406             {
407                 throw new RepositoryAdminException( "Cannot delete repository " + dir );
408             }
409         }
410 
411         // olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException
412         List<ProxyConnectorConfiguration> proxyConnectors =
413             new ArrayList<ProxyConnectorConfiguration>( config.getProxyConnectors() );
414         for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
415         {
416             if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) )
417             {
418                 config.removeProxyConnector( proxyConnector );
419             }
420         }
421 
422         Map<String, List<String>> repoToGroupMap = config.getRepositoryToGroupMap();
423         if ( repoToGroupMap != null )
424         {
425             if ( repoToGroupMap.containsKey( repository.getId() ) )
426             {
427                 List<String> repoGroups = repoToGroupMap.get( repository.getId() );
428                 for ( String repoGroup : repoGroups )
429                 {
430                     // copy to prevent UnsupportedOperationException
431                     RepositoryGroupConfiguration repositoryGroupConfiguration =
432                         config.findRepositoryGroupById( repoGroup );
433                     List<String> repos = new ArrayList<String>( repositoryGroupConfiguration.getRepositories() );
434                     config.removeRepositoryGroup( repositoryGroupConfiguration );
435                     repos.remove( repository.getId() );
436                     repositoryGroupConfiguration.setRepositories( repos );
437                     config.addRepositoryGroup( repositoryGroupConfiguration );
438                 }
439             }
440         }
441 
442         try
443         {
444             removeRepositoryRoles( repository );
445         }
446         catch ( RoleManagerException e )
447         {
448             throw new RepositoryAdminException(
449                 "fail to remove repository roles for repository " + repository.getId() + " : " + e.getMessage(), e );
450         }
451 
452         saveConfiguration( config );
453 
454         return Boolean.TRUE;
455     }
456 
457 
458     public Boolean updateManagedRepository( ManagedRepository managedRepository, boolean needStageRepo,
459                                             AuditInformation auditInformation, boolean resetStats )
460         throws RepositoryAdminException
461     {
462 
463         log.debug( "updateManagedConfiguration repo {} needStage {} resetStats {} ", managedRepository, needStageRepo,
464                    resetStats );
465 
466         // Ensure that the fields are valid.
467 
468         getRepositoryCommonValidator().basicValidation( managedRepository, true );
469 
470         getRepositoryCommonValidator().validateManagedRepository( managedRepository );
471 
472         Configuration configuration = getArchivaConfiguration().getConfiguration();
473 
474         ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( managedRepository.getId() );
475 
476         boolean updateIndexContext = false;
477 
478         if ( toremove != null )
479         {
480             configuration.removeManagedRepository( toremove );
481 
482             updateIndexContext = !StringUtils.equals( toremove.getIndexDir(), managedRepository.getIndexDirectory() );
483         }
484 
485         ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig( toremove );
486 
487         // TODO remove content from old if path has changed !!!!!
488 
489         if ( stagingRepository != null )
490         {
491             configuration.removeManagedRepository( stagingRepository );
492         }
493 
494         ManagedRepositoryConfiguration managedRepositoryConfiguration =
495             addManagedRepository( managedRepository.getId(), managedRepository.getLayout(), managedRepository.getName(),
496                                   managedRepository.getLocation(), managedRepository.isBlockRedeployments(),
497                                   managedRepository.isReleases(), managedRepository.isSnapshots(), needStageRepo,
498                                   managedRepository.getCronExpression(), managedRepository.getIndexDirectory(),
499                                   managedRepository.getDaysOlder(), managedRepository.getRetentionCount(),
500                                   managedRepository.isDeleteReleasedSnapshots(), managedRepository.getDescription(),
501                                   managedRepository.isSkipPackedIndexCreation(), managedRepository.isScanned(),
502                                   auditInformation, getArchivaConfiguration().getConfiguration() );
503 
504         // Save the repository configuration.
505         RepositorySession repositorySession = getRepositorySessionFactory().createSession();
506 
507         try
508         {
509             triggerAuditEvent( managedRepositoryConfiguration.getId(), null, AuditEvent.MODIFY_MANAGED_REPO,
510                                auditInformation );
511 
512             saveConfiguration( this.getArchivaConfiguration().getConfiguration() );
513             if ( resetStats )
514             {
515                 log.debug( "call repositoryStatisticsManager.deleteStatistics" );
516                 getRepositoryStatisticsManager().deleteStatistics( repositorySession.getRepository(),
517                                                                    managedRepositoryConfiguration.getId() );
518                 repositorySession.save();
519             }
520 
521         }
522         catch ( MetadataRepositoryException e )
523         {
524             throw new RepositoryAdminException( e.getMessage(), e );
525         }
526         finally
527         {
528             repositorySession.close();
529         }
530 
531         if ( updateIndexContext )
532         {
533             try
534             {
535                 IndexingContext indexingContext = indexer.getIndexingContexts().get( managedRepository.getId() );
536                 if ( indexingContext != null )
537                 {
538                     indexer.removeIndexingContext( indexingContext, true );
539                 }
540 
541                 // delete directory too as only content is deleted
542                 File indexDirectory = indexingContext.getIndexDirectoryFile();
543                 FileUtils.deleteDirectory( indexDirectory );
544 
545                 createIndexContext( managedRepository );
546             }
547             catch ( IOException e )
548             {
549                 throw new RepositoryAdminException( e.getMessage(), e );
550             }
551         }
552 
553         return true;
554     }
555 
556     //--------------------------
557     // utils methods
558     //--------------------------
559 
560 
561     protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
562         throws RepositoryAdminException, IOException
563     {
564         // Normalize the path
565         File file = new File( repository.getLocation() );
566         if ( !file.isAbsolute() )
567         {
568             // add appserver.base/repositories
569             file = new File( getRegistry().getString( "appserver.base" ) + File.separatorChar + "repositories",
570                              repository.getLocation() );
571         }
572         repository.setLocation( file.getCanonicalPath() );
573         if ( !file.exists() )
574         {
575             file.mkdirs();
576         }
577         if ( !file.exists() || !file.isDirectory() )
578         {
579             throw new RepositoryAdminException(
580                 "Unable to add repository - no write access, can not create the root directory: " + file );
581         }
582 
583         configuration.addManagedRepository( repository );
584 
585     }
586 
587     public IndexingContext createIndexContext( ManagedRepository repository )
588         throws RepositoryAdminException
589     {
590 
591         IndexingContext context = indexer.getIndexingContexts().get( repository.getId() );
592 
593         if ( context != null )
594         {
595             log.debug( "skip creating repository indexingContent with id {} as already exists", repository.getId() );
596             return context;
597         }
598 
599         // take care first about repository location as can be relative
600         File repositoryDirectory = new File( repository.getLocation() );
601 
602         if ( !repositoryDirectory.isAbsolute() )
603         {
604             repositoryDirectory =
605                 new File( getRegistry().getString( "appserver.base" ) + File.separatorChar + "repositories",
606                           repository.getLocation() );
607         }
608 
609         if ( !repositoryDirectory.exists() )
610         {
611             repositoryDirectory.mkdirs();
612         }
613 
614         try
615         {
616 
617             String indexDir = repository.getIndexDirectory();
618             //File managedRepository = new File( repository.getLocation() );
619 
620             File indexDirectory = null;
621             if ( StringUtils.isNotBlank( indexDir ) )
622             {
623                 indexDirectory = new File( repository.getIndexDirectory() );
624                 // not absolute so create it in repository directory
625                 if ( !indexDirectory.isAbsolute() )
626                 {
627                     indexDirectory = new File( repositoryDirectory, repository.getIndexDirectory() );
628                 }
629                 repository.setIndexDirectory( indexDirectory.getAbsolutePath() );
630             }
631             else
632             {
633                 indexDirectory = new File( repositoryDirectory, ".indexer" );
634                 if ( !repositoryDirectory.isAbsolute() )
635                 {
636                     indexDirectory = new File( repositoryDirectory, ".indexer" );
637                 }
638                 repository.setIndexDirectory( indexDirectory.getAbsolutePath() );
639             }
640 
641             if ( !indexDirectory.exists() )
642             {
643                 indexDirectory.mkdirs();
644             }
645 
646             context = indexer.getIndexingContexts().get( repository.getId() );
647 
648             if ( context == null )
649             {
650                 context = indexer.addIndexingContext( repository.getId(), repository.getId(), repositoryDirectory,
651                                                       indexDirectory,
652                                                       repositoryDirectory.toURI().toURL().toExternalForm(),
653                                                       indexDirectory.toURI().toURL().toString(), indexCreators );
654 
655                 context.setSearchable( repository.isScanned() );
656             }
657             return context;
658         }
659         catch ( MalformedURLException e )
660         {
661             throw new RepositoryAdminException( e.getMessage(), e );
662         }
663         catch ( IOException e )
664         {
665             throw new RepositoryAdminException( e.getMessage(), e );
666         }
667         catch ( UnsupportedExistingLuceneIndexException e )
668         {
669             throw new RepositoryAdminException( e.getMessage(), e );
670         }
671     }
672 
673     private ManagedRepositoryConfiguration getStageRepoConfig( ManagedRepositoryConfiguration repository )
674     {
675         ManagedRepositoryConfiguration stagingRepository = new ManagedRepositoryConfiguration();
676         stagingRepository.setId( repository.getId() + STAGE_REPO_ID_END );
677         stagingRepository.setLayout( repository.getLayout() );
678         stagingRepository.setName( repository.getName() + STAGE_REPO_ID_END );
679         stagingRepository.setBlockRedeployments( repository.isBlockRedeployments() );
680         stagingRepository.setDaysOlder( repository.getDaysOlder() );
681         stagingRepository.setDeleteReleasedSnapshots( repository.isDeleteReleasedSnapshots() );
682 
683         String path = repository.getLocation();
684         int lastIndex = path.replace( '\\', '/' ).lastIndexOf( '/' );
685         stagingRepository.setLocation( path.substring( 0, lastIndex ) + "/" + stagingRepository.getId() );
686 
687         if ( StringUtils.isNotBlank( repository.getIndexDir() ) )
688         {
689             File indexDir = new File( repository.getIndexDir() );
690             // in case of absolute dir do not use the same
691             if ( indexDir.isAbsolute() )
692             {
693                 stagingRepository.setIndexDir( stagingRepository.getLocation() + "/.index" );
694             }
695             else
696             {
697                 stagingRepository.setIndexDir( repository.getIndexDir() );
698             }
699         }
700         stagingRepository.setRefreshCronExpression( repository.getRefreshCronExpression() );
701         stagingRepository.setReleases( repository.isReleases() );
702         stagingRepository.setRetentionCount( repository.getRetentionCount() );
703         stagingRepository.setScanned( repository.isScanned() );
704         stagingRepository.setSnapshots( repository.isSnapshots() );
705         stagingRepository.setSkipPackedIndexCreation( repository.isSkipPackedIndexCreation() );
706         // do not duplicate description
707         //stagingRepository.getDescription("")
708         return stagingRepository;
709     }
710 
711     public Boolean scanRepository( String repositoryId, boolean fullScan )
712     {
713         if ( getRepositoryTaskScheduler().isProcessingRepositoryTask( repositoryId ) )
714         {
715             log.info( "scanning of repository with id {} already scheduled", repositoryId );
716         }
717         RepositoryTask task = new RepositoryTask();
718         task.setRepositoryId( repositoryId );
719         task.setScanAll( fullScan );
720         try
721         {
722             getRepositoryTaskScheduler().queueTask( task );
723         }
724         catch ( TaskQueueException e )
725         {
726             log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
727             return false;
728         }
729         return true;
730     }
731 
732 
733     private void addRepositoryRoles( ManagedRepository newRepository )
734         throws RoleManagerException
735     {
736         String repoId = newRepository.getId();
737 
738         // TODO: double check these are configured on start up
739         // TODO: belongs in the business logic
740 
741         if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
742         {
743             getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
744         }
745 
746         if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
747         {
748             getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
749         }
750     }
751 
752 
753     private void addRepositoryRoles( String repoId )
754         throws RoleManagerException
755     {
756         // TODO: double check these are configured on start up
757         // TODO: belongs in the business logic
758 
759         if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
760         {
761             getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
762         }
763 
764         if ( !getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
765         {
766             getRoleManager().createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
767         }
768     }
769 
770     protected void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
771         throws RoleManagerException
772     {
773         String repoId = existingRepository.getId();
774 
775         if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId ) )
776         {
777             getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, repoId );
778         }
779 
780         if ( getRoleManager().templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId ) )
781         {
782             getRoleManager().removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId );
783         }
784 
785         log.debug( "removed user roles associated with repository {}", repoId );
786     }
787 
788     //--------------------------
789     // setters/getters
790     //--------------------------
791 
792 
793     public RoleManager getRoleManager()
794     {
795         return roleManager;
796     }
797 
798     public void setRoleManager( RoleManager roleManager )
799     {
800         this.roleManager = roleManager;
801     }
802 
803     public RepositoryStatisticsManager getRepositoryStatisticsManager()
804     {
805         return repositoryStatisticsManager;
806     }
807 
808     public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
809     {
810         this.repositoryStatisticsManager = repositoryStatisticsManager;
811     }
812 
813     public RepositorySessionFactory getRepositorySessionFactory()
814     {
815         return repositorySessionFactory;
816     }
817 
818     public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
819     {
820         this.repositorySessionFactory = repositorySessionFactory;
821     }
822 
823 
824     public RepositoryArchivaTaskScheduler getRepositoryTaskScheduler()
825     {
826         return repositoryTaskScheduler;
827     }
828 
829     public void setRepositoryTaskScheduler( RepositoryArchivaTaskScheduler repositoryTaskScheduler )
830     {
831         this.repositoryTaskScheduler = repositoryTaskScheduler;
832     }
833 
834     public PlexusSisuBridge getPlexusSisuBridge()
835     {
836         return plexusSisuBridge;
837     }
838 
839     public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
840     {
841         this.plexusSisuBridge = plexusSisuBridge;
842     }
843 
844     public MavenIndexerUtils getMavenIndexerUtils()
845     {
846         return mavenIndexerUtils;
847     }
848 
849     public void setMavenIndexerUtils( MavenIndexerUtils mavenIndexerUtils )
850     {
851         this.mavenIndexerUtils = mavenIndexerUtils;
852     }
853 
854     public NexusIndexer getIndexer()
855     {
856         return indexer;
857     }
858 
859     public void setIndexer( NexusIndexer indexer )
860     {
861         this.indexer = indexer;
862     }
863 
864     public List<? extends IndexCreator> getIndexCreators()
865     {
866         return indexCreators;
867     }
868 
869     public void setIndexCreators( List<? extends IndexCreator> indexCreators )
870     {
871         this.indexCreators = indexCreators;
872     }
873 }