DATAKV-87 - Polished implementation of configurability of Map type on @EnableMapRepositories.

Removed the MapKeyValueAdapterFactory in favor of using instantiating MapKeyValueAdapters directly. Reverted additional hook in KeyValueRepositoryConfigurationExtension as we now rather use inner bean definitions.

Original pull request: #2.
This commit is contained in:
Oliver Gierke
2014-12-05 19:14:06 +01:00
parent b16a454c6c
commit bc88cb48ac
7 changed files with 77 additions and 332 deletions

View File

@@ -19,12 +19,12 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.data.keyvalue.core.KeyValueAdapter;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
import org.springframework.data.keyvalue.repository.KeyValueRepository;
import org.springframework.data.keyvalue.repository.query.SpelQueryCreator;
@@ -136,8 +136,7 @@ public abstract class KeyValueRepositoryConfigurationExtension extends Repositor
if (getDefaultKeyValueTemplateRef().equals(keyValueTemplateName)
&& !registry.containsBeanDefinition(keyValueTemplateName)) {
registerTemplateInfrastructure(registry, configurationSource);
RootBeanDefinition beanDefinition = getDefaultKeyValueTemplateBeanDefinition(configurationSource);
AbstractBeanDefinition beanDefinition = getDefaultKeyValueTemplateBeanDefinition(configurationSource);
if (beanDefinition != null) {
registerIfNotAlreadyRegistered(beanDefinition, registry, keyValueTemplateName, configurationSource.getSource());
@@ -145,23 +144,12 @@ public abstract class KeyValueRepositoryConfigurationExtension extends Repositor
}
}
/**
* Register infrastructure components such as {@link KeyValueAdapter} required for default template.
*
* @param registry
* @param configurationSource
*/
protected void registerTemplateInfrastructure(BeanDefinitionRegistry registry,
RepositoryConfigurationSource configurationSource) {
// nothing to register by default
}
/**
* Get the default {@link RootBeanDefinition} for {@link org.springframework.data.keyvalue.core.KeyValueTemplate}.
*
* @return {@literal null} to explicitly not register a template.
*/
protected RootBeanDefinition getDefaultKeyValueTemplateBeanDefinition(
protected AbstractBeanDefinition getDefaultKeyValueTemplateBeanDefinition(
RepositoryConfigurationSource configurationSource) {
return null;
}

View File

@@ -34,30 +34,51 @@ import org.springframework.util.ClassUtils;
*/
public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
private final Map<Serializable, Map<Serializable, Object>> data;
@SuppressWarnings("rawtypes")//
private final Class<? extends Map> mapType;
private final Class<? extends Map> keySpaceMapType;
private final Map<Serializable, Map<Serializable, Object>> store;
/**
* Create new instance of {@link MapKeyValueAdapter} using {@link ConcurrentHashMap}.
* Create new {@link MapKeyValueAdapter} using {@link ConcurrentHashMap} as backing store type.
*/
public MapKeyValueAdapter() {
this(new ConcurrentHashMap<Serializable, Map<Serializable, Object>>());
this(ConcurrentHashMap.class);
}
/**
* Creates a new {@link MapKeyValueAdapter} using the given {@link Map} as backing store.
*
* @param mapType must not be {@literal null}.
*/
@SuppressWarnings("rawtypes")
public MapKeyValueAdapter(Class<? extends Map> mapType) {
this(CollectionFactory.<Serializable, Map<Serializable, Object>> createMap(mapType, 100), mapType);
}
/**
* Create new instance of {@link MapKeyValueAdapter} using given dataStore for persistence.
*
* @param dataStore must not be {@literal null}.
* @param store must not be {@literal null}.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public MapKeyValueAdapter(Map<Serializable, Map<Serializable, Object>> dataStore) {
public MapKeyValueAdapter(Map<Serializable, Map<Serializable, Object>> store) {
this(store, (Class<? extends Map>) ClassUtils.getUserClass(store));
}
Assert.notNull(dataStore, "Cannot initilalize adapter with 'null' datastore.");
/**
* Creates a new {@link MapKeyValueAdapter} with the given store and type to be used when creating key spaces.
*
* @param store must not be {@literal null}.
* @param keySpaceMapType must not be {@literal null}.
*/
@SuppressWarnings("rawtypes")
private MapKeyValueAdapter(Map<Serializable, Map<Serializable, Object>> store, Class<? extends Map> keySpaceMapType) {
this.data = dataStore;
this.mapType = (Class<? extends Map>) ClassUtils.getUserClass(dataStore);
Assert.notNull(store, "Store must not be null.");
Assert.notNull(keySpaceMapType, "Map type to be used for key spaces must not be null!");
this.store = store;
this.keySpaceMapType = keySpaceMapType;
}
/*
@@ -67,8 +88,8 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
@Override
public Object put(Serializable id, Object item, Serializable keyspace) {
Assert.notNull(id, "Cannot add item with 'null' id.");
Assert.notNull(keyspace, "Cannot add item for 'null' collection.");
Assert.notNull(id, "Cannot add item with null id.");
Assert.notNull(keyspace, "Cannot add item for null collection.");
return getKeySpaceMap(keyspace).put(id, item);
}
@@ -89,7 +110,7 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
@Override
public Object get(Serializable id, Serializable keyspace) {
Assert.notNull(id, "Cannot get item with 'null' id.");
Assert.notNull(id, "Cannot get item with null id.");
return getKeySpaceMap(keyspace).get(id);
}
@@ -100,7 +121,7 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
@Override
public Object delete(Serializable id, Serializable keyspace) {
Assert.notNull(id, "Cannot delete item with 'null' id.");
Assert.notNull(id, "Cannot delete item with null id.");
return getKeySpaceMap(keyspace).remove(id);
}
@@ -128,7 +149,7 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
*/
@Override
public void clear() {
data.clear();
store.clear();
}
/*
@@ -148,19 +169,19 @@ public class MapKeyValueAdapter extends AbstractKeyValueAdapter {
*/
protected Map<Serializable, Object> getKeySpaceMap(Serializable keyspace) {
Assert.notNull(keyspace, "Collection must not be 'null' for lookup.");
Assert.notNull(keyspace, "Collection must not be null for lookup.");
Map<Serializable, Object> map = data.get(keyspace);
Map<Serializable, Object> map = store.get(keyspace);
if (map != null) {
return map;
}
addMapForKeySpace(keyspace);
return data.get(keyspace);
return store.get(keyspace);
}
private void addMapForKeySpace(Serializable keyspace) {
data.put(keyspace, CollectionFactory.<Serializable, Object> createMap(mapType, 1000));
store.put(keyspace, CollectionFactory.<Serializable, Object> createMap(keySpaceMapType, 1000));
}
}

