diff --git a/agent/src/com/cloud/agent/AgentShell.java b/agent/src/com/cloud/agent/AgentShell.java index 774f222dfb9..e3d1063e6b8 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 { diff --git a/awsapi/src/com/cloud/bridge/io/S3CAStorBucketAdapter.java b/awsapi/src/com/cloud/bridge/io/S3CAStorBucketAdapter.java index 2101afe1418..d7adc286816 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 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 26e7e0d663c..43a6c0b5be8 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 executeCreateObject(T newObject, Type returnObjectType, String uri, Map 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 executeRetrieveObject(Type returnObjectType, String uri, Map 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); diff --git a/server/src/com/cloud/cluster/ClusterServiceServletImpl.java b/server/src/com/cloud/cluster/ClusterServiceServletImpl.java index 0c3a175c5c5..3270315785b 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; diff --git a/server/src/com/cloud/maint/UpgradeManagerImpl.java b/server/src/com/cloud/maint/UpgradeManagerImpl.java index c1ce3f0517f..7875c97ec50 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();