incubator-cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From h...@apache.org
Subject git commit: HttpClient needs releaseConnection method call
Date Thu, 29 Nov 2012 19:24:37 GMT
Updated Branches:
  refs/heads/master fefec372a -> a28f4cac3


HttpClient needs releaseConnection method call

reviewboard: https://reviews.apache.org/r/8186/

Signed-off-by: Hugo Trippaers <trippie@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/a28f4cac
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/a28f4cac
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/a28f4cac

Branch: refs/heads/master
Commit: a28f4cac3c6e98439d92d72ed6a29f89ecc2c7b5
Parents: fefec37
Author: Hiroaki Kawai <kawai@stratosphere.co.jp>
Authored: Thu Nov 29 20:23:30 2012 +0100
Committer: Hugo Trippaers <trippie@gmail.com>
Committed: Thu Nov 29 20:23:30 2012 +0100

----------------------------------------------------------------------
 agent/src/com/cloud/agent/AgentShell.java          |    2 +
 .../com/cloud/bridge/io/S3CAStorBucketAdapter.java |   56 +++++++++------
 .../src/com/cloud/network/nicira/NiciraNvpApi.java |   15 ++++-
 .../cloud/cluster/ClusterServiceServletImpl.java   |   20 ++----
 server/src/com/cloud/maint/UpgradeManagerImpl.java |    2 +
 5 files changed, 57 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/a28f4cac/agent/src/com/cloud/agent/AgentShell.java