View File

@@ -1,121 +0,0 @@
/*
* Copyright 2014 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.map;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.core.CollectionFactory;
import org.springframework.util.Assert;
/**
* @author Christoph Strobl
* @since 1.10
*/
public class MapKeyValueAdapterFactory {
@SuppressWarnings("rawtypes")//
private static final Class<? extends Map> DEFAULT_MAP_TYPE = ConcurrentHashMap.class;
@SuppressWarnings("rawtypes")//
private Class<? extends Map> mapType;
private Map<Serializable, Map<? extends Serializable, ?>> initialValues;
/**
* Creates a new {@link MapKeyValueAdapterFactory}.
*
* @see MapKeyValueAdapterFactory#MapKeyValueAdapterFactory(Class)
*/
public MapKeyValueAdapterFactory() {
this(null);
}
/**
* Creates a new MKVAF with the given {@link Map} type to be used to hold the values in.
*
* @param type any {@link Class} of type {@link Map}. Can be {@literal null} and will be defaulted to
* {@link ConcurrentHashMap}.
*/
@SuppressWarnings("rawtypes")
public MapKeyValueAdapterFactory(Class<? extends Map> type) {
this.mapType = type;
this.initialValues = new HashMap<Serializable, Map<? extends Serializable, ?>>();
}
/**
* Set values for a given {@literal keyspace} that to populate the adapter after creation.
*
* @param keyspace must not be {@literal null}.
* @param values must not be {@literal null}.
*/
public void setInitialValuesForKeyspace(Serializable keyspace, Map<? extends Serializable, ?> values) {
Assert.notNull(keyspace, "KeySpace must not be null!");
Assert.notNull(values, "Values must not be null!");
initialValues.put(keyspace, values);
}
/**
* Configures the {@link Map} type to be used as backing store.
*
* @param mapType must not be {@literal null}.
*/
@SuppressWarnings("rawtypes")
public void setMapType(Class<? extends Map> mapType) {
Assert.notNull(mapType, "May type must not be null!");
this.mapType = mapType;
}
/**
* Creates and populates the adapter.
*
* @return
*/
public MapKeyValueAdapter getAdapter() {
MapKeyValueAdapter adapter = createAdapter();
populateAdapter(adapter);
return adapter;
}
private MapKeyValueAdapter createAdapter() {
Class<?> type = this.mapType == null ? DEFAULT_MAP_TYPE : this.mapType;
MapKeyValueAdapter adapter = new MapKeyValueAdapter(
CollectionFactory.<Serializable, Map<Serializable, Object>> createMap(type, 100));
return adapter;
}
private void populateAdapter(MapKeyValueAdapter adapter) {
if (!initialValues.isEmpty()) {
for (Entry<Serializable, Map<? extends Serializable, ?>> entry : initialValues.entrySet()) {
for (Entry<? extends Serializable, ?> obj : entry.getValue().entrySet()) {
adapter.put(obj.getKey(), obj.getValue(), entry.getKey());
}
}
}
}
}

