summaryrefslogtreecommitdiffstats
path: root/openstack-console/src/main/java/com/woorea/openstack/console/Console.java
blob: 70f2990b60907b1d3811dd0733a425ccc30bb068 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*-
 * ============LICENSE_START=======================================================
 * Licensed 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.
 * ============LICENSE_END=========================================================
 */

package com.woorea.openstack.console;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import jline.UnsupportedTerminal;
import jline.console.ConsoleReader;
import jline.console.completer.Completer;
import jline.console.completer.StringsCompleter;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;

public class Console {
	
	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());
				}
			}
		}
	}

	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;
		}
	}

}