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.MavenIndexerUtils;
29 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
30 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
31 import org.apache.archiva.configuration.ArchivaConfiguration;
32 import org.apache.archiva.configuration.ConfigurationEvent;
33 import org.apache.archiva.configuration.ConfigurationListener;
34 import org.apache.archiva.proxy.common.WagonFactory;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.context.IndexingContext;
38 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
39 import org.apache.maven.index.packer.IndexPacker;
40 import org.apache.maven.index.updater.IndexUpdater;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.scheduling.TaskScheduler;
44 import org.springframework.scheduling.support.CronTrigger;
45 import org.springframework.stereotype.Service;
47 import javax.annotation.PostConstruct;
48 import javax.annotation.PreDestroy;
49 import javax.inject.Inject;
50 import javax.inject.Named;
51 import java.io.IOException;
52 import java.util.Date;
53 import java.util.List;
54 import java.util.concurrent.CopyOnWriteArrayList;
57 * @author Olivier Lamy
60 @Service( "downloadRemoteIndexScheduler#default" )
61 public class DefaultDownloadRemoteIndexScheduler
62 implements ConfigurationListener, DownloadRemoteIndexScheduler
65 private Logger log = LoggerFactory.getLogger( getClass() );
68 @Named( value = "taskScheduler#indexDownloadRemote" )
69 private TaskScheduler taskScheduler;
72 private ArchivaConfiguration archivaConfiguration;
75 private WagonFactory wagonFactory;
78 private RemoteRepositoryAdmin remoteRepositoryAdmin;
81 private ProxyConnectorAdmin proxyConnectorAdmin;
84 private NetworkProxyAdmin networkProxyAdmin;
87 private MavenIndexerUtils mavenIndexerUtils;
90 private NexusIndexer nexusIndexer;
93 private IndexUpdater indexUpdater;
96 private IndexPacker indexPacker;
98 // store ids about currently running remote download : updated in DownloadRemoteIndexTask
99 private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
102 public void startup()
103 throws ArchivaException, RepositoryAdminException, PlexusSisuBridgeException, IOException,
104 UnsupportedExistingLuceneIndexException, DownloadRemoteIndexException
106 archivaConfiguration.addListener( this );
107 // TODO add indexContexts even if null
109 for ( RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
111 String contextKey = "remote-" + remoteRepository.getId();
112 IndexingContext context = nexusIndexer.getIndexingContexts().get( contextKey );
113 if ( context == null )
118 // TODO record jobs from configuration
119 if ( remoteRepository.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
120 remoteRepository.getCronExpression() ) )
122 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
123 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
131 public void shutdown()
132 throws RepositoryAdminException, IOException
134 for ( RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
136 String contextKey = "remote-" + remoteRepository.getId();
137 IndexingContext context = nexusIndexer.getIndexingContexts().get( contextKey );
138 if ( context == null )
142 nexusIndexer.removeIndexingContext( context, false );
147 public void configurationEvent( ConfigurationEvent event )
149 // TODO remove jobs and add again
154 public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
155 throws DownloadRemoteIndexException
159 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
160 if ( remoteRepository == null )
162 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
165 NetworkProxy networkProxy = null;
166 if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
168 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
169 if ( networkProxy == null )
172 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
173 remoteRepository.getRemoteDownloadNetworkProxyId() );
177 DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest() //
178 .setRemoteRepository( remoteRepository ) //
179 .setNetworkProxy( networkProxy ) //
180 .setFullDownload( fullDownload ) //
181 .setWagonFactory( wagonFactory ) //
182 .setRemoteRepositoryAdmin( remoteRepositoryAdmin ) //
183 .setIndexUpdater( indexUpdater ) //
184 .setIndexPacker( this.indexPacker );
188 log.info( "schedule download remote index for repository {}", remoteRepository.getId() );
190 taskScheduler.schedule(
191 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
196 log.info( "schedule download remote index for repository {} with cron expression {}",
197 remoteRepository.getId(), remoteRepository.getCronExpression() );
200 CronTrigger cronTrigger = new CronTrigger( remoteRepository.getCronExpression() );
201 taskScheduler.schedule(
202 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
205 catch ( IllegalArgumentException e )
207 log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
210 if ( remoteRepository.isDownloadRemoteIndexOnStartup() )
213 "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
214 remoteRepository.getId() );
215 taskScheduler.schedule(
216 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
222 catch ( RepositoryAdminException e )
224 log.error( e.getMessage(), e );
225 throw new DownloadRemoteIndexException( e.getMessage(), e );
229 public TaskScheduler getTaskScheduler()
231 return taskScheduler;
234 public void setTaskScheduler( TaskScheduler taskScheduler )
236 this.taskScheduler = taskScheduler;
240 public List<String> getRunningRemoteDownloadIds()
242 return runningRemoteDownloadIds;