incubator-kitty-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anovar...@apache.org
Subject svn commit: r1299288 - in /incubator/kitty/trunk/src/main/groovy/org/apache/kitty: CmdShell.groovy CmdShellBuilder.groovy IODevice.groovy Terminal.groovy utils/Help.groovy
Date Sat, 10 Mar 2012 22:17:18 GMT
Author: anovarini
Date: Sat Mar 10 22:17:17 2012
New Revision: 1299288

URL: http://svn.apache.org/viewvc?rev=1299288&view=rev
Log:
Extracted the responsibility of reading and writing on terminal, minor refactoring

Added:
    incubator/kitty/trunk/src/main/groovy/org/apache/kitty/IODevice.groovy
    incubator/kitty/trunk/src/main/groovy/org/apache/kitty/Terminal.groovy
Modified:
    incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShell.groovy
    incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShellBuilder.groovy
    incubator/kitty/trunk/src/main/groovy/org/apache/kitty/utils/Help.groovy

Modified: incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShell.groovy
URL: http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShell.groovy?rev=1299288&r1=1299287&r2=1299288&view=diff
==============================================================================
--- incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShell.groovy (original)
+++ incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShell.groovy Sat Mar 10 22:17:17
2012
@@ -18,10 +18,6 @@
 
 package org.apache.kitty
 
-import jline.ConsoleReader
-import jline.History
-import jline.SimpleCompletor
-
 import org.apache.kitty.client.Client
 import org.apache.kitty.utils.Constants
 import org.apache.kitty.utils.Help
