Author: brianf Date: Mon Mar 17 11:40:31 2008 New Revision: 638016 URL: http://svn.apache.org/viewvc?rev=638016&view=rev Log: MNG-3461: allow more mirrorOf options
Modified: maven/artifact/trunk/src/main/java/org/apache/maven/artifact/manager/DefaultWagonManager.java maven/artifact/trunk/src/test/java/org/apache/maven/artifact/manager/DefaultWagonManagerTest.java Modified: maven/artifact/trunk/src/main/java/org/apache/maven/artifact/manager/DefaultWagonManager.java URL: http://svn.apache.org/viewvc/maven/artifact/trunk/src/main/java/org/apache/maven/artifact/manager/DefaultWagonManager.java?rev=638016&r1=638015&r2=638016&view=diff ============================================================================== --- maven/artifact/trunk/src/main/java/org/apache/maven/artifact/manager/DefaultWagonManager.java (original) +++ maven/artifact/trunk/src/main/java/org/apache/maven/artifact/manager/DefaultWagonManager.java Mon Mar 17 11:40:31 2008 @@ -21,6 +21,8 @@ import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.security.NoSuchAlgorithmException; import java.util.Collection; import java.util.HashMap; @@ -72,6 +74,8 @@ Contextualizable { private static final String WILDCARD = "*"; + + private static final String EXTERNAL_WILDCARD = "external:*"; private static final String[] CHECKSUM_IDS = {"md5", "sha1"}; @@ -770,6 +774,7 @@ } } + private void disconnectWagon( Wagon wagon ) { try @@ -811,9 +816,9 @@ } /** - * This method finds a matching mirror for the selected repository. If there is an exact match, - * this will be used. If there is no exact match, then the list of mirrors is examined to see - * if a pattern applies. + * This method finds a matching mirror for the selected repository. If there is an exact match, this will be used. + * If there is no exact match, then the list of mirrors is examined to see if a pattern applies. + * * @param originalRepository See if there is a mirror for this repository. * @return the selected mirror or null if none are found. */ @@ -840,36 +845,100 @@ } return selectedMirror; } - + /** - * This method checks if the pattern matches the originalRepository. Currently only wildcard - * matches are peformed on the repository id. Wildcard == '*' + * This method checks if the pattern matches the originalRepository. + * Valid patterns: + * * = everything + * external:* = everything not on the localhost and not file based. + * repo,repo1 = repo or repo1 + * *,!repo1 = everything except repo1 + * * @param originalRepository to compare for a match. * @param pattern used for match. Currently only '*' is supported. * @return true if the repository is a match to this pattern. */ - public boolean matchPattern (ArtifactRepository originalRepository, String pattern) + public boolean matchPattern( ArtifactRepository originalRepository, String pattern ) { - if (WILDCARD.equals( pattern ) || pattern.equals( originalRepository.getId() )) + boolean result = false; + String originalId = originalRepository.getId(); + + // simple checks first to short circuit processing below. + if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) ) { - return true; + result = true; } else { + // process the list + String[] repos = pattern.split( "," ); + for ( int i = 0; i < repos.length; i++ ) + { + String repo = repos[i]; + + // see if this is a negative match + if ( repo.length() > 1 && repo.startsWith( "!" ) ) + { + if ( originalId.equals( repo.substring( 1 ) ) ) + { + // explicitly exclude. Set result and stop processing. + result = false; + break; + } + } + // check for exact match + else if ( originalId.equals( repo ) ) + { + result = true; + break; + } + // check for external:* + else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) ) + { + result = true; + // don't stop processing in case a future segment explicitly excludes this repo + } + else if ( WILDCARD.equals( repo ) ) + { + result = true; + // don't stop processing in case a future segment explicitly excludes this repo + } + } + } + return result; + } + + /** + * Checks the URL to see if this repository refers to an external repository + * + * @param originalRepository + * @return true if external. + */ + public boolean isExternalRepo( ArtifactRepository originalRepository ) + { + try + { + URL url = new URL( originalRepository.getUrl() ); + return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" ) || url.getProtocol().equals( + "file" ) ); + } + catch ( MalformedURLException e ) + { + // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it return false; } } /** * Set the proxy used for a particular protocol. - * - * @param protocol the protocol (required) - * @param host the proxy host name (required) - * @param port the proxy port (required) - * @param username the username for the proxy, or null if there is none - * @param password the password for the proxy, or null if there is none - * @param nonProxyHosts the set of hosts not to use the proxy for. Follows Java system - * property format: <code>*.foo.com|localhost</code>. + * + * @param protocol the protocol (required) + * @param host the proxy host name (required) + * @param port the proxy port (required) + * @param username the username for the proxy, or null if there is none + * @param password the password for the proxy, or null if there is none + * @param nonProxyHosts the set of hosts not to use the proxy for. Follows Java system property format: + * <code>*.foo.com|localhost</code>. * @todo [BP] would be nice to configure this via plexus in some way */ public void addProxy( String protocol, Modified: maven/artifact/trunk/src/test/java/org/apache/maven/artifact/manager/DefaultWagonManagerTest.java URL: http://svn.apache.org/viewvc/maven/artifact/trunk/src/test/java/org/apache/maven/artifact/manager/DefaultWagonManagerTest.java?rev=638016&r1=638015&r2=638016&view=diff ============================================================================== --- maven/artifact/trunk/src/test/java/org/apache/maven/artifact/manager/DefaultWagonManagerTest.java (original) +++ maven/artifact/trunk/src/test/java/org/apache/maven/artifact/manager/DefaultWagonManagerTest.java Mon Mar 17 11:40:31 2008 @@ -58,6 +58,30 @@ wagonManager = (WagonManager) lookup( WagonManager.ROLE ); } + + /** + * checks the handling of urls + */ + public void testExternalURL() + { + DefaultWagonManager mgr = new DefaultWagonManager(); + assertTrue(mgr.isExternalRepo( getRepo( "foo", "http://somehost" ) )); + assertTrue(mgr.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) )); + assertTrue(mgr.isExternalRepo( getRepo( "foo", "ftp://somehost" ) )); + assertTrue(mgr.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) )); + assertTrue(mgr.isExternalRepo( getRepo( "foo", "http://" ) )); + //these are local + assertFalse(mgr.isExternalRepo( getRepo( "foo", "http://localhost" ) )); + assertFalse(mgr.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) )); + assertFalse(mgr.isExternalRepo( getRepo( "foo", "file:///somepath" ) )); + assertFalse(mgr.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) )); + + //not a proper url so returns false; + assertFalse(mgr.isExternalRepo( getRepo( "foo", "192.168.101.1" ) )); + assertFalse(mgr.isExternalRepo( getRepo( "foo", "" ) )); + } + + /** * Check that lookups with exact matches work and that no matches don't corrupt the repo. */ @@ -78,7 +102,7 @@ } - + /** * Check that wildcards don't override exact id matches. */ @@ -99,8 +123,74 @@ assertEquals( "http://wildcard", repo.getUrl() ); } + + /** + * Check that patterns are processed correctly + * Valid patterns: + * * = everything + * external:* = everything not on the localhost and not file based. + * repo,repo1 = repo or repo1 + * *,!repo1 = everything except repo1 + * + */ + public void testPatterns() + { + DefaultWagonManager mgr = new DefaultWagonManager(); + + assertTrue(mgr.matchPattern( getRepo("a"), "*" )); + assertTrue(mgr.matchPattern( getRepo("a"), "*," )); + assertTrue(mgr.matchPattern( getRepo("a"), ",*," )); + assertTrue(mgr.matchPattern( getRepo("a"), "*," )); + + assertTrue(mgr.matchPattern( getRepo("a"), "a" )); + assertTrue(mgr.matchPattern( getRepo("a"), "a," )); + assertTrue(mgr.matchPattern( getRepo("a"), ",a," )); + assertTrue(mgr.matchPattern( getRepo("a"), "a," )); + + assertFalse(mgr.matchPattern( getRepo("b"), "a" )); + assertFalse(mgr.matchPattern( getRepo("b"), "a," )); + assertFalse(mgr.matchPattern( getRepo("b"), ",a" )); + assertFalse(mgr.matchPattern( getRepo("b"), ",a," )); + + assertTrue(mgr.matchPattern( getRepo("a"), "a,b" )); + assertTrue(mgr.matchPattern( getRepo("b"), "a,b" )); + + assertFalse(mgr.matchPattern( getRepo("c"), "a,b" )); + + assertTrue(mgr.matchPattern( getRepo("a"), "*" )); + assertTrue(mgr.matchPattern( getRepo("a"), "*,b" )); + assertTrue(mgr.matchPattern( getRepo("a"), "*,!b" )); + + assertFalse(mgr.matchPattern( getRepo("a"), "*,!a" )); + assertFalse(mgr.matchPattern( getRepo("a"), "!a,*" )); + + assertTrue(mgr.matchPattern( getRepo("c"), "*,!a" )); + assertTrue(mgr.matchPattern( getRepo("c"), "!a,*" )); + + assertFalse(mgr.matchPattern( getRepo("c"), "!a,!c" )); + assertFalse(mgr.matchPattern( getRepo("d"), "!a,!c*" )); + } /** + * make sure the external if is fully exercised. We can assume file and ips are also handled because they + * have a separate test above. + */ + public void testPatternsWithExternal() + { + DefaultWagonManager mgr = new DefaultWagonManager(); + + assertTrue( mgr.matchPattern( getRepo( "a","http://localhost" ), "*" ) ); + assertFalse( mgr.matchPattern( getRepo( "a","http://localhost" ), "external:*" ) ); + + assertTrue( mgr.matchPattern( getRepo( "a","http://localhost" ), "external:*,a" ) ); + assertFalse( mgr.matchPattern( getRepo( "a","http://localhost" ), "external:*,!a" ) ); + assertTrue( mgr.matchPattern( getRepo( "a","http://localhost" ), "a,external:*" ) ); + assertFalse( mgr.matchPattern( getRepo( "a","http://localhost" ), "!a,external:*" ) ); + + assertFalse( mgr.matchPattern( getRepo( "c","http://localhost" ), "!a,external:*" ) ); + assertTrue( mgr.matchPattern( getRepo( "c","http://somehost" ), "!a,external:*" ) ); + } + /** * Build an ArtifactRepository object. * @param id * @param url @@ -109,6 +199,17 @@ private ArtifactRepository getRepo (String id, String url) { return (ArtifactRepository) new DefaultArtifactRepository(id,url,new DefaultRepositoryLayout()); + } + + /** + * Build an ArtifactRepository object. + * @param id + * @param url + * @return + */ + private ArtifactRepository getRepo (String id) + { + return getRepo(id,"http://something"); } public void testDefaultWagonManager()