diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisConnection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisConnection.java new file mode 100644 index 000000000..15f7dc73b --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisConnection.java @@ -0,0 +1,183 @@ +/* + * Copyright 2006-2009 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.datastore.redis.core.connection.jredis; + +import java.util.Collection; + +import org.jredis.JRedis; +import org.jredis.RedisException; +import org.springframework.dao.DataAccessException; +import org.springframework.datastore.keyvalue.UncategorizedKeyvalueStoreException; +import org.springframework.datastore.redis.UncategorizedRedisException; +import org.springframework.datastore.redis.core.connection.DataType; +import org.springframework.datastore.redis.core.connection.RedisConnection; + +/** + * @author Costin Leau + */ +public class JredisConnection implements RedisConnection { + + private final JRedis jredis; + private final String charset; + + public JredisConnection(JRedis jredis, String charset) { + this.jredis = jredis; + this.charset = charset; + } + + protected DataAccessException convertJedisAccessException(Exception ex) { + if (ex instanceof RedisException) { + return JredisUtils.convertJredisAccessException((RedisException) ex); + } + throw new UncategorizedKeyvalueStoreException("Unknown JRedis exception", ex); + } + + @Override + public void close() throws UncategorizedRedisException { + jredis.quit(); + + } + + @Override + public String getCharset() { + return charset; + } + + @Override + public JRedis getNativeConnection() { + return jredis; + } + + @Override + public boolean isClosed() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isQueueing() { + return false; + } + + @Override + public Integer dbSize() { + throw new UnsupportedOperationException(); + } + + @Override + public Integer del(String... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public void discard() { + throw new UnsupportedOperationException(); + } + + @Override + public void exec() { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean exists(String key) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean expire(String key, int seconds) { + throw new UnsupportedOperationException(); + } + + @Override + public Collection keys(String pattern) { + throw new UnsupportedOperationException(); + } + + @Override + public void multi() { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean persist(String key) { + throw new UnsupportedOperationException(); + } + + @Override + public String randomKey() { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean rename(String oldName, String newName) { + throw new UnsupportedOperationException(); + } + + @Override + public Boolean renameNx(String oldName, String newName) { + throw new UnsupportedOperationException(); + } + + @Override + public void select(int dbIndex) { + throw new UnsupportedOperationException(); + } + + @Override + public Integer ttl(String key) { + throw new UnsupportedOperationException(); + } + + @Override + public DataType type(String key) { + throw new UnsupportedOperationException(); + } + + @Override + public void unwatch() { + throw new UnsupportedOperationException(); + } + + @Override + public void watch(String... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public Integer hSet(String key, String field, String value) { + throw new UnsupportedOperationException(); + } + + @Override + public Integer lPush(String key, String value) { + throw new UnsupportedOperationException(); + } + + @Override + public Integer rPush(String key, String value) { + throw new UnsupportedOperationException(); + } + + @Override + public String get(String key) { + throw new UnsupportedOperationException(); + } + + @Override + public void set(String key, String value) { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisConnectionFactory.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisConnectionFactory.java new file mode 100644 index 000000000..b1453642f --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisConnectionFactory.java @@ -0,0 +1,194 @@ +/* + * Copyright 2006-2009 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.datastore.redis.core.connection.jredis; + +import org.jredis.JRedis; +import org.jredis.connector.ConnectionSpec; +import org.jredis.connector.Connection.Socket.Property; +import org.jredis.ri.alphazero.JRedisClient; +import org.jredis.ri.alphazero.JRedisService; +import org.jredis.ri.alphazero.connection.DefaultConnectionSpec; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.dao.DataAccessException; +import org.springframework.datastore.redis.core.connection.RedisConnection; +import org.springframework.datastore.redis.core.connection.RedisConnectionFactory; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +/** + * Connection factory on top of {@link JRedis} client. + * + * @author Costin Leau + */ +public class JredisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory { + + private String encoding = "UTF-8"; + private ConnectionSpec connectionSpec; + + private String password; + private int timeout; + + private boolean usePool = true; + + private JRedisService pool = null; + // taken from JRedis code + private int poolSize = 5; + + + /** + * Constructs a new JredisConnectionFactory instance. + */ + public JredisConnectionFactory() { + this(DefaultConnectionSpec.newSpec()); + } + + + /** + * Constructs a new JredisConnectionFactory instance. + * + * @param hostName + */ + public JredisConnectionFactory(String hostName) { + Assert.hasText(hostName); + throw new UnsupportedOperationException(); + } + + + /** + * Constructs a new JredisConnectionFactory instance. + * + * @param hostName + * @param port + */ + public JredisConnectionFactory(String hostName, int port) { + Assert.hasText(hostName); + throw new UnsupportedOperationException(); + } + + /** + * Constructs a new JredisConnectionFactory instance. + * + * @param connectionSpec + */ + public JredisConnectionFactory(ConnectionSpec connectionSpec) { + this.connectionSpec = connectionSpec; + } + + + @Override + public void afterPropertiesSet() { + if (StringUtils.hasLength(password)) { + connectionSpec.setCredentials(password); + } + + if (timeout > 0) { + connectionSpec.setSocketProperty(Property.SO_TIMEOUT, timeout); + } + + if (usePool) { + int size = getPoolSize(); + pool = new JRedisService(connectionSpec, size); + } + } + + + @Override + public void destroy() { + if (usePool && pool != null) { + //pool.quit(); + pool = null; + } + } + + + @Override + public RedisConnection getConnection() { + return new JredisConnection((usePool ? pool : new JRedisClient(connectionSpec))); + } + + + @Override + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + return null; + } + + /** + * Returns the encoding. + * + * @return Returns the encoding + */ + public String getEncoding() { + return encoding; + } + + /** + * @param encoding The encoding to set. + */ + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + /** + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } + + /** + * Indicates the use of a connection pool. + * + * @return Returns the use of connection pooling. + */ + public boolean isPooling() { + return usePool; + } + + /** + * Turns on or off the use of connection pooling. + * + * @param usePool The usePool to set. + */ + public void setPooling(boolean usePool) { + this.usePool = usePool; + } + + /** + * Returns the poolSize. + * + * @return Returns the poolSize + */ + public int getPoolSize() { + return poolSize; + } + + /** + * @param poolSize The poolSize to set. + */ + public void setPoolSize(int poolSize) { + Assert.isTrue(poolSize > 0, "pool size needs to be bigger then zero"); + this.poolSize = poolSize; + usePool = true; + } +} \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisUtils.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisUtils.java new file mode 100644 index 000000000..fbee9aed2 --- /dev/null +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/core/connection/jredis/JredisUtils.java @@ -0,0 +1,33 @@ +/* + * 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.datastore.redis.core.connection.jredis; + +import org.jredis.RedisException; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.InvalidDataAccessApiUsageException; + +/** + * Helper class featuring methods for JRedis connection handling, providing support for exception translation. + * + * @author Costin Leau + */ +public abstract class JredisUtils { + + public static DataAccessException convertJredisAccessException(RedisException ex) { + return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); + } +}