----------------------------------------------------------------------
diff --git a/agent/src/com/cloud/agent/AgentShell.java b/agent/src/com/cloud/agent/AgentShell.java
index 774f222..e3d1063 100644
--- a/agent/src/com/cloud/agent/AgentShell.java
+++ b/agent/src/com/cloud/agent/AgentShell.java
@@ -231,6 +231,7 @@ public class AgentShell implements IAgentShell {
         int response;
         response = client.executeMethod(method);
         if (response != HttpURLConnection.HTTP_OK) {
+            method.releaseConnection();
             s_logger.warn("Retrieving from " + url + " gives response code: "
                     + response);
             throw new CloudRuntimeException("Unable to download from " + url
@@ -253,6 +254,7 @@ public class AgentShell implements IAgentShell {
             s_logger.warn("Exception while closing download stream from  "
                     + url + ", ", e);
         }
+        method.releaseConnection();
     }
 
     private void loadProperties() throws ConfigurationException {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/a28f4cac/awsapi/src/com/cloud/bridge/io/S3CAStorBucketAdapter.java
----------------------------------------------------------------------
diff --git a/awsapi/src/com/cloud/bridge/io/S3CAStorBucketAdapter.java b/awsapi/src/com/cloud/bridge/io/S3CAStorBucketAdapter.java
index 2101afe..d7adc28 100644
--- a/awsapi/src/com/cloud/bridge/io/S3CAStorBucketAdapter.java
+++ b/awsapi/src/com/cloud/bridge/io/S3CAStorBucketAdapter.java
@@ -58,6 +58,7 @@ import com.caringo.client.ScspResponse;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 
 /**
@@ -413,23 +414,26 @@ public class S3CAStorBucketAdapter implements S3BucketAdapter {
     }
 
     public class ScspDataSource implements DataSource {
-        GetMethod method;
-        public ScspDataSource(GetMethod m) {
-            method = m;
+        String content_type = null;
+        byte content[] = null;
+        public ScspDataSource(GetMethod method) {
+            Header h = method.getResponseHeader("Content-type");
+            if (h != null) {
+                content_type = h.getValue();
+            }
+            try{
+                content = method.getResponseBody();
+            }catch(IOException e){
+                s_logger.error("CAStor loadObjectRange getInputStream error", e);
+            }
         }
         @Override
         public String getContentType() {
-            Header h = method.getResponseHeader("Content-type");
-            return h==null ? null : h.getValue();
+            return content_type;
         }
         @Override
-        public InputStream getInputStream() throws IOException {
-            try {
-                return method.getResponseBodyAsStream();
-            } catch (Exception e) {
-                s_logger.error("CAStor loadObjectRange getInputStream error", e);
-                return null;
-            }
+        public InputStream getInputStream() {
+            return new ByteArrayInputStream(content);
         }
         @Override
         public String getName() {
@@ -445,21 +449,27 @@ public class S3CAStorBucketAdapter implements S3BucketAdapter {
 
     @Override
     public DataHandler loadObjectRange(String mountedRoot, String bucket, String fileName,
long startPos, long endPos) {
+        HttpClient httpClient = new HttpClient(s_httpClientManager);
+        // Create a method instance.
+        GetMethod method = new GetMethod(castorURL(mountedRoot, bucket, fileName));
+        method.addRequestHeader("Range", "bytes=" + startPos + "-" + endPos);
+        int statusCode;
         try {
-            HttpClient httpClient = new HttpClient(s_httpClientManager);
-            // Create a method instance.
-            GetMethod method = new GetMethod(castorURL(mountedRoot, bucket, fileName));
-            method.addRequestHeader("Range", "bytes=" + startPos + "-" + endPos);
-            int statusCode = httpClient.executeMethod(method);
-            if (statusCode < HTTP_OK || statusCode >= HTTP_UNSUCCESSFUL) {
-                s_logger.error("CAStor loadObjectRange response: "+  statusCode);
-                throw new FileNotExistException("CAStor loadObjectRange response: " + statusCode);
-            }
-            return new DataHandler(new ScspDataSource(method));
-        } catch (Exception e) {
+            statusCode = httpClient.executeMethod(method);
+        } catch (HttpException e) {
             s_logger.error("CAStor loadObjectRange failure", e);
             throw new FileNotExistException("CAStor loadObjectRange failure: " + e);
+        } catch (IOException e) {
+            s_logger.error("CAStor loadObjectRange failure", e);
+            throw new FileNotExistException("CAStor loadObjectRange failure: " + e);
+        }
+        if (statusCode < HTTP_OK || statusCode >= HTTP_UNSUCCESSFUL) {
+            s_logger.error("CAStor loadObjectRange response: "+  statusCode);
+            throw new FileNotExistException("CAStor loadObjectRange response: " + statusCode);
         }
+        DataHandler ret = new DataHandler(new ScspDataSource(method));
+        method.releaseConnection();
+        return ret;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/a28f4cac/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
index 26e7e0d..43a6c0b 100644
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/nicira/NiciraNvpApi.java
@@ -119,6 +119,8 @@ public class NiciraNvpApi {
             throw new NiciraNvpApiException("Nicira NVP API login failed ", e);
         } catch (IOException e) {
             throw new NiciraNvpApiException("Nicira NVP API login failed ", e);
+        } finally {
+            pm.releaseConnection();
         }
         
         if (pm.getStatusCode() != HttpStatus.SC_OK) {
@@ -322,10 +324,11 @@ public class NiciraNvpApi {
         
         if (pm.getStatusCode() != HttpStatus.SC_OK) {
             String errorMessage = responseToErrorMessage(pm);
+            pm.releaseConnection();
             s_logger.error("Failed to update object : " + errorMessage);
             throw new NiciraNvpApiException("Failed to update object : " + errorMessage);
         }
-        
+        pm.releaseConnection();
     }
     
     private <T> T executeCreateObject(T newObject, Type returnObjectType, String uri,
Map<String,String> parameters) throws NiciraNvpApiException {
@@ -352,6 +355,7 @@ public class NiciraNvpApi {
         
         if (pm.getStatusCode() != HttpStatus.SC_CREATED) {
             String errorMessage = responseToErrorMessage(pm);
+            pm.releaseConnection();
             s_logger.error("Failed to create object : " + errorMessage);
             throw new NiciraNvpApiException("Failed to create object : " + errorMessage);
         }
@@ -361,6 +365,8 @@ public class NiciraNvpApi {
             result = (T)gson.fromJson(pm.getResponseBodyAsString(), TypeToken.get(newObject.getClass()).getType());
         } catch (IOException e) {
             throw new NiciraNvpApiException("Failed to decode json response body", e);
+        } finally {
+            pm.releaseConnection();
         }
         
         return result;        
@@ -382,9 +388,11 @@ public class NiciraNvpApi {
         
         if (dm.getStatusCode() != HttpStatus.SC_NO_CONTENT) {
             String errorMessage = responseToErrorMessage(dm);
+            dm.releaseConnection();
             s_logger.error("Failed to delete object : " + errorMessage);
             throw new NiciraNvpApiException("Failed to delete object : " + errorMessage);
         }
+        dm.releaseConnection();
     }
     
     private <T> T executeRetrieveObject(Type returnObjectType, String uri, Map<String,String>
parameters) throws NiciraNvpApiException {
@@ -410,6 +418,7 @@ public class NiciraNvpApi {
         
         if (gm.getStatusCode() != HttpStatus.SC_OK) {
             String errorMessage = responseToErrorMessage(gm);
+            gm.releaseConnection();
             s_logger.error("Failed to retrieve object : " + errorMessage);
             throw new NiciraNvpApiException("Failed to retrieve object : " + errorMessage);
         }
@@ -421,8 +430,9 @@ public class NiciraNvpApi {
         } catch (IOException e) {
             s_logger.error("IOException while retrieving response body",e);
             throw new NiciraNvpApiException(e);
+        } finally {
+            gm.releaseConnection();
         }
-        
         return returnValue;
     }
     
@@ -430,6 +440,7 @@ public class NiciraNvpApi {
         try {
             _client.executeMethod(method);
             if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
+                method.releaseConnection();
                 // login and try again
                 login();
                 _client.executeMethod(method);

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/a28f4cac/server/src/com/cloud/cluster/ClusterServiceServletImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/cluster/ClusterServiceServletImpl.java b/server/src/com/cloud/cluster/ClusterServiceServletImpl.java
index 0c3a175..3270315 100644
--- a/server/src/com/cloud/cluster/ClusterServiceServletImpl.java
+++ b/server/src/com/cloud/cluster/ClusterServiceServletImpl.java
@@ -62,11 +62,7 @@ public class ClusterServiceServletImpl implements ClusterService {
         method.addParameter("stopOnError", pdu.isStopOnError() ? "1" : "0");
         method.addParameter("pduType", Integer.toString(pdu.getPduType()));
 
-        try {
-        	return executePostMethod(client, method);
-        } finally {
-        	method.releaseConnection();
-        }
+        return executePostMethod(client, method);
     }
 
     @Override
@@ -81,15 +77,11 @@ public class ClusterServiceServletImpl implements ClusterService {
         method.addParameter("method", Integer.toString(RemoteMethodConstants.METHOD_PING));
         method.addParameter("callingPeer", callingPeer);
         
-        try {
-	        String returnVal =  executePostMethod(client, method);
-	        if("true".equalsIgnoreCase(returnVal)) {
-	            return true;
-	        }
-	        return false;
-        } finally {
-        	method.releaseConnection();
+        String returnVal =  executePostMethod(client, method);
+        if("true".equalsIgnoreCase(returnVal)) {
+            return true;
         }
+        return false;
     }
 
     private String executePostMethod(HttpClient client, PostMethod method) {
@@ -115,6 +107,8 @@ public class ClusterServiceServletImpl implements ClusterService {
             s_logger.error("IOException from : " + _serviceUrl + ", method : " + method.getParameter("method"));
         } catch(Throwable e) {
             s_logger.error("Exception from : " + _serviceUrl + ", method : " + method.getParameter("method")
+ ", exception :", e);
+        } finally {
+            method.releaseConnection();
         }
 
         return result;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/a28f4cac/server/src/com/cloud/maint/UpgradeManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/maint/UpgradeManagerImpl.java b/server/src/com/cloud/maint/UpgradeManagerImpl.java
index c1ce3f0..7875c97 100644
--- a/server/src/com/cloud/maint/UpgradeManagerImpl.java
+++ b/server/src/com/cloud/maint/UpgradeManagerImpl.java
@@ -131,6 +131,8 @@ public class UpgradeManagerImpl implements UpgradeManager {
         	return "Unable to retrieve the file from " + url;
         } catch (final IOException e) {
         	return "Unable to retrieve the file from " + url;
+        } finally {
+               method.releaseConnection();
         }
         
         file.delete();


Mime
View raw message