From eabfc460e3c1037aa36ca322b281ffe5d4594293 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 22 Jan 2016 16:39:22 +0300 Subject: [PATCH] Spring cache YAML config format support. #346 --- pom.xml | 11 +++- .../redisson/spring/cache/CacheConfig.java | 65 +++++++++++++++++++ .../spring/cache/CacheConfigSupport.java | 41 ++++++++++-- 3 files changed, 107 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 315c5e1c9..96b7de00b 100644 --- a/pom.xml +++ b/pom.xml @@ -193,20 +193,25 @@ 1.7.12 + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.6.5 + com.fasterxml.jackson.core jackson-core - 2.6.3 + 2.6.5 com.fasterxml.jackson.core jackson-databind - 2.6.3 + 2.6.5 com.fasterxml.jackson.dataformat jackson-dataformat-cbor - 2.6.3 + 2.6.5 provided diff --git a/src/main/java/org/redisson/spring/cache/CacheConfig.java b/src/main/java/org/redisson/spring/cache/CacheConfig.java index f89208e17..ce947a6c0 100644 --- a/src/main/java/org/redisson/spring/cache/CacheConfig.java +++ b/src/main/java/org/redisson/spring/cache/CacheConfig.java @@ -154,4 +154,69 @@ public class CacheConfig { return new CacheConfigSupport().toJSON(configs); } + /** + * Read config objects stored in YAML format from String + * + * @param content + * @return + * @throws IOException + */ + public static Map fromYAML(String content) throws IOException { + return new CacheConfigSupport().fromYAML(content); + } + + /** + * Read config objects stored in YAML format from InputStream + * + * @param inputStream + * @return + * @throws IOException + */ + public static Map fromYAML(InputStream inputStream) throws IOException { + return new CacheConfigSupport().fromYAML(inputStream); + } + + /** + * Read config objects stored in YAML format from File + * + * @param file + * @return + * @throws IOException + */ + public static Map fromYAML(File file) throws IOException { + return new CacheConfigSupport().fromYAML(file); + } + + /** + * Read config objects stored in YAML format from URL + * + * @param url + * @return + * @throws IOException + */ + public static Map fromYAML(URL url) throws IOException { + return new CacheConfigSupport().fromYAML(url); + } + + /** + * Read config objects stored in YAML format from Reader + * + * @param reader + * @return + * @throws IOException + */ + public static Map fromYAML(Reader reader) throws IOException { + return new CacheConfigSupport().fromYAML(reader); + } + + /** + * Convert current configuration to YAML format + * + * @return + * @throws IOException + */ + public static String toYAML(Map configs) throws IOException { + return new CacheConfigSupport().toYAML(configs); + } + } diff --git a/src/main/java/org/redisson/spring/cache/CacheConfigSupport.java b/src/main/java/org/redisson/spring/cache/CacheConfigSupport.java index e1a042d24..840101d64 100644 --- a/src/main/java/org/redisson/spring/cache/CacheConfigSupport.java +++ b/src/main/java/org/redisson/spring/cache/CacheConfigSupport.java @@ -24,33 +24,60 @@ import java.util.Map; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; public class CacheConfigSupport { - ObjectMapper mapper = new ObjectMapper(); + ObjectMapper jsonMapper = new ObjectMapper(); + ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); public Map fromJSON(String content) throws IOException { - return mapper.readValue(content, new TypeReference>() {}); + return jsonMapper.readValue(content, new TypeReference>() {}); } public Map fromJSON(File file) throws IOException { - return mapper.readValue(file, new TypeReference>() {}); + return jsonMapper.readValue(file, new TypeReference>() {}); } public Map fromJSON(URL url) throws IOException { - return mapper.readValue(url, new TypeReference>() {}); + return jsonMapper.readValue(url, new TypeReference>() {}); } public Map fromJSON(Reader reader) throws IOException { - return mapper.readValue(reader, new TypeReference>() {}); + return jsonMapper.readValue(reader, new TypeReference>() {}); } public Map fromJSON(InputStream inputStream) throws IOException { - return mapper.readValue(inputStream, new TypeReference>() {}); + return jsonMapper.readValue(inputStream, new TypeReference>() {}); } public String toJSON(Map configs) throws IOException { - return mapper.writeValueAsString(configs); + return jsonMapper.writeValueAsString(configs); } + public Map fromYAML(String content) throws IOException { + return yamlMapper.readValue(content, new TypeReference>() {}); + } + + public Map fromYAML(File file) throws IOException { + return yamlMapper.readValue(file, new TypeReference>() {}); + } + + public Map fromYAML(URL url) throws IOException { + return yamlMapper.readValue(url, new TypeReference>() {}); + } + + public Map fromYAML(Reader reader) throws IOException { + return yamlMapper.readValue(reader, new TypeReference>() {}); + } + + public Map fromYAML(InputStream inputStream) throws IOException { + return yamlMapper.readValue(inputStream, new TypeReference>() {}); + } + + public String toYAML(Map configs) throws IOException { + return yamlMapper.writeValueAsString(configs); + } + + }