You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ma...@apache.org on 2013/12/20 11:10:47 UTC

[1/2] git commit: Handle exceptions in CLI

Updated Branches:
  refs/heads/master 5314c31c2 -> 9edc9231f


Handle exceptions in CLI


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

Branch: refs/heads/master
Commit: 89225d20c0dd7c2f14feb6110b151e0d8fb7f57f
Parents: 6ac8339
Author: Manula Thantriwatte <ma...@apache.org>
Authored: Fri Dec 20 15:39:58 2013 +0530
Committer: Manula Thantriwatte <ma...@apache.org>
Committed: Fri Dec 20 15:39:58 2013 +0530

----------------------------------------------------------------------
 .../java/org/apache/stratos/cli/RestClient.java | 52 ++---------------
 .../stratos/cli/RestCommandLineService.java     | 60 +++++++++++---------
 2 files changed, 39 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/89225d20/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java
index f5f855b..d5de9a9 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java
@@ -48,6 +48,8 @@ public class RestClient implements GenericRestClient{
     /**
      * Handle http post request. Return String
      *
+     * @param  httpClient
+     *              This should be httpClient which used to connect to rest endpoint
      * @param resourcePath
      *              This should be REST endpoint
      * @param jsonParamString
@@ -60,11 +62,9 @@ public class RestClient implements GenericRestClient{
      * @throws org.apache.http.client.ClientProtocolException and IOException
      *             if any errors occur when executing the request
      */
-    public HttpResponse doPost(DefaultHttpClient httpClient, String resourcePath, String jsonParamString, String userName, String passWord) throws Exception{
+    public HttpResponse doPost(DefaultHttpClient httpClient, String resourcePath, String jsonParamString, String userName,
+                               String passWord) throws Exception{
         try {
-
-            //DefaultHttpClient httpClient = new DefaultHttpClient();
-
             HttpPost postRequest = new HttpPost(resourcePath);
 
             StringEntity input = new StringEntity(jsonParamString);
@@ -83,32 +83,7 @@ public class RestClient implements GenericRestClient{
 
             HttpResponse response = httpClient.execute(postRequest);
 
-            /*
-            int responseCode = response.getStatusLine().getStatusCode();
-
-            if (responseCode == CliConstants.RESPONSE_AUTHORIZATION_FAIL) {
-                return "" + CliConstants.RESPONSE_AUTHORIZATION_FAIL;
-            } else if (responseCode == CliConstants.RESPONSE_NO_CONTENT) {
-                return "" + CliConstants.RESPONSE_NO_CONTENT;
-            } else if (responseCode == CliConstants.RESPONSE_INTERNAL_SERVER_ERROR) {
-                return "" + CliConstants.RESPONSE_INTERNAL_SERVER_ERROR;
-            } else if (response.getStatusLine().getStatusCode() != 200) {
-                throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
-            }
-
-            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
-
-            String output;
-            String result = "";
-            while ((output = br.readLine()) != null) {
-                result += output;
-            }
-            */
-
-            //httpClient.getConnectionManager().shutdown();
-            //return result;
             return response;
-
         } catch (ClientProtocolException e) {
             throw new ClientProtocolException();
         } catch (ConnectException e) {
@@ -123,6 +98,8 @@ public class RestClient implements GenericRestClient{
     /**
      * Handle http get request. Return String
      *
+     * @param  httpClient
+     *              This should be httpClient which used to connect to rest endpoint
      * @param resourcePath
      *              This should be REST endpoint
      * @param userName
@@ -135,7 +112,6 @@ public class RestClient implements GenericRestClient{
      */
     public HttpResponse doGet(DefaultHttpClient httpClient, String resourcePath, String userName, String passWord) {
         try {
-            //DefaultHttpClient httpClient = new DefaultHttpClient();
             HttpGet getRequest = new HttpGet(resourcePath);
             getRequest.addHeader("Content-Type", "application/json");
 
@@ -151,23 +127,7 @@ public class RestClient implements GenericRestClient{
 
             HttpResponse response = httpClient.execute(getRequest);
 
-            /*
-            if (response.getStatusLine().getStatusCode() != 200) {
-                throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
-            }
-
-            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
-
-            String output;
-            String result = "";
-            while ((output = br.readLine()) != null) {
-                result += output;
-            }
-            */
-
-            //httpClient.getConnectionManager().shutdown();
             return response;
-
         } catch (ClientProtocolException e) {
             e.printStackTrace();
             return null;

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/89225d20/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
index a35cb8e..cfc37b9 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
@@ -259,7 +259,7 @@ public class RestCommandLineService {
                 System.out.println();
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in listing available cartridges", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -336,7 +336,7 @@ public class RestCommandLineService {
             CommandLineUtils.printTable(cartridges, cartridgeMapper, headers.toArray(new String[headers.size()]));
             System.out.println();
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in listing subscribe cartridges", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -351,7 +351,6 @@ public class RestCommandLineService {
         CartridgeInfoBean cartridgeInfoBean = new CartridgeInfoBean();
         cartridgeInfoBean.setCartridgeType(null);
         cartridgeInfoBean.setAlias(null);
-        //cartridgeInfoBean.setPolicy(null);
         cartridgeInfoBean.setRepoURL(null);
         cartridgeInfoBean.setPrivateRepo(false);
         cartridgeInfoBean.setRepoUsername(null);
@@ -379,9 +378,6 @@ public class RestCommandLineService {
                 if (responseCode.equals(CliConstants.RESPONSE_AUTHORIZATION_FAIL)) {
                     System.out.println("Invalid operation. Authorization failed");
                     return;
-                //} else if (responseCode.equals(CliConstants.RESPONSE_NO_CONTENT)) {
-                  //  System.out.println("Duplicate alias. Please choose different alias");
-                  //  return;
                 } else if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
                     System.out.println("Error occured while subscribing cartridge");
                     return;
@@ -402,7 +398,7 @@ public class RestCommandLineService {
                 System.out.format("%nSubscribing to %s cartridge and connecting with %s data cartridge.%n", alias,
                         dataCartridgeAlias);
             } catch (Exception e) {
-                e.printStackTrace();
+                handleException("Exception in subscribing to data cartridge", e);
             }
             finally {
                 httpClient.getConnectionManager().shutdown();
@@ -416,7 +412,6 @@ public class RestCommandLineService {
         try {
             cartridgeInfoBean.setCartridgeType(cartridgeType);
             cartridgeInfoBean.setAlias(alias);
-            //cartridgeInfoBean.setPolicy(policy);
             cartridgeInfoBean.setRepoURL(externalRepoURL);
             cartridgeInfoBean.setPrivateRepo(privateRepo);
             cartridgeInfoBean.setRepoUsername(username);
@@ -436,9 +431,6 @@ public class RestCommandLineService {
             if (responseCode.equals(CliConstants.RESPONSE_AUTHORIZATION_FAIL)) {
                 System.out.println("Invalid operation. Authorization failed");
                 return;
-            //} else if (responseCode.equals(CliConstants.RESPONSE_NO_CONTENT)) {
-            //    System.out.println("Duplicate alias. Please choose different alias");
-            //    return;
             } else if ( ! responseCode.equals(CliConstants.RESPONSE_OK)) {
                 System.out.println("Error occured while subscribing cartridge");
                 return;
@@ -485,14 +477,15 @@ public class RestCommandLineService {
 
             System.out.format("Please map the %s \"%s\" to LB IP%n", hostnamesLabel, hostnames);
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in subscribing to cartridge", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
     }
 
     // This method helps to create the new tenant
-    public void addTenant(String admin, String firstName, String lastaName, String password, String domain, String email){
+    public void addTenant(String admin, String firstName, String lastaName, String password, String domain, String email)
+            throws CommandException{
         DefaultHttpClient httpClient = new DefaultHttpClient();
         try {
             TenantInfoBean tenantInfo = new TenantInfoBean();
@@ -502,7 +495,6 @@ public class RestCommandLineService {
             tenantInfo.setAdminPassword(password);
             tenantInfo.setTenantDomain(domain);
             tenantInfo.setEmail(email);
-            //tenantInfo.setActive(active);
 
             GsonBuilder gsonBuilder = new GsonBuilder();
             Gson gson = gsonBuilder.create();
@@ -514,6 +506,7 @@ public class RestCommandLineService {
                     completeJsonString, restClientService.getUsername(), restClientService.getPassword());
 
             String responseCode = "" + response.getStatusLine().getStatusCode();
+
             if (responseCode.equals(CliConstants.RESPONSE_AUTHORIZATION_FAIL)) {
                 System.out.println("Invalid operation. Authorization failed");
                 return;
@@ -529,7 +522,7 @@ public class RestCommandLineService {
             }
 
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in creating tenant", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -543,14 +536,14 @@ public class RestCommandLineService {
                     restClientService.getUsername(), restClientService.getPassword());
             System.out.println("You have successfully unsubscribed " + alias);
         } catch ( Exception e) {
-            e.printStackTrace();
+            handleException("Exception in un-subscribing cartridge", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
     }
 
     // This method helps to deploy cartridge definitions
-    public void deployCartridgeDefinition (String cartridgeDefinition) {
+    public void deployCartridgeDefinition (String cartridgeDefinition) throws CommandException{
         DefaultHttpClient httpClient = new DefaultHttpClient();
         try {
             HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + cartridgeDeploymentEndPoint,
@@ -569,14 +562,14 @@ public class RestCommandLineService {
                 return;
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in deploy cartridge definition", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
     }
 
     // This method helps to deploy partitions
-    public void deployPartition (String partitionDefinition) {
+    public void deployPartition (String partitionDefinition) throws CommandException{
         DefaultHttpClient httpClient = new DefaultHttpClient();
         try {
             HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + partitionDeploymentEndPoint,
@@ -600,14 +593,14 @@ public class RestCommandLineService {
             }
 
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in deploying partitions", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
     }
 
     // This method helps to deploy autoscalling polices
-    public void deployAutoscalingPolicy (String autoScalingPolicy) {
+    public void deployAutoscalingPolicy (String autoScalingPolicy) throws CommandException{
         DefaultHttpClient httpClient= new DefaultHttpClient();
         try {
             HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + autoscalingPolicyDeploymentEndPoint,
@@ -630,14 +623,14 @@ public class RestCommandLineService {
             }
 
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in deploying autoscale police", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
     }
 
     // This method helps to deploy deployment polices
-    public void deployDeploymentPolicy (String deploymentPolicy) {
+    public void deployDeploymentPolicy (String deploymentPolicy) throws CommandException{
         DefaultHttpClient httpClient = new DefaultHttpClient();
         try {
             HttpResponse response = restClientService.doPost(httpClient, restClientService.getUrl() + deploymentPolicyDeploymentEndPoint,
@@ -660,7 +653,7 @@ public class RestCommandLineService {
             }
 
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in deploying deployment policy", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -718,7 +711,7 @@ public class RestCommandLineService {
             System.out.println();
 
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in listing partitions", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -775,7 +768,7 @@ public class RestCommandLineService {
             System.out.println();
 
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in listing autoscale policies", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -829,7 +822,7 @@ public class RestCommandLineService {
             System.out.println();
 
         } catch (Exception e) {
-            e.printStackTrace();
+            handleException("Exception in listing deployment polices", e);
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -944,4 +937,17 @@ public class RestCommandLineService {
             return null;
         }
     }
+
+    // This is for handle exception
+    private void handleException(String key, Exception e, Object... args) throws CommandException {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Displaying message for {}. Exception thrown is {}", key, e.getClass());
+        }
+        String message = CommandLineUtils.getMessage(key, args);
+        if (logger.isErrorEnabled()) {
+            logger.error(message);
+        }
+        System.out.println(message);
+        throw new CommandException(message, e);
+    }
 }


[2/2] git commit: Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/incubator-stratos

Posted by ma...@apache.org.
Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/incubator-stratos


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

Branch: refs/heads/master
Commit: 9edc9231f179d7f926f807b2da6fd3ba011d7570
Parents: 89225d2 5314c31
Author: Manula Thantriwatte <ma...@apache.org>
Authored: Fri Dec 20 15:40:18 2013 +0530
Committer: Manula Thantriwatte <ma...@apache.org>
Committed: Fri Dec 20 15:40:18 2013 +0530

----------------------------------------------------------------------
 .../publisher/CartridgeAgentEventPublisher.java | 78 +++++++++++++-------
 .../publisher/HealthStatisticsNotifier.java     |  4 +-
 .../publisher/HealthStatisticsPublisher.java    |  5 +-
 .../publisher/WSO2CEPStatisticsPublisher.java   |  2 +-
 .../conf/configurator/CEPConfigurator.java      |  2 +-
 .../health/stat/AverageLoadAverageEvent.java    |  6 +-
 .../stat/AverageMemoryConsumptionEvent.java     |  6 +-
 .../stat/AverageRequestsInFlightEvent.java      |  6 +-
 .../health/stat/GradientOfLoadAverageEvent.java |  6 +-
 .../stat/GradientOfMemoryConsumptionEvent.java  |  6 +-
 .../stat/GradientOfRequestsInFlightEvent.java   |  6 +-
 .../event/health/stat/HealthStatEvent.java      | 32 ++++++++
 .../stat/MemberAverageLoadAverageEvent.java     |  6 +-
 .../MemberAverageMemoryConsumptionEvent.java    |  6 +-
 .../event/health/stat/MemberFaultEvent.java     |  6 +-
 .../stat/MemberGradientOfLoadAverageEvent.java  |  6 +-
 .../MemberGradientOfMemoryConsumptionEvent.java |  6 +-
 ...emberSecondDerivativeOfLoadAverageEvent.java |  6 +-
 ...econdDerivativeOfMemoryConsumptionEvent.java |  6 +-
 .../SecondDerivativeOfLoadAverageEvent.java     |  5 +-
 ...econdDerivativeOfMemoryConsumptionEvent.java |  6 +-
 ...SecondDerivativeOfRequestsInFlightEvent.java |  6 +-
 .../AverageLoadAverageMessageProcessor.java     | 66 +++++++++++++++++
 ...verageMemoryConsumptionMessageProcessor.java | 66 +++++++++++++++++
 ...AverageRequestsInFlightMessageProcessor.java | 66 +++++++++++++++++
 .../GradientOfLoadAverageMessageProcessor.java  | 66 +++++++++++++++++
 ...ientOfMemoryConsumptionMessageProcessor.java | 65 ++++++++++++++++
 ...dientOfRequestsInFlightMessageProcessor.java | 66 +++++++++++++++++
 ...emberAverageLoadAverageMessageProcessor.java | 66 +++++++++++++++++
 ...verageMemoryConsumptionMessageProcessor.java | 66 +++++++++++++++++
 .../stat/MemberFaultMessageProcessor.java       | 66 +++++++++++++++++
 ...erGradientOfLoadAverageMessageProcessor.java | 66 +++++++++++++++++
 ...ientOfMemoryConsumptionMessageProcessor.java | 67 +++++++++++++++++
 ...DerivativeOfLoadAverageMessageProcessor.java | 66 +++++++++++++++++
 ...tiveOfMemoryConsumptionMessageProcessor.java | 66 +++++++++++++++++
 ...DerivativeOfLoadAverageMessageProcessor.java | 66 +++++++++++++++++
 ...tiveOfMemoryConsumptionMessageProcessor.java | 66 +++++++++++++++++
 ...ativeOfRequestsInFlightMessageProcessor.java | 66 +++++++++++++++++
 .../src/main/bin/haproxy-extension.sh           |  2 +-
 .../src/main/bin/cartridge-agent.sh             |  3 +-
 .../modules/scripts/ec2/cartridge-agent.sh      | 19 +++--
 41 files changed, 1190 insertions(+), 108 deletions(-)
----------------------------------------------------------------------