diff options
author | 2018-07-25 19:35:12 +0800 | |
---|---|---|
committer | 2018-07-25 19:35:12 +0800 | |
commit | 60bfc985e39353ee97f23b68ea612bf79124da43 (patch) | |
tree | c2717044b33efdd0c33a84c87f16437f5c1d0598 /openstack-console/src/main/java/com/woorea/openstack | |
parent | f8c97097d7956d2556c128fd289233917821dbcf (diff) |
basic code refactoring console take 1
Issue-ID: SO-729
Change-Id: Ief0e13a137cc0933c37f8bf416a4e92b57848298
Signed-off-by: seshukm <seshu.kumar.m@huawei.com>
Diffstat (limited to 'openstack-console/src/main/java/com/woorea/openstack')
8 files changed, 211 insertions, 211 deletions
diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Command.java b/openstack-console/src/main/java/com/woorea/openstack/console/Command.java index 13ed98b..8aa56c0 100644 --- a/openstack-console/src/main/java/com/woorea/openstack/console/Command.java +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Command.java @@ -21,17 +21,17 @@ import org.apache.commons.cli.Options; public abstract class Command { - - protected String name; - - public Command(String name) { - this.name = name; - } + + protected String name; + + public Command(String name) { + this.name = name; + } - public abstract void execute(Console console, CommandLine args); - - public Options getOptions() { - return new Options(); - } + public abstract void execute(Console console, CommandLine args); + + public Options getOptions() { + return new Options(); + } } diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/CommandLineHelper.java b/openstack-console/src/main/java/com/woorea/openstack/console/CommandLineHelper.java index eb44b34..c11c403 100644 --- a/openstack-console/src/main/java/com/woorea/openstack/console/CommandLineHelper.java +++ b/openstack-console/src/main/java/com/woorea/openstack/console/CommandLineHelper.java @@ -21,7 +21,7 @@ import java.util.Vector; public class CommandLineHelper { - public static String[] parse(String input) { + public static String[] parse(String input) { if (input == null || input.length() == 0) { //no command? no string return new String[0]; diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Commands.java b/openstack-console/src/main/java/com/woorea/openstack/console/Commands.java index 0c13192..d9a395b 100644 --- a/openstack-console/src/main/java/com/woorea/openstack/console/Commands.java +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Commands.java @@ -22,26 +22,26 @@ import org.apache.commons.cli.CommandLine; public class Commands { - public static final Command EXIT = new Command("exit") { - - @Override - public void execute(Console console, CommandLine args) { - console.exit(); - } - - }; - - public static final Command SET = new Command("set") { - - @Override - public void execute(Console console, CommandLine args) { - if(args.getArgs().length == 2) { - console.setProperty(args.getArgs()[0], args.getArgs()[1]); - } else { - console.properties(); - } - } - - }; + public static final Command EXIT = new Command("exit") { + + @Override + public void execute(Console console, CommandLine args) { + console.exit(); + } + + }; + + public static final Command SET = new Command("set") { + + @Override + public void execute(Console console, CommandLine args) { + if(args.getArgs().length == 2) { + console.setProperty(args.getArgs()[0], args.getArgs()[1]); + } else { + console.properties(); + } + } + + }; } diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Console.java b/openstack-console/src/main/java/com/woorea/openstack/console/Console.java index 70f2990..51a316e 100644 --- a/openstack-console/src/main/java/com/woorea/openstack/console/Console.java +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Console.java @@ -34,94 +34,94 @@ import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; public class Console { - - private Properties properties; - - private ConsoleReader reader; + + private Properties properties; + + private ConsoleReader reader; - private Environment environment; - - private HelpFormatter helpFormatter = new HelpFormatter(); - - private static final CommandLineParser PARSER = new GnuParser(); - - public Console(Environment environment, Properties properties) { - this.properties = properties; - this.environment = environment; - } - - public void start() throws IOException { - if(System.console() == null) { - reader = new ConsoleReader(System.in, System.out, new UnsupportedTerminal()); - } else { - reader = new ConsoleReader(); - } - do { - String line = reader.readLine(environment.getPrompt()); - execute(line); - } while(true); - } - - public void execute(String line) { - String[] tokens = CommandLineHelper.parse(line); - if(tokens.length > 0) { - Command command = environment.commands.get(tokens[0]); - if(command != null) { - try { - CommandLine args = Console.PARSER.parse(command.getOptions(), Arrays.copyOfRange(tokens, 1, tokens.length)); - command.execute(this, args); - } catch (Exception e) { - e.printStackTrace(); - helpFormatter.printHelp(command.name, command.getOptions()); - } - } - } - } + private Environment environment; + + private HelpFormatter helpFormatter = new HelpFormatter(); + + private static final CommandLineParser PARSER = new GnuParser(); + + public Console(Environment environment, Properties properties) { + this.properties = properties; + this.environment = environment; + } + + public void start() throws IOException { + if(System.console() == null) { + reader = new ConsoleReader(System.in, System.out, new UnsupportedTerminal()); + } else { + reader = new ConsoleReader(); + } + do { + String line = reader.readLine(environment.getPrompt()); + execute(line); + } while(true); + } + + public void execute(String line) { + String[] tokens = CommandLineHelper.parse(line); + if(tokens.length > 0) { + Command command = environment.commands.get(tokens[0]); + if(command != null) { + try { + CommandLine args = Console.PARSER.parse(command.getOptions(), Arrays.copyOfRange(tokens, 1, tokens.length)); + command.execute(this, args); + } catch (Exception e) { + e.printStackTrace(); + helpFormatter.printHelp(command.name, command.getOptions()); + } + } + } + } - public void setEnvironment(Environment environment) { - Set<Completer> completers = new HashSet<Completer>(reader.getCompleters()); - for(Completer c : completers) { - reader.removeCompleter(c); - } - Set<String> commands = new HashSet<String>(); - for(Map.Entry<String,Command> c : environment.commands.entrySet()) { - commands.add(c.getKey()); - } - reader.addCompleter(new StringsCompleter(commands)); - this.environment = environment; - } - - public Environment getEnvironment() { - return this.environment; - } - - /** - * @return the properties - */ - public String getProperty(String name) { - return properties.getProperty(name); - } - - /** - * @return the properties - */ - public void setProperty(String name, Object value) { - properties.put(name, value); - } - - public void properties() { - for(Map.Entry<Object, Object> entry : properties.entrySet()) { - System.out.printf("%25s = %55s",entry.getKey(), entry.getValue()); - } - } + public void setEnvironment(Environment environment) { + Set<Completer> completers = new HashSet<Completer>(reader.getCompleters()); + for(Completer c : completers) { + reader.removeCompleter(c); + } + Set<String> commands = new HashSet<String>(); + for(Map.Entry<String,Command> c : environment.commands.entrySet()) { + commands.add(c.getKey()); + } + reader.addCompleter(new StringsCompleter(commands)); + this.environment = environment; + } + + public Environment getEnvironment() { + return this.environment; + } + + /** + * @return the properties + */ + public String getProperty(String name) { + return properties.getProperty(name); + } + + /** + * @return the properties + */ + public void setProperty(String name, Object value) { + properties.put(name, value); + } + + public void properties() { + for(Map.Entry<Object, Object> entry : properties.entrySet()) { + System.out.printf("%25s = %55s",entry.getKey(), entry.getValue()); + } + } - public void exit() { - if(environment.parent == null) { - System.out.println("Goodbye"); - System.exit(1); - } else { - environment = environment.parent; - } - } + public void exit() { + if(environment.parent == null) { + System.out.println("Goodbye"); + System.exit(1); + } else { + environment = environment.parent; + } + } } diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Environment.java b/openstack-console/src/main/java/com/woorea/openstack/console/Environment.java index 30ea7b1..9342349 100644 --- a/openstack-console/src/main/java/com/woorea/openstack/console/Environment.java +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Environment.java @@ -21,26 +21,26 @@ import java.util.TreeMap; public class Environment { - protected final Environment parent; - - protected Map<String, Command> commands = new TreeMap<String, Command>(); - - public Environment(Environment parent) { - register(Commands.EXIT); - register(Commands.SET); - this.parent = parent; - } - - public Environment() { - this(null); - } + protected final Environment parent; + + protected Map<String, Command> commands = new TreeMap<String, Command>(); + + public Environment(Environment parent) { + register(Commands.EXIT); + register(Commands.SET); + this.parent = parent; + } + + public Environment() { + this(null); + } - public void register(Command command) { - commands.put(command.name, command); - } + public void register(Command command) { + commands.put(command.name, command); + } - public String getPrompt() { - return "> "; - } + public String getPrompt() { + return "> "; + } } diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/Main.java b/openstack-console/src/main/java/com/woorea/openstack/console/Main.java index 3aa4229..730ba68 100644 --- a/openstack-console/src/main/java/com/woorea/openstack/console/Main.java +++ b/openstack-console/src/main/java/com/woorea/openstack/console/Main.java @@ -25,19 +25,19 @@ import com.woorea.openstack.console.nova.NovaEnvironment; public class Main { - /** - * @param args - */ - public static void main(String[] args) throws IOException { - Environment environment = new Environment(); - environment.register(KeystoneEnvironment.KEYSTONE); - environment.register(NovaEnvironment.NOVA); - - Properties properties = new Properties(); - properties.load(new FileInputStream("src/main/resources/console.properties")); - - Console console = new Console(environment, properties); - console.start(); - } + /** + * @param args + */ + public static void main(String[] args) throws IOException { + Environment environment = new Environment(); + environment.register(KeystoneEnvironment.KEYSTONE); + environment.register(NovaEnvironment.NOVA); + + Properties properties = new Properties(); + properties.load(new FileInputStream("src/main/resources/console.properties")); + + Console console = new Console(environment, properties); + console.start(); + } } diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneCommand.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneCommand.java index 8eb230a..48cfc9b 100644 --- a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneCommand.java +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneCommand.java @@ -23,18 +23,18 @@ import com.woorea.openstack.console.Console; import com.woorea.openstack.keystone.Keystone; public abstract class KeystoneCommand extends Command { - - public KeystoneCommand(String name) { - super(name); - } + + public KeystoneCommand(String name) { + super(name); + } - @Override - public void execute(Console console, CommandLine args) { - KeystoneEnvironment environment = (KeystoneEnvironment) console.getEnvironment(); - execute(environment.CLIENT, args); - - } + @Override + public void execute(Console console, CommandLine args) { + KeystoneEnvironment environment = (KeystoneEnvironment) console.getEnvironment(); + execute(environment.CLIENT, args); + + } - protected abstract void execute(Keystone keystone, CommandLine args); + protected abstract void execute(Keystone keystone, CommandLine args); } diff --git a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneEnvironment.java b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneEnvironment.java index 06ca23b..b9c4c3c 100644 --- a/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneEnvironment.java +++ b/openstack-console/src/main/java/com/woorea/openstack/console/keystone/KeystoneEnvironment.java @@ -27,53 +27,53 @@ import com.woorea.openstack.keystone.model.Access; import com.woorea.openstack.keystone.model.authentication.UsernamePassword; public class KeystoneEnvironment extends Environment { - - public final Keystone CLIENT; - - public static final Command KEYSTONE = new Command("keystone") { - - @Override - public void execute(Console console, CommandLine args) { - - Keystone client = new Keystone(console.getProperty("keystone.endpoint")); - - Access access = client.tokens() - .authenticate(new UsernamePassword( - console.getProperty("keystone.username"), - console.getProperty("keystone.password") - )) - .withTenantName(console.getProperty("keystone.tenant_name")) - .execute(); - - client.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); - - KeystoneEnvironment environment = new KeystoneEnvironment(console.getEnvironment(), client); - - environment.register(new KeystoneTenantList()); - environment.register(new KeystoneTenantCreate()); - environment.register(new KeystoneTenantDelete()); - environment.register(new KeystoneUserList()); - environment.register(new KeystoneUserCreate()); - environment.register(new KeystoneUserDelete()); - environment.register(new KeystoneRoleList()); - environment.register(new KeystoneRoleDelete()); - environment.register(new KeystoneServiceList()); - console.setEnvironment(environment); - } - - }; - - public KeystoneEnvironment(Environment parent, Keystone client) { - super(parent); - CLIENT = client; - } + + public final Keystone CLIENT; + + public static final Command KEYSTONE = new Command("keystone") { + + @Override + public void execute(Console console, CommandLine args) { + + Keystone client = new Keystone(console.getProperty("keystone.endpoint")); + + Access access = client.tokens() + .authenticate(new UsernamePassword( + console.getProperty("keystone.username"), + console.getProperty("keystone.password") + )) + .withTenantName(console.getProperty("keystone.tenant_name")) + .execute(); + + client.setTokenProvider(new OpenStackSimpleTokenProvider(access.getToken().getId())); + + KeystoneEnvironment environment = new KeystoneEnvironment(console.getEnvironment(), client); + + environment.register(new KeystoneTenantList()); + environment.register(new KeystoneTenantCreate()); + environment.register(new KeystoneTenantDelete()); + environment.register(new KeystoneUserList()); + environment.register(new KeystoneUserCreate()); + environment.register(new KeystoneUserDelete()); + environment.register(new KeystoneRoleList()); + environment.register(new KeystoneRoleDelete()); + environment.register(new KeystoneServiceList()); + console.setEnvironment(environment); + } + + }; + + public KeystoneEnvironment(Environment parent, Keystone client) { + super(parent); + CLIENT = client; + } - /* (non-Javadoc) - * @see org.woorea.wsh.Environment#getPrompt() - */ - @Override - public String getPrompt() { - return "keystone> "; - } - + /* (non-Javadoc) + * @see org.woorea.wsh.Environment#getPrompt() + */ + @Override + public String getPrompt() { + return "keystone> "; + } + } |