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.admin.model.RepositoryAdminException;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.ConfigurationEvent;
28 import org.apache.archiva.configuration.ConfigurationListener;
29 import org.apache.archiva.indexer.UnsupportedBaseContextException;
30 import org.apache.archiva.proxy.maven.WagonFactory;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.archiva.repository.features.RemoteIndexFeature;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.apache.maven.index.packer.IndexPacker;
36 import org.apache.maven.index.updater.IndexUpdater;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.scheduling.TaskScheduler;
40 import org.springframework.scheduling.support.CronTrigger;
41 import org.springframework.stereotype.Service;
43 import javax.annotation.PostConstruct;
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import java.util.Date;
47 import java.util.List;
48 import java.util.concurrent.CopyOnWriteArrayList;
51 * @author Olivier Lamy
54 @Service( "downloadRemoteIndexScheduler#default" )
55 public class DefaultDownloadRemoteIndexScheduler
56 implements ConfigurationListener, DownloadRemoteIndexScheduler
59 private Logger log = LoggerFactory.getLogger( getClass() );
62 @Named( value = "taskScheduler#indexDownloadRemote" )
63 private TaskScheduler taskScheduler;
66 RepositoryRegistry repositoryRegistry;
69 private ArchivaConfiguration archivaConfiguration;
72 private WagonFactory wagonFactory;
75 private IndexUpdater indexUpdater;
78 private IndexPacker indexPacker;
81 private ProxyRegistry proxyRegistry;
83 // store ids about currently running remote download : updated in DownloadRemoteIndexTask
84 private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
89 DownloadRemoteIndexException, UnsupportedBaseContextException {
90 archivaConfiguration.addListener( this );
91 // TODO add indexContexts even if null
93 for ( org.apache.archiva.repository.RemoteRepository remoteRepository : repositoryRegistry.getRemoteRepositories() )
95 String contextKey = "remote-" + remoteRepository.getId();
96 IndexingContext context = remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
97 if ( context == null )
101 RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
104 // TODO record jobs from configuration
105 if ( rif.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
106 remoteRepository.getSchedulingDefinition() ) )
108 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
109 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
117 public void configurationEvent( ConfigurationEvent event )
119 // TODO remove jobs and add again
124 public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
125 throws DownloadRemoteIndexException
127 org.apache.archiva.repository.RemoteRepository remoteRepo = repositoryRegistry.getRemoteRepository(repositoryId);
129 if ( remoteRepo == null )
131 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
134 if (!remoteRepo.supportsFeature(RemoteIndexFeature.class)) {
135 log.warn("ignore scheduleDownloadRemote for repo with id {}. Does not support remote index.", repositoryId);
138 RemoteIndexFeature rif = remoteRepo.getFeature(RemoteIndexFeature.class).get();
139 NetworkProxy networkProxy = null;
140 if ( StringUtils.isNotBlank( rif.getProxyId() ) )
142 networkProxy = proxyRegistry.getNetworkProxy( rif.getProxyId() );
143 if ( networkProxy == null )
146 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
151 DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest() //
152 .setRemoteRepository( remoteRepo ) //
153 .setNetworkProxy( networkProxy ) //
154 .setFullDownload( fullDownload ) //
155 .setWagonFactory( wagonFactory ) //
156 .setIndexUpdater( indexUpdater ) //
157 .setIndexPacker( this.indexPacker );
161 log.info( "schedule download remote index for repository {}", remoteRepo.getId() );
163 taskScheduler.schedule(
164 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
169 log.info( "schedule download remote index for repository {} with cron expression {}",
170 remoteRepo.getId(), remoteRepo.getSchedulingDefinition());
173 CronTrigger cronTrigger = new CronTrigger( remoteRepo.getSchedulingDefinition());
174 taskScheduler.schedule(
175 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
178 catch ( IllegalArgumentException e )
180 log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
183 if ( rif.isDownloadRemoteIndexOnStartup() )
186 "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
187 remoteRepo.getId() );
188 taskScheduler.schedule(
189 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
196 public TaskScheduler getTaskScheduler()
198 return taskScheduler;
201 public void setTaskScheduler( TaskScheduler taskScheduler )
203 this.taskScheduler = taskScheduler;
207 public List<String> getRunningRemoteDownloadIds()
209 return runningRemoteDownloadIds;