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