1 package org.apache.maven.archiva.proxy;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ConfigurationNames;
27 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
28 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.maven.archiva.model.ArtifactReference;
30 import org.apache.maven.archiva.model.Keys;
31 import org.apache.maven.archiva.model.ProjectReference;
32 import org.apache.maven.archiva.model.RepositoryURL;
33 import org.apache.maven.archiva.model.VersionedReference;
34 import org.apache.maven.archiva.policies.DownloadPolicy;
35 import org.apache.maven.archiva.policies.PolicyConfigurationException;
36 import org.apache.maven.archiva.policies.PolicyViolationException;
37 import org.apache.maven.archiva.policies.PostDownloadPolicy;
38 import org.apache.maven.archiva.policies.PreDownloadPolicy;
39 import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
40 import org.apache.maven.archiva.repository.ContentNotFoundException;
41 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
42 import org.apache.maven.archiva.repository.RemoteRepositoryContent;
43 import org.apache.maven.archiva.repository.RepositoryContentFactory;
44 import org.apache.maven.archiva.repository.RepositoryException;
45 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
46 import org.apache.maven.archiva.repository.layout.LayoutException;
47 import org.apache.maven.archiva.repository.metadata.MetadataTools;
48 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
49 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
50 import org.apache.maven.wagon.ConnectionException;
51 import org.apache.maven.wagon.ResourceDoesNotExistException;
52 import org.apache.maven.wagon.Wagon;
53 import org.apache.maven.wagon.WagonException;
54 import org.apache.maven.wagon.authentication.AuthenticationException;
55 import org.apache.maven.wagon.authentication.AuthenticationInfo;
56 import org.apache.maven.wagon.proxy.ProxyInfo;
57 import org.apache.maven.wagon.repository.Repository;
58 import org.codehaus.plexus.logging.AbstractLogEnabled;
59 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
60 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
61 import org.codehaus.plexus.registry.Registry;
62 import org.codehaus.plexus.registry.RegistryListener;
63 import org.codehaus.plexus.util.SelectorUtils;
66 import java.io.IOException;
67 import java.util.ArrayList;
68 import java.util.Collections;
69 import java.util.HashMap;
70 import java.util.List;
72 import java.util.Properties;
73 import java.util.Map.Entry;
76 * DefaultRepositoryProxyConnectors
78 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
80 * @plexus.component role-hint="default"
82 public class DefaultRepositoryProxyConnectors
83 extends AbstractLogEnabled
84 implements RepositoryProxyConnectors, RegistryListener, Initializable
89 private ArchivaConfiguration archivaConfiguration;
92 * @plexus.requirement role="org.apache.maven.wagon.Wagon"
94 private Map<String, Wagon> wagons;
99 private RepositoryContentFactory repositoryFactory;
102 * @plexus.requirement
104 private MetadataTools metadataTools;
107 * @plexus.requirement role="org.apache.maven.archiva.policies.PreDownloadPolicy"
109 private Map<String, PreDownloadPolicy> preDownloadPolicies;
112 * @plexus.requirement role="org.apache.maven.archiva.policies.PostDownloadPolicy"
114 private Map<String, PostDownloadPolicy> postDownloadPolicies;
117 * @plexus.requirement role-hint="default"
119 private UrlFailureCache urlFailureCache;
121 private Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<String, List<ProxyConnector>>();
123 private Map<String, ProxyInfo> networkProxyMap = new HashMap<String, ProxyInfo>();
126 * @plexus.requirement
128 private RepositoryContentConsumers consumers;
131 * Fetch an artifact from a remote repository.
133 * @param repository the managed repository to utilize for the request.
134 * @param artifact the artifact reference to fetch.
135 * @return the local file in the managed repository that was fetched, or null if the artifact was not (or
136 * could not be) fetched.
137 * @throws ProxyException if there was a problem fetching the artifact.
139 public File fetchFromProxies( ManagedRepositoryContent repository, ArtifactReference artifact )
140 throws ProxyException
142 File localFile = toLocalFile( repository, artifact );
144 Properties requestProperties = new Properties();
145 requestProperties.setProperty( "filetype", "artifact" );
146 requestProperties.setProperty( "version", artifact.getVersion() );
148 List<ProxyConnector> connectors = getProxyConnectors( repository );
149 for ( ProxyConnector connector : connectors )
151 RemoteRepositoryContent targetRepository = connector.getTargetRepository();
152 String targetPath = targetRepository.toPath( artifact );
156 File downloadedFile = transferFile( connector, targetRepository, targetPath, localFile,
159 if ( fileExists( downloadedFile ) )
161 getLogger().debug( "Successfully transferred: " + downloadedFile.getAbsolutePath() );
162 return downloadedFile;
165 catch ( NotFoundException e )
167 getLogger().debug( "Artifact " + Keys.toKey( artifact ) + " not found on repository \""
168 + targetRepository.getRepository().getId() + "\"." );
170 catch ( NotModifiedException e )
172 getLogger().debug( "Artifact " + Keys.toKey( artifact ) + " not updated on repository \""
173 + targetRepository.getRepository().getId() + "\"." );
176 getLogger().debug( "Exhausted all target repositories, artifact " + Keys.toKey( artifact ) + " not found." );
182 * Fetch, from the proxies, a metadata.xml file for the groupId:artifactId:version metadata contents.
184 * @return the (local) metadata file that was fetched/merged/updated, or null if no metadata file exists.
186 public File fetchFromProxies( ManagedRepositoryContent repository, VersionedReference metadata )
187 throws ProxyException
189 File localFile = toLocalFile( repository, metadata );
191 Properties requestProperties = new Properties();
192 requestProperties.setProperty( "filetype", "metadata" );
193 boolean metadataNeedsUpdating = false;
194 long originalTimestamp = getLastModified( localFile );
196 List<ProxyConnector> connectors = getProxyConnectors( repository );
197 for ( ProxyConnector connector : connectors )
199 RemoteRepositoryContent targetRepository = connector.getTargetRepository();
200 String targetPath = metadataTools.toPath( metadata );
202 File localRepoFile = toLocalRepoFile( repository, targetRepository, targetPath );
203 long originalMetadataTimestamp = getLastModified( localRepoFile );
207 transferFile( connector, targetRepository, targetPath, localRepoFile, requestProperties );
209 if ( hasBeenUpdated( localRepoFile, originalMetadataTimestamp ) )
211 metadataNeedsUpdating = true;
214 catch ( NotFoundException e )
216 getLogger().debug( "Versioned Metadata " + Keys.toKey( metadata )
217 + " not found on remote repository \""
218 + targetRepository.getRepository().getId() + "\"." );
220 catch ( NotModifiedException e )
222 getLogger().debug( "Versioned Metadata " + Keys.toKey( metadata )
223 + " not updated on remote repository \""
224 + targetRepository.getRepository().getId() + "\"." );
228 if ( hasBeenUpdated( localFile, originalTimestamp ) )
230 metadataNeedsUpdating = true;
233 if ( metadataNeedsUpdating )
237 metadataTools.updateMetadata( repository, metadata );
239 catch ( LayoutException e )
241 getLogger().warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage() );
242 // TODO: add into repository report?
244 catch ( RepositoryMetadataException e )
247 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
248 // TODO: add into repository report?
250 catch ( IOException e )
253 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
254 // TODO: add into repository report?
256 catch ( ContentNotFoundException e )
259 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
260 // TODO: add into repository report?
264 if ( fileExists( localFile ) )
272 private long getLastModified( File file )
274 if ( !file.exists() || !file.isFile() )
279 return file.lastModified();
282 private boolean hasBeenUpdated( File file, long originalLastModified )
284 if ( !file.exists() || !file.isFile() )
289 long currentLastModified = getLastModified( file );
290 return ( currentLastModified > originalLastModified );
294 * Fetch from the proxies a metadata.xml file for the groupId:artifactId metadata contents.
296 * @return the (local) metadata file that was fetched/merged/updated, or null if no metadata file exists.
297 * @throws ProxyException if there was a problem fetching the metadata file.
299 public File fetchFromProxies( ManagedRepositoryContent repository, ProjectReference metadata )
300 throws NotFoundException, NotModifiedException, ProxyException
302 File localFile = toLocalFile( repository, metadata );
304 Properties requestProperties = new Properties();
305 requestProperties.setProperty( "filetype", "metadata" );
306 boolean metadataNeedsUpdating = false;
307 long originalTimestamp = getLastModified( localFile );
309 List<ProxyConnector> connectors = getProxyConnectors( repository );
310 for ( ProxyConnector connector : connectors )
312 RemoteRepositoryContent targetRepository = connector.getTargetRepository();
313 String targetPath = metadataTools.toPath( metadata );
315 File localRepoFile = toLocalRepoFile( repository, targetRepository, targetPath );
316 long originalMetadataTimestamp = getLastModified( localRepoFile );
319 transferFile( connector, targetRepository, targetPath, localRepoFile, requestProperties );
321 if ( hasBeenUpdated( localRepoFile, originalMetadataTimestamp ) )
323 metadataNeedsUpdating = true;
326 catch ( NotFoundException e )
328 getLogger().debug( "Project Metadata " + Keys.toKey( metadata ) + " not found on remote repository \""
329 + targetRepository.getRepository().getId() + "\"." );
331 catch ( NotModifiedException e )
333 getLogger().debug( "Project Metadata " + Keys.toKey( metadata )
334 + " not updated on remote repository \""
335 + targetRepository.getRepository().getId() + "\"." );
340 if ( hasBeenUpdated( localFile, originalTimestamp ) )
342 metadataNeedsUpdating = true;
345 if ( metadataNeedsUpdating )
349 metadataTools.updateMetadata( repository, metadata );
351 catch ( LayoutException e )
353 getLogger().warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage() );
354 // TODO: add into repository report?
356 catch ( RepositoryMetadataException e )
359 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
360 // TODO: add into repository report?
362 catch ( IOException e )
365 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
366 // TODO: add into repository report?
368 catch ( ContentNotFoundException e )
371 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
372 // TODO: add into repository report?
376 if ( fileExists( localFile ) )
384 private File toLocalRepoFile( ManagedRepositoryContent repository, RemoteRepositoryContent targetRepository,
387 String repoPath = metadataTools.getRepositorySpecificName( targetRepository, targetPath );
388 return new File( repository.getRepoRoot(), repoPath );
392 * Test if the provided ManagedRepositoryContent has any proxies configured for it.
394 public boolean hasProxies( ManagedRepositoryContent repository )
396 synchronized ( this.proxyConnectorMap )
398 return this.proxyConnectorMap.containsKey( repository.getId() );
402 private File toLocalFile( ManagedRepositoryContent repository, ArtifactReference artifact )
403 throws ProxyException
405 return repository.toFile( artifact );
408 private File toLocalFile( ManagedRepositoryContent repository, ProjectReference metadata )
409 throws ProxyException
411 String sourcePath = metadataTools.toPath( metadata );
412 return new File( repository.getRepoRoot(), sourcePath );
415 private File toLocalFile( ManagedRepositoryContent repository, VersionedReference metadata )
416 throws ProxyException
418 String sourcePath = metadataTools.toPath( metadata );
419 return new File( repository.getRepoRoot(), sourcePath );
423 * Simple method to test if the file exists on the local disk.
425 * @param file the file to test. (may be null)
426 * @return true if file exists. false if the file param is null, doesn't exist, or is not of type File.
428 private boolean fileExists( File file )
435 if ( !file.exists() )
440 if ( !file.isFile() )
449 * Perform the transfer of the file.
451 * @param connector the connector configuration to use.
452 * @param remoteRepository the remote repository get the resource from.
453 * @param remotePath the path in the remote repository to the resource to get.
454 * @param localFile the local file to place the downloaded resource into
455 * @param requestProperties the request properties to utilize for policy handling.
456 * @return the local file that was downloaded, or null if not downloaded.
457 * @throws NotFoundException if the file was not found on the remote repository.
458 * @throws NotModifiedException if the localFile was present, and the resource was present on remote repository,
459 * but the remote resource is not newer than the local File.
460 * @throws ProxyException if transfer was unsuccessful.
462 private File transferFile( ProxyConnector connector, RemoteRepositoryContent remoteRepository, String remotePath,
463 File localFile, Properties requestProperties )
464 throws NotFoundException, NotModifiedException, ProxyException
466 String url = remoteRepository.getURL().getUrl() + remotePath;
467 requestProperties.setProperty( "url", url );
469 // Is a whitelist defined?
470 if ( CollectionUtils.isNotEmpty( connector.getWhitelist() ) )
472 // Path must belong to whitelist.
473 if ( !matchesPattern( remotePath, connector.getWhitelist() ) )
475 getLogger().debug( "Path [" + remotePath + "] is not part of defined whitelist (skipping transfer)." );
480 // Is target path part of blacklist?
481 if ( matchesPattern( remotePath, connector.getBlacklist() ) )
483 getLogger().debug( "Path [" + remotePath + "] is part of blacklist (skipping transfer)." );
487 // Handle pre-download policy
490 validatePolicies( this.preDownloadPolicies, connector.getPolicies(), requestProperties, localFile );
492 catch ( PolicyViolationException e )
494 String emsg = "Transfer not attempted on " + url + " : " + e.getMessage();
495 if ( fileExists( localFile ) )
497 getLogger().info( emsg + ": using already present local file." );
501 getLogger().info( emsg );
508 RepositoryURL repoUrl = remoteRepository.getURL();
509 String protocol = repoUrl.getProtocol();
510 wagon = (Wagon) wagons.get( protocol );
513 throw new ProxyException( "Unsupported target repository protocol: " + protocol );
516 boolean connected = connectToRepository( connector, wagon, remoteRepository );
519 localFile = transferSimpleFile( wagon, remoteRepository, remotePath, localFile );
521 transferChecksum( wagon, remoteRepository, remotePath, localFile, ".sha1" );
522 transferChecksum( wagon, remoteRepository, remotePath, localFile, ".md5" );
525 catch ( NotFoundException e )
527 // Do not cache url here.
530 catch ( NotModifiedException e )
532 // Do not cache url here.
535 catch ( ProxyException e )
537 urlFailureCache.cacheFailure( url );
548 catch ( ConnectionException e )
550 getLogger().warn( "Unable to disconnect wagon.", e );
555 // Handle post-download policies.
558 validatePolicies( this.postDownloadPolicies, connector.getPolicies(), requestProperties, localFile );
560 catch ( PolicyViolationException e )
562 getLogger().info( "Transfer invalidated from " + url + " : " + e.getMessage() );
563 if ( fileExists( localFile ) )
571 // Just-in-time update of the index and database by executing the consumers for this artifact
572 consumers.executeConsumers( connector.getSourceRepository().getRepository(), localFile );
574 // Everything passes.
580 * Quietly transfer the checksum file from the remote repository to the local file.
583 * @param wagon the wagon instance (should already be connected) to use.
584 * @param remoteRepository the remote repository to transfer from.
585 * @param remotePath the remote path to the resource to get.
586 * @param localFile the local file that should contain the downloaded contents
587 * @param type the type of checksum to transfer (example: ".md5" or ".sha1")
588 * @throws ProxyException if copying the downloaded file into place did not succeed.
590 private void transferChecksum( Wagon wagon, RemoteRepositoryContent remoteRepository, String remotePath,
591 File localFile, String type )
592 throws ProxyException
594 String url = remoteRepository.getURL().getUrl() + remotePath;
596 // Transfer checksum does not use the policy.
597 if ( urlFailureCache.hasFailedBefore( url + type ) )
604 File hashFile = new File( localFile.getAbsolutePath() + type );
605 transferSimpleFile( wagon, remoteRepository, remotePath + type, hashFile );
606 getLogger().debug( "Checksum" + type + " Downloaded: " + hashFile );
608 catch ( NotFoundException e )
610 getLogger().debug( "Transfer failed, checksum not found: " + url );
611 // Consume it, do not pass this on.
613 catch ( NotModifiedException e )
615 getLogger().debug( "Transfer skipped, checksum not modified: " + url );
616 // Consume it, do not pass this on.
618 catch ( ProxyException e )
620 urlFailureCache.cacheFailure( url + type );
621 getLogger().warn( "Transfer failed on checksum: " + url + " : " + e.getMessage(), e );
622 // Critical issue, pass it on.
628 * Perform the transfer of the remote file to the local file specified.
630 * @param wagon the wagon instance to use.
631 * @param remoteRepository the remote repository to use
632 * @param remotePath the remote path to attempt to get
633 * @param localFile the local file to save to
634 * @return The local file that was transfered.
635 * @throws ProxyException if there was a problem moving the downloaded file into place.
636 * @throws WagonException if there was a problem tranfering the file.
638 private File transferSimpleFile( Wagon wagon, RemoteRepositoryContent remoteRepository, String remotePath,
640 throws NotFoundException, NotModifiedException, ProxyException
642 assert ( remotePath != null );
644 // Transfer the file.
649 temp = new File( localFile.getAbsolutePath() + ".tmp" );
651 boolean success = false;
653 if ( !localFile.exists() )
655 getLogger().debug( "Retrieving " + remotePath + " from " + remoteRepository.getRepository().getName() );
656 wagon.get( remotePath, temp );
661 moveTempToTarget( temp, localFile );
664 // You wouldn't get here on failure, a WagonException would have been thrown.
665 getLogger().debug( "Downloaded successfully." );
669 getLogger().debug( "Retrieving " + remotePath + " from " + remoteRepository.getRepository().getName()
671 success = wagon.getIfNewer( remotePath, temp, localFile.lastModified() );
674 throw new NotModifiedException( "Not downloaded, as local file is newer than remote side: "
675 + localFile.getAbsolutePath() );
680 getLogger().debug( "Downloaded successfully." );
681 moveTempToTarget( temp, localFile );
687 catch ( ResourceDoesNotExistException e )
689 throw new NotFoundException( "Resource [" + remoteRepository.getURL() + "/" + remotePath
690 + "] does not exist: " + e.getMessage(), e );
692 catch ( WagonException e )
694 throw new ProxyException( "Download failure on resource [" + remoteRepository.getURL() + "/" + remotePath + "]:"
695 + e.getMessage(), e );
707 * Apply the policies.
709 * @param policies the map of policies to execute. (Map of String policy keys, to {@link DownloadPolicy} objects)
710 * @param settings the map of settings for the policies to execute. (Map of String policy keys, to String policy setting)
711 * @param request the request properties (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)})
712 * @param localFile the local file (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)})
714 private void validatePolicies( Map<String, ? extends DownloadPolicy> policies, Map<String, String> settings,
715 Properties request, File localFile )
716 throws PolicyViolationException
718 for ( Entry<String, ? extends DownloadPolicy> entry : policies.entrySet() )
720 String key = (String) entry.getKey();
721 DownloadPolicy policy = entry.getValue();
722 String defaultSetting = policy.getDefaultOption();
723 String setting = StringUtils.defaultString( (String) settings.get( key ), defaultSetting );
725 getLogger().debug( "Applying [" + key + "] policy with [" + setting + "]" );
728 policy.applyPolicy( setting, request, localFile );
730 catch ( PolicyConfigurationException e )
732 getLogger().error( e.getMessage(), e );
738 * Used to move the temporary file to its real destination. This is patterned from the way WagonManager handles
739 * its downloaded files.
741 * @param temp The completed download file
742 * @param target The final location of the downloaded file
743 * @throws ProxyException when the temp file cannot replace the target file
745 private void moveTempToTarget( File temp, File target )
746 throws ProxyException
748 if ( target.exists() && !target.delete() )
750 throw new ProxyException( "Unable to overwrite existing target file: " + target.getAbsolutePath() );
753 if ( !temp.renameTo( target ) )
755 getLogger().warn( "Unable to rename tmp file to its final name... resorting to copy command." );
759 FileUtils.copyFile( temp, target );
761 catch ( IOException e )
763 throw new ProxyException( "Cannot copy tmp file to its final location", e );
773 * Using wagon, connect to the remote repository.
775 * @param connector the connector configuration to utilize (for obtaining network proxy configuration from)
776 * @param wagon the wagon instance to establish the connection on.
777 * @param remoteRepository the remote repository to connect to.
778 * @return true if the connection was successful. false if not connected.
780 private boolean connectToRepository( ProxyConnector connector, Wagon wagon, RemoteRepositoryContent remoteRepository )
782 boolean connected = false;
784 ProxyInfo networkProxy = null;
785 synchronized ( this.networkProxyMap )
787 networkProxy = (ProxyInfo) this.networkProxyMap.get( connector.getProxyId() );
792 AuthenticationInfo authInfo = null;
793 String username = remoteRepository.getRepository().getUsername();
794 String password = remoteRepository.getRepository().getPassword();
795 if ( username != null && password != null )
798 "Using username " + username + " to connect to remote repository "
799 + remoteRepository.getURL() );
800 authInfo = new AuthenticationInfo();
801 authInfo.setUserName( username );
802 authInfo.setPassword( password );
806 getLogger().debug( "No authentication for remote repository needed" );
809 Repository wagonRepository = new Repository( remoteRepository.getId(), remoteRepository.getURL().toString() );
810 if ( networkProxy != null )
812 wagon.connect( wagonRepository, authInfo, networkProxy );
816 wagon.connect( wagonRepository, authInfo );
820 catch ( ConnectionException e )
823 "Could not connect to " + remoteRepository.getRepository().getName() + ": "
827 catch ( AuthenticationException e )
830 "Could not connect to " + remoteRepository.getRepository().getName() + ": "
839 * Tests whitelist and blacklist patterns against path.
841 * @param path the path to test.
842 * @param patterns the list of patterns to check.
843 * @return true if the path matches at least 1 pattern in the provided patterns list.
845 private boolean matchesPattern( String path, List<String> patterns )
847 if ( CollectionUtils.isEmpty( patterns ) )
852 for ( String pattern : patterns )
854 if ( SelectorUtils.matchPath( pattern, path, false ) )
864 * TODO: Ensure that list is correctly ordered based on configuration. See MRM-477
866 public List<ProxyConnector> getProxyConnectors( ManagedRepositoryContent repository )
868 synchronized ( this.proxyConnectorMap )
870 List<ProxyConnector> ret = (List<ProxyConnector>) this.proxyConnectorMap.get( repository.getId() );
873 return Collections.EMPTY_LIST;
876 Collections.sort( ret, ProxyConnectorOrderComparator.getInstance() );
881 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
883 if ( ConfigurationNames.isNetworkProxy( propertyName )
884 || ConfigurationNames.isManagedRepositories( propertyName )
885 || ConfigurationNames.isRemoteRepositories( propertyName )
886 || ConfigurationNames.isProxyConnector( propertyName ) )
888 initConnectorsAndNetworkProxies();
892 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
897 private void initConnectorsAndNetworkProxies()
899 synchronized ( this.proxyConnectorMap )
901 ProxyConnectorOrderComparator proxyOrderSorter = new ProxyConnectorOrderComparator();
902 this.proxyConnectorMap.clear();
904 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration()
905 .getProxyConnectors();
906 for ( ProxyConnectorConfiguration proxyConfig : proxyConfigs )
908 String key = proxyConfig.getSourceRepoId();
912 // Create connector object.
913 ProxyConnector connector = new ProxyConnector();
915 connector.setSourceRepository( repositoryFactory.getManagedRepositoryContent( proxyConfig
916 .getSourceRepoId() ) );
917 connector.setTargetRepository( repositoryFactory.getRemoteRepositoryContent( proxyConfig
918 .getTargetRepoId() ) );
920 connector.setProxyId( proxyConfig.getProxyId() );
921 connector.setPolicies( proxyConfig.getPolicies() );
922 connector.setOrder( proxyConfig.getOrder() );
924 // Copy any blacklist patterns.
925 List<String> blacklist = new ArrayList<String>();
926 if ( CollectionUtils.isNotEmpty( proxyConfig.getBlackListPatterns() ) )
928 blacklist.addAll( proxyConfig.getBlackListPatterns() );
930 connector.setBlacklist( blacklist );
932 // Copy any whitelist patterns.
933 List<String> whitelist = new ArrayList<String>();
934 if ( CollectionUtils.isNotEmpty( proxyConfig.getWhiteListPatterns() ) )
936 whitelist.addAll( proxyConfig.getWhiteListPatterns() );
938 connector.setWhitelist( whitelist );
940 // Get other connectors
941 List<ProxyConnector> connectors = this.proxyConnectorMap.get( key );
942 if ( connectors == null )
944 // Create if we are the first.
945 connectors = new ArrayList<ProxyConnector>();
948 // Add the connector.
949 connectors.add( connector );
951 // Ensure the list is sorted.
952 Collections.sort( connectors, proxyOrderSorter );
954 // Set the key to the list of connectors.
955 this.proxyConnectorMap.put( key, connectors );
957 catch ( RepositoryNotFoundException e )
959 getLogger().warn( "Unable to use proxy connector: " + e.getMessage(), e );
961 catch ( RepositoryException e )
963 getLogger().warn( "Unable to use proxy connector: " + e.getMessage(), e );
969 synchronized ( this.networkProxyMap )
971 this.networkProxyMap.clear();
973 List<NetworkProxyConfiguration> networkProxies = archivaConfiguration.getConfiguration()
974 .getNetworkProxies();
975 for ( NetworkProxyConfiguration networkProxyConfig : networkProxies )
977 String key = networkProxyConfig.getId();
979 ProxyInfo proxy = new ProxyInfo();
981 proxy.setType( networkProxyConfig.getProtocol() );
982 proxy.setHost( networkProxyConfig.getHost() );
983 proxy.setPort( networkProxyConfig.getPort() );
984 proxy.setUserName( networkProxyConfig.getUsername() );
985 proxy.setPassword( networkProxyConfig.getPassword() );
987 this.networkProxyMap.put( key, proxy );
992 public void initialize()
993 throws InitializationException
995 initConnectorsAndNetworkProxies();
996 archivaConfiguration.addChangeListener( this );