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.beans.RemoteRepository;
24 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
25 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
26 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
27 import org.apache.archiva.common.ArchivaException;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
29 import org.apache.archiva.configuration.ArchivaConfiguration;
30 import org.apache.archiva.configuration.ConfigurationEvent;
31 import org.apache.archiva.configuration.ConfigurationListener;
32 import org.apache.archiva.indexer.UnsupportedBaseContextException;
33 import org.apache.archiva.proxy.common.WagonFactory;
34 import org.apache.archiva.repository.RepositoryRegistry;
35 import org.apache.archiva.repository.features.RemoteIndexFeature;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.maven.index.NexusIndexer;
38 import org.apache.maven.index.context.IndexingContext;
39 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
40 import org.apache.maven.index.packer.IndexPacker;
41 import org.apache.maven.index.updater.IndexUpdater;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.scheduling.TaskScheduler;
45 import org.springframework.scheduling.support.CronTrigger;
46 import org.springframework.stereotype.Service;
48 import javax.annotation.PostConstruct;
49 import javax.annotation.PreDestroy;
50 import javax.inject.Inject;
51 import javax.inject.Named;
52 import java.io.IOException;
53 import java.util.Date;
54 import java.util.List;
55 import java.util.concurrent.CopyOnWriteArrayList;
58 * @author Olivier Lamy
61 @Service( "downloadRemoteIndexScheduler#default" )
62 public class DefaultDownloadRemoteIndexScheduler
63 implements ConfigurationListener, DownloadRemoteIndexScheduler
66 private Logger log = LoggerFactory.getLogger( getClass() );
69 @Named( value = "taskScheduler#indexDownloadRemote" )
70 private TaskScheduler taskScheduler;
73 RepositoryRegistry repositoryRegistry;
76 private ArchivaConfiguration archivaConfiguration;
79 private WagonFactory wagonFactory;
82 private RemoteRepositoryAdmin remoteRepositoryAdmin;
85 private ProxyConnectorAdmin proxyConnectorAdmin;
88 private NetworkProxyAdmin networkProxyAdmin;
91 private NexusIndexer nexusIndexer;
94 private IndexUpdater indexUpdater;
97 private IndexPacker indexPacker;
99 // store ids about currently running remote download : updated in DownloadRemoteIndexTask
100 private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
103 public void startup()
104 throws ArchivaException, RepositoryAdminException, PlexusSisuBridgeException, IOException,
105 UnsupportedExistingLuceneIndexException, DownloadRemoteIndexException, UnsupportedBaseContextException {
106 archivaConfiguration.addListener( this );
107 // TODO add indexContexts even if null
109 for ( org.apache.archiva.repository.RemoteRepository remoteRepository : repositoryRegistry.getRemoteRepositories() )
111 String contextKey = "remote-" + remoteRepository.getId();
112 IndexingContext context = remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
113 if ( context == null )
117 RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
120 // TODO record jobs from configuration
121 if ( rif.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
122 remoteRepository.getSchedulingDefinition() ) )
124 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
125 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
133 public void shutdown()
134 throws RepositoryAdminException, IOException
136 for ( RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
138 String contextKey = "remote-" + remoteRepository.getId();
139 IndexingContext context = nexusIndexer.getIndexingContexts().get( contextKey );
140 if ( context == null )
144 nexusIndexer.removeIndexingContext( context, false );
149 public void configurationEvent( ConfigurationEvent event )
151 // TODO remove jobs and add again
156 public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
157 throws DownloadRemoteIndexException
161 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
162 if ( remoteRepository == null )
164 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
167 NetworkProxy networkProxy = null;
168 if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
170 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
171 if ( networkProxy == null )
174 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
175 remoteRepository.getRemoteDownloadNetworkProxyId() );
179 DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest() //
180 .setRemoteRepository( remoteRepository ) //
181 .setNetworkProxy( networkProxy ) //
182 .setFullDownload( fullDownload ) //
183 .setWagonFactory( wagonFactory ) //
184 .setRemoteRepositoryAdmin( remoteRepositoryAdmin ) //
185 .setIndexUpdater( indexUpdater ) //
186 .setIndexPacker( this.indexPacker );
190 log.info( "schedule download remote index for repository {}", remoteRepository.getId() );
192 taskScheduler.schedule(
193 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
198 log.info( "schedule download remote index for repository {} with cron expression {}",
199 remoteRepository.getId(), remoteRepository.getCronExpression() );
202 CronTrigger cronTrigger = new CronTrigger( remoteRepository.getCronExpression() );
203 taskScheduler.schedule(
204 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
207 catch ( IllegalArgumentException e )
209 log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
212 if ( remoteRepository.isDownloadRemoteIndexOnStartup() )
215 "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
216 remoteRepository.getId() );
217 taskScheduler.schedule(
218 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
224 catch ( RepositoryAdminException e )
226 log.error( e.getMessage(), e );
227 throw new DownloadRemoteIndexException( e.getMessage(), e );
231 public TaskScheduler getTaskScheduler()
233 return taskScheduler;
236 public void setTaskScheduler( TaskScheduler taskScheduler )
238 this.taskScheduler = taskScheduler;
242 public List<String> getRunningRemoteDownloadIds()
244 return runningRemoteDownloadIds;