Author: nicolas
Date: Mon Sep  5 08:31:45 2011
New Revision: 1165204

URL: http://svn.apache.org/viewvc?rev=1165204&view=rev
Log:
[WAGON-346] use a class level synchronized block to ensure thread-safety

Modified:
    
maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
    
maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java

Modified: 
maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
URL: 
http://svn.apache.org/viewvc/maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java?rev=1165204&r1=1165203&r2=1165204&view=diff
==============================================================================
--- 
maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
 (original)
+++ 
maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
 Mon Sep  5 08:31:45 2011
@@ -100,49 +100,65 @@ public class LightweightHttpWagon
     public void fillInputData( InputData inputData )
         throws TransferFailedException, ResourceDoesNotExistException, 
AuthorizationException
     {
-        Resource resource = inputData.getResource();
-        try
+        synchronized ( LightweightHttpWagon.class )
         {
-            URL url = new URL( buildUrl( resource.getName() ) );
-            HttpURLConnection urlConnection = (HttpURLConnection) 
url.openConnection();
-            urlConnection.setRequestProperty( "Accept-Encoding", "gzip" );
-            if ( !useCache )
+            try
             {
-                urlConnection.setRequestProperty( "Pragma", "no-cache" );
-            }
+                Resource resource = inputData.getResource();
+                URL url = new URL( buildUrl( resource.getName() ) );
+                prepareOpenConnection();
+                HttpURLConnection urlConnection = (HttpURLConnection) 
url.openConnection();
+                urlConnection.setRequestProperty( "Accept-Encoding", "gzip" );
+                if ( !useCache )
+                {
+                    urlConnection.setRequestProperty( "Pragma", "no-cache" );
+                }
+
+                addHeaders( urlConnection );
 
-            addHeaders( urlConnection );
+                // TODO: handle all response codes
+                int responseCode = urlConnection.getResponseCode();
+                if ( responseCode == HttpURLConnection.HTTP_FORBIDDEN
+                        || responseCode == HttpURLConnection.HTTP_UNAUTHORIZED 
)
+                {
+                    throw new AuthorizationException( "Access denied to: " + 
buildUrl( resource.getName() ) );
+                }
 
-            // TODO: handle all response codes
-            int responseCode = urlConnection.getResponseCode();
-            if ( responseCode == HttpURLConnection.HTTP_FORBIDDEN
-                || responseCode == HttpURLConnection.HTTP_UNAUTHORIZED )
+                InputStream is = urlConnection.getInputStream();
+                String contentEncoding = urlConnection.getHeaderField( 
"Content-Encoding" );
+                boolean isGZipped = contentEncoding == null ? false : 
"gzip".equalsIgnoreCase( contentEncoding );
+                if ( isGZipped )
+                {
+                    is = new GZIPInputStream( is );
+                }
+                inputData.setInputStream( is );
+                resource.setLastModified( urlConnection.getLastModified() );
+                resource.setContentLength( urlConnection.getContentLength() );
+            }
+            catch ( MalformedURLException e )
             {
-                throw new AuthorizationException( "Access denied to: " + 
buildUrl( resource.getName() ) );
+                throw new ResourceDoesNotExistException( "Invalid repository 
URL: " + e.getMessage(), e );
             }
-
-            InputStream is = urlConnection.getInputStream();
-            String contentEncoding = urlConnection.getHeaderField( 
"Content-Encoding" );
-            boolean isGZipped = contentEncoding == null ? false : 
"gzip".equalsIgnoreCase( contentEncoding );
-            if ( isGZipped )
+            catch ( FileNotFoundException e )
             {
-                is = new GZIPInputStream( is );
+                throw new ResourceDoesNotExistException( "Unable to locate 
resource in repository", e );
+            }
+            catch ( IOException e )
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            }
+            catch (ConnectionException e)
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            }
+            catch (AuthenticationException e)
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            }
+            finally
+            {
+                cleanupAfterOpenConnection();
             }
-            inputData.setInputStream( is );
-            resource.setLastModified( urlConnection.getLastModified() );
-            resource.setContentLength( urlConnection.getContentLength() );
-        }
-        catch ( MalformedURLException e )
-        {
-            throw new ResourceDoesNotExistException( "Invalid repository URL: 
" + e.getMessage(), e );
-        }
-        catch ( FileNotFoundException e )
-        {
-            throw new ResourceDoesNotExistException( "Unable to locate 
resource in repository", e );
-        }
-        catch ( IOException e )
-        {
-            throw new TransferFailedException( "Error transferring file: " + 
e.getMessage(), e );
         }
     }
 
@@ -172,21 +188,37 @@ public class LightweightHttpWagon
     public void fillOutputData( OutputData outputData )
         throws TransferFailedException
     {
-        Resource resource = outputData.getResource();
-        try
+        synchronized ( LightweightHttpWagon.class )
         {
-            URL url = new URL( buildUrl( resource.getName() ) );
-            putConnection = (HttpURLConnection) url.openConnection();
-
-            addHeaders( putConnection );
+            try
+            {
+                Resource resource = outputData.getResource();
+                URL url = new URL( buildUrl( resource.getName() ) );
+                prepareOpenConnection();
+                putConnection = (HttpURLConnection) url.openConnection();
+
+                addHeaders( putConnection );
+
+                putConnection.setRequestMethod( "PUT" );
+                putConnection.setDoOutput( true );
+                outputData.setOutputStream( putConnection.getOutputStream() );
+            }
+            catch ( IOException e )
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            }
+            catch (ConnectionException e)
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            }
+            catch (AuthenticationException e)
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            } finally
+            {
+                cleanupAfterOpenConnection();
+            }
 
