maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [04/50] [abbrv] maven-aether git commit: Optimized non-proxy host matching in DefaultProxySelector
Date Sun, 01 May 2016 10:56:06 GMT
Optimized non-proxy host matching in DefaultProxySelector


Project: http://git-wip-us.apache.org/repos/asf/maven-aether/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-aether/commit/705cd01c
Tree: http://git-wip-us.apache.org/repos/asf/maven-aether/tree/705cd01c
Diff: http://git-wip-us.apache.org/repos/asf/maven-aether/diff/705cd01c

Branch: refs/heads/master
Commit: 705cd01c0b83d8d0cfe88486372c4ff7cb92a313
Parents: b7fc57c
Author: Benjamin Bentmann <bentmann@sonatype.com>
Authored: Sun May 25 16:33:12 2014 +0200
Committer: Benjamin Bentmann <bentmann@sonatype.com>
Committed: Sun May 25 16:33:12 2014 +0200

----------------------------------------------------------------------
 .../util/repository/DefaultProxySelector.java   | 48 +++++++++++++++-----
 .../repository/DefaultProxySelectorTest.java    | 28 ++++++------
 2 files changed, 50 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-aether/blob/705cd01c/aether-util/src/main/java/org/eclipse/aether/util/repository/DefaultProxySelector.java
----------------------------------------------------------------------
diff --git a/aether-util/src/main/java/org/eclipse/aether/util/repository/DefaultProxySelector.java
b/aether-util/src/main/java/org/eclipse/aether/util/repository/DefaultProxySelector.java
index c761aa7..fc30c55 100644
--- a/aether-util/src/main/java/org/eclipse/aether/util/repository/DefaultProxySelector.java
+++ b/aether-util/src/main/java/org/eclipse/aether/util/repository/DefaultProxySelector.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2013 Sonatype, Inc.
+ * Copyright (c) 2010, 2014 Sonatype, Inc.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -16,6 +16,7 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.util.regex.Pattern;
 
 import org.eclipse.aether.repository.Proxy;
 import org.eclipse.aether.repository.ProxySelector;