View File

@@ -25,9 +25,11 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Import;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.keyvalue.repository.config.QueryCreatorType;
import org.springframework.data.keyvalue.repository.query.SpelQueryCreator;
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactoryBean;
@@ -126,7 +128,9 @@ public @interface EnableMapRepositories {
/**
* Configures the {@link Map} structure used for data storage. Defaults to {@link ConcurrentHashMap}. Will be ignored
* in favor of existing {@link KeyValueOperations} definition.
* in case an explicit bean for the {@link KeyValueTemplate} is available in the {@link ApplicationContext}.
*
* @see #keyValueTemplateRef()
*/
@SuppressWarnings("rawtypes")
Class<? extends Map> mapType() default ConcurrentHashMap.class;

View File

@@ -17,17 +17,14 @@ package org.springframework.data.map.repository.config;
import java.util.Map;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.data.config.ParsingUtils;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension;
import org.springframework.data.map.MapKeyValueAdapter;
import org.springframework.data.map.MapKeyValueAdapterFactory;
import org.springframework.data.repository.config.RepositoryConfigurationSource;
/**
@@ -67,43 +64,24 @@ public class MapRepositoryConfigurationExtension extends KeyValueRepositoryConfi
* @see org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension#getDefaultKeyValueTemplateBeanDefinition()
*/
@Override
protected RootBeanDefinition getDefaultKeyValueTemplateBeanDefinition(
protected AbstractBeanDefinition getDefaultKeyValueTemplateBeanDefinition(
RepositoryConfigurationSource configurationSource) {
ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder.rootBeanDefinition(MapKeyValueAdapter.class);
adapterBuilder.addConstructorArgValue(getMapTypeToUse(configurationSource));
GenericBeanDefinition referencingMapKeyValueAdapterBeanDefintion = new GenericBeanDefinition();
referencingMapKeyValueAdapterBeanDefintion.setBeanClass(MapKeyValueAdapter.class);
referencingMapKeyValueAdapterBeanDefintion.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(KeyValueTemplate.class);
builder
.addConstructorArgValue(ParsingUtils.getSourceBeanDefinition(adapterBuilder, configurationSource.getSource()));
builder.setRole(BeanDefinition.ROLE_SUPPORT);
constructorArgumentValues.addGenericArgumentValue(referencingMapKeyValueAdapterBeanDefintion);
RootBeanDefinition keyValueTemplateDefinition = new RootBeanDefinition(KeyValueTemplate.class);
keyValueTemplateDefinition.setConstructorArgumentValues(constructorArgumentValues);
keyValueTemplateDefinition.setRole(BeanDefinition.ROLE_APPLICATION);
return keyValueTemplateDefinition;
return ParsingUtils.getSourceBeanDefinition(builder, configurationSource.getSource());
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void registerTemplateInfrastructure(BeanDefinitionRegistry registry,
RepositoryConfigurationSource configurationSource) {
private static Class<? extends Map> getMapTypeToUse(RepositoryConfigurationSource source) {
Class<? extends Map> type = (Class<? extends Map>) ((AnnotationMetadata) configurationSource.getSource())
.getAnnotationAttributes(EnableMapRepositories.class.getName()).get("mapType");
ConstructorArgumentValues mapAdapterFactoryArgs = new ConstructorArgumentValues();
mapAdapterFactoryArgs.addGenericArgumentValue(type);
RootBeanDefinition mapAdapterFactory = new RootBeanDefinition(MapKeyValueAdapterFactory.class,
mapAdapterFactoryArgs, null);
registry.registerBeanDefinition("mapKeyValueAdapterFactory", mapAdapterFactory);
RootBeanDefinition mapKeyValueAdapter = new RootBeanDefinition(MapKeyValueAdapter.class);
mapKeyValueAdapter.setFactoryBeanName("mapKeyValueAdapterFactory");
mapKeyValueAdapter.setFactoryMethodName("getAdapter");
registry.registerBeanDefinition("mapKeyValueAdapter", mapAdapterFactory);
return (Class<? extends Map>) ((AnnotationMetadata) source.getSource()).getAnnotationAttributes(
EnableMapRepositories.class.getName()).get("mapType");
}
}