1 package org.apache.archiva.scheduler.indexing.maven;
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.proxy.ProxyRegistry;
22 import org.apache.archiva.proxy.model.NetworkProxy;
23 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
24 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.ConfigurationEvent;
27 import org.apache.archiva.configuration.ConfigurationListener;
28 import org.apache.archiva.indexer.UnsupportedBaseContextException;
29 import org.apache.archiva.proxy.maven.WagonFactory;
30 import org.apache.archiva.repository.RepositoryRegistry;
31 import org.apache.archiva.repository.features.RemoteIndexFeature;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.maven.index.context.IndexingContext;
34 import org.apache.maven.index.packer.IndexPacker;
35 import org.apache.maven.index.updater.IndexUpdater;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.scheduling.TaskScheduler;
39 import org.springframework.scheduling.support.CronTrigger;
40 import org.springframework.stereotype.Service;
42 import javax.annotation.PostConstruct;
43 import javax.inject.Inject;
44 import javax.inject.Named;
45 import java.util.Date;
46 import java.util.List;
47 import java.util.concurrent.CopyOnWriteArrayList;
50 * @author Olivier Lamy
53 @Service( "downloadRemoteIndexScheduler#default" )
54 public class DefaultDownloadRemoteIndexScheduler
55 implements ConfigurationListener, DownloadRemoteIndexScheduler
58 private Logger log = LoggerFactory.getLogger( getClass() );
61 @Named( value = "taskScheduler#indexDownloadRemote" )
62 private TaskScheduler taskScheduler;
65 RepositoryRegistry repositoryRegistry;
68 private ArchivaConfiguration archivaConfiguration;
71 private WagonFactory wagonFactory;
74 private IndexUpdater indexUpdater;
77 private IndexPacker indexPacker;
80 private ProxyRegistry proxyRegistry;
82 // store ids about currently running remote download : updated in DownloadRemoteIndexTask
83 private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
88 DownloadRemoteIndexException, UnsupportedBaseContextException {
89 archivaConfiguration.addListener( this );
90 // TODO add indexContexts even if null
92 for ( org.apache.archiva.repository.RemoteRepository remoteRepository : repositoryRegistry.getRemoteRepositories() )
94 String contextKey = "remote-" + remoteRepository.getId();
95 IndexingContext context = remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
96 if ( context == null )
100 RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
103 // TODO record jobs from configuration
104 if ( rif.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
105 remoteRepository.getSchedulingDefinition() ) )
107 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
108 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
116 public void configurationEvent( ConfigurationEvent event )
118 // TODO remove jobs and add again
123 public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
124 throws DownloadRemoteIndexException
126 org.apache.archiva.repository.RemoteRepository remoteRepo = repositoryRegistry.getRemoteRepository(repositoryId);
128 if ( remoteRepo == null )
130 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
133 if (!remoteRepo.supportsFeature(RemoteIndexFeature.class)) {
134 log.warn("ignore scheduleDownloadRemote for repo with id {}. Does not support remote index.", repositoryId);
137 RemoteIndexFeature rif = remoteRepo.getFeature(RemoteIndexFeature.class).get();
138 NetworkProxy networkProxy = null;
139 if ( StringUtils.isNotBlank( rif.getProxyId() ) )
141 networkProxy = proxyRegistry.getNetworkProxy( rif.getProxyId() );
142 if ( networkProxy == null )
145 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
150 DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest() //
151 .setRemoteRepository( remoteRepo ) //
152 .setNetworkProxy( networkProxy ) //
153 .setFullDownload( fullDownload ) //
154 .setWagonFactory( wagonFactory ) //
155 .setIndexUpdater( indexUpdater ) //
156 .setIndexPacker( this.indexPacker );
160 log.info( "schedule download remote index for repository {}", remoteRepo.getId() );
162 taskScheduler.schedule(
163 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
168 log.info( "schedule download remote index for repository {} with cron expression {}",
169 remoteRepo.getId(), remoteRepo.getSchedulingDefinition());
172 CronTrigger cronTrigger = new CronTrigger( remoteRepo.getSchedulingDefinition());
173 taskScheduler.schedule(
174 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
177 catch ( IllegalArgumentException e )
179 log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
182 if ( rif.isDownloadRemoteIndexOnStartup() )
185 "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
186 remoteRepo.getId() );
187 taskScheduler.schedule(
188 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
195 public TaskScheduler getTaskScheduler()
197 return taskScheduler;
200 public void setTaskScheduler( TaskScheduler taskScheduler )
202 this.taskScheduler = taskScheduler;
206 public List<String> getRunningRemoteDownloadIds()
208 return runningRemoteDownloadIds;