-            putConnection.setRequestMethod( "PUT" );
-            putConnection.setDoOutput( true );
-            outputData.setOutputStream( putConnection.getOutputStream() );
-        }
-        catch ( IOException e )
-        {
-            throw new TransferFailedException( "Error transferring file: " + 
e.getMessage(), e );
         }
     }
 
@@ -230,6 +262,15 @@ public class LightweightHttpWagon
     protected void openConnectionInternal()
         throws ConnectionException, AuthenticationException
     {
+    }
+
+    /**
+     * Prepare a call to URL.openConnection.
+     * This method requires a exclusive access to the JVM
+     */
+    private final void prepareOpenConnection()
+        throws ConnectionException, AuthenticationException
+    {
         previousHttpProxyHost = System.getProperty( "http.proxyHost" );
         previousHttpProxyPort = System.getProperty( "http.proxyPort" );
         previousProxyExclusions = System.getProperty( "http.nonProxyHosts" );
@@ -291,6 +332,13 @@ public class LightweightHttpWagon
         }
     }
 
+    private final void cleanupAfterOpenConnection()
+    {
+        setSystemProperty( "http.proxyHost", previousHttpProxyHost );
+        setSystemProperty( "http.proxyPort", previousHttpProxyPort );
+        setSystemProperty( "http.nonProxyHosts", previousProxyExclusions );
+    }
+
     public void closeConnection()
         throws ConnectionException
     {
@@ -298,10 +346,6 @@ public class LightweightHttpWagon
         {
             putConnection.disconnect();
         }
-
-        setSystemProperty( "http.proxyHost", previousHttpProxyHost );
-        setSystemProperty( "http.proxyPort", previousHttpProxyPort );
-        setSystemProperty( "http.nonProxyHosts", previousProxyExclusions );
     }
 
     public List getFileList( String destinationDirectory )
@@ -336,42 +380,58 @@ public class LightweightHttpWagon
     public boolean resourceExists( String resourceName )
         throws TransferFailedException, AuthorizationException
     {
-        HttpURLConnection headConnection;
-
-        try
+        synchronized ( LightweightHttpWagon.class )
         {
-            URL url = new URL( buildUrl( new Resource( resourceName 
).getName() ) );
-            headConnection = (HttpURLConnection) url.openConnection();
+            try
+            {
+                HttpURLConnection headConnection;
+                String s = buildUrl(new Resource(resourceName).getName());
+                URL url = new URL(s);
+                prepareOpenConnection();
+                headConnection = (HttpURLConnection) url.openConnection();
 
-            addHeaders( headConnection );
+                addHeaders( headConnection );
 
-            headConnection.setRequestMethod( "HEAD" );
-            headConnection.setDoOutput( true );
+                headConnection.setRequestMethod( "HEAD" );
+                headConnection.setDoOutput( true );
 
-            int statusCode = headConnection.getResponseCode();
+                int statusCode = headConnection.getResponseCode();
 
-            switch ( statusCode )
-            {
-                case HttpURLConnection.HTTP_OK:
-                    return true;
+                switch ( statusCode )
+                {
+                    case HttpURLConnection.HTTP_OK:
+                        return true;
 
-                case HttpURLConnection.HTTP_FORBIDDEN:
-                    throw new AuthorizationException( "Access denied to: " + 
url );
+                    case HttpURLConnection.HTTP_FORBIDDEN:
+                        throw new AuthorizationException( "Access denied to: " 
+ url );
 
-                case HttpURLConnection.HTTP_NOT_FOUND:
-                    return false;
+                    case HttpURLConnection.HTTP_NOT_FOUND:
+                        return false;
 
-                case HttpURLConnection.HTTP_UNAUTHORIZED:
-                    throw new AuthorizationException( "Access denied to: " + 
url );
+                    case HttpURLConnection.HTTP_UNAUTHORIZED:
+                        throw new AuthorizationException( "Access denied to: " 
+ url );
 
-                default:
-                    throw new TransferFailedException( "Failed to look for 
file: " + buildUrl( resourceName )
-                        + ". Return code is: " + statusCode );
+                    default:
+                        throw new TransferFailedException( "Failed to look for 
file: " + buildUrl( resourceName )
+                            + ". Return code is: " + statusCode );
+                }
+            }
+            catch ( IOException e )
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            }
+            catch (ConnectionException e)
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            }
+            catch (AuthenticationException e)
+            {
+                throw new TransferFailedException( "Error transferring file: " 
+ e.getMessage(), e );
+            }
+            finally
+            {
+                cleanupAfterOpenConnection();
             }
-        }
-        catch ( IOException e )
-        {
-            throw new TransferFailedException( "Error transferring file: " + 
e.getMessage(), e );
         }
     }
 

