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.commons.io.FileUtils;
28 import org.apache.commons.lang.time.StopWatch;
29 import org.apache.maven.index.context.IndexingContext;
30 import org.apache.maven.index.updater.IndexUpdateRequest;
31 import org.apache.maven.index.updater.IndexUpdater;
32 import org.apache.maven.index.updater.ResourceFetcher;
33 import org.apache.maven.wagon.ConnectionException;
34 import org.apache.maven.wagon.ResourceDoesNotExistException;
35 import org.apache.maven.wagon.TransferFailedException;
36 import org.apache.maven.wagon.Wagon;
37 import org.apache.maven.wagon.authentication.AuthenticationException;
38 import org.apache.maven.wagon.authentication.AuthenticationInfo;
39 import org.apache.maven.wagon.authorization.AuthorizationException;
40 import org.apache.maven.wagon.events.TransferEvent;
41 import org.apache.maven.wagon.events.TransferListener;
42 import org.apache.maven.wagon.proxy.ProxyInfo;
43 import org.apache.maven.wagon.repository.Repository;
44 import org.apache.maven.wagon.shared.http.HttpConfiguration;
45 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 import java.io.FileInputStream;
51 import java.io.FileNotFoundException;
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.lang.reflect.Method;
55 import java.net.MalformedURLException;
57 import java.util.Arrays;
58 import java.util.List;
61 * @author Olivier Lamy
64 public class DownloadRemoteIndexTask
67 private Logger log = LoggerFactory.getLogger( getClass() );
69 private RemoteRepository remoteRepository;
71 private RemoteRepositoryAdmin remoteRepositoryAdmin;
73 private WagonFactory wagonFactory;
75 private NetworkProxy networkProxy;
77 private boolean fullDownload;
79 private List<String> runningRemoteDownloadIds;
81 private IndexUpdater indexUpdater;
83 public DownloadRemoteIndexTask( DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest,
84 List<String> runningRemoteDownloadIds )
86 this.remoteRepository = downloadRemoteIndexTaskRequest.getRemoteRepository();
87 this.wagonFactory = downloadRemoteIndexTaskRequest.getWagonFactory();
88 this.networkProxy = downloadRemoteIndexTaskRequest.getNetworkProxy();
89 this.fullDownload = downloadRemoteIndexTaskRequest.isFullDownload();
90 this.runningRemoteDownloadIds = runningRemoteDownloadIds;
91 this.indexUpdater = downloadRemoteIndexTaskRequest.getIndexUpdater();
92 this.remoteRepositoryAdmin = downloadRemoteIndexTaskRequest.getRemoteRepositoryAdmin();
98 // so short lock : not sure we need it
99 synchronized ( this.runningRemoteDownloadIds )
101 if ( this.runningRemoteDownloadIds.contains( this.remoteRepository.getId() ) )
103 // skip it as it's running
104 log.info( "skip download index remote for repo {} it's already running",
105 this.remoteRepository.getId() );
108 this.runningRemoteDownloadIds.add( this.remoteRepository.getId() );
110 File tempIndexDirectory = null;
111 StopWatch stopWatch = new StopWatch();
115 log.info( "start download remote index for remote repository " + this.remoteRepository.getId() );
116 IndexingContext indexingContext = remoteRepositoryAdmin.createIndexContext( this.remoteRepository );
118 // create a temp directory to download files
119 tempIndexDirectory = new File( indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex" );
120 File indexCacheDirectory = new File( indexingContext.getIndexDirectoryFile().getParent(), ".indexCache" );
121 indexCacheDirectory.mkdirs();
122 if ( tempIndexDirectory.exists() )
124 FileUtils.deleteDirectory( tempIndexDirectory );
126 tempIndexDirectory.mkdirs();
127 tempIndexDirectory.deleteOnExit();
128 String baseIndexUrl = indexingContext.getIndexUpdateUrl();
130 String wagonProtocol =
131 new URL( this.remoteRepository.getUrl() ).getProtocol() + ( ( this.networkProxy != null
132 && this.networkProxy.isUseNtlm() ) ? "-ntlm" : "" );
134 final Wagon wagon = wagonFactory.getWagon( wagonProtocol );
135 setupWagonReadTimeout( wagon );
137 wagon.addTransferListener( new DownloadListener() );
138 ProxyInfo proxyInfo = null;
139 if ( this.networkProxy != null )
141 proxyInfo = new ProxyInfo();
142 proxyInfo.setHost( this.networkProxy.getHost() );
143 proxyInfo.setPort( this.networkProxy.getPort() );
144 proxyInfo.setUserName( this.networkProxy.getUsername() );
145 proxyInfo.setPassword( this.networkProxy.getPassword() );
147 AuthenticationInfo authenticationInfo = null;
148 if ( this.remoteRepository.getUserName() != null )
150 authenticationInfo = new AuthenticationInfo();
151 authenticationInfo.setUserName( this.remoteRepository.getUserName() );
152 authenticationInfo.setPassword( this.remoteRepository.getPassword() );
154 wagon.connect( new Repository( this.remoteRepository.getId(), baseIndexUrl ), authenticationInfo,
157 File indexDirectory = indexingContext.getIndexDirectoryFile();
158 if ( !indexDirectory.exists() )
160 indexDirectory.mkdirs();
163 ResourceFetcher resourceFetcher = new WagonResourceFetcher( log, tempIndexDirectory, wagon );
164 IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
165 request.setForceFullUpdate( this.fullDownload );
166 request.setLocalIndexCacheDir( indexCacheDirectory );
168 this.indexUpdater.fetchAndUpdateIndex( request );
170 log.info( "time to download remote repository index for repository {}: {} s", this.remoteRepository.getId(),
171 ( stopWatch.getTime() / 1000 ) );
173 catch ( MalformedURLException e )
175 log.error( e.getMessage(), e );
176 throw new RuntimeException( e.getMessage(), e );
178 catch ( WagonFactoryException e )
180 log.error( e.getMessage(), e );
181 throw new RuntimeException( e.getMessage(), e );
183 catch ( ConnectionException e )
185 log.error( e.getMessage(), e );
186 throw new RuntimeException( e.getMessage(), e );
188 catch ( AuthenticationException e )
190 log.error( e.getMessage(), e );
191 throw new RuntimeException( e.getMessage(), e );
193 catch ( IOException e )
195 log.error( e.getMessage(), e );
196 throw new RuntimeException( e.getMessage(), e );
198 catch ( RepositoryAdminException e )
200 log.error( e.getMessage(), e );
201 throw new RuntimeException( e.getMessage(), e );
205 deleteDirectoryQuiet( tempIndexDirectory );
206 this.runningRemoteDownloadIds.remove( this.remoteRepository.getId() );
208 log.info( "end download remote index for remote repository " + this.remoteRepository.getId() );
211 private void deleteDirectoryQuiet( File f )
215 FileUtils.deleteDirectory( f );
217 catch ( IOException e )
219 log.warn( "skip error delete " + f + ": " + e.getMessage() );
223 private void setupWagonReadTimeout( Wagon wagon )
227 HttpConfiguration httpConfiguration = new HttpConfiguration().setAll(
228 new HttpMethodConfiguration().setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 ) );
229 Method setHttpConfigurationMethod =
230 wagon.getClass().getMethod( "setHttpConfiguration", HttpConfiguration.class );
231 setHttpConfigurationMethod.invoke( wagon, httpConfiguration );
233 catch ( Exception e )
235 log.debug( "unable to set download remote time out for index {}", e.getMessage(), e );
240 public static class DownloadListener
241 implements TransferListener
243 private Logger log = LoggerFactory.getLogger( getClass() );
249 public void transferInitiated( TransferEvent transferEvent )
251 reourceName = transferEvent.getResource().getName();
252 log.debug( "initiate transfer of {}", reourceName );
255 public void transferStarted( TransferEvent transferEvent )
257 reourceName = transferEvent.getResource().getName();
258 startTime = System.currentTimeMillis();
259 log.info( "start transfer of {}", transferEvent.getResource().getName() );
262 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
264 log.debug( "transfer of {} : {}/{}",
265 Arrays.asList( transferEvent.getResource().getName(), buffer.length, length ).toArray() );
268 public void transferCompleted( TransferEvent transferEvent )
270 reourceName = transferEvent.getResource().getName();
271 long endTime = System.currentTimeMillis();
272 log.info( "end of transfer file {}: {}s", transferEvent.getResource().getName(),
273 ( endTime - startTime ) / 1000 );
276 public void transferError( TransferEvent transferEvent )
278 log.info( "error of transfer file {}: {}", Arrays.asList( transferEvent.getResource().getName(),
279 transferEvent.getException().getMessage() ).toArray(
280 new Object[2] ), transferEvent.getException() );
283 public void debug( String message )
285 log.debug( "transfer debug {}", message );
289 private static class WagonResourceFetcher
290 implements ResourceFetcher
295 File tempIndexDirectory;
299 private WagonResourceFetcher( Logger log, File tempIndexDirectory, Wagon wagon )
302 this.tempIndexDirectory = tempIndexDirectory;
306 public void connect( String id, String url )
312 public void disconnect()
318 public InputStream retrieve( String name )
319 throws IOException, FileNotFoundException
323 log.info( "index update retrieve file, name:{}", name );
324 File file = new File( tempIndexDirectory, name );
330 wagon.get( name, file );
331 return new FileInputStream( file );
333 catch ( AuthorizationException e )
335 throw new IOException( e.getMessage() );
337 catch ( TransferFailedException e )
339 throw new IOException( e.getMessage() );
341 catch ( ResourceDoesNotExistException e )
343 throw new FileNotFoundException( e.getMessage() );