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.remote.RemoteRepositoryAdmin;
25 import org.apache.archiva.proxy.common.WagonFactory;
26 import org.apache.archiva.proxy.common.WagonFactoryException;
27 import org.apache.archiva.proxy.common.WagonFactoryRequest;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.commons.lang.time.StopWatch;
30 import org.apache.maven.index.context.IndexingContext;
31 import org.apache.maven.index.updater.IndexUpdateRequest;
32 import org.apache.maven.index.updater.IndexUpdater;
33 import org.apache.maven.index.updater.ResourceFetcher;
34 import org.apache.maven.wagon.ConnectionException;
35 import org.apache.maven.wagon.ResourceDoesNotExistException;
36 import org.apache.maven.wagon.StreamWagon;
37 import org.apache.maven.wagon.TransferFailedException;
38 import org.apache.maven.wagon.Wagon;
39 import org.apache.maven.wagon.authentication.AuthenticationException;
40 import org.apache.maven.wagon.authentication.AuthenticationInfo;
41 import org.apache.maven.wagon.authorization.AuthorizationException;
42 import org.apache.maven.wagon.events.TransferEvent;
43 import org.apache.maven.wagon.events.TransferListener;
44 import org.apache.maven.wagon.providers.http.AbstractHttpClientWagon;
45 import org.apache.maven.wagon.providers.http.HttpConfiguration;
46 import org.apache.maven.wagon.providers.http.HttpMethodConfiguration;
47 import org.apache.maven.wagon.proxy.ProxyInfo;
48 import org.apache.maven.wagon.repository.Repository;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 import java.io.FileInputStream;
54 import java.io.FileNotFoundException;
55 import java.io.IOException;
56 import java.io.InputStream;
57 import java.net.MalformedURLException;
59 import java.nio.file.Files;
60 import java.util.List;
64 * @author Olivier Lamy
67 public class DownloadRemoteIndexTask
70 private Logger log = LoggerFactory.getLogger( getClass() );
72 private RemoteRepository remoteRepository;
74 private RemoteRepositoryAdmin remoteRepositoryAdmin;
76 private WagonFactory wagonFactory;
78 private NetworkProxy networkProxy;
80 private boolean fullDownload;
82 private List<String> runningRemoteDownloadIds;
84 private IndexUpdater indexUpdater;
87 public DownloadRemoteIndexTask( DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest,
88 List<String> runningRemoteDownloadIds )
90 this.remoteRepository = downloadRemoteIndexTaskRequest.getRemoteRepository();
91 this.wagonFactory = downloadRemoteIndexTaskRequest.getWagonFactory();
92 this.networkProxy = downloadRemoteIndexTaskRequest.getNetworkProxy();
93 this.fullDownload = downloadRemoteIndexTaskRequest.isFullDownload();
94 this.runningRemoteDownloadIds = runningRemoteDownloadIds;
95 this.indexUpdater = downloadRemoteIndexTaskRequest.getIndexUpdater();
96 this.remoteRepositoryAdmin = downloadRemoteIndexTaskRequest.getRemoteRepositoryAdmin();
103 // so short lock : not sure we need it
104 synchronized ( this.runningRemoteDownloadIds )
106 if ( this.runningRemoteDownloadIds.contains( this.remoteRepository.getId() ) )
108 // skip it as it's running
109 log.info( "skip download index remote for repo {} it's already running",
110 this.remoteRepository.getId() );
113 this.runningRemoteDownloadIds.add( this.remoteRepository.getId() );
115 File tempIndexDirectory = null;
116 StopWatch stopWatch = new StopWatch();
120 log.info( "start download remote index for remote repository {}", this.remoteRepository.getId() );
121 IndexingContext indexingContext = remoteRepositoryAdmin.createIndexContext( this.remoteRepository );
123 // create a temp directory to download files
124 tempIndexDirectory = new File( indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex" );
125 File indexCacheDirectory = new File( indexingContext.getIndexDirectoryFile().getParent(), ".indexCache" );
126 indexCacheDirectory.mkdirs();
127 if ( tempIndexDirectory.exists() )
129 FileUtils.deleteDirectory( tempIndexDirectory );
131 tempIndexDirectory.mkdirs();
132 tempIndexDirectory.deleteOnExit();
133 String baseIndexUrl = indexingContext.getIndexUpdateUrl();
135 String wagonProtocol = new URL( this.remoteRepository.getUrl() ).getProtocol();
137 final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
138 new WagonFactoryRequest( wagonProtocol, this.remoteRepository.getExtraHeaders() ).networkProxy(
141 // FIXME olamy having 2 config values
142 wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
143 wagon.setTimeout( remoteRepository.getTimeout() * 1000 );
145 if ( wagon instanceof AbstractHttpClientWagon )
147 HttpConfiguration httpConfiguration = new HttpConfiguration();
148 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
149 httpMethodConfiguration.setUsePreemptive( true );
150 httpMethodConfiguration.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
151 httpConfiguration.setGet( httpMethodConfiguration );
152 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
155 wagon.addTransferListener( new DownloadListener() );
156 ProxyInfo proxyInfo = null;
157 if ( this.networkProxy != null )
159 proxyInfo = new ProxyInfo();
160 proxyInfo.setHost( this.networkProxy.getHost() );
161 proxyInfo.setPort( this.networkProxy.getPort() );
162 proxyInfo.setUserName( this.networkProxy.getUsername() );
163 proxyInfo.setPassword( this.networkProxy.getPassword() );
165 AuthenticationInfo authenticationInfo = null;
166 if ( this.remoteRepository.getUserName() != null )
168 authenticationInfo = new AuthenticationInfo();
169 authenticationInfo.setUserName( this.remoteRepository.getUserName() );
170 authenticationInfo.setPassword( this.remoteRepository.getPassword() );
172 wagon.connect( new Repository( this.remoteRepository.getId(), baseIndexUrl ), authenticationInfo,
175 File indexDirectory = indexingContext.getIndexDirectoryFile();
176 if ( !indexDirectory.exists() )
178 indexDirectory.mkdirs();
181 ResourceFetcher resourceFetcher =
182 new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
183 IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
184 request.setForceFullUpdate( this.fullDownload );
185 request.setLocalIndexCacheDir( indexCacheDirectory );
187 this.indexUpdater.fetchAndUpdateIndex( request );
189 log.info( "time update index from remote for repository {}: {} s", this.remoteRepository.getId(),
190 ( stopWatch.getTime() / 1000 ) );
192 // index packing optionnal ??
193 //IndexPackingRequest indexPackingRequest =
194 // new IndexPackingRequest( indexingContext, indexingContext.getIndexDirectoryFile() );
195 //indexPacker.packIndex( indexPackingRequest );
196 indexingContext.updateTimestamp( true );
199 catch ( MalformedURLException e )
201 log.error( e.getMessage(), e );
202 throw new RuntimeException( e.getMessage(), e );
204 catch ( WagonFactoryException e )
206 log.error( e.getMessage(), e );
207 throw new RuntimeException( e.getMessage(), e );
209 catch ( ConnectionException e )
211 log.error( e.getMessage(), e );
212 throw new RuntimeException( e.getMessage(), e );
214 catch ( AuthenticationException e )
216 log.error( e.getMessage(), e );
217 throw new RuntimeException( e.getMessage(), e );
219 catch ( IOException e )
221 log.error( e.getMessage(), e );
222 throw new RuntimeException( e.getMessage(), e );
224 catch ( RepositoryAdminException e )
226 log.error( e.getMessage(), e );
227 throw new RuntimeException( e.getMessage(), e );
231 deleteDirectoryQuiet( tempIndexDirectory );
232 this.runningRemoteDownloadIds.remove( this.remoteRepository.getId() );
234 log.info( "end download remote index for remote repository {}", this.remoteRepository.getId() );
237 private void deleteDirectoryQuiet( File f )
241 FileUtils.deleteDirectory( f );
243 catch ( IOException e )
245 log.warn( "skip error delete {} : {}", f, e.getMessage() );
250 private static final class DownloadListener
251 implements TransferListener
253 private Logger log = LoggerFactory.getLogger( getClass() );
255 private String resourceName;
257 private long startTime;
259 private int totalLength = 0;
262 public void transferInitiated( TransferEvent transferEvent )
264 startTime = System.currentTimeMillis();
265 resourceName = transferEvent.getResource().getName();
266 log.debug( "initiate transfer of {}", resourceName );
270 public void transferStarted( TransferEvent transferEvent )
272 this.totalLength = 0;
273 resourceName = transferEvent.getResource().getName();
274 log.info( "start transfer of {}", transferEvent.getResource().getName() );
278 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
280 log.debug( "transfer of {} : {}/{}", transferEvent.getResource().getName(), buffer.length, length );
281 this.totalLength += length;
285 public void transferCompleted( TransferEvent transferEvent )
287 resourceName = transferEvent.getResource().getName();
288 long endTime = System.currentTimeMillis();
289 log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource().getName(),
290 this.totalLength / 1024, ( endTime - startTime ) / 1000 );
294 public void transferError( TransferEvent transferEvent )
296 log.info( "error of transfer file {}: {}", transferEvent.getResource().getName(),
297 transferEvent.getException().getMessage(), transferEvent.getException() );
301 public void debug( String message )
303 log.debug( "transfer debug {}", message );
307 private static class WagonResourceFetcher
308 implements ResourceFetcher
313 File tempIndexDirectory;
317 RemoteRepository remoteRepository;
319 private WagonResourceFetcher( Logger log, File tempIndexDirectory, Wagon wagon,
320 RemoteRepository remoteRepository )
323 this.tempIndexDirectory = tempIndexDirectory;
325 this.remoteRepository = remoteRepository;
329 public void connect( String id, String url )
336 public void disconnect()
343 public InputStream retrieve( String name )
344 throws IOException, FileNotFoundException
348 log.info( "index update retrieve file, name:{}", name );
349 File file = new File( tempIndexDirectory, name );
350 Files.deleteIfExists( file.toPath() );
352 wagon.get( addParameters( name, this.remoteRepository ), file );
353 return Files.newInputStream( file.toPath() );
355 catch ( AuthorizationException e )
357 throw new IOException( e.getMessage(), e );
359 catch ( TransferFailedException e )
361 throw new IOException( e.getMessage(), e );
363 catch ( ResourceDoesNotExistException e )
365 FileNotFoundException fnfe = new FileNotFoundException( e.getMessage() );
371 // FIXME remove crappy copy/paste
372 protected String addParameters( String path, RemoteRepository remoteRepository )
374 if ( remoteRepository.getExtraParameters().isEmpty() )
379 boolean question = false;
381 StringBuilder res = new StringBuilder( path == null ? "" : path );
383 for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters().entrySet() )
387 res.append( '?' ).append( entry.getKey() ).append( '=' ).append( entry.getValue() );
391 return res.toString();