]> source.dussan.org Git - archiva.git/blob
7ff7e086967df8631d31dd7b51bc7d44c6c365be
[archiva.git] /
1 package org.apache.archiva.maven.scheduler.indexing;
2 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19
20 import org.apache.archiva.proxy.ProxyRegistry;
21 import org.apache.archiva.proxy.model.NetworkProxy;
22 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
23 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.ConfigurationEvent;
26 import org.apache.archiva.configuration.ConfigurationListener;
27 import org.apache.archiva.indexer.UnsupportedBaseContextException;
28 import org.apache.archiva.maven.common.proxy.WagonFactory;
29 import org.apache.archiva.repository.RepositoryRegistry;
30 import org.apache.archiva.repository.features.RemoteIndexFeature;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.maven.index.context.IndexingContext;
33 import org.apache.maven.index.packer.IndexPacker;
34 import org.apache.maven.index.updater.IndexUpdater;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.scheduling.TaskScheduler;
38 import org.springframework.scheduling.support.CronTrigger;
39 import org.springframework.stereotype.Service;
40
41 import javax.annotation.PostConstruct;
42 import javax.inject.Inject;
43 import javax.inject.Named;
44 import java.util.Date;
45 import java.util.List;
46 import java.util.concurrent.CopyOnWriteArrayList;
47
48 /**
49  * @author Olivier Lamy
50  * @since 1.4-M1
51  */
52 @Service( "downloadRemoteIndexScheduler#default" )
53 public class DefaultDownloadRemoteIndexScheduler
54     implements ConfigurationListener, DownloadRemoteIndexScheduler
55 {
56
57     private Logger log = LoggerFactory.getLogger( getClass() );
58
59     @Inject
60     @Named( value = "taskScheduler#indexDownloadRemote" )
61     private TaskScheduler taskScheduler;
62
63     @Inject
64     RepositoryRegistry repositoryRegistry;
65
66     @Inject
67     private ArchivaConfiguration archivaConfiguration;
68
69     @Inject
70     private WagonFactory wagonFactory;
71
72     @Inject
73     private IndexUpdater indexUpdater;
74
75     @Inject
76     private IndexPacker indexPacker;
77
78     @Inject
79     private ProxyRegistry proxyRegistry;
80
81     // store ids about currently running remote download : updated in DownloadRemoteIndexTask
82     private List<String> runningRemoteDownloadIds = new CopyOnWriteArrayList<String>();
83
84     @PostConstruct
85     public void startup()
86             throws
87         DownloadRemoteIndexException, UnsupportedBaseContextException {
88         archivaConfiguration.addListener( this );
89         // TODO add indexContexts even if null
90
91         for ( org.apache.archiva.repository.RemoteRepository remoteRepository : repositoryRegistry.getRemoteRepositories() )
92         {
93             String contextKey = "remote-" + remoteRepository.getId();
94             IndexingContext context = remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
95             if ( context == null )
96             {
97                 continue;
98             }
99             RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
100
101
102             // TODO record jobs from configuration
103             if ( rif.isDownloadRemoteIndex() && StringUtils.isNotEmpty(
104                 remoteRepository.getSchedulingDefinition() ) )
105             {
106                 boolean fullDownload = context.getIndexDirectoryFile().list().length == 0;
107                 scheduleDownloadRemote( remoteRepository.getId(), false, fullDownload );
108             }
109         }
110
111
112     }
113
114     @Override
115     public void configurationEvent( ConfigurationEvent event )
116     {
117         // TODO remove jobs and add again
118     }
119
120
121     @Override
122     public void scheduleDownloadRemote( String repositoryId, boolean now, boolean fullDownload )
123         throws DownloadRemoteIndexException
124     {
125         org.apache.archiva.repository.RemoteRepository remoteRepo = repositoryRegistry.getRemoteRepository(repositoryId);
126
127         if ( remoteRepo == null )
128         {
129             log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
130             return;
131         }
132         if (!remoteRepo.supportsFeature(RemoteIndexFeature.class)) {
133             log.warn("ignore scheduleDownloadRemote for repo with id {}. Does not support remote index.", repositoryId);
134             return;
135         }
136         RemoteIndexFeature rif = remoteRepo.getFeature(RemoteIndexFeature.class).get();
137         NetworkProxy networkProxy = null;
138         if ( StringUtils.isNotBlank( rif.getProxyId() ) )
139         {
140             networkProxy = proxyRegistry.getNetworkProxy( rif.getProxyId() );
141             if ( networkProxy == null )
142             {
143                 log.warn(
144                     "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
145                     rif.getProxyId() );
146             }
147         }
148
149         DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest() //
150             .setRemoteRepository( remoteRepo ) //
151             .setNetworkProxy( networkProxy ) //
152             .setFullDownload( fullDownload ) //
153             .setWagonFactory( wagonFactory ) //
154             .setIndexUpdater( indexUpdater ) //
155             .setIndexPacker( this.indexPacker );
156
157         if ( now )
158         {
159             log.info( "schedule download remote index for repository {}", remoteRepo.getId() );
160             // do it now
161             taskScheduler.schedule(
162                 new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
163                 new Date() );
164         }
165         else
166         {
167             log.info( "schedule download remote index for repository {} with cron expression {}",
168                       remoteRepo.getId(), remoteRepo.getSchedulingDefinition());
169             try
170             {
171                 CronTrigger cronTrigger = new CronTrigger( remoteRepo.getSchedulingDefinition());
172                 taskScheduler.schedule(
173                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
174                     cronTrigger );
175             }
176             catch ( IllegalArgumentException e )
177             {
178                 log.warn( "Unable to schedule remote index download: {}", e.getLocalizedMessage() );
179             }
180
181             if ( rif.isDownloadRemoteIndexOnStartup() )
182             {
183                 log.info(
184                     "remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download",
185                     remoteRepo.getId() );
186                 taskScheduler.schedule(
187                     new DownloadRemoteIndexTask( downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds ),
188                     new Date() );
189             }
190         }
191
192     }
193
194     public TaskScheduler getTaskScheduler()
195     {
196         return taskScheduler;
197     }
198
199     public void setTaskScheduler( TaskScheduler taskScheduler )
200     {
201         this.taskScheduler = taskScheduler;
202     }
203
204     @Override
205     public List<String> getRunningRemoteDownloadIds()
206     {
207         return runningRemoteDownloadIds;
208     }
209 }