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