Adding null condition to application name and generic method to print errors
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/1decbc6c Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/1decbc6c Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/1decbc6c Branch: refs/heads/master Commit: 1decbc6c0cb8236dde48330555d8e32057148ab9 Parents: 7e6aaf1 Author: Imesh Gunaratne <[email protected]> Authored: Wed Dec 17 11:53:49 2014 +0530 Committer: Udara Liyanage <[email protected]> Committed: Thu Dec 18 12:06:58 2014 +0530 ---------------------------------------------------------------------- components/org.apache.stratos.cli/pom.xml | 28 ----- .../java/org/apache/stratos/cli/CliTool.java | 32 ++---- .../main/java/org/apache/stratos/cli/Main.java | 35 +++++++ .../stratos/cli/RestCommandLineService.java | 105 ++++++++++--------- .../apache/stratos/cli/utils/CliConstants.java | 4 - 5 files changed, 101 insertions(+), 103 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/1decbc6c/components/org.apache.stratos.cli/pom.xml ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/pom.xml b/components/org.apache.stratos.cli/pom.xml index 60467eb..a53c2e3 100644 --- a/components/org.apache.stratos.cli/pom.xml +++ b/components/org.apache.stratos.cli/pom.xml @@ -38,10 +38,6 @@ </properties> <dependencies> - <!-- <dependency> - <groupId>org.wso2.carbon</groupId> - <artifactId>org.wso2.carbon.ui</artifactId> - </dependency> --> <!-- Adding HttpClient dependencies. Those were resolved with above one --> <dependency> <groupId>commons-httpclient</groupId> @@ -118,30 +114,6 @@ </dependencies> <build> <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-assembly-plugin</artifactId> - <configuration> - <archive> - <manifest> - <mainClass>org.apache.stratos.cli.CliTool</mainClass> - </manifest> - </archive> - <descriptors> - <descriptor>src/main/assembly/src.xml</descriptor> - </descriptors> - <appendAssemblyId>false</appendAssemblyId> - </configuration> - <executions> - <execution> - <id>make-assembly</id> <!-- this is used for inheritance merges --> - <phase>package</phase> <!-- bind to the packaging phase --> - <goals> - <goal>single</goal> - </goals> - </execution> - </executions> - </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> http://git-wip-us.apache.org/repos/asf/stratos/blob/1decbc6c/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CliTool.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CliTool.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CliTool.java index ac21579..2175abf 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CliTool.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/CliTool.java @@ -30,17 +30,7 @@ import org.slf4j.LoggerFactory; */ public class CliTool { - private static final Logger logger = LoggerFactory.getLogger(CliTool.class); - - /** - * Main executable method used to call from CLI. - * - */ - public static void main(final String[] args) { - CliTool cliTool = new CliTool(); - cliTool.createConfigDirectory(); - cliTool.handleConsoleInputs(args); - } + private static final Logger log = LoggerFactory.getLogger(CliTool.class); /** * Here is the place all the command line inputs get processed @@ -48,27 +38,27 @@ public class CliTool { * @param arguments * passed to CLI tool. */ - private void handleConsoleInputs(String[] arguments) { - if (logger.isInfoEnabled()) { - logger.info("Stratos CLI Started..."); + void handleConsoleInputs(String[] arguments) { + if (log.isInfoEnabled()) { + log.info("Stratos CLI started..."); } StratosApplication application = new StratosApplication(arguments); application.start(arguments); } - private void createConfigDirectory() { + void createConfigDirectory() { File stratosFile = new File(System.getProperty("user.home"), STRATOS_DIR); if (stratosFile.exists()) { - if (logger.isInfoEnabled()) { - logger.info("Using directory: {}", stratosFile.getPath()); + if (log.isInfoEnabled()) { + log.info("Using directory: {}", stratosFile.getPath()); } } else { if (stratosFile.mkdir()) { - if (logger.isInfoEnabled()) { - logger.info("Created directory: {}", stratosFile.getPath()); + if (log.isInfoEnabled()) { + log.info("Created directory: {}", stratosFile.getPath()); } - } else if (logger.isWarnEnabled()) { - logger.warn("Failed to created directory: {}", stratosFile.getPath()); + } else if (log.isWarnEnabled()) { + log.warn("Failed to created directory: {}", stratosFile.getPath()); } } } http://git-wip-us.apache.org/repos/asf/stratos/blob/1decbc6c/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/Main.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/Main.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/Main.java new file mode 100644 index 0000000..d14ee07 --- /dev/null +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/Main.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.stratos.cli; + +/** + * CLI main class. + */ +public class Main { + /** + * Main executable method used to call from CLI. + * + */ + public static void main(final String[] args) { + CliTool cliTool = new CliTool(); + cliTool.createConfigDirectory(); + cliTool.handleConsoleInputs(args); + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/1decbc6c/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 bb015bc..0f1cc09 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 @@ -26,6 +26,7 @@ import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContextFactory; import org.apache.axis2.description.TransportOutDescription; import org.apache.axis2.transport.http.HttpTransportProperties; +import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.stratos.cli.exception.CommandException; @@ -174,7 +175,8 @@ public class RestCommandLineService { log.debug("Initialized REST Client for user {}", username); } } catch (AxisFault e) { - System.out.println("Error connecting to the stratos server"); + String message = "Error connecting to the stratos server"; + printError(message, e); throw new CommandException(e); } @@ -198,18 +200,15 @@ public class RestCommandLineService { } } catch (ConnectException e) { String message = "Could not connect to stratos manager"; - System.out.println(message); - log.error(message, e); + printError(message, e); return false; } catch (java.lang.NoSuchMethodError e) { String message = "Authentication failed!"; - System.out.println(message); - log.error(message, e); + printError(message, e); return false; } catch (Exception e) { String message = "An unknown error occurred: " + e.getMessage(); - System.out.println(message); - log.error(message, e); + printError(message, e); return false; } finally { httpClient.getConnectionManager().shutdown(); @@ -282,8 +281,7 @@ public class RestCommandLineService { "Multi-Tenant"); } catch (Exception e) { String message = "Error in listing cartridges"; - System.out.println(message); - log.error(message, e); + printError(message, e); } } @@ -349,8 +347,7 @@ public class RestCommandLineService { System.out.println("-------------------------------------"); } catch (Exception e) { String message = "Error in describing cartridge: " + cartridgeType; - System.out.println(message); - log.error(message, e); + printError(message, e); } } @@ -438,7 +435,8 @@ public class RestCommandLineService { } } catch (Exception e) { - handleException("Exception in creating User", e); + String message = "Could not add user"; + printError(message, e); } finally { httpClient.getConnectionManager().shutdown(); } @@ -466,7 +464,8 @@ public class RestCommandLineService { } } catch (Exception e) { - handleException("Exception in deleting " + tenantDomain + " tenant", e); + String message = "Could not delete tenant"; + printError(message, e); } finally { httpClient.getConnectionManager().shutdown(); } @@ -494,7 +493,8 @@ public class RestCommandLineService { } } catch (Exception e) { - handleException("Exception in deleting " + userName + " user", e); + String message = "Could not delete user"; + printError(message, e); } finally { httpClient.getConnectionManager().shutdown(); } @@ -522,7 +522,8 @@ public class RestCommandLineService { } } catch (Exception e) { - handleException("Exception in deactivating " + tenantDomain + " tenant", e); + String message = "Could not de-activate tenant"; + printError(message, e); } finally { httpClient.getConnectionManager().shutdown(); } @@ -550,7 +551,8 @@ public class RestCommandLineService { } } catch (Exception e) { - handleException("Error in activating tenant: " + tenantDomain, e); + String message = "Could not activate tenant"; + printError(message, e); } finally { httpClient.getConnectionManager().shutdown(); } @@ -587,9 +589,8 @@ public class RestCommandLineService { System.out.println("Tenants:"); CliUtils.printTable(tenantArray, rowMapper, "Domain", "Tenant ID", "Email", "State"); } catch (Exception e) { - String message = "Error in listing users"; - System.out.println(message); - log.error(message, e); + String message = "Could not list tenants"; + printError(message, e); } } @@ -621,9 +622,8 @@ public class RestCommandLineService { System.out.println("Users:"); CliUtils.printTable(usersArray, rowMapper, "Username", "Role"); } catch (Exception e) { - String message = "Error in listing users"; - System.out.println(message); - log.error(message, e); + String message = "Could not list users"; + printError(message, e); } } @@ -664,7 +664,8 @@ public class RestCommandLineService { public String[] getData(ApplicationDefinition applicationDefinition) { String[] data = new String[4]; data[0] = applicationDefinition.getApplicationId(); - data[1] = applicationDefinition.getName(); + data[1] = StringUtils.isEmpty(applicationDefinition.getName()) ? "" : + applicationDefinition.getName(); data[2] = applicationDefinition.getAlias(); data[3] = applicationDefinition.getStatus(); return data; @@ -677,12 +678,23 @@ public class RestCommandLineService { System.out.println("Applications found:"); CliUtils.printTable(array, rowMapper, "Application ID", "Name", "Alias", "Status"); } catch (Exception e) { - String message = "Error in listing applications"; - System.out.println(message); - log.error(message, e); + String message = "Could not list applications"; + printError(message, e); } } + /** + * Print error on console and log + * @param message + * @param e + */ + private void printError(String message, Throwable e) { + // CLI console only get system output + System.out.println(message); + // Log error + log.error(message, e); + } + public void listAutoscalingPolicies() throws CommandException { try { Type listType = new TypeToken<ArrayList<AutoscalePolicy>>() { @@ -711,9 +723,8 @@ public class RestCommandLineService { System.out.println("Autoscaling policies found:"); CliUtils.printTable(array, rowMapper, "ID", "Accessibility"); } catch (Exception e) { - String message = "Error in listing autoscaling policies"; - System.out.println(message); - log.error(message, e); + String message = "Could not list autoscaling policies"; + printError(message, e); } } @@ -731,8 +742,7 @@ public class RestCommandLineService { System.out.println(getGson().toJson(policy)); } catch (Exception e) { String message = "Error in describing deployment policy: " + id; - System.out.println(message); - log.error(message, e); + printError(message, e); } } @@ -749,9 +759,8 @@ public class RestCommandLineService { System.out.println("Autoscaling policy: " + id); System.out.println(getGson().toJson(policy)); } catch (Exception e) { - String message = "Error in describing autoscaling policy: " + id; - System.out.println(message); - log.error(message, e); + String message = "Could not describe autoscaling policy: " + id; + printError(message, e); } } @@ -784,9 +793,8 @@ public class RestCommandLineService { return; } } catch (Exception e) { - String message = "Error in listing kubernetes groups"; - System.out.println(message); - log.error(message, e); + String message = "Could not list kubernetes groups"; + printError(message, e); } } @@ -815,7 +823,8 @@ public class RestCommandLineService { } } catch (Exception e) { - handleException("Error in deploying host to Kubernetes cluster: " + clusterId, e); + String message = "Could not add host to Kubernetes cluster: " + clusterId; + printError(message, e); } finally { httpClient.getConnectionManager().shutdown(); } @@ -847,9 +856,8 @@ public class RestCommandLineService { return; } } catch (Exception e) { - String message = "Error in listing kubernetes hosts"; - System.out.println(message); - log.error(message, e); + String message = "Could not list kubernetes hosts"; + printError(message, e); } } @@ -885,9 +893,8 @@ public class RestCommandLineService { System.out.println(exception); } } catch (Exception e) { - String message = "Error in synchronizing artifacts for cartridge subscription alias: " + cartridgeAlias; - System.out.println(message); - log.error(message, e); + String message = "Could not synchronize artifacts for cartridge subscription alias: " + cartridgeAlias; + printError(message, e); } finally { httpClient.getConnectionManager().shutdown(); } @@ -917,9 +924,8 @@ public class RestCommandLineService { System.out.println("Service Group : " + groupDefinitionName); System.out.println(getGson().toJson(bean)); } catch (Exception e) { - String message = "Error in describing service group: " + groupDefinitionName; - System.out.println(message); - log.error(message, e); + String message = "Could not describe service group: " + groupDefinitionName; + printError(message, e); } } @@ -950,9 +956,8 @@ public class RestCommandLineService { System.out.println("Application: " + applicationID); System.out.println(getGson().toJson(application)); } catch (Exception e) { - String message = "Error in describing application: " + applicationID; - System.out.println(message); - log.error(message, e); + String message = "Could not describe application: " + applicationID; + printError(message, e); } } http://git-wip-us.apache.org/repos/asf/stratos/blob/1decbc6c/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java index 1c616e9..19482c3 100644 --- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java +++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java @@ -25,13 +25,9 @@ package org.apache.stratos.cli.utils; public class CliConstants { public static final String STRATOS_APPLICATION_NAME = "stratos"; - public static final String STRATOS_URL_ENV_PROPERTY = "STRATOS_URL"; - public static final String STRATOS_USERNAME_ENV_PROPERTY = "STRATOS_USERNAME"; - public static final String STRATOS_PASSWORD_ENV_PROPERTY = "STRATOS_PASSWORD"; - public static final String STRATOS_SHELL_PROMPT = "stratos> "; public static final int COMMAND_SUCCESSFULL = 0;