Modified: 
maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java
URL: 
http://svn.apache.org/viewvc/maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java?rev=1165204&r1=1165203&r2=1165204&view=diff
==============================================================================
--- 
maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java
 (original)
+++ 
maven/wagon/branches/wagon-1.x/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java
 Mon Sep  5 08:31:45 2011
@@ -26,6 +26,7 @@ import org.apache.maven.wagon.Wagon;
 import org.apache.maven.wagon.http.HttpWagonTestCase;
 import org.apache.maven.wagon.proxy.ProxyInfo;
 import org.apache.maven.wagon.repository.Repository;
+import org.mortbay.jetty.Server;
 
 /**
  * @author <a href="michal.mac...@dimatics.com">Michal Maczka</a>
@@ -57,59 +58,57 @@ public class LightweightHttpWagonTest
         proxyInfo.setHost( "proxyhost" );
         proxyInfo.setPort( 1234 );
         proxyInfo.setNonProxyHosts( "non" );
-
-        Repository repository = new Repository();
+        Server server = new Server( 0 );
+        addConnectors( server );
+        server.start();
 
         String proxyHost = System.getProperty( "http.proxyHost" );
         String proxyPort = System.getProperty( "http.proxyPort" );
         String nonProxyHosts = System.getProperty( "http.nonProxyHosts" );
+        try
+        {
+            Repository repository = new Repository( "test", getRepositoryUrl( 
server ) );
 
-        System.getProperties().remove( "http.proxyHost" );
-        System.getProperties().remove( "http.proxyPort" );
-
-        Wagon wagon = getWagon();
-
-        wagon.connect( repository, proxyInfo );
-
-        assertEquals( "proxyhost", System.getProperty( "http.proxyHost" ) );
-        assertEquals( "1234", System.getProperty( "http.proxyPort" ) );
-        assertEquals( "non", System.getProperty( "http.nonProxyHosts" ) );
-
-        wagon.disconnect();
-
-        assertNull( System.getProperty( "http.proxyHost" ) );
-        assertNull( System.getProperty( "http.proxyPort" ) );
-
-        System.setProperty( "http.proxyHost", "host" );
-        System.setProperty( "http.proxyPort", "port" );
-        System.setProperty( "http.nonProxyHosts", "hosts" );
+            System.getProperties().remove( "http.proxyHost" );
+            System.getProperties().remove( "http.proxyPort" );
 
-        wagon = getWagon();
+            Wagon wagon = getWagon();
 
-        wagon.connect( repository, proxyInfo );
+            wagon.connect( repository, proxyInfo );
+            wagon.resourceExists( "/test"  );
+            wagon.disconnect();
 
-        assertEquals( "proxyhost", System.getProperty( "http.proxyHost" ) );
-        assertEquals( "1234", System.getProperty( "http.proxyPort" ) );
-        assertEquals( "non", System.getProperty( "http.nonProxyHosts" ) );
+            assertNull( System.getProperty( "http.proxyHost" ) );
+            assertNull( System.getProperty( "http.proxyPort" ) );
 
-        wagon.disconnect();
+            System.setProperty( "http.proxyHost", "host" );
+            System.setProperty( "http.proxyPort", "port" );
+            System.setProperty( "http.nonProxyHosts", "hosts" );
 
-        assertEquals( "host", System.getProperty( "http.proxyHost" ) );
-        assertEquals( "port", System.getProperty( "http.proxyPort" ) );
-        assertEquals( "hosts", System.getProperty( "http.nonProxyHosts" ) );
+            wagon = getWagon();
 
-        wagon = getWagon();
+            wagon.connect( repository, proxyInfo );
+            wagon.resourceExists( "/test"  );
+            wagon.disconnect();
 
-        wagon.connect( repository );
+            assertEquals( "host", System.getProperty( "http.proxyHost" ) );
+            assertEquals( "port", System.getProperty( "http.proxyPort" ) );
+            assertEquals( "hosts", System.getProperty( "http.nonProxyHosts" ) 
);
 
-        assertNull( System.getProperty( "http.proxyHost" ) );
-        assertNull( System.getProperty( "http.proxyPort" ) );
+            wagon = getWagon();
 
-        wagon.disconnect();
+            wagon.connect( repository );
+            wagon.resourceExists( "/test"  );
+            wagon.disconnect();
 
-        assertEquals( "host", System.getProperty( "http.proxyHost" ) );
-        assertEquals( "port", System.getProperty( "http.proxyPort" ) );
-        assertEquals( "hosts", System.getProperty( "http.nonProxyHosts" ) );
+            assertEquals( "host", System.getProperty( "http.proxyHost" ) );
+            assertEquals( "port", System.getProperty( "http.proxyPort" ) );
+            assertEquals( "hosts", System.getProperty( "http.nonProxyHosts" ) 
);
+        }
+        finally
+        {
+            server.stop();
+        }
 
         if ( proxyHost != null )
         {


Reply via email to