Author: anovarini
Date: Sat Mar 17 17:10:17 2012
New Revision: 1301964
URL: http://svn.apache.org/viewvc?rev=1301964&view=rev
Log:
Started refactoring for events handling; connect and setdomain now change the
prompt, added some test
Added:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/event/
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/ConnectListener.groovy
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/DomainListener.groovy
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/command/
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/command/ConnectTest.groovy
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/io/
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/io/TerminalTest.groovy
Modified:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/CmdShell.groovy
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/Client.groovy
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/jmxmp/JMXMPClient.groovy
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/Connect.groovy
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/SetDomain.groovy
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/io/Terminal.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=1301964&r1=1301963&r2=1301964&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 17 17:10:17 2012
@@ -195,7 +195,8 @@ class CmdShell {
}
void cmdConnect(String... args) {
- Command connect = new Connect(ioDevice)
+ Command connect = new Connect()
+ connect.addConnectListener ioDevice
connect.execute(client, args)
}
@@ -245,7 +246,8 @@ class CmdShell {
}
void cmdSetDomain(String domain) {
- Command setDomain = new SetDomain(ioDevice)
+ Command setDomain = new SetDomain()
+ setDomain.addDomainListener ioDevice
setDomain.execute(client, domain)
}
}
Modified:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/Client.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/Client.groovy?rev=1301964&r1=1301963&r2=1301964&view=diff
==============================================================================
--- incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/Client.groovy
(original)
+++ incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/Client.groovy
Sat Mar 17 17:10:17 2012
@@ -36,11 +36,11 @@ class Client {
def url
def mbean
- public connect(def host, def port) {
+ void connect(def host, def port) {
connect(host, port, null, null)
}
- public connect(def host, def port, def username, def password) {
+ void connect(def host, def port, def username, def password) {
def serviceURL
def properties = new HashMap()
@@ -53,24 +53,15 @@ class Client {
properties.put JMXConnector.CREDENTIALS, [username, password] as
String[]
}
- try {
- serviceURL = "service:jmx:rmi:///jndi/rmi://$host:$port/jmxrmi"
- this.url = new JMXServiceURL(serviceURL)
+ serviceURL = "service:jmx:rmi:///jndi/rmi://$host:$port/jmxrmi"
+ this.url = new JMXServiceURL(serviceURL)
- // TODO add auth & credentials to properties
- this.connector = JMXConnectorFactory.connect(this.url, properties)
- this.remote = this.connector.getMBeanServerConnection()
- }
- catch (Exception e) {
- connector = null
- remote = null
- println "ERROR! " + e.getMessage()
- }
- finally {
- if (this.remote != null) {
- this.host = host
- this.port = port
- }
+ // TODO add auth & credentials to properties
+ this.connector = JMXConnectorFactory.connect(this.url, properties)
+ this.remote = this.connector.getMBeanServerConnection()
+ if (this.remote != null) {
+ this.host = host
+ this.port = port
}
}
@@ -191,16 +182,16 @@ class Client {
}
try {
- mbean = this.remote.getMBeanInfo(new ObjectName(objectName))
- for (ops in mbean.getOperations()) {
- def params = []
- for (p in ops.getSignature()) {
- params.append(p.getType())
+ mbean = this.remote.getMBeanInfo(new ObjectName(objectName))
+ for (ops in mbean.getOperations()) {
+ def params = []
+ for (p in ops.getSignature()) {
+ params.append(p.getType())
+ }
+ println "O " + ops.getReturnType() + " " + ops.getName() + " (
" + ",".concat(params.join()) + ")"
}
- println "O " + ops.getReturnType() + " " + ops.getName() + " ( " +
",".concat(params.join()) + ")"
}
- }
- catch(Exception e) {
+ catch (Exception e) {
// ObjectName not found
}
Modified:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/jmxmp/JMXMPClient.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/jmxmp/JMXMPClient.groovy?rev=1301964&r1=1301963&r2=1301964&view=diff
==============================================================================
---
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/jmxmp/JMXMPClient.groovy
(original)
+++
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/client/jmxmp/JMXMPClient.groovy
Sat Mar 17 17:10:17 2012
@@ -38,7 +38,7 @@ import org.apache.kitty.client.Client
class JMXMPClient extends Client {
@Override
- public connect(def host, def port) {
+ void connect(def host, def port) {
if (this.remote != null) {
disconnect()
Modified:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/Connect.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/Connect.groovy?rev=1301964&r1=1301963&r2=1301964&view=diff
==============================================================================
---
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/Connect.groovy
(original)
+++
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/Connect.groovy
Sat Mar 17 17:10:17 2012
@@ -19,16 +19,15 @@ package org.apache.kitty.command
import org.apache.kitty.client.Client
import org.apache.kitty.Command
import org.apache.kitty.IODevice
+import org.apache.kitty.listener.DomainListener
+import org.apache.kitty.listener.ConnectListener
class Connect implements Command {
static final String HOST = "localhost"
static final String PORT = "1099"
- IODevice ioDevice
-
- Connect(IODevice ioDevice) {
- this.ioDevice = ioDevice
- }
+ @groovy.beans.ListenerList
+ List<ConnectListener> listeners
@Override
void execute(Client client, String... args) {
@@ -36,13 +35,17 @@ class Connect implements Command {
def port = args.length >= 2 ? args[1] : PORT
if (args?.length == 4) {
- client.connect(host, port, args[2], args[3])
+ String user = args[2]
+ String password = args[3]
+ client.connect host, port, user, password
+ fireConnectionSucceeded host, port, user
}
else {
- client.connect(host, port)
+ client.connect host, port
+ fireConnectionSucceeded host, port
}
if (!client.connected) {
- ioDevice.write "Couldn't connect to $host:$port"
+ fireConnectionErrorOccurred "Couldn't connect to $host:$port"
}
}
}
Modified:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/SetDomain.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/SetDomain.groovy?rev=1301964&r1=1301963&r2=1301964&view=diff
==============================================================================
---
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/SetDomain.groovy
(original)
+++
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/command/SetDomain.groovy
Sat Mar 17 17:10:17 2012
@@ -20,22 +20,22 @@ import org.apache.kitty.Command
import org.apache.kitty.IODevice
import org.apache.kitty.utils.Constants
import org.apache.kitty.client.Client
+import org.apache.kitty.listener.DomainListener
class SetDomain implements Command {
- IODevice ioDevice
- SetDomain(IODevice ioDevice) {
- this.ioDevice = ioDevice
- }
+ @groovy.beans.ListenerList
+ List<DomainListener> listeners
@Override
void execute(Client client, String... args) {
String domain = args[0]
if (client.connected) {
client.domain = domain
+ fireDomainChanged domain
}
else {
- ioDevice.write Constants.ERROR_NOT_CONNECTED
+ fireDomainErrorOccurred Constants.ERROR_NOT_CONNECTED
}
}
}
Modified:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/io/Terminal.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/io/Terminal.groovy?rev=1301964&r1=1301963&r2=1301964&view=diff
==============================================================================
--- incubator/kitty/trunk/src/main/groovy/org/apache/kitty/io/Terminal.groovy
(original)
+++ incubator/kitty/trunk/src/main/groovy/org/apache/kitty/io/Terminal.groovy
Sat Mar 17 17:10:17 2012
@@ -20,12 +20,17 @@ import jline.History
import jline.SimpleCompletor
import jline.ConsoleReader
import org.apache.kitty.IODevice
+import org.apache.kitty.listener.DomainListener
+import org.apache.kitty.listener.ConnectListener
-class Terminal implements IODevice {
+class Terminal implements IODevice, DomainListener, ConnectListener {
- String prompt = 'kitty> '
String[] commands
ConsoleReader reader
+ String domain
+ String host
+ String port
+ String user
Terminal(ConsoleReader reader, String[] commands) {
this.reader = reader
@@ -44,7 +49,16 @@ class Terminal implements IODevice {
}
String read() {
- reader?.readLine(prompt, 0 as char)?.trim()
+ reader?.readLine(createPrompt(), 0 as char)?.trim()
+ }
+
+ String createPrompt() {
+ String userToDisplay = user?:''
+ String hostToDisplay = host?"@$host":''
+ String portToDisplay = port?":$port":''
+ String domainToDisplay = domain?"/$domain":''
+ String stringToDisplay =
"$userToDisplay$hostToDisplay$portToDisplay$domainToDisplay"?:'kitty'
+ "$stringToDisplay> "
}
void write(String message) {
@@ -54,4 +68,32 @@ class Terminal implements IODevice {
void close() {
reader.flushConsole()
}
+
+ @Override
+ void domainChanged(String domain) {
+ this.domain = domain
+ }
+
+ @Override
+ void domainErrorOccurred(String description) {
+ reader.printString description
+ }
+
+ @Override
+ void connectionSucceeded(String host, String port) {
+ this.host = host
+ this.port = port
+ }
+
+ @Override
+ void connectionSucceeded(String host, String port, String user) {
+ this.host = host
+ this.port = port
+ this.user = user
+ }
+
+ @Override
+ void connectionErrorOccurred(String description) {
+ reader.printString description
+ }
}
Added:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/ConnectListener.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/ConnectListener.groovy?rev=1301964&view=auto
==============================================================================
---
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/ConnectListener.groovy
(added)
+++
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/ConnectListener.groovy
Sat Mar 17 17:10: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.listener
+
+public interface ConnectListener {
+
+ void connectionSucceeded(String host, String port)
+ void connectionSucceeded(String host,String port,String user)
+ void connectionErrorOccurred(String description)
+}
\ No newline at end of file
Added:
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/DomainListener.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/DomainListener.groovy?rev=1301964&view=auto
==============================================================================
---
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/DomainListener.groovy
(added)
+++
incubator/kitty/trunk/src/main/groovy/org/apache/kitty/listener/DomainListener.groovy
Sat Mar 17 17:10:17 2012
@@ -0,0 +1,23 @@
+/*
+ * 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.listener
+
+public interface DomainListener {
+
+ void domainChanged(String domain)
+ void domainErrorOccurred(String description)
+}
\ No newline at end of file
Added:
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/command/ConnectTest.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/test/groovy/org/apache/kitty/command/ConnectTest.groovy?rev=1301964&view=auto
==============================================================================
---
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/command/ConnectTest.groovy
(added)
+++
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/command/ConnectTest.groovy
Sat Mar 17 17:10:17 2012
@@ -0,0 +1,70 @@
+/*
+ * 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.command
+
+import org.apache.kitty.client.Client
+import org.apache.kitty.listener.ConnectListener
+
+class ConnectTest extends GroovyTestCase {
+
+ Connect command
+ String eventFired
+ ConnectListener connectListener
+
+ void setUp() {
+
+ command = new Connect()
+ eventFired = ''
+
+ connectListener = [
+ 'connectionSucceeded': { host, port -> eventFired =
"$host:$port" },
+ 'connectionErrorOccurred': { eventFired = 'error' }
+ ] as ConnectListener
+
+ command.addConnectListener connectListener
+ }
+
+ void testShouldFireConnectionSucceededWhenConnectedToClient() {
+
+ def client = new Client() {
+ void connect(def host, def port, def user, def password) {}
+
+ boolean isConnected() {
+ true
+ }
+ }
+
+ command.execute client, 'myHost', 'myPort'
+
+ assertEquals "Event fired", "myHost:myPort", eventFired
+ }
+
+ void
testShouldFireConnectionErrorOccurredWhenSomeErrorDuringConnectionHappens() {
+
+ def client = new Client() {
+ void connect(def host, def port, def user, def password) {}
+
+ boolean isConnected() {
+ false
+ }
+ }
+
+ command.execute client, 'myHost', 'myPort'
+
+ assertEquals "Event fired", "error", eventFired
+ }
+}
Added:
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/io/TerminalTest.groovy
URL:
http://svn.apache.org/viewvc/incubator/kitty/trunk/src/test/groovy/org/apache/kitty/io/TerminalTest.groovy?rev=1301964&view=auto
==============================================================================
---
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/io/TerminalTest.groovy
(added)
+++
incubator/kitty/trunk/src/test/groovy/org/apache/kitty/io/TerminalTest.groovy
Sat Mar 17 17:10:17 2012
@@ -0,0 +1,55 @@
+/*
+ * 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.io
+
+class TerminalTest extends GroovyTestCase {
+
+ void testShouldReturnDefaultPromptWhenNotConnected() {
+ Terminal terminal = new Terminal(null, null)
+ String prompt = terminal.createPrompt()
+
+ assertEquals "Default prompt", "kitty> ", prompt
+ }
+
+ void testShouldReturnHostAndPortAfterSuccessfullyConnected() {
+ Terminal terminal = new Terminal(null, null)
+ terminal.connectionSucceeded 'aHost', 'aPort'
+
+ String prompt = terminal.createPrompt()
+
+ assertEquals "Prompt", "@aHost:aPort> ", prompt
+ }
+
+ void
testShouldReturnUserHostAndPortAfterSuccessfullyConnectedWithCredentials() {
+ Terminal terminal = new Terminal(null, null)
+ terminal.connectionSucceeded 'aHost', 'aPort', 'anUser'
+
+ String prompt = terminal.createPrompt()
+
+ assertEquals "Prompt", "anUser@aHost:aPort> ", prompt
+ }
+
+ void
testShouldReturnHostPortAndDomainAfterSuccessfullyConnectedAndDomainSet() {
+ Terminal terminal = new Terminal(null, null)
+ terminal.connectionSucceeded 'aHost', 'aPort'
+ terminal.domainChanged 'aDomain'
+
+ String prompt = terminal.createPrompt()
+
+ assertEquals "Prompt", "@aHost:aPort/aDomain> ", prompt
+ }
+}