diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CommandProcessor.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CommandProcessor.java index a86bee9a..28d0a013 100644 --- a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CommandProcessor.java +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CommandProcessor.java @@ -16,13 +16,18 @@ package org.springframework.data.gemfire.samples.helloworld; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.util.Scanner; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import com.gemstone.gemfire.cache.Region; + /** * @author Costin Leau */ @@ -30,33 +35,47 @@ public class CommandProcessor { private static final Log log = LogFactory.getLog(CommandProcessor.class); + private static String help = initHelp(); + private static String EMPTY = ""; + boolean threadActive; private Thread thread; + private Region region; + + public CommandProcessor(Region region) { + this.region = region; + } + void start() { if (thread == null) { - threadActive = false; + threadActive = true; thread = new Thread(new Task(), "cmd-processor"); thread.start(); } } void stop() throws Exception { - threadActive = true; - thread.join(3 * 1000); + threadActive = false; + thread.join(); + } + + void awaitCommands() throws Exception { + thread.join(); } private class Task implements Runnable { public void run() { + System.out.println("Hello World!"); + System.out.println("Want to interact with the world ? ..."); + System.out.println(help); try { - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (threadActive) { if (br.ready()) { - String line = br.readLine(); - log.info("Read line " + line); + process(br.readLine()); } } } catch (IOException ioe) { @@ -65,4 +84,74 @@ public class CommandProcessor { } } } -} + + private static String initHelp() { + try { + InputStream stream = CommandProcessor.class.getResourceAsStream("help.txt"); + byte[] buffer = new byte[stream.available() > 0 ? stream.available() : 300]; + + BufferedInputStream bf = new BufferedInputStream(stream); + bf.read(buffer); + return new String(buffer); + } catch (IOException io) { + throw new IllegalStateException("Cannot read help file"); + } + } + + String process(String line) throws Exception { + Scanner sc = new Scanner(line); + String command = sc.next(); + + // parse commands w/o arguments + if ("exit".equalsIgnoreCase(command)) { + threadActive = false; + return EMPTY; + } + if ("help".equalsIgnoreCase(command)) { + return help; + } + if ("size".equalsIgnoreCase(command)) { + return EMPTY + region.size(); + } + if ("clear".equalsIgnoreCase(command)) { + region.clear(); + return EMPTY; + } + if ("keys".equalsIgnoreCase(command)) { + return region.keySet().toString(); + } + if ("values".equalsIgnoreCase(command)) { + return region.values().toString(); + } + + String arg = sc.next(); + + // commands w/ 1 arg + if ("containsKey".equalsIgnoreCase(command)) { + return EMPTY + region.containsKey(arg); + } + if ("containsValue".equalsIgnoreCase(command)) { + return EMPTY + region.containsValue(arg); + } + if ("query".equalsIgnoreCase(command)) { + return region.query(command).toString(); + } + if ("get".equalsIgnoreCase(command)) { + return region.get(arg); + } + if ("remove".equalsIgnoreCase(command)) { + return region.remove(arg); + } + + // commands w/ 2 args + + String arg2 = sc.next(); + + if ("put".equalsIgnoreCase(command)) { + return region.put(arg, arg2); + } + + sc.close(); + return "unknown command - run 'help' for available commands"; + } +} \ No newline at end of file diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/HelloWorld.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/HelloWorld.java index cb915672..3901e926 100644 --- a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/HelloWorld.java +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/HelloWorld.java @@ -39,7 +39,7 @@ public class HelloWorld { private static final Log log = LogFactory.getLog(HelloWorld.class); // inject the region - @Resource + @Resource(name = "hw-region") private Region region; // re-inject the region (as a Map) @@ -54,7 +54,7 @@ public class HelloWorld { void start() { log.info("Member " + region.getCache().getDistributedSystem().getDistributedMember().getId() + " connecting to region [" + region.getName() + "]"); - processor = new CommandProcessor(); + processor = new CommandProcessor(region); processor.start(); } @@ -64,4 +64,12 @@ public class HelloWorld { + " disconnecting from region [" + region.getName() + "]"); processor.stop(); } + + public void greetWorld() { + try { + processor.awaitCommands(); + } catch (Exception ex) { + throw new IllegalStateException("Cannot greet world", ex); + } + } } \ No newline at end of file diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/Main.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/Main.java index e229dc0c..0e8a5edb 100644 --- a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/Main.java +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/Main.java @@ -45,5 +45,9 @@ public class Main { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(res); // shutdown the context along with the VM ctx.registerShutdownHook(); + + // call greet world to prevent the thread from ending + HelloWorld bean = ctx.getBean(HelloWorld.class); + bean.greetWorld(); } } diff --git a/samples/hello-world/src/main/resources/org/springframework/data/gemfire/samples/helloworld/help.txt b/samples/hello-world/src/main/resources/org/springframework/data/gemfire/samples/helloworld/help.txt new file mode 100644 index 00000000..37ca4d25 --- /dev/null +++ b/samples/hello-world/src/main/resources/org/springframework/data/gemfire/samples/helloworld/help.txt @@ -0,0 +1,16 @@ +Supported commands are: + +get - retrieves an entry (by key) from the grid +put - puts a new entry into the grid +remove - removes an entry (by key) from the grid +size - returns the size of the grid +clear - removes all mapping in the grid +keys - returns the keys contained by the grid +values - returns the values contained by the grid +containsKey - indicates if the given key is contained by the grid +containsValue - indicates if the given value is contained by the grid + +query - executes a query on the grid + +help - this info +exit - this node exists \ No newline at end of file