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.util.List;
63 * @author Olivier Lamy
66 public class DownloadRemoteIndexTask
69 private Logger log = LoggerFactory.getLogger( getClass() );
71 private RemoteRepository remoteRepository;
73 private RemoteRepositoryAdmin remoteRepositoryAdmin;
75 private WagonFactory wagonFactory;
77 private NetworkProxy networkProxy;
79 private boolean fullDownload;
81 private List<String> runningRemoteDownloadIds;
83 private IndexUpdater indexUpdater;
86 public DownloadRemoteIndexTask( DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest,
87 List<String> runningRemoteDownloadIds )
89 this.remoteRepository = downloadRemoteIndexTaskRequest.getRemoteRepository();
90 this.wagonFactory = downloadRemoteIndexTaskRequest.getWagonFactory();
91 this.networkProxy = downloadRemoteIndexTaskRequest.getNetworkProxy();
92 this.fullDownload = downloadRemoteIndexTaskRequest.isFullDownload();
93 this.runningRemoteDownloadIds = runningRemoteDownloadIds;
94 this.indexUpdater = downloadRemoteIndexTaskRequest.getIndexUpdater();
95 this.remoteRepositoryAdmin = downloadRemoteIndexTaskRequest.getRemoteRepositoryAdmin();
101 // so short lock : not sure we need it
102 synchronized ( this.runningRemoteDownloadIds )
104 if ( this.runningRemoteDownloadIds.contains( this.remoteRepository.getId() ) )
106 // skip it as it's running
107 log.info( "skip download index remote for repo {} it's already running",
108 this.remoteRepository.getId() );
111 this.runningRemoteDownloadIds.add( this.remoteRepository.getId() );
113 File tempIndexDirectory = null;
114 StopWatch stopWatch = new StopWatch();
118 log.info( "start download remote index for remote repository {}", this.remoteRepository.getId() );
119 IndexingContext indexingContext = remoteRepositoryAdmin.createIndexContext( this.remoteRepository );
121 // create a temp directory to download files
122 tempIndexDirectory = new File( indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex" );
123 File indexCacheDirectory = new File( indexingContext.getIndexDirectoryFile().getParent(), ".indexCache" );
124 indexCacheDirectory.mkdirs();
125 if ( tempIndexDirectory.exists() )
127 FileUtils.deleteDirectory( tempIndexDirectory );
129 tempIndexDirectory.mkdirs();
130 tempIndexDirectory.deleteOnExit();
131 String baseIndexUrl = indexingContext.getIndexUpdateUrl();
133 String wagonProtocol = new URL( this.remoteRepository.getUrl() ).getProtocol();
135 final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
136 new WagonFactoryRequest( wagonProtocol, this.remoteRepository.getExtraHeaders() ).networkProxy(
137 this.networkProxy ) );
138 int timeoutInMilliseconds = remoteRepository.getTimeout() * 1000;
139 // FIXME olamy having 2 config values
140 wagon.setReadTimeout( timeoutInMilliseconds );
141 wagon.setTimeout( timeoutInMilliseconds );
143 if ( wagon instanceof AbstractHttpClientWagon )
145 HttpConfiguration httpConfiguration = new HttpConfiguration();
146 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
147 httpMethodConfiguration.setUsePreemptive( true );
148 httpMethodConfiguration.setReadTimeout( timeoutInMilliseconds );
149 httpConfiguration.setGet( httpMethodConfiguration );
150 ( (AbstractHttpClientWagon) wagon ).setHttpConfiguration( httpConfiguration );
153 wagon.addTransferListener( new DownloadListener() );
154 ProxyInfo proxyInfo = null;
155 if ( this.networkProxy != null )
157 proxyInfo = new ProxyInfo();
158 proxyInfo.setHost( this.networkProxy.getHost() );
159 proxyInfo.setPort( this.networkProxy.getPort() );
160 proxyInfo.setUserName( this.networkProxy.getUsername() );
161 proxyInfo.setPassword( this.networkProxy.getPassword() );
163 AuthenticationInfo authenticationInfo = null;
164 if ( this.remoteRepository.getUserName() != null )
166 authenticationInfo = new AuthenticationInfo();
167 authenticationInfo.setUserName( this.remoteRepository.getUserName() );
168 authenticationInfo.setPassword( this.remoteRepository.getPassword() );
170 wagon.connect( new Repository( this.remoteRepository.getId(), baseIndexUrl ), authenticationInfo,
173 File indexDirectory = indexingContext.getIndexDirectoryFile();
174 if ( !indexDirectory.exists() )
176 indexDirectory.mkdirs();
179 ResourceFetcher resourceFetcher =
180 new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
181 IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
182 request.setForceFullUpdate( this.fullDownload );
183 request.setLocalIndexCacheDir( indexCacheDirectory );
185 this.indexUpdater.fetchAndUpdateIndex( request );
187 log.info( "time update index from remote for repository {}: {} s", this.remoteRepository.getId(),
188 ( stopWatch.getTime() / 1000 ) );
190 // index packing optionnal ??
191 //IndexPackingRequest indexPackingRequest =
192 // new IndexPackingRequest( indexingContext, indexingContext.getIndexDirectoryFile() );
193 //indexPacker.packIndex( indexPackingRequest );
194 indexingContext.updateTimestamp( true );
197 catch ( MalformedURLException e )
199 log.error( e.getMessage(), e );
200 throw new RuntimeException( e.getMessage(), e );
202 catch ( WagonFactoryException e )
204 log.error( e.getMessage(), e );
205 throw new RuntimeException( e.getMessage(), e );
207 catch ( ConnectionException e )
209 log.error( e.getMessage(), e );
210 throw new RuntimeException( e.getMessage(), e );
212 catch ( AuthenticationException e )
214 log.error( e.getMessage(), e );
215 throw new RuntimeException( e.getMessage(), e );
217 catch ( IOException e )
219 log.error( e.getMessage(), e );
220 throw new RuntimeException( e.getMessage(), e );
222 catch ( RepositoryAdminException e )
224 log.error( e.getMessage(), e );
225 throw new RuntimeException( e.getMessage(), e );
229 deleteDirectoryQuiet( tempIndexDirectory );
230 this.runningRemoteDownloadIds.remove( this.remoteRepository.getId() );
232 log.info( "end download remote index for remote repository {}", this.remoteRepository.getId() );
235 private void deleteDirectoryQuiet( File f )
239 FileUtils.deleteDirectory( f );
241 catch ( IOException e )
243 log.warn( "skip error delete {} : {}", f, e.getMessage() );
248 private static final class DownloadListener
249 implements TransferListener
251 private Logger log = LoggerFactory.getLogger( getClass() );
253 private String resourceName;
255 private long startTime;
257 private int totalLength = 0;
259 public void transferInitiated( TransferEvent transferEvent )
261 startTime = System.currentTimeMillis();
262 resourceName = transferEvent.getResource().getName();
263 log.debug( "initiate transfer of {}", resourceName );
266 public void transferStarted( TransferEvent transferEvent )
268 this.totalLength = 0;
269 resourceName = transferEvent.getResource().getName();
270 log.info( "start transfer of {}", transferEvent.getResource().getName() );
273 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
275 log.debug( "transfer of {} : {}/{}", transferEvent.getResource().getName(), buffer.length, length );
276 this.totalLength += length;
279 public void transferCompleted( TransferEvent transferEvent )
281 resourceName = transferEvent.getResource().getName();
282 long endTime = System.currentTimeMillis();
283 log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource().getName(),
284 this.totalLength / 1024, ( endTime - startTime ) / 1000 );
287 public void transferError( TransferEvent transferEvent )
289 log.info( "error of transfer file {}: {}", transferEvent.getResource().getName(),
290 transferEvent.getException().getMessage(), transferEvent.getException() );
293 public void debug( String message )
295 log.debug( "transfer debug {}", message );
299 private static class WagonResourceFetcher
300 implements ResourceFetcher
305 File tempIndexDirectory;
309 RemoteRepository remoteRepository;
311 private WagonResourceFetcher( Logger log, File tempIndexDirectory, Wagon wagon,
312 RemoteRepository remoteRepository )
315 this.tempIndexDirectory = tempIndexDirectory;
317 this.remoteRepository = remoteRepository;
320 public void connect( String id, String url )
326 public void disconnect()
332 public InputStream retrieve( String name )
333 throws IOException, FileNotFoundException
337 log.info( "index update retrieve file, name:{}", name );
338 File file = new File( tempIndexDirectory, name );
344 wagon.get( addParameters( name, this.remoteRepository ), file );
345 return new FileInputStream( file );
347 catch ( AuthorizationException e )
349 throw new IOException( e.getMessage(), e );
351 catch ( TransferFailedException e )
353 throw new IOException( e.getMessage(), e );
355 catch ( ResourceDoesNotExistException e )
357 FileNotFoundException fnfe = new FileNotFoundException( e.getMessage() );
363 // FIXME remove crappy copy/paste
364 protected String addParameters( String path, RemoteRepository remoteRepository )
366 if ( remoteRepository.getExtraParameters().isEmpty() )
371 boolean question = false;
373 StringBuilder res = new StringBuilder( path == null ? "" : path );
375 for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters().entrySet() )
379 res.append( '?' ).append( entry.getKey() ).append( '=' ).append( entry.getValue() );
383 return res.toString();