@@ -40,6 +41,10 @@ public final class DefaultProxySelector
      */
     public DefaultProxySelector add( Proxy proxy, String nonProxyHosts )
     {
+        if ( proxy == null )
+        {
+            throw new IllegalArgumentException( "proxy not specified" );
+        }
         proxies.add( new ProxyDef( proxy, nonProxyHosts ) );
 
         return this;
@@ -52,7 +57,7 @@ public final class DefaultProxySelector
         String host = repository.getHost();
         for ( ProxyDef proxy : proxies )
         {
-            if ( !isNonProxyHosts( host, proxy.nonProxyHosts ) )
+            if ( !proxy.nonProxyHosts.isNonProxyHost( host ) )
             {
                 String key = proxy.proxy.getType().toLowerCase( Locale.ENGLISH );
                 if ( !candidates.containsKey( key ) )
@@ -86,22 +91,41 @@ public final class DefaultProxySelector
         return ( proxy != null ) ? proxy.proxy : null;
     }
 
-    static boolean isNonProxyHosts( String host, String nonProxyHosts )
+    static class NonProxyHosts
     {
-        if ( host != null && nonProxyHosts != null && nonProxyHosts.length()
> 0 )
+
+        private final Pattern[] patterns;
+
+        public NonProxyHosts( String nonProxyHosts )
+        {
+            List<Pattern> patterns = new ArrayList<Pattern>();
+            if ( nonProxyHosts != null )
+            {
+                for ( StringTokenizer tokenizer = new StringTokenizer( nonProxyHosts, "|"
); tokenizer.hasMoreTokens(); )
+                {
+                    String pattern = tokenizer.nextToken();
+                    pattern = pattern.replace( ".", "\\." ).replace( "*", ".*" );
+                    patterns.add( Pattern.compile( pattern ) );
+                }
+            }
+            this.patterns = patterns.toArray( new Pattern[patterns.size()] );
+        }
+
+        boolean isNonProxyHost( String host )
         {
-            for ( StringTokenizer tokenizer = new StringTokenizer( nonProxyHosts, "|" );
tokenizer.hasMoreTokens(); )
+            if ( host != null )
             {
-                String pattern = tokenizer.nextToken();
-                pattern = pattern.replace( ".", "\\." ).replace( "*", ".*" );
-                if ( host.matches( pattern ) )
+                for ( Pattern pattern : patterns )
                 {
-                    return true;
+                    if ( pattern.matcher( host ).matches() )
+                    {
+                        return true;
+                    }
                 }
             }
+            return false;
         }
 
-        return false;
     }
 
     static class ProxyDef
@@ -109,12 +133,12 @@ public final class DefaultProxySelector
 
         final Proxy proxy;
 
-        final String nonProxyHosts;
+        final NonProxyHosts nonProxyHosts;
 
         public ProxyDef( Proxy proxy, String nonProxyHosts )
         {
             this.proxy = proxy;
-            this.nonProxyHosts = nonProxyHosts;
+            this.nonProxyHosts = new NonProxyHosts( nonProxyHosts );
         }
 
     }

http://git-wip-us.apache.org/repos/asf/maven-aether/blob/705cd01c/aether-util/src/test/java/org/eclipse/aether/util/repository/DefaultProxySelectorTest.java
----------------------------------------------------------------------
diff --git a/aether-util/src/test/java/org/eclipse/aether/util/repository/DefaultProxySelectorTest.java
b/aether-util/src/test/java/org/eclipse/aether/util/repository/DefaultProxySelectorTest.java
index ef25a86..1da7ac6 100644
--- a/aether-util/src/test/java/org/eclipse/aether/util/repository/DefaultProxySelectorTest.java
+++ b/aether-util/src/test/java/org/eclipse/aether/util/repository/DefaultProxySelectorTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2011 Sonatype, Inc.
+ * Copyright (c) 2010, 2014 Sonatype, Inc.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -20,27 +20,27 @@ import org.junit.Test;
 public class DefaultProxySelectorTest
 {
 
-    private boolean isNonProxyHosts( String host, String nonProxyHosts )
+    private boolean isNonProxyHost( String host, String nonProxyHosts )
     {
-        return DefaultProxySelector.isNonProxyHosts( host, nonProxyHosts );
+        return new DefaultProxySelector.NonProxyHosts( nonProxyHosts ).isNonProxyHost( host
);
     }
 
     @Test
-    public void testIsNonProxyHosts()
+    public void testIsNonProxyHost()
     {
-        assertFalse( isNonProxyHosts( "www.sonatype.org", null ) );
-        assertFalse( isNonProxyHosts( "www.sonatype.org", "" ) );
+        assertFalse( isNonProxyHost( "www.sonatype.org", null ) );
+        assertFalse( isNonProxyHost( "www.sonatype.org", "" ) );
 
-        assertTrue( isNonProxyHosts( "www.sonatype.org", "*" ) );
-        assertTrue( isNonProxyHosts( "www.sonatype.org", "*.org" ) );
-        assertTrue( isNonProxyHosts( "www.sonatype.org", "www.*" ) );
-        assertTrue( isNonProxyHosts( "www.sonatype.org", "www.*.org" ) );
+        assertTrue( isNonProxyHost( "www.sonatype.org", "*" ) );
+        assertTrue( isNonProxyHost( "www.sonatype.org", "*.org" ) );
+        assertTrue( isNonProxyHost( "www.sonatype.org", "www.*" ) );
+        assertTrue( isNonProxyHost( "www.sonatype.org", "www.*.org" ) );
 
-        assertFalse( isNonProxyHosts( "www.sonatype.org", "www.sonatype.com" ) );
-        assertFalse( isNonProxyHosts( "www.sonatype.org", "*.com" ) );
-        assertFalse( isNonProxyHosts( "www.sonatype.org", "sonatype.org" ) );
+        assertFalse( isNonProxyHost( "www.sonatype.org", "www.sonatype.com" ) );
+        assertFalse( isNonProxyHost( "www.sonatype.org", "*.com" ) );
+        assertFalse( isNonProxyHost( "www.sonatype.org", "sonatype.org" ) );
 
-        assertTrue( isNonProxyHosts( "www.sonatype.org", "*.com|*.org" ) );
+        assertTrue( isNonProxyHost( "www.sonatype.org", "*.com|*.org" ) );
     }
 
 }


Mime
View raw message