1 package org.apache.archiva.maven.scheduler.indexing;
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
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 import org.apache.archiva.proxy.ProxyRegistry;
21 import org.apache.archiva.proxy.model.NetworkProxy;
22 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
23 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.ConfigurationEvent;
26 import org.apache.archiva.configuration.ConfigurationListener;
27 import org.apache.archiva.indexer.UnsupportedBaseContextException;
28 import org.apache.archiva.maven.common.proxy.WagonFactory;
29 import org.apache.archiva.repository.RepositoryRegistry;
30 import org.apache.archiva.repository.features.RemoteIndexFeature;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.maven.index.context.IndexingContext;
33 import org.apache.maven.index.packer.IndexPacker;
34 import org.apache.maven.index.updater.IndexUpdater;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.scheduling.TaskScheduler;
38 import org.springframework.scheduling.support.CronTrigger;
39 import org.springframework.stereotype.Service;
41 import javax.annotation.PostConstruct;
42 import javax.inject.Inject;
43 import javax.inject.Named;
44 import java.util.Date;
45 import java.util.List;
46 import java.util.concurrent.CopyOnWriteArrayList;
49 * @author Olivier Lamy
52 @Service( "downloadRemoteIndexScheduler#default" )
53 public class DefaultDownloadRemoteIndexScheduler
54 implements ConfigurationListener, DownloadRemoteIndexScheduler
57 private Logger log = LoggerFactory.getLogger( getClass() );
60 @Named( value = "taskScheduler#indexDownloadRemote" )
61 private TaskScheduler taskScheduler;
64 RepositoryRegistry repositoryRegistry;
67 private ArchivaConfiguration archivaConfiguration;
70 private WagonFactory wagonFactory;
73 private IndexUpdater indexUpdater;
76 private IndexPacker indexPacker;
79 private ProxyRegistry proxyRegistry;
81 // store ids about currently running remote download : updated in DownloadRemoteIndexTask
82 private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
87 DownloadRemoteIndexException, UnsupportedBaseContextException {
88 archivaConfiguration.addListener( this );
89 // TODO add indexContexts even if null
91 for ( org.apache.archiva.repository.RemoteRepository remoteRepository : repositoryRegistry.getRemoteRepositories() )
93 String contextKey = "remote-" + remoteRepository.getId();
94 IndexingContext context = remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
95 if ( context == null )
99 RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
102 // TODO record jobs from configuration
103 if ( rif.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
104 remoteRepository.getSchedulingDefinition() ) )
106 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
107 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
115 public void configurationEvent( ConfigurationEvent event )
117 // TODO remove jobs and add again
122 public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
123 throws DownloadRemoteIndexException
125 org.apache.archiva.repository.RemoteRepository remoteRepo = repositoryRegistry.getRemoteRepository(repositoryId);
127 if ( remoteRepo == null )
129 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
132 if (!remoteRepo.supportsFeature(RemoteIndexFeature.class)) {
133 log.warn("ignore scheduleDownloadRemote for repo with id {}. Does not support remote index.", repositoryId);
136 RemoteIndexFeature rif = remoteRepo.getFeature(RemoteIndexFeature.class).get();
137 NetworkProxy networkProxy = null;
138 if ( StringUtils.isNotBlank( rif.getProxyId() ) )
140 networkProxy = proxyRegistry.getNetworkProxy( rif.getProxyId() );
141 if ( networkProxy == null )
144 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
149 DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest() //
150 .setRemoteRepository( remoteRepo ) //
151 .setNetworkProxy( networkProxy ) //
152 .setFullDownload( fullDownload ) //
153 .setWagonFactory( wagonFactory ) //
154 .setIndexUpdater( indexUpdater ) //
155 .setIndexPacker( this.indexPacker );
159 log.info( "schedule download remote index for repository {}", remoteRepo.getId() );
161 taskScheduler.schedule(
162 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
167 log.info( "schedule download remote index for repository {} with cron expression {}",
168 remoteRepo.getId(), remoteRepo.getSchedulingDefinition());
171 CronTrigger cronTrigger = new CronTrigger( remoteRepo.getSchedulingDefinition());
172 taskScheduler.schedule(
173 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
176 catch ( IllegalArgumentException e )
178 log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
181 if ( rif.isDownloadRemoteIndexOnStartup() )
184 "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
185 remoteRepo.getId() );
186 taskScheduler.schedule(
187 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
194 public TaskScheduler getTaskScheduler()
196 return taskScheduler;
199 public void setTaskScheduler( TaskScheduler taskScheduler )
201 this.taskScheduler = taskScheduler;
205 public List<String> getRunningRemoteDownloadIds()
207 return runningRemoteDownloadIds;