diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java index df33b4f41..52ea0341f 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java @@ -15,6 +15,7 @@ */ package org.springframework.datastore.redis.connection.jredis; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashSet; @@ -39,9 +40,9 @@ public class JredisConnection implements RedisConnection { private final JRedis jredis; - private final String charset; + private final Charset charset; - public JredisConnection(JRedis jredis, String charset) { + public JredisConnection(JRedis jredis, Charset charset) { this.jredis = jredis; this.charset = charset; } @@ -127,7 +128,7 @@ public class JredisConnection implements RedisConnection { @Override public Collection keys(byte[] pattern) { try { - return JredisUtils.convert(charset, jredis.keys(pattern)); + return JredisUtils.convert(charset, jredis.keys(JredisUtils.convert(charset, pattern))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -475,7 +476,7 @@ public class JredisConnection implements RedisConnection { try { List result = jredis.sdiff(set1, sets); - return JredisUtils.convertToStringCollection(result, Set.class); + return new LinkedHashSet(result); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -500,7 +501,7 @@ public class JredisConnection implements RedisConnection { try { List result = jredis.sinter(set1, sets); - return JredisUtils.convertToStringCollection(result, Set.class); + return new LinkedHashSet(result); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -650,8 +651,7 @@ public class JredisConnection implements RedisConnection { @Override public Set zRange(byte[] key, int start, int end) { try { - return JredisUtils.convertToStringCollection(jredis.zrange(JredisUtils.convert(charset, key), (long) start, - (long) end), Set.class); + return new LinkedHashSet(jredis.zrange(JredisUtils.convert(charset, key), (long) start, (long) end)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -666,8 +666,7 @@ public class JredisConnection implements RedisConnection { @Override public Set zRangeByScore(byte[] key, double min, double max) { try { - return JredisUtils.convertToStringCollection(jredis.zrangebyscore(JredisUtils.convert(charset, key), min, - max), Set.class); + return new LinkedHashSet(jredis.zrangebyscore(JredisUtils.convert(charset, key), min, max)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -727,8 +726,7 @@ public class JredisConnection implements RedisConnection { @Override public Set zRevRange(byte[] key, int start, int end) { try { - return JredisUtils.convertToStringCollection( - jredis.zrevrange(JredisUtils.convert(charset, key), start, end), Set.class); + return new LinkedHashSet(jredis.zrevrange(JredisUtils.convert(charset, key), start, end)); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -800,9 +798,9 @@ public class JredisConnection implements RedisConnection { } @Override - public Set hGetAll(byte[] key) { + public Map hGetAll(byte[] key) { try { - return JredisUtils.convert(jredis.hgetall(JredisUtils.convert(charset, key))); + return JredisUtils.convertMap(charset, jredis.hgetall(JredisUtils.convert(charset, key))); } catch (RedisException ex) { throw JredisUtils.convertJredisAccessException(ex); } @@ -838,7 +836,7 @@ public class JredisConnection implements RedisConnection { } @Override - public void hMSet(byte[] key, byte[][] fields, byte[][] values) { + public void hMSet(byte[] key, Map values) { throw new UnsupportedOperationException(); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnectionFactory.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnectionFactory.java index a8e6c697a..4037c0783 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnectionFactory.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnectionFactory.java @@ -15,6 +15,8 @@ */ package org.springframework.datastore.redis.connection.jredis; +import java.nio.charset.Charset; + import org.jredis.JRedis; import org.jredis.connector.ConnectionSpec; import org.jredis.connector.Connection.Socket.Property; @@ -47,8 +49,9 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean // taken from JRedis code private int poolSize = 5; - - private String charset = "ISO-8859-1"; + + private Charset charset = Charset.forName("UTF8"); + /** * Constructs a new JredisConnectionFactory instance. @@ -89,7 +92,6 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean this.connectionSpec = connectionSpec; } - @Override public void afterPropertiesSet() { if (StringUtils.hasLength(password)) { @@ -182,7 +184,7 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean * * @return */ - public String getCharset() { + public Charset getCharset() { return charset; } @@ -190,7 +192,7 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean /** * @param charset */ - public void setCharset(String charset) { + public void setCharset(Charset charset) { this.charset = charset; } } \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisUtils.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisUtils.java index 9c33d58c9..38a5e73b6 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisUtils.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisUtils.java @@ -16,22 +16,18 @@ package org.springframework.datastore.redis.connection.jredis; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.Set; import org.jredis.RedisException; import org.jredis.RedisType; import org.springframework.dao.DataAccessException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.datastore.redis.connection.DataType; -import org.springframework.datastore.redis.connection.DefaultEntry; -import org.springframework.datastore.redis.connection.RedisHashCommands.Entry; /** * Helper class featuring methods for JRedis connection handling, providing support for exception translation. @@ -44,41 +40,18 @@ public abstract class JredisUtils { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } - static String convert(byte[] bytes) { - return new String(bytes); + static String convert(Charset charset, byte[] bytes) { + return new String(bytes, charset); } - static String convert(String encoding, byte[] bytes) { - try { - return new String(bytes, encoding); - } catch (UnsupportedEncodingException ex) { - throw new RuntimeException(ex); - } - } - - static String[] convertMultiple(String encoding, byte[]... bytes) { + static String[] convertMultiple(Charset charset, byte[]... bytes) { String[] result = new String[bytes.length]; - try { - for (int i = 0; i < bytes.length; i++) { - result[i] = new String(bytes[i], encoding); - } - } catch (UnsupportedEncodingException ex) { - throw new RuntimeException(ex); + for (int i = 0; i < bytes.length; i++) { + result[i] = new String(bytes[i], charset); } return result; } - static > T convertToStringCollection(List bytes, Class collectionType) { - - Collection col = (List.class.isAssignableFrom(collectionType) ? new ArrayList(bytes.size()) - : new LinkedHashSet(bytes.size())); - - for (byte[] bs : bytes) { - col.add(new String(bs)); - } - return (T) col; - } - static DataType convertDataType(RedisType type) { switch (type) { case NONE: @@ -98,56 +71,32 @@ public abstract class JredisUtils { return null; } - static Set convert(Map map) { - Set entries = new LinkedHashSet(map.size()); + static Map convertMap(Charset charset, Map map) { + Map result = new LinkedHashMap(map.size()); for (Map.Entry entry : map.entrySet()) { - entries.add(new DefaultEntry(entry.getKey(), new String(entry.getValue()))); - } - return entries; - } - - static Map convert(String[] keys, String[] values) { - Map result = new LinkedHashMap(keys.length); - - for (int i = 0; i < values.length; i++) { - result.put(keys[i], values[i].getBytes()); + result.put(entry.getKey().getBytes(charset), entry.getValue()); } return result; } - static Collection convert(String charset, List keys) { + static Collection convert(Charset charset, List keys) { Collection list = new ArrayList(keys.size()); - try { - for (String string : keys) { - list.add(string.getBytes(charset)); - } - } catch (UnsupportedEncodingException ex) { - throw new RuntimeException(ex); + for (String string : keys) { + list.add(string.getBytes(charset)); } - return list; } - static byte[] convert(String charset, String string) { - try { - return string.getBytes(charset); - } catch (UnsupportedEncodingException ex) { - throw new RuntimeException(ex); - } + static byte[] convert(Charset charset, String string) { + return string.getBytes(charset); } - static Map convert(String encoding, Map tuple) { + static Map convert(Charset charset, Map tuple) { Map result = new LinkedHashMap(tuple.size()); - try { - - for (Map.Entry entry : tuple.entrySet()) { - result.put(new String(entry.getKey(), encoding), entry.getValue()); - } - } catch (UnsupportedEncodingException ex) { - throw new RuntimeException(ex); + for (Map.Entry entry : tuple.entrySet()) { + result.put(new String(entry.getKey(), charset), entry.getValue()); } - return result; } } \ No newline at end of file diff --git a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisCollectionTest.java b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisCollectionTest.java index 06cb093c4..1b493d6ca 100644 --- a/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisCollectionTest.java +++ b/spring-datastore-redis/src/test/java/org/springframework/datastore/redis/util/AbstractRedisCollectionTest.java @@ -60,7 +60,7 @@ public abstract class AbstractRedisCollectionTest { @After public void tearDown() throws Exception { // remove the collection entirely since clear() doesn't always work - collection.getCommands().del(collection.getKey()); + collection.getCommands().del(collection.getKey().getBytes()); ((RedisConnection) collection.getCommands()).close(); destroyCollection(); }