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.PostDownloadPolicy;
36 import org.apache.maven.archiva.policies.PreDownloadPolicy;
37 import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
38 import org.apache.maven.archiva.repository.ContentNotFoundException;
39 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
40 import org.apache.maven.archiva.repository.RemoteRepositoryContent;
41 import org.apache.maven.archiva.repository.RepositoryContentFactory;
42 import org.apache.maven.archiva.repository.RepositoryException;
43 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
44 import org.apache.maven.archiva.repository.layout.LayoutException;
45 import org.apache.maven.archiva.repository.metadata.MetadataTools;
46 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
47 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
48 import org.apache.maven.wagon.ConnectionException;
49 import org.apache.maven.wagon.ResourceDoesNotExistException;
50 import org.apache.maven.wagon.Wagon;
51 import org.apache.maven.wagon.WagonException;
52 import org.apache.maven.wagon.authentication.AuthenticationException;
53 import org.apache.maven.wagon.authentication.AuthenticationInfo;
54 import org.apache.maven.wagon.proxy.ProxyInfo;
55 import org.apache.maven.wagon.repository.Repository;
56 import org.codehaus.plexus.logging.AbstractLogEnabled;
57 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
58 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
59 import org.codehaus.plexus.registry.Registry;
60 import org.codehaus.plexus.registry.RegistryListener;
61 import org.codehaus.plexus.util.SelectorUtils;
64 import java.io.IOException;
65 import java.util.ArrayList;
66 import java.util.Collections;
67 import java.util.HashMap;
68 import java.util.List;
70 import java.util.Properties;
71 import java.util.Map.Entry;
74 * DefaultRepositoryProxyConnectors
76 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
78 * @plexus.component role-hint="default"
80 public class DefaultRepositoryProxyConnectors
81 extends AbstractLogEnabled
82 implements RepositoryProxyConnectors, RegistryListener, Initializable
87 private ArchivaConfiguration archivaConfiguration;
90 * @plexus.requirement role="org.apache.maven.wagon.Wagon"
92 private Map<String, Wagon> wagons;
97 private RepositoryContentFactory repositoryFactory;
100 * @plexus.requirement
102 private MetadataTools metadataTools;
105 * @plexus.requirement role="org.apache.maven.archiva.policies.PreDownloadPolicy"
107 private Map<String, PreDownloadPolicy> preDownloadPolicies;
110 * @plexus.requirement role="org.apache.maven.archiva.policies.PostDownloadPolicy"
112 private Map<String, PostDownloadPolicy> postDownloadPolicies;
115 * @plexus.requirement role-hint="default"
117 private UrlFailureCache urlFailureCache;
119 private Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<String, List<ProxyConnector>>();
121 private Map<String, ProxyInfo> networkProxyMap = new HashMap<String, ProxyInfo>();
124 * @plexus.requirement
126 private RepositoryContentConsumers consumers;
129 * Fetch an artifact from a remote repository.
131 * @param repository the managed repository to utilize for the request.
132 * @param artifact the artifact reference to fetch.
133 * @return the local file in the managed repository that was fetched, or null if the artifact was not (or
134 * could not be) fetched.
135 * @throws ProxyException if there was a problem fetching the artifact.
137 public File fetchFromProxies( ManagedRepositoryContent repository, ArtifactReference artifact )
138 throws ProxyException
140 File localFile = toLocalFile( repository, artifact );
142 Properties requestProperties = new Properties();
143 requestProperties.setProperty( "version", artifact.getVersion() );
145 List<ProxyConnector> connectors = getProxyConnectors( repository );
146 for ( ProxyConnector connector : connectors )
148 RemoteRepositoryContent targetRepository = connector.getTargetRepository();
149 String targetPath = targetRepository.toPath( artifact );
153 File downloadedFile = transferFile( connector, targetRepository, targetPath, localFile,
156 if ( fileExists( downloadedFile ) )
158 getLogger().debug( "Successfully transferred: " + downloadedFile.getAbsolutePath() );
159 return downloadedFile;
162 catch ( NotFoundException e )
164 getLogger().debug( "Artifact " + Keys.toKey( artifact ) + " not found on repository \""
165 + targetRepository.getRepository().getId() + "\"." );
167 catch ( NotModifiedException e )
169 getLogger().debug( "Artifact " + Keys.toKey( artifact ) + " not updated on repository \""
170 + targetRepository.getRepository().getId() + "\"." );
173 getLogger().debug( "Exhausted all target repositories, artifact " + Keys.toKey( artifact ) + " not found." );
179 * Fetch, from the proxies, a metadata.xml file for the groupId:artifactId:version metadata contents.
181 * @return the (local) metadata file that was fetched/merged/updated, or null if no metadata file exists.
183 public File fetchFromProxies( ManagedRepositoryContent repository, VersionedReference metadata )
184 throws ProxyException
186 File localFile = toLocalFile( repository, metadata );
188 Properties requestProperties = new Properties();
189 boolean metadataNeedsUpdating = false;
190 long originalTimestamp = getLastModified( localFile );
192 List<ProxyConnector> connectors = getProxyConnectors( repository );
193 for ( ProxyConnector connector : connectors )
195 RemoteRepositoryContent targetRepository = connector.getTargetRepository();
196 String targetPath = metadataTools.toPath( metadata );
198 File localRepoFile = toLocalRepoFile( repository, targetRepository, targetPath );
199 long originalMetadataTimestamp = getLastModified( localRepoFile );
203 transferFile( connector, targetRepository, targetPath, localRepoFile, requestProperties );
205 if ( hasBeenUpdated( localRepoFile, originalMetadataTimestamp ) )
207 metadataNeedsUpdating = true;
210 catch ( NotFoundException e )
212 getLogger().debug( "Versioned Metadata " + Keys.toKey( metadata )
213 + " not found on remote repository \""
214 + targetRepository.getRepository().getId() + "\"." );
216 catch ( NotModifiedException e )
218 getLogger().debug( "Versioned Metadata " + Keys.toKey( metadata )
219 + " not updated on remote repository \""
220 + targetRepository.getRepository().getId() + "\"." );
224 if ( hasBeenUpdated( localFile, originalTimestamp ) )
226 metadataNeedsUpdating = true;
229 if ( metadataNeedsUpdating )
233 metadataTools.updateMetadata( repository, metadata );
235 catch ( LayoutException e )
237 getLogger().warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage() );
238 // TODO: add into repository report?
240 catch ( RepositoryMetadataException e )
243 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
244 // TODO: add into repository report?
246 catch ( IOException e )
249 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
250 // TODO: add into repository report?
252 catch ( ContentNotFoundException e )
255 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
256 // TODO: add into repository report?
260 if ( fileExists( localFile ) )
268 private long getLastModified( File file )
270 if ( !file.exists() || !file.isFile() )
275 return file.lastModified();
278 private boolean hasBeenUpdated( File file, long originalLastModified )
280 if ( !file.exists() || !file.isFile() )
285 long currentLastModified = getLastModified( file );
286 return ( currentLastModified > originalLastModified );
290 * Fetch from the proxies a metadata.xml file for the groupId:artifactId metadata contents.
292 * @return the (local) metadata file that was fetched/merged/updated, or null if no metadata file exists.
293 * @throws ProxyException if there was a problem fetching the metadata file.
295 public File fetchFromProxies( ManagedRepositoryContent repository, ProjectReference metadata )
296 throws NotFoundException, NotModifiedException, ProxyException
298 File localFile = toLocalFile( repository, metadata );
300 Properties requestProperties = new Properties();
301 boolean metadataNeedsUpdating = false;
302 long originalTimestamp = getLastModified( localFile );
304 List<ProxyConnector> connectors = getProxyConnectors( repository );
305 for ( ProxyConnector connector : connectors )
307 RemoteRepositoryContent targetRepository = connector.getTargetRepository();
308 String targetPath = metadataTools.toPath( metadata );
310 File localRepoFile = toLocalRepoFile( repository, targetRepository, targetPath );
311 long originalMetadataTimestamp = getLastModified( localRepoFile );
314 transferFile( connector, targetRepository, targetPath, localRepoFile, requestProperties );
316 if ( hasBeenUpdated( localRepoFile, originalMetadataTimestamp ) )
318 metadataNeedsUpdating = true;
321 catch ( NotFoundException e )
323 getLogger().debug( "Project Metadata " + Keys.toKey( metadata ) + " not found on remote repository \""
324 + targetRepository.getRepository().getId() + "\"." );
326 catch ( NotModifiedException e )
328 getLogger().debug( "Project Metadata " + Keys.toKey( metadata )
329 + " not updated on remote repository \""
330 + targetRepository.getRepository().getId() + "\"." );
335 if ( hasBeenUpdated( localFile, originalTimestamp ) )
337 metadataNeedsUpdating = true;
340 if ( metadataNeedsUpdating )
344 metadataTools.updateMetadata( repository, metadata );
346 catch ( LayoutException e )
348 getLogger().warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage() );
349 // TODO: add into repository report?
351 catch ( RepositoryMetadataException e )
354 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
355 // TODO: add into repository report?
357 catch ( IOException e )
360 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
361 // TODO: add into repository report?
363 catch ( ContentNotFoundException e )
366 .warn( "Unable to update metadata " + localFile.getAbsolutePath() + ": " + e.getMessage(), e );
367 // TODO: add into repository report?
371 if ( fileExists( localFile ) )
379 private File toLocalRepoFile( ManagedRepositoryContent repository, RemoteRepositoryContent targetRepository,
382 String repoPath = metadataTools.getRepositorySpecificName( targetRepository, targetPath );
383 return new File( repository.getRepoRoot(), repoPath );
387 * Test if the provided ManagedRepositoryContent has any proxies configured for it.
389 public boolean hasProxies( ManagedRepositoryContent repository )
391 synchronized ( this.proxyConnectorMap )
393 return this.proxyConnectorMap.containsKey( repository.getId() );
397 private File toLocalFile( ManagedRepositoryContent repository, ArtifactReference artifact )
398 throws ProxyException
400 return repository.toFile( artifact );
403 private File toLocalFile( ManagedRepositoryContent repository, ProjectReference metadata )
404 throws ProxyException
406 String sourcePath = metadataTools.toPath( metadata );
407 return new File( repository.getRepoRoot(), sourcePath );
410 private File toLocalFile( ManagedRepositoryContent repository, VersionedReference metadata )
411 throws ProxyException
413 String sourcePath = metadataTools.toPath( metadata );
414 return new File( repository.getRepoRoot(), sourcePath );
418 * Simple method to test if the file exists on the local disk.
420 * @param file the file to test. (may be null)
421 * @return true if file exists. false if the file param is null, doesn't exist, or is not of type File.
423 private boolean fileExists( File file )
430 if ( !file.exists() )
435 if ( !file.isFile() )
444 * Perform the transfer of the file.
446 * @param connector the connector configuration to use.
447 * @param remoteRepository the remote repository get the resource from.
448 * @param remotePath the path in the remote repository to the resource to get.
449 * @param localFile the local file to place the downloaded resource into
450 * @param requestProperties the request properties to utilize for policy handling.
451 * @return the local file that was downloaded, or null if not downloaded.
452 * @throws NotFoundException if the file was not found on the remote repository.
453 * @throws NotModifiedException if the localFile was present, and the resource was present on remote repository,
454 * but the remote resource is not newer than the local File.
455 * @throws ProxyException if transfer was unsuccessful.
457 private File transferFile( ProxyConnector connector, RemoteRepositoryContent remoteRepository, String remotePath,
458 File localFile, Properties requestProperties )
459 throws NotFoundException, NotModifiedException, ProxyException
461 String url = remoteRepository.getURL().getUrl() + remotePath;
462 requestProperties.setProperty( "url", url );
464 // Is a whitelist defined?
465 if ( CollectionUtils.isNotEmpty( connector.getWhitelist() ) )
467 // Path must belong to whitelist.
468 if ( !matchesPattern( remotePath, connector.getWhitelist() ) )
470 getLogger().debug( "Path [" + remotePath + "] is not part of defined whitelist (skipping transfer)." );
475 // Is target path part of blacklist?
476 if ( matchesPattern( remotePath, connector.getBlacklist() ) )
478 getLogger().debug( "Path [" + remotePath + "] is part of blacklist (skipping transfer)." );
482 // Handle pre-download policy
483 if ( !applyPolicies( this.preDownloadPolicies, connector.getPolicies(), requestProperties, localFile ) )
485 getLogger().debug( "Failed pre-download policies - " + localFile.getAbsolutePath() );
487 if ( fileExists( localFile ) )
498 RepositoryURL repoUrl = remoteRepository.getURL();
499 String protocol = repoUrl.getProtocol();
500 wagon = (Wagon) wagons.get( protocol );
503 throw new ProxyException( "Unsupported target repository protocol: " + protocol );
506 boolean connected = connectToRepository( connector, wagon, remoteRepository );
509 localFile = transferSimpleFile( wagon, remoteRepository, remotePath, localFile );
511 transferChecksum( wagon, remoteRepository, remotePath, localFile, ".sha1" );
512 transferChecksum( wagon, remoteRepository, remotePath, localFile, ".md5" );
515 catch ( NotFoundException e )
517 // Do not cache url here.
520 catch ( NotModifiedException e )
522 // Do not cache url here.
525 catch ( ProxyException e )
527 urlFailureCache.cacheFailure( url );
538 catch ( ConnectionException e )
540 getLogger().warn( "Unable to disconnect wagon.", e );
545 // Handle post-download policies.
546 if ( !applyPolicies( this.postDownloadPolicies, connector.getPolicies(), requestProperties, localFile ) )
548 getLogger().debug( "Failed post-download policies - " + localFile.getAbsolutePath() );
550 if ( fileExists( localFile ) )
558 // Just-in-time update of the index and database by executing the consumers for this artifact
559 consumers.executeConsumers( connector.getSourceRepository().getRepository(), localFile );
561 // Everything passes.
567 * Quietly transfer the checksum file from the remote repository to the local file.
570 * @param wagon the wagon instance (should already be connected) to use.
571 * @param remoteRepository the remote repository to transfer from.
572 * @param remotePath the remote path to the resource to get.
573 * @param localFile the local file that should contain the downloaded contents
574 * @param type the type of checksum to transfer (example: ".md5" or ".sha1")
575 * @throws ProxyException if copying the downloaded file into place did not succeed.
577 private void transferChecksum( Wagon wagon, RemoteRepositoryContent remoteRepository, String remotePath,
578 File localFile, String type )
579 throws ProxyException
581 String url = remoteRepository.getURL().getUrl() + remotePath;
583 // Transfer checksum does not use the policy.
584 if ( urlFailureCache.hasFailedBefore( url + type ) )
591 File hashFile = new File( localFile.getAbsolutePath() + type );
592 transferSimpleFile( wagon, remoteRepository, remotePath + type, hashFile );
593 getLogger().debug( "Checksum" + type + " Downloaded: " + hashFile );
595 catch ( NotFoundException e )
597 getLogger().debug( "Transfer failed, checksum not found: " + url );
598 // Consume it, do not pass this on.
600 catch ( NotModifiedException e )
602 getLogger().debug( "Transfer skipped, checksum not modified: " + url );
603 // Consume it, do not pass this on.
605 catch ( ProxyException e )
607 urlFailureCache.cacheFailure( url + type );
608 getLogger().warn( "Transfer failed on checksum: " + url + " : " + e.getMessage(), e );
609 // Critical issue, pass it on.
615 * Perform the transfer of the remote file to the local file specified.
617 * @param wagon the wagon instance to use.
618 * @param remoteRepository the remote repository to use
619 * @param remotePath the remote path to attempt to get
620 * @param localFile the local file to save to
621 * @return The local file that was transfered.
622 * @throws ProxyException if there was a problem moving the downloaded file into place.
623 * @throws WagonException if there was a problem tranfering the file.
625 private File transferSimpleFile( Wagon wagon, RemoteRepositoryContent remoteRepository, String remotePath,
627 throws NotFoundException, NotModifiedException, ProxyException
629 assert ( remotePath != null );
631 // Transfer the file.
636 temp = new File( localFile.getAbsolutePath() + ".tmp" );
638 boolean success = false;
640 if ( !localFile.exists() )
642 getLogger().debug( "Retrieving " + remotePath + " from " + remoteRepository.getRepository().getName() );
643 wagon.get( remotePath, temp );
648 moveTempToTarget( temp, localFile );
651 // You wouldn't get here on failure, a WagonException would have been thrown.
652 getLogger().debug( "Downloaded successfully." );
656 getLogger().debug( "Retrieving " + remotePath + " from " + remoteRepository.getRepository().getName()
658 success = wagon.getIfNewer( remotePath, temp, localFile.lastModified() );
661 throw new NotModifiedException( "Not downloaded, as local file is newer than remote side: "
662 + localFile.getAbsolutePath() );
667 getLogger().debug( "Downloaded successfully." );
668 moveTempToTarget( temp, localFile );
674 catch ( ResourceDoesNotExistException e )
676 throw new NotFoundException( "Resource [" + remoteRepository.getURL() + "/" + remotePath
677 + "] does not exist: " + e.getMessage(), e );
679 catch ( WagonException e )
681 throw new ProxyException( "Download failure on resource [" + remoteRepository.getURL() + "/" + remotePath + "]:"
682 + e.getMessage(), e );
694 * Apply the policies.
696 * @param policies the map of policies to execute. (Map of String policy keys, to {@link DownloadPolicy} objects)
697 * @param settings the map of settings for the policies to execute. (Map of String policy keys, to String policy setting)
698 * @param request the request properties (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)})
699 * @param localFile the local file (utilized by the {@link DownloadPolicy#applyPolicy(String,Properties,File)})
700 * @return true if all of the policies passed, false if a policy failed.
702 private boolean applyPolicies( Map<String, ? extends DownloadPolicy> policies, Map<String, String> settings,
703 Properties request, File localFile )
705 for ( Entry<String, ? extends DownloadPolicy> entry : policies.entrySet() )
707 String key = (String) entry.getKey();
708 DownloadPolicy policy = entry.getValue();
709 String defaultSetting = policy.getDefaultOption();
710 String setting = StringUtils.defaultString( (String) settings.get( key ), defaultSetting );
712 getLogger().debug( "Applying [" + key + "] policy with [" + setting + "]" );
713 if ( !policy.applyPolicy( setting, request, localFile ) )
715 getLogger().debug( "Didn't pass the [" + key + "] policy." );
723 * Used to move the temporary file to its real destination. This is patterned from the way WagonManager handles
724 * its downloaded files.
726 * @param temp The completed download file
727 * @param target The final location of the downloaded file
728 * @throws ProxyException when the temp file cannot replace the target file
730 private void moveTempToTarget( File temp, File target )
731 throws ProxyException
733 if ( target.exists() && !target.delete() )
735 throw new ProxyException( "Unable to overwrite existing target file: " + target.getAbsolutePath() );
738 if ( !temp.renameTo( target ) )
740 getLogger().warn( "Unable to rename tmp file to its final name... resorting to copy command." );
744 FileUtils.copyFile( temp, target );
746 catch ( IOException e )
748 throw new ProxyException( "Cannot copy tmp file to its final location", e );
758 * Using wagon, connect to the remote repository.
760 * @param connector the connector configuration to utilize (for obtaining network proxy configuration from)
761 * @param wagon the wagon instance to establish the connection on.
762 * @param remoteRepository the remote repository to connect to.
763 * @return true if the connection was successful. false if not connected.
765 private boolean connectToRepository( ProxyConnector connector, Wagon wagon, RemoteRepositoryContent remoteRepository )
767 boolean connected = false;
769 ProxyInfo networkProxy = null;
770 synchronized ( this.networkProxyMap )
772 networkProxy = (ProxyInfo) this.networkProxyMap.get( connector.getProxyId() );
777 AuthenticationInfo authInfo = null;
778 String username = remoteRepository.getRepository().getUsername();
779 String password = remoteRepository.getRepository().getPassword();
780 if ( username != null && password != null )
783 "Using username " + username + " to connect to remote repository "
784 + remoteRepository.getURL() );
785 authInfo = new AuthenticationInfo();
786 authInfo.setUserName( username );
787 authInfo.setPassword( password );
791 getLogger().debug( "No authentication for remote repository needed" );
794 Repository wagonRepository = new Repository( remoteRepository.getId(), remoteRepository.getURL().toString() );
795 if ( networkProxy != null )
797 wagon.connect( wagonRepository, authInfo, networkProxy );
801 wagon.connect( wagonRepository, authInfo );
805 catch ( ConnectionException e )
808 "Could not connect to " + remoteRepository.getRepository().getName() + ": "
812 catch ( AuthenticationException e )
815 "Could not connect to " + remoteRepository.getRepository().getName() + ": "
824 * Tests whitelist and blacklist patterns against path.
826 * @param path the path to test.
827 * @param patterns the list of patterns to check.
828 * @return true if the path matches at least 1 pattern in the provided patterns list.
830 private boolean matchesPattern( String path, List<String> patterns )
832 if ( CollectionUtils.isEmpty( patterns ) )
837 for ( String pattern : patterns )
839 if ( SelectorUtils.matchPath( pattern, path, false ) )
849 * TODO: Ensure that list is correctly ordered based on configuration. See MRM-477
851 public List<ProxyConnector> getProxyConnectors( ManagedRepositoryContent repository )
853 synchronized ( this.proxyConnectorMap )
855 List<ProxyConnector> ret = (List<ProxyConnector>) this.proxyConnectorMap.get( repository.getId() );
858 return Collections.EMPTY_LIST;
861 Collections.sort( ret, ProxyConnectorOrderComparator.getInstance() );
866 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
868 if ( ConfigurationNames.isNetworkProxy( propertyName )
869 || ConfigurationNames.isManagedRepositories( propertyName )
870 || ConfigurationNames.isRemoteRepositories( propertyName )
871 || ConfigurationNames.isProxyConnector( propertyName ) )
873 initConnectorsAndNetworkProxies();
877 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
882 private void initConnectorsAndNetworkProxies()
884 synchronized ( this.proxyConnectorMap )
886 ProxyConnectorOrderComparator proxyOrderSorter = new ProxyConnectorOrderComparator();
887 this.proxyConnectorMap.clear();
889 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration()
890 .getProxyConnectors();
891 for ( ProxyConnectorConfiguration proxyConfig : proxyConfigs )
893 String key = proxyConfig.getSourceRepoId();
897 // Create connector object.
898 ProxyConnector connector = new ProxyConnector();
900 connector.setSourceRepository( repositoryFactory.getManagedRepositoryContent( proxyConfig
901 .getSourceRepoId() ) );
902 connector.setTargetRepository( repositoryFactory.getRemoteRepositoryContent( proxyConfig
903 .getTargetRepoId() ) );
905 connector.setProxyId( proxyConfig.getProxyId() );
906 connector.setPolicies( proxyConfig.getPolicies() );
907 connector.setOrder( proxyConfig.getOrder() );
909 // Copy any blacklist patterns.
910 List<String> blacklist = new ArrayList<String>();
911 if ( CollectionUtils.isNotEmpty( proxyConfig.getBlackListPatterns() ) )
913 blacklist.addAll( proxyConfig.getBlackListPatterns() );
915 connector.setBlacklist( blacklist );
917 // Copy any whitelist patterns.
918 List<String> whitelist = new ArrayList<String>();
919 if ( CollectionUtils.isNotEmpty( proxyConfig.getWhiteListPatterns() ) )
921 whitelist.addAll( proxyConfig.getWhiteListPatterns() );
923 connector.setWhitelist( whitelist );
925 // Get other connectors
926 List<ProxyConnector> connectors = this.proxyConnectorMap.get( key );
927 if ( connectors == null )
929 // Create if we are the first.
930 connectors = new ArrayList<ProxyConnector>();
933 // Add the connector.
934 connectors.add( connector );
936 // Ensure the list is sorted.
937 Collections.sort( connectors, proxyOrderSorter );
939 // Set the key to the list of connectors.
940 this.proxyConnectorMap.put( key, connectors );
942 catch ( RepositoryNotFoundException e )
944 getLogger().warn( "Unable to use proxy connector: " + e.getMessage(), e );
946 catch ( RepositoryException e )
948 getLogger().warn( "Unable to use proxy connector: " + e.getMessage(), e );
954 synchronized ( this.networkProxyMap )
956 this.networkProxyMap.clear();
958 List<NetworkProxyConfiguration> networkProxies = archivaConfiguration.getConfiguration()
959 .getNetworkProxies();
960 for ( NetworkProxyConfiguration networkProxyConfig : networkProxies )
962 String key = networkProxyConfig.getId();
964 ProxyInfo proxy = new ProxyInfo();
966 proxy.setType( networkProxyConfig.getProtocol() );
967 proxy.setHost( networkProxyConfig.getHost() );
968 proxy.setPort( networkProxyConfig.getPort() );
969 proxy.setUserName( networkProxyConfig.getUsername() );
970 proxy.setPassword( networkProxyConfig.getPassword() );
972 this.networkProxyMap.put( key, proxy );
977 public void initialize()
978 throws InitializationException
980 initConnectorsAndNetworkProxies();
981 archivaConfiguration.addChangeListener( this );