From 431018ad4beba543af14ebfbbaf549f6a3ecf39e Mon Sep 17 00:00:00 2001 From: costin Date: Mon, 19 Jul 2010 14:06:46 +0300 Subject: [PATCH] + updated sample - removed GridToWorldWriter --- .../samples/helloworld/CacheLogger.java | 2 +- .../samples/helloworld/CommandProcessor.java | 50 +++++++++++-------- .../samples/helloworld/GridToWorldWriter.java | 27 ---------- .../samples/helloworld/HelloWorld.java | 2 +- .../src/main/resources/app-context.xml | 5 +- .../src/main/resources/cache-context.xml | 8 ++- .../src/main/resources/cache.properties | 1 + samples/pom.xml | 7 +++ .../data/gemfire/GemfireTemplate.java | 4 ++ 9 files changed, 49 insertions(+), 57 deletions(-) delete mode 100644 samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/GridToWorldWriter.java diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CacheLogger.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CacheLogger.java index 8000d296..0790ba5f 100644 --- a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CacheLogger.java +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CacheLogger.java @@ -51,7 +51,7 @@ public class CacheLogger extends CacheListenerAdapter { Object value = event.getNewValue(); if (event.getOperation().isUpdate()) { - return "[" + key + "=" + event.getOldValue() + " to " + event.getNewValue() + "]"; + return "[" + key + "] from [" + event.getOldValue() + "] to [" + event.getNewValue() + "]"; } return "[" + key + "=" + value + "]"; } 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 3c45c909..7d0d5227 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 @@ -22,6 +22,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Scanner; +import java.util.Set; +import java.util.Map.Entry; +import java.util.regex.Pattern; import javax.annotation.Resource; @@ -43,6 +46,8 @@ import com.gemstone.gemfire.cache.Region; @Component public class CommandProcessor { + private static final Pattern COM = Pattern.compile("query|exit|help|size|clear|keys|values|map|containsKey|containsValue|get|remove|put"); + private static final Log log = LogFactory.getLog(CommandProcessor.class); private static String help = initHelp(); @@ -120,15 +125,16 @@ public class CommandProcessor { final Scanner sc = new Scanner(line); return template.execute(new GemfireCallback() { + public String doInGemfire(Region reg) throws GemFireCheckedException, GemFireException { Region region = reg; - if (!sc.hasNext()) { - return "Invalid command"; + if (!sc.hasNext(COM)) { + return "Invalid command - type 'help' for supported operations"; } String command = sc.next(); - - + String arg1 = (sc.hasNext() ? sc.next() : null); + String arg2 = (sc.hasNext() ? sc.next() : null); // query shortcut if ("query".equalsIgnoreCase(command)) { @@ -149,7 +155,7 @@ public class CommandProcessor { } if ("clear".equalsIgnoreCase(command)) { region.clear(); - return "Clearing grid"; + return "Clearing grid.."; } if ("keys".equalsIgnoreCase(command)) { return region.keySet().toString(); @@ -158,36 +164,40 @@ public class CommandProcessor { return region.values().toString(); } - if (!sc.hasNext()) { - return "Argument(s) needed"; - } + if ("map".equalsIgnoreCase(command)) { + Set> entrySet = region.entrySet(); + if (entrySet.size() == 0) + return "[]"; - String arg = sc.next(); + StringBuilder sb = new StringBuilder(); + for (Entry entry : entrySet) { + sb.append("["); + sb.append(entry.getKey()); + sb.append("="); + sb.append(entry.getValue()); + sb.append("] "); + } + return sb.toString(); + } // commands w/ 1 arg if ("containsKey".equalsIgnoreCase(command)) { - return EMPTY + region.containsKey(arg); + return EMPTY + region.containsKey(arg1); } if ("containsValue".equalsIgnoreCase(command)) { - return EMPTY + region.containsValue(arg); + return EMPTY + region.containsValue(arg1); } if ("get".equalsIgnoreCase(command)) { - return region.get(arg); + return region.get(arg1); } if ("remove".equalsIgnoreCase(command)) { - return region.remove(arg); + return region.remove(arg1); } // commands w/ 2 args - if (!sc.hasNext()) { - return "2 Argument needed"; - } - - String arg2 = sc.next(); - if ("put".equalsIgnoreCase(command)) { - return region.put(arg, arg2); + return region.put(arg1, arg2); } sc.close(); diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/GridToWorldWriter.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/GridToWorldWriter.java deleted file mode 100644 index 600489d3..00000000 --- a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/GridToWorldWriter.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2010 the original author or authors. - * - * 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. - */ - -package org.springframework.data.gemfire.samples.helloworld; - -import com.gemstone.gemfire.cache.util.CacheWriterAdapter; - -/** - * @author Costin Leau - */ -public class GridToWorldWriter extends CacheWriterAdapter { - - -} 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 5319c28b..aea6598a 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 @@ -37,7 +37,7 @@ public class HelloWorld { private static final Log log = LogFactory.getLog(HelloWorld.class); // inject the region - @Resource(name = "hw-region") + @Resource(name = "myWorld") private Region region; @Resource diff --git a/samples/hello-world/src/main/resources/app-context.xml b/samples/hello-world/src/main/resources/app-context.xml index 8821c712..34d6ebe4 100644 --- a/samples/hello-world/src/main/resources/app-context.xml +++ b/samples/hello-world/src/main/resources/app-context.xml @@ -4,12 +4,11 @@ xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - - + diff --git a/samples/hello-world/src/main/resources/cache-context.xml b/samples/hello-world/src/main/resources/cache-context.xml index ba0a314a..a6424dbd 100644 --- a/samples/hello-world/src/main/resources/cache-context.xml +++ b/samples/hello-world/src/main/resources/cache-context.xml @@ -15,13 +15,11 @@ - + - - - - + + diff --git a/samples/hello-world/src/main/resources/cache.properties b/samples/hello-world/src/main/resources/cache.properties index ec96ee6f..769bd33b 100644 --- a/samples/hello-world/src/main/resources/cache.properties +++ b/samples/hello-world/src/main/resources/cache.properties @@ -1 +1,2 @@ log-level=warning +name=Spring GemFire World diff --git a/samples/pom.xml b/samples/pom.xml index 64eba646..557caee5 100644 --- a/samples/pom.xml +++ b/samples/pom.xml @@ -57,6 +57,13 @@ test + + junit + junit + ${junit.version} + test + + diff --git a/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java b/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java index b1d15aa0..3744f426 100644 --- a/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java +++ b/src/main/java/org/springframework/data/gemfire/GemfireTemplate.java @@ -150,6 +150,10 @@ public class GemfireTemplate extends GemfireAccessor { new CloseSuppressingInvocationHandler(region)); } + //------------------------------------------------------------------------- + // Convenience methods for load, save, delete + //------------------------------------------------------------------------- + /** * Invocation handler that suppresses close calls on GemFire Regions. * @see Region#close()