diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/hash/BeanUtilsHashMapper.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/hash/BeanUtilsHashMapper.java new file mode 100644 index 000000000..1283eb26e --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/hash/BeanUtilsHashMapper.java @@ -0,0 +1,54 @@ +/* + * 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.hash; + +import java.util.Map; + +import org.apache.commons.beanutils.BeanUtils; + +/** + * HashMapper based on Apache Commons BeanUtils project. Does NOT supports nested properties. + * + * @author Costin Leau + */ +public class BeanUtilsHashMapper implements HashMapper { + + private Class type; + + public BeanUtilsHashMapper(Class type) { + this.type = type; + } + + @Override + public T fromHash(Map hash) { + T instance = org.springframework.beans.BeanUtils.instantiate(type); + try { + BeanUtils.populate(instance, hash); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + return instance; + } + + @Override + public Map toHash(T object) { + try { + return BeanUtils.describe(object); + } catch (Exception ex) { + throw new IllegalArgumentException("Cannot describe object " + object); + } + } +} diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/hash/DecoratingStringHashMapper.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/hash/DecoratingStringHashMapper.java new file mode 100644 index 000000000..378203134 --- /dev/null +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/hash/DecoratingStringHashMapper.java @@ -0,0 +1,51 @@ +/* + * 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.hash; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Delegating hash mapper used for flattening objects into Strings. + * Suitable when dealing with mappers that support Strings and type conversion. + * + * @author Costin Leau + */ +public class DecoratingStringHashMapper implements HashMapper { + + private final HashMapper delegate; + + public DecoratingStringHashMapper(HashMapper mapper) { + this.delegate = mapper; + } + + @SuppressWarnings("unchecked") + @Override + public T fromHash(Map hash) { + Map h = hash; + return delegate.fromHash(h); + } + + @Override + public Map toHash(T object) { + Map hash = delegate.toHash(object); + Map flatten = new LinkedHashMap(hash.size()); + for (Map.Entry entry : hash.entrySet()) { + flatten.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue())); + } + return flatten; + } +} diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/BeanUtilsHashMapperTest.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/BeanUtilsHashMapperTest.java new file mode 100644 index 000000000..de3cc1dad --- /dev/null +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/mapping/BeanUtilsHashMapperTest.java @@ -0,0 +1,36 @@ +/* + * 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.mapping; + +import org.junit.Test; +import org.springframework.data.keyvalue.redis.hash.BeanUtilsHashMapper; +import org.springframework.data.keyvalue.redis.hash.HashMapper; + +/** + * @author Costin Leau + */ +public class BeanUtilsHashMapperTest extends AbstractHashMapperTest { + + @Override + protected HashMapper mapperFor(Class t) { + return new BeanUtilsHashMapper(t); + } + + @Test(expected = IllegalArgumentException.class) + public void testNestedBean() throws Exception { + super.testNestedBean(); + } +} diff --git a/spring-data-redis/template.mf b/spring-data-redis/template.mf index f1ee3fb01..6c01d8133 100644 --- a/spring-data-redis/template.mf +++ b/spring-data-redis/template.mf @@ -23,4 +23,6 @@ Import-Template: redis.clients.jedis.*;version=${jedis.range}, redis.clients.util.*;version=${jedis.range}, org.apache.commons.pool.impl.*;version="[1.0.0, 3.0.0)", - org.codehaus.jackson.*;version=${jackson.range} + org.codehaus.jackson.*;version=${jackson.range}, + org.apache.commons.beanutils.*;version="[1.8.0, 2.0.0)" +