1 package org.apache.archiva.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
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.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.NetworkProxy;
23 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
24 import org.apache.archiva.common.ArchivaException;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
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.common.WagonFactory;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.archiva.repository.features.RemoteIndexFeature;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
36 import org.apache.maven.index.packer.IndexPacker;
37 import org.apache.maven.index.updater.IndexUpdater;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.scheduling.TaskScheduler;
41 import org.springframework.scheduling.support.CronTrigger;
42 import org.springframework.stereotype.Service;
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
47 import java.io.IOException;
48 import java.util.Date;
49 import java.util.List;
50 import java.util.concurrent.CopyOnWriteArrayList;
53 * @author Olivier Lamy
56 @Service( "downloadRemoteIndexScheduler#default" )
57 public class DefaultDownloadRemoteIndexScheduler
58 implements ConfigurationListener, DownloadRemoteIndexScheduler
61 private Logger log = LoggerFactory.getLogger( getClass() );
64 @Named( value = "taskScheduler#indexDownloadRemote" )
65 private TaskScheduler taskScheduler;
68 RepositoryRegistry repositoryRegistry;
71 private ArchivaConfiguration archivaConfiguration;
74 private WagonFactory wagonFactory;
77 private NetworkProxyAdmin networkProxyAdmin;
80 private IndexUpdater indexUpdater;
83 private IndexPacker indexPacker;
85 // store ids about currently running remote download : updated in DownloadRemoteIndexTask
86 private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
91 DownloadRemoteIndexException, UnsupportedBaseContextException {
92 archivaConfiguration.addListener( this );
93 // TODO add indexContexts even if null
95 for ( org.apache.archiva.repository.RemoteRepository remoteRepository : repositoryRegistry.getRemoteRepositories() )
97 String contextKey = "remote-" + remoteRepository.getId();
98 IndexingContext context = remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
99 if ( context == null )
103 RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
106 // TODO record jobs from configuration
107 if ( rif.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
108 remoteRepository.getSchedulingDefinition() ) )
110 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
111 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
119 public void configurationEvent( ConfigurationEvent event )
121 // TODO remove jobs and add again
126 public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
127 throws DownloadRemoteIndexException
131 org.apache.archiva.repository.RemoteRepository remoteRepo = repositoryRegistry.getRemoteRepository(repositoryId);
133 if ( remoteRepo == null )
135 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
138 if (!remoteRepo.supportsFeature(RemoteIndexFeature.class)) {
139 log.warn("ignore scheduleDownloadRemote for repo with id {}. Does not support remote index.", repositoryId);
142 RemoteIndexFeature rif = remoteRepo.getFeature(RemoteIndexFeature.class).get();
143 NetworkProxy networkProxy = null;
144 if ( StringUtils.isNotBlank( rif.getProxyId() ) )
146 networkProxy = networkProxyAdmin.getNetworkProxy( rif.getProxyId() );
147 if ( networkProxy == null )
150 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
155 DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest() //
156 .setRemoteRepository( remoteRepo ) //
157 .setNetworkProxy( networkProxy ) //
158 .setFullDownload( fullDownload ) //
159 .setWagonFactory( wagonFactory ) //
160 .setIndexUpdater( indexUpdater ) //
161 .setIndexPacker( this.indexPacker );
165 log.info( "schedule download remote index for repository {}", remoteRepo.getId() );
167 taskScheduler.schedule(
168 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
173 log.info( "schedule download remote index for repository {} with cron expression {}",
174 remoteRepo.getId(), remoteRepo.getSchedulingDefinition());
177 CronTrigger cronTrigger = new CronTrigger( remoteRepo.getSchedulingDefinition());
178 taskScheduler.schedule(
179 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
182 catch ( IllegalArgumentException e )
184 log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
187 if ( rif.isDownloadRemoteIndexOnStartup() )
190 "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
191 remoteRepo.getId() );
192 taskScheduler.schedule(
193 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
199 catch ( RepositoryAdminException e )
201 log.error( e.getMessage(), e );
202 throw new DownloadRemoteIndexException( e.getMessage(), e );
206 public TaskScheduler getTaskScheduler()
208 return taskScheduler;
211 public void setTaskScheduler( TaskScheduler taskScheduler )
213 this.taskScheduler = taskScheduler;
217 public List<String> getRunningRemoteDownloadIds()
219 return runningRemoteDownloadIds;