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