From 850560f2237f5d8964479ba2ba16e25c7b752c49 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Wed, 16 Mar 2011 09:49:07 +0200 Subject: [PATCH] DATAKV-46 + add initial Rjc connection/connection factory support --- spring-data-redis/pom.xml | 23 +- .../jedis/JedisConnectionFactory.java | 2 +- .../connection/jredis/JredisConnection.java | 6 +- .../redis/connection/rjc/RjcConnection.java | 709 ++++++++++++++++++ .../connection/rjc/RjcConnectionFactory.java | 209 ++++++ .../redis/connection/rjc/RjcUtils.java | 41 + .../connection/rjc/SingleDataSource.java | 38 + 7 files changed, 1011 insertions(+), 17 deletions(-) create mode 100644 spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcConnection.java create mode 100644 spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcConnectionFactory.java create mode 100644 spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcUtils.java create mode 100644 spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/SingleDataSource.java diff --git a/spring-data-redis/pom.xml b/spring-data-redis/pom.xml index 2f683aefe..c0fe69d97 100644 --- a/spring-data-redis/pom.xml +++ b/spring-data-redis/pom.xml @@ -16,8 +16,10 @@ "[3.0.0, 4.0.0)" 03122010 1.5.2 + 0.6.2 "[1.0.0,2.0.0)" "[1.6, 2.0.0)" + "[0.6.2, 0.6.2]" @@ -135,27 +137,18 @@ compile - org.jredis jredis-anthonylauzon ${jredis.ver} compile + + org.idevlab + rjc + ${rjc.ver} + compile + diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnectionFactory.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnectionFactory.java index 099067bbd..51f326a39 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnectionFactory.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnectionFactory.java @@ -34,7 +34,7 @@ import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Protocol; /** - * Connection factory using creating Jedis based connections. + * Connection factory creating Jedis based connections. * * @author Costin Leau */ diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnection.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnection.java index dca7e829d..6357ff522 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnection.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnection.java @@ -81,7 +81,11 @@ public class JredisConnection implements RedisConnection { // don't actually close the connection // if a pool is used if (!isPool) { - jredis.quit(); + try { + jredis.quit(); + } catch (Exception ex) { + throw convertJredisAccessException(ex); + } } } diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcConnection.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcConnection.java new file mode 100644 index 000000000..1983555d5 --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcConnection.java @@ -0,0 +1,709 @@ +/* + * Copyright 2011 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.keyvalue.redis.connection.rjc; + +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import org.idevlab.rjc.RedisException; +import org.idevlab.rjc.Session; +import org.idevlab.rjc.SessionFactoryImpl; +import org.springframework.dao.DataAccessException; +import org.springframework.data.keyvalue.UncategorizedKeyvalueStoreException; +import org.springframework.data.keyvalue.redis.connection.DataType; +import org.springframework.data.keyvalue.redis.connection.MessageListener; +import org.springframework.data.keyvalue.redis.connection.RedisConnection; +import org.springframework.data.keyvalue.redis.connection.SortParameters; +import org.springframework.data.keyvalue.redis.connection.Subscription; + +/** + * {@code RedisConnection} implementation on top of rjc library. + * + * @author Costin Leau + */ +public class RjcConnection implements RedisConnection { + + private final int dbIndex; + private final Session session; + private boolean isClosed = false; + + public RjcConnection(org.idevlab.rjc.ds.RedisConnection connection, int dbIndex) { + session = new SessionFactoryImpl(new SingleDataSource(connection)).create(); + this.dbIndex = dbIndex; + + // select the db + if (dbIndex > 0) { + select(dbIndex); + } + } + + protected DataAccessException convertRjcAccessException(Exception ex) { + if (ex instanceof RedisException) { + return RjcUtils.convertRjcAccessException((RedisException) ex); + } + return new UncategorizedKeyvalueStoreException("Unknown rjc exception", ex); + } + + @Override + public void close() throws DataAccessException { + isClosed = true; + try { + session.close(); + } catch (Exception ex) { + throw convertRjcAccessException(ex); + } + } + + + @Override + public boolean isClosed() { + return isClosed; + } + + @Override + public Session getNativeConnection() { + return session; + } + + @Override + public List closePipeline() { + throw new UnsupportedOperationException(); + } + + + @Override + public boolean isPipelined() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isQueueing() { + throw new UnsupportedOperationException(); + } + + @Override + public void openPipeline() { + throw new UnsupportedOperationException(); + } + + @Override + public Long del(byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] echo(byte[] message) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean exists(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean expire(byte[] key, long seconds) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean expireAt(byte[] key, long unixTime) { + throw new UnsupportedOperationException(); + } + + @Override + public Set keys(byte[] pattern) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean persist(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public String ping() { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] randomKey() { + throw new UnsupportedOperationException(); + } + + @Override + public void rename(byte[] oldName, byte[] newName) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean renameNX(byte[] oldName, byte[] newName) { + throw new UnsupportedOperationException(); + } + + @Override + public void select(int dbIndex) { + throw new UnsupportedOperationException(); + } + + @Override + public List sort(byte[] key, SortParameters params) { + throw new UnsupportedOperationException(); + } + + @Override + public Long sort(byte[] key, SortParameters params, byte[] storeKey) { + throw new UnsupportedOperationException(); + } + + @Override + public Long ttl(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public DataType type(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public void discard() { + throw new UnsupportedOperationException(); + } + + @Override + public List exec() { + throw new UnsupportedOperationException(); + } + + @Override + public void multi() { + throw new UnsupportedOperationException(); + } + + @Override + public void unwatch() { + throw new UnsupportedOperationException(); + } + + @Override + public void watch(byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public Long append(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long decr(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Long decrBy(byte[] key, long value) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] get(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean getBit(byte[] key, long offset) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] getRange(byte[] key, int begin, int end) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] getSet(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long incr(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Long incrBy(byte[] key, long value) { + throw new UnsupportedOperationException(); + } + + @Override + public List mGet(byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public void mSet(Map tuple) { + throw new UnsupportedOperationException(); + } + + @Override + public void mSetNX(Map tuple) { + throw new UnsupportedOperationException(); + } + + @Override + public void set(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public void setBit(byte[] key, long offset, boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public void setEx(byte[] key, long seconds, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean setNX(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public void setRange(byte[] key, int begin, int end) { + throw new UnsupportedOperationException(); + } + + @Override + public Long strLen(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public List bLPop(int timeout, byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public List bRPop(int timeout, byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] bRPopLPush(int timeout, byte[] srcKey, byte[] dstKey) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] lIndex(byte[] key, long index) { + throw new UnsupportedOperationException(); + } + + @Override + public Long lInsert(byte[] key, Position where, byte[] pivot, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long lLen(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] lPop(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Long lPush(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long lPushX(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public List lRange(byte[] key, long begin, long end) { + throw new UnsupportedOperationException(); + } + + @Override + public Long lRem(byte[] key, long count, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public void lSet(byte[] key, long index, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public void lTrim(byte[] key, long begin, long end) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] rPop(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] rPopLPush(byte[] srcKey, byte[] dstKey) { + throw new UnsupportedOperationException(); + } + + @Override + public Long rPush(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long rPushX(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean sAdd(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long sCard(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Set sDiff(byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public void sDiffStore(byte[] destKey, byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public Set sInter(byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public void sInterStore(byte[] destKey, byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean sIsMember(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Set sMembers(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean sMove(byte[] srcKey, byte[] destKey, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] sPop(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] sRandMember(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean sRem(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Set sUnion(byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public void sUnionStore(byte[] destKey, byte[]... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean zAdd(byte[] key, double score, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zCard(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zCount(byte[] key, double min, double max) { + throw new UnsupportedOperationException(); + } + + @Override + public Double zIncrBy(byte[] key, double increment, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zInterStore(byte[] destKey, byte[]... sets) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) { + throw new UnsupportedOperationException(); + } + + @Override + public Set zRange(byte[] key, long begin, long end) { + throw new UnsupportedOperationException(); + } + + @Override + public Set zRangeByScore(byte[] key, double min, double max) { + throw new UnsupportedOperationException(); + } + + @Override + public Set zRangeByScore(byte[] key, double min, double max, long offset, long count) { + throw new UnsupportedOperationException(); + } + + @Override + public Set zRangeByScoreWithScore(byte[] key, double min, double max) { + throw new UnsupportedOperationException(); + } + + @Override + public Set zRangeByScoreWithScore(byte[] key, double min, double max, long offset, long count) { + throw new UnsupportedOperationException(); + } + + @Override + public Set zRangeWithScore(byte[] key, long begin, long end) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zRank(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean zRem(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zRemRange(byte[] key, long begin, long end) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zRemRangeByScore(byte[] key, double min, double max) { + throw new UnsupportedOperationException(); + } + + @Override + public Set zRevRange(byte[] key, long begin, long end) { + throw new UnsupportedOperationException(); + } + + @Override + public Set zRevRangeWithScore(byte[] key, long begin, long end) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zRevRank(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Double zScore(byte[] key, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zUnionStore(byte[] destKey, byte[]... sets) { + throw new UnsupportedOperationException(); + } + + @Override + public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean hDel(byte[] key, byte[] field) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean hExists(byte[] key, byte[] field) { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] hGet(byte[] key, byte[] field) { + throw new UnsupportedOperationException(); + } + + @Override + public Map hGetAll(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Long hIncrBy(byte[] key, byte[] field, long delta) { + throw new UnsupportedOperationException(); + } + + @Override + public Set hKeys(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public Long hLen(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public List hMGet(byte[] key, byte[]... fields) { + throw new UnsupportedOperationException(); + } + + @Override + public void hMSet(byte[] key, Map hashes) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean hSet(byte[] key, byte[] field, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean hSetNX(byte[] key, byte[] field, byte[] value) { + throw new UnsupportedOperationException(); + } + + @Override + public List hVals(byte[] key) { + throw new UnsupportedOperationException(); + } + + @Override + public void bgSave() { + throw new UnsupportedOperationException(); + } + + @Override + public void bgWriteAof() { + throw new UnsupportedOperationException(); + } + + @Override + public Long dbSize() { + throw new UnsupportedOperationException(); + } + + @Override + public void flushAll() { + throw new UnsupportedOperationException(); + } + + @Override + public void flushDb() { + throw new UnsupportedOperationException(); + } + + @Override + public List getConfig(String pattern) { + throw new UnsupportedOperationException(); + } + + @Override + public Properties info() { + throw new UnsupportedOperationException(); + } + + @Override + public Long lastSave() { + throw new UnsupportedOperationException(); + } + + @Override + public void resetConfigStats() { + throw new UnsupportedOperationException(); + } + + @Override + public void save() { + throw new UnsupportedOperationException(); + } + + @Override + public void setConfig(String param, String value) { + throw new UnsupportedOperationException(); + } + + @Override + public void shutdown() { + throw new UnsupportedOperationException(); + } + + @Override + public Subscription getSubscription() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isSubscribed() { + throw new UnsupportedOperationException(); + } + + @Override + public void pSubscribe(MessageListener listener, byte[]... patterns) { + throw new UnsupportedOperationException(); + } + + @Override + public Long publish(byte[] channel, byte[] message) { + throw new UnsupportedOperationException(); + } + + @Override + public void subscribe(MessageListener listener, byte[]... channels) { + throw new UnsupportedOperationException(); + } + +} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcConnectionFactory.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcConnectionFactory.java new file mode 100644 index 000000000..97f1c65cd --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcConnectionFactory.java @@ -0,0 +1,209 @@ +/* + * Copyright 2011 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.keyvalue.redis.connection.rjc; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.idevlab.rjc.ds.DataSource; +import org.idevlab.rjc.ds.PoolableDataSource; +import org.idevlab.rjc.ds.SimpleDataSource; +import org.idevlab.rjc.protocol.Protocol; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.dao.DataAccessException; +import org.springframework.data.keyvalue.redis.connection.RedisConnection; +import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory; +import org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.util.Assert; + +/** + * Connection factory creating rjc based connections. + * + * @author Costin Leau + */ +public class RjcConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory { + + private final static Log log = LogFactory.getLog(JedisConnectionFactory.class); + + private String hostName = "localhost"; + private int port = Protocol.DEFAULT_PORT; + private int timeout = Protocol.DEFAULT_TIMEOUT; + private String password; + + private boolean usePool = true; + private int dbIndex = 0; + private DataSource dataSource; + + + /** + * Constructs a new RjcConnectionFactory instance + * with default settings (default connection pooling, no shard information). + */ + public RjcConnectionFactory() { + } + + + public void afterPropertiesSet() { + if (usePool) { + PoolableDataSource pool = new PoolableDataSource(); + pool.setHost(hostName); + pool.setPort(port); + pool.setPassword(password); + pool.setTimeout(timeout); + + pool.init(); + + dataSource = pool; + + } + else { + dataSource = new SimpleDataSource(hostName, port, timeout, password); + } + } + + public void destroy() { + if (usePool && dataSource != null) { + try { + ((PoolableDataSource) dataSource).close(); + } catch (Exception ex) { + log.warn("Cannot properly close Rjc pool", ex); + } + dataSource = null; + } + } + + @Override + public RedisConnection getConnection() { + return postProcessConnection(new RjcConnection(dataSource.getConnection(), usePool, dbIndex)); + } + + /** + * Post process a newly retrieved connection. Useful for decorating or executing + * initialization commands on a new connection. + * This implementation simply returns the connection. + * + * @param connection + * @return processed connection + */ + protected RjcConnection postProcessConnection(RjcConnection connection) { + return connection; + } + + @Override + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + return RjcUtils.convertRjcAccessException(ex); + } + + + /** + * Returns the Redis hostName. + * + * @return Returns the hostName + */ + public String getHostName() { + return hostName; + } + + /** + * Sets the Redis hostName. + * + * @param hostName The hostName to set. + */ + public void setHostName(String hostName) { + this.hostName = hostName; + } + + /** + * Returns the password used for authenticating with the Redis server. + * + * @return password for authentication + */ + public String getPassword() { + return password; + } + + /** + * Sets the password used for authenticating with the Redis server. + * + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } + + /** + * Returns the port used to connect to the Redis instance. + * + * @return Redis port. + */ + public int getPort() { + return port; + + } + + /** + * Sets the port used to connect to the Redis instance. + * + * @param port Redis port + */ + public void setPort(int port) { + this.port = port; + } + /** + * Returns the timeout. + * + * @return Returns the timeout + */ + public int getTimeout() { + return timeout; + } + + /** + * @param timeout The timeout to set. + */ + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + /** + * Indicates the use of a connection pool. + * + * @return Returns the use of connection pooling. + */ + public boolean getUsePool() { + return usePool; + } + + /** + * Turns on or off the use of connection pooling. + * + * @param usePool The usePool to set. + */ + public void setUsePool(boolean usePool) { + this.usePool = usePool; + } + + /** + * Sets the index of the database used by this connection factory. + * Can be between 0 (default) and 15. + * + * @param index database index + */ + public void setDatabase(int index) { + Assert.isTrue(index >= 0, "invalid DB index (a positive index required)"); + this.dbIndex = index; + } +} \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcUtils.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcUtils.java new file mode 100644 index 000000000..9c0370bb0 --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/RjcUtils.java @@ -0,0 +1,41 @@ +/* + * Copyright 2011 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.keyvalue.redis.connection.rjc; + +import org.idevlab.rjc.RedisException; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.InvalidDataAccessApiUsageException; +import org.springframework.data.keyvalue.redis.UncategorizedRedisException; + +/** + * Helper class featuring methods for RJC connection handling, providing support for exception translation. + * + * @author Costin Leau + */ +public abstract class RjcUtils { + + public static DataAccessException convertRjcAccessException(RuntimeException ex) { + if (ex instanceof RedisException) { + return convertRjcAccessException((RedisException) ex); + } + + return new UncategorizedRedisException("Unknown exception", ex); + } + + public static DataAccessException convertRjcAccessException(RedisException ex) { + return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); + } +} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/SingleDataSource.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/SingleDataSource.java new file mode 100644 index 000000000..db152b72c --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/rjc/SingleDataSource.java @@ -0,0 +1,38 @@ +/* + * Copyright 2011 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.keyvalue.redis.connection.rjc; + +import org.idevlab.rjc.ds.DataSource; +import org.idevlab.rjc.ds.RedisConnection; + +/** + * Basic data source that always returns the same connection. + * + * @author Costin Leau + */ +class SingleDataSource implements DataSource { + + private final RedisConnection connection; + + SingleDataSource(RedisConnection connection) { + this.connection = connection; + } + + @Override + public RedisConnection getConnection() { + return connection; + } +}