@@ -39,15 +35,12 @@ import org.apache.kitty.utils.Help
  *
  */
 class CmdShell {
-
-	static final PROMPT = "kitty> "
-
 	static final String HOST = "localhost"
 
 	static final String PORT = "1099"
 
 	Client client
-    ConsoleReader reader = new ConsoleReader()
+    IODevice ioDevice
 
 	def remote
 
@@ -67,26 +60,14 @@ class CmdShell {
 		"setdomain"
 	]
 
-    CmdShell(Client client) {
+    CmdShell(IODevice ioDevice, Client client) {
+        this.ioDevice = ioDevice
         this.client = client
     }
 
 	void startShell() {
-
-		// TODO add Windows compatibility check
-		def historyFile = new File(System.getProperty("user.home"), "kitty.history")
-		historyFile.createNewFile()
-
-		def history = new History(historyFile)
-
-		reader.setBellEnabled false
-		reader.setUseHistory true
-        reader.setHistory history
-        reader.setDefaultPrompt PROMPT
-		reader.addCompletor(new SimpleCompletor(commands as String[]))
-
 		LOOP: while (true) {
-			def input = reader?.readLine().trim()
+			def input = ioDevice.read()
 
 			if (input.length() == 0)
 				continue
@@ -104,7 +85,6 @@ class CmdShell {
 	}
 
 	void inputHandler(String input) {
-		Integer index = 0
 		String[] params
 
 		if (input.contains(" ")) {
@@ -116,8 +96,7 @@ class CmdShell {
 		}
 
 		if (commands.contains(input)) {
-			index = commands.indexOf(input)
-			//println "The index is" + index
+			int index = commands.indexOf(input)
 
 			switch(index) {
 				case 0:
@@ -125,14 +104,14 @@ class CmdShell {
 					break;
 				case 1:
 					if (params.length > 5) {
-						reader.printString "You have entered an invalid number of parameters, you may enter
host and port as parameters only"
+                        ioDevice.write 'You have entered an invalid number of parameters,
you may enter host and port as parameters only'
 						break;
 					}
 					if (params.length == 5) {
 						cmdConnect(params[1], params[2], params[3], params[4])
 					}
 					else if (params.length == 4) {
-                        reader.printString "Please supply a password"
+                        ioDevice.write 'Please supply a password'
 					}
 					else if (params.length == 3) {
 						cmdConnect(params[1], params[2])
@@ -167,8 +146,8 @@ class CmdShell {
 						cmdCd(params[1])
 					}
 					else {
-                        reader.printString Constants.ERROR_TOO_FEW_PARAMETERS
-                        reader.printString "You must enter a path parameter after 'cd'"
+                        ioDevice.write Constants.ERROR_TOO_FEW_PARAMETERS
+                        ioDevice.write 'You must enter a path parameter after "cd"'
 					}
 					break;
 				case 8:
@@ -176,8 +155,8 @@ class CmdShell {
 						cmdGet(params[1])
 					}
 					else {
-                        reader.printString Constants.ERROR_TOO_FEW_PARAMETERS
-                        reader.printString "You must enter at 1 parameter after the command"
+                        ioDevice.write Constants.ERROR_TOO_FEW_PARAMETERS
+                        ioDevice.write 'You must enter at least 1 parameter after the command'
 					}
 					break;
 				case 9:
@@ -185,8 +164,8 @@ class CmdShell {
 						cmdSet(params[1], params[2])
 					}
 					else {
-                        reader.printString Constants.ERROR_TOO_FEW_PARAMETERS
-                        reader.printString "You must enter an attribute parameter and a value
parameter"
+                        ioDevice.write Constants.ERROR_TOO_FEW_PARAMETERS
+                        ioDevice.write 'You must enter an attribute parameter and a value
parameter'
 					}
 					break;
 				case 10:
@@ -194,8 +173,8 @@ class CmdShell {
 						cmdInvoke(params[1],params[2])
 					}
 					else {
-                        reader.printString Constants.ERROR_TOO_FEW_PARAMETERS
-                        reader.printString "You must enter an operation name followed by
parameter(s)"
+                        ioDevice.write Constants.ERROR_TOO_FEW_PARAMETERS
+                        ioDevice.write 'You must enter an operation name followed by parameter(s)'
 					}
 
 					break;
@@ -207,8 +186,8 @@ class CmdShell {
 						cmdSetDomain(params[1])
 					}
 					else {
-                        reader.printString Constants.ERROR_TOO_FEW_PARAMETERS
-                        reader.printString "You must enter a domain name parameter after
'setdomain' command"
+                        ioDevice.write Constants.ERROR_TOO_FEW_PARAMETERS
+                        ioDevice.write 'You must enter a domain name parameter after "setdomain"
command'
 					}
 					break;
 				default:
@@ -216,13 +195,13 @@ class CmdShell {
 			}
 		}
 		else {
-            reader.printString (input + " is not a valid command")
+            ioDevice.write "$input is not a valid command"
 		}
 	}
 
 	void cmdHelp() {
-		Help h = new Help()
-        reader.printString h.toString()
+		Help help = new Help()
+        ioDevice.write help.availableCommands()
 	}
 
     void cmdConnect(String... args) {
@@ -238,23 +217,23 @@ class CmdShell {
         }
         this.remote = client.remote
         if (this.remote) {
-            reader.printString "Successfully connected to host"
+            ioDevice.write "Successfully connected to host $host:$port"
         }
         else {
-            reader.printString "Connection attempt was unsuccessful"
+            ioDevice.write 'Connection attempt was unsuccessful'
         }
     }
 
 	void cmdDisconnect() {
-        reader.printString "disconnecting..."
+        ioDevice.write 'Disconnecting...'
 		if (client.remote) {
 			client.disconnect()
 			if (!client.remote) {
-                reader.printString "successfully disconnected from host"
+                ioDevice.write 'Successfully disconnected from host'
 			}
 		}
 		else {
-            reader.printString "client can not disconnect because it is not connected to
a host"
+            ioDevice.write 'Client can not disconnect because it is not connected to a host'
 		}
 	}
 
@@ -262,93 +241,93 @@ class CmdShell {
         if (client.getRemote()) {
             client.disconnect()
             if (!client.getRemote()) {
-                reader.printString "successfully disconnected from host"
+                ioDevice.write 'Successfully disconnected from host'
             }
         }
-        reader.printString  "exiting..."
-        reader.printString  "successfully exited kitty"
+        ioDevice.write 'Exiting...'
+        ioDevice.write 'Successfully exited kitty'
 	}
 
 	void cmdLs() {
-        reader.printString  "listing files and directories..."
+        ioDevice.write 'listing files and directories...'
 		if (remote) {
 			getClient().ls()
 		}
 		else {
-            reader.printString  Constants.ERROR_NOT_CONNECTED
+            ioDevice.write Constants.ERROR_NOT_CONNECTED
 		}
 	}
 
 	void cmdDomains() {
 		def domains = getClient().domains()
 		if (domains) {
-            reader.printString  "Domains Detected: "
+            ioDevice.write 'Domains Detected: '
             domains.eachWithIndex() { obj, i -> println " ${i}: ${obj}" }
 
 		}
 		else {
-            reader.printString  "No domains are available"
+            ioDevice.write 'No domains are available'
 		}
 	}
 
 	void cmdCd(String path) {
-        reader.printString  "changing remote path..."
+        ioDevice.write 'Changing remote path...'
 		if (remote) {
 			getClient().cd(path)
 		}
 		else {
-            reader.printString  Constants.ERROR_NOT_CONNECTED
+            ioDevice.write Constants.ERROR_NOT_CONNECTED
 		}
 	}
 
 	void cmdGet(def attr) {
-        reader.printString  "get $attr..."
+        ioDevice.write "Get $attr..."
 		if (remote) {
 			getClient().get(attr)
 		}
 		else {
-            reader.printString  Constants.ERROR_NOT_CONNECTED
+            ioDevice.write Constants.ERROR_NOT_CONNECTED
 		}
 	}
 
 	void cmdSet(def attr, def val) {
-        reader.printString  "set $attr to $val"
+        ioDevice.write "Set $attr to $val"
 		if (remote) {
 			getClient().set(attr, val)
 		}
 		else {
-            reader.printString  Constants.ERROR_NOT_CONNECTED
+            ioDevice.write Constants.ERROR_NOT_CONNECTED
 		}
 	}
 
 	void cmdInvoke(def op, def params) {
-        reader.printString "Invoking the following operation: $op"
+        ioDevice.write "Invoking the following operation: $op"
 		if (remote) {
 			getClient().invoke(op, params)
 		}
 		else {
-            reader.printString  Constants.ERROR_NOT_CONNECTED
+            ioDevice.write Constants.ERROR_NOT_CONNECTED
 		}
 	}
 
 	void cmdPwd() {
-        reader.printString  "Displaying the current remote path..."
+        ioDevice.write 'Displaying the current remote path...'
 		if (remote) {
 			String name = (String) getClient().pwd()
-            reader.printString  name
+            ioDevice.write name
 		}
 		else {
-            reader.printString  Constants.ERROR_NOT_CONNECTED
+            ioDevice.write Constants.ERROR_NOT_CONNECTED
 		}
 	}
 
 	void cmdSetDomain(def domain) {
-        reader.printString  "Setting the domain to $domain..."
+        ioDevice.write "Setting the domain to $domain..."
 		if (remote) {
 			getClient().setDomain(domain)
 		}
 		else {
-            reader.printString  Constants.ERROR_NOT_CONNECTED
+            ioDevice.write Constants.ERROR_NOT_CONNECTED
 		}
 	}
 }

Modified: incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShellBuilder.groovy
URL: http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShellBuilder.groovy?rev=1299288&r1=1299287&r2=1299288&view=diff
==============================================================================
--- incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShellBuilder.groovy (original)
+++ incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShellBuilder.groovy Sat Mar
10 22:17:17 2012
@@ -1,18 +1,46 @@
+/*
+ * 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.kitty
 
 import org.apache.kitty.client.Client
+import jline.ConsoleReader
 
-/**
- * Created with IntelliJ IDEA.
- * User: ale
- * Date: 10/03/2012
- * Time: 20:32
- * To change this template use File | Settings | File Templates.
- */
 class CmdShellBuilder {
 
     CmdShell build() {
         Client client = new Client()
-        return new CmdShell(client)
+        String[] commands = [
+                "help",
+                "connect",
+                "disconnect",
+                "exit",
+                "ls",
+                "echo",
+                "domains",
+                "cd",
+                "get",
+                "set",
+                "invoke",
+                "pwd",
+                "setdomain"
+        ]
+        IODevice terminal = new Terminal(new ConsoleReader(), commands)
+        terminal.setup()
+        return new CmdShell(terminal, client)
     }
 }

Added: incubator/kitty/trunk/src/main/groovy/org/apache/kitty/IODevice.groovy
URL: http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/IODevice.groovy?rev=1299288&view=auto
==============================================================================
--- incubator/kitty/trunk/src/main/groovy/org/apache/kitty/IODevice.groovy (added)
+++ incubator/kitty/trunk/src/main/groovy/org/apache/kitty/IODevice.groovy Sat Mar 10 22:17:17
2012
@@ -0,0 +1,24 @@
+/*
+ * 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.kitty
+
+interface IODevice {
+
+    String read()
+    void write(String msg)
+}

Added: incubator/kitty/trunk/src/main/groovy/org/apache/kitty/Terminal.groovy
URL: http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/Terminal.groovy?rev=1299288&view=auto
==============================================================================
--- incubator/kitty/trunk/src/main/groovy/org/apache/kitty/Terminal.groovy (added)
+++ incubator/kitty/trunk/src/main/groovy/org/apache/kitty/Terminal.groovy Sat Mar 10 22:17:17
2012
@@ -0,0 +1,53 @@
+/*
+ * 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.kitty
+
+import jline.History
+import jline.SimpleCompletor
+import jline.ConsoleReader
+
+class Terminal implements IODevice {
+
+    String prompt = 'kitty> '
+    String[] commands
+    ConsoleReader reader
+
+    Terminal(ConsoleReader reader, String[] commands) {
+        this.reader = reader
+        this.commands = commands
+    }
+
+    void setup() {
+        reader.setBellEnabled false
+        reader.setUseHistory true
+
+        def historyFile = new File(System.getProperty("user.home"), "kitty.history")
+        historyFile.createNewFile()
+        def history = new History(historyFile)
+        reader.setHistory history
+        reader.setDefaultPrompt prompt
+        reader.addCompletor(new SimpleCompletor(commands))
+    }
+
+    String read() {
+        reader?.readLine()?.trim()
+    }
+
+    void write(String message) {
+        reader.printString message
+    }
+}

Modified: incubator/kitty/trunk/src/main/groovy/org/apache/kitty/utils/Help.groovy
URL: http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/utils/Help.groovy?rev=1299288&r1=1299287&r2=1299288&view=diff
==============================================================================
--- incubator/kitty/trunk/src/main/groovy/org/apache/kitty/utils/Help.groovy (original)
+++ incubator/kitty/trunk/src/main/groovy/org/apache/kitty/utils/Help.groovy Sat Mar 10 22:17:17
2012
@@ -24,14 +24,7 @@ package org.apache.kitty.utils
  */
 class Help {
 
-	/**
-	 * 
-	 */
-	public Help() {
-		toString()
-	}
-	
-	public String toString()
+	public String availableCommands()
 	{
 		StringBuffer sb = new StringBuffer()
 		
@@ -43,14 +36,12 @@ class Help {
 		sb.append "get <attribute> - Get an attribute from the remote host\n"
 		sb.append "set <attribute> <value> - Set an attribute on the remote host\n"
 		sb.append "setdomain <domain> - Set the domain in the current session\n"
-        sb.append "domains - List all available MBeanServer domains"
+        sb.append "domains - List all available MBeanServer domains\n"
 		sb.append "invoke <operation> <parameters...> - Invoke an operation on the
remote host\n"
 		sb.append "ls - List all objects and attributes in current mbean path\n"
 		sb.append "pwd - print working directory of mbean path \n"
 
-        /* echo command removed from help, but still currently functional. echo is deprecated
*/
-		
-		return sb.toString()
+        return sb.toString()
 	}
 
 }



Mime
View raw message