Make console reader be safer 67/31367/2
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Mon, 12 Feb 2018 04:53:56 +0000 (10:23 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Mon, 12 Feb 2018 05:56:31 +0000 (11:26 +0530)
Issue-ID: CLI-92

Change-Id: I5c415b2fcdf92b2a861a871d9ab907221290107d
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
main/src/main/java/org/onap/cli/main/OnapCli.java

index 65edfc1..62b78fd 100644 (file)
@@ -303,24 +303,24 @@ public class OnapCli {
      *             exception
      */
     private ConsoleReader createConsoleReader() throws IOException {
-        ConsoleReader console = new ConsoleReader();
-        try {
-            StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion());
-            strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT,
-                    OnapCliConstants.PARAM_INTERACTIVE_CLEAR,
-                    OnapCliConstants.PARAM_INTERACTIVE_USE,
-                    OnapCliConstants.PARAM_INTERACTIVE_HELP,
-                    OnapCliConstants.PARAM_INTERACTIVE_VERSION,
-                    OnapCliConstants.PARAM_INTERACTIVE_SET,
-                    OnapCliConstants.PARAM_INTERACTIVE_UNSET,
-                    OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
-            console.addCompleter(strCompleter);
-            console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT + ":" + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion() + ">");
-        } catch (OnapCommandException e) { // NOSONAR
-            this.print("Failed to load oclip commands," + e.getMessage());
+        try(ConsoleReader console = new ConsoleReader()){
+            try {
+                StringCompleter strCompleter = new StringCompleter(OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion());
+                strCompleter.add(OnapCliConstants.PARAM_INTERACTIVE_EXIT,
+                        OnapCliConstants.PARAM_INTERACTIVE_CLEAR,
+                        OnapCliConstants.PARAM_INTERACTIVE_USE,
+                        OnapCliConstants.PARAM_INTERACTIVE_HELP,
+                        OnapCliConstants.PARAM_INTERACTIVE_VERSION,
+                        OnapCliConstants.PARAM_INTERACTIVE_SET,
+                        OnapCliConstants.PARAM_INTERACTIVE_UNSET,
+                        OnapCliConstants.PARAM_INTERACTIVE_PROFILE);
+                console.addCompleter(strCompleter);
+                console.setPrompt(OnapCliConstants.PARAM_INTERACTIVE_PROMPT + ":" + OnapCommandRegistrar.getRegistrar().getEnabledProductVersion() + ">");
+            } catch (OnapCommandException e) { // NOSONAR
+                this.print("Failed to load oclip commands," + e.getMessage());
+            }
+            return console;
         }
-
-        return console;
     }
 
     /**