From b21b53b919a741bc30ad835db14aefbeb4579f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Fri, 17 Feb 2012 10:27:54 +0100 Subject: VariableFactory: Support set() operation. --- .../main/java/com/orbekk/same/VariableFactory.java | 30 ++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'same/src/main/java/com/orbekk/same/VariableFactory.java') diff --git a/same/src/main/java/com/orbekk/same/VariableFactory.java b/same/src/main/java/com/orbekk/same/VariableFactory.java index 81b7615..79c2cdc 100644 --- a/same/src/main/java/com/orbekk/same/VariableFactory.java +++ b/same/src/main/java/com/orbekk/same/VariableFactory.java @@ -1,17 +1,27 @@ package com.orbekk.same; +import java.io.IOException; + +import org.codehaus.jackson.JsonGenerationException; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * TODO: Use WeakReference in order to make variables GC-able. */ public class VariableFactory { - Client.ClientInterface client; + private Logger logger = LoggerFactory.getLogger(getClass()); + private Client.ClientInterface client; + private ObjectMapper mapper = new ObjectMapper(); private class VariableImpl implements Variable { String identifier; TypeReference type; T value; + long revision = 0; public VariableImpl(String identifier, TypeReference type) { this.identifier = identifier; @@ -24,7 +34,23 @@ public class VariableFactory { } @Override - public void set(T value) { + public void set(T value) throws UpdateConflict { + try { + String serializedValue = mapper.writeValueAsString(value); + client.set(identifier, serializedValue, revision); + } catch (JsonGenerationException e) { + logger.warn("Failed to convert to JSON: {}", value); + logger.warn("Parse exception.", e); + throw new RuntimeException(e); + } catch (JsonMappingException e) { + logger.warn("Failed to convert to JSON: {}", value); + logger.warn("Parse exception.", e); + throw new RuntimeException(e); + } catch (IOException e) { + logger.warn("Failed to cornvert to JSON: {}", value); + logger.warn("Parse exception.", e); + throw new RuntimeException(e); + } } @Override -- cgit v1.2.3