DATACMNS-456 - Introduced getAttribute(String) on RepositoryConfigurationSupport.
RepositoryConfigurationSupport now has a getAttribute(String) method to lookup String attributes on XML elements and annotations. The XML implementation converts the given attribute name in a lower-case, dashed representation for the lookup. RepositoryConfigurationExtensionSupport now ha a generic postProcess(…) method to allow implementations that only need to work with String attributes unify their customizations into that single method.
This commit is contained in:
@@ -257,6 +257,17 @@ public class AnnotationRepositoryConfigurationSource extends RepositoryConfigura
|
||||
return attributes.containsKey(CONSIDER_NESTED_REPOSITORIES) && attributes.getBoolean(CONSIDER_NESTED_REPOSITORIES);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getAttribute(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String getAttribute(String name) {
|
||||
|
||||
String attribute = attributes.getString(name);
|
||||
return StringUtils.hasText(attribute) ? attribute : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely reads the {@code pattern} attribute from the given {@link AnnotationAttributes} and returns an empty list if
|
||||
* the attribute is not present.
|
||||
|
||||
@@ -90,6 +90,8 @@ public class RepositoryConfigurationDelegate {
|
||||
|
||||
BeanDefinitionBuilder definitionBuilder = builder.build(configuration);
|
||||
|
||||
extension.postProcess(definitionBuilder, configurationSource);
|
||||
|
||||
if (isXml) {
|
||||
extension.postProcess(definitionBuilder, (XmlRepositoryConfigurationSource) configurationSource);
|
||||
} else {
|
||||
|
||||
@@ -64,6 +64,14 @@ public interface RepositoryConfigurationExtension {
|
||||
*/
|
||||
void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource configurationSource);
|
||||
|
||||
/**
|
||||
* Callback to post process the {@link BeanDefinition} and tweak the configuration if necessary.
|
||||
*
|
||||
* @param builder will never be {@literal null}.
|
||||
* @param config will never be {@literal null}.
|
||||
*/
|
||||
void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSource config);
|
||||
|
||||
/**
|
||||
* Callback to post process the {@link BeanDefinition} built from annotations and tweak the configuration if
|
||||
* necessary.
|
||||
|
||||
@@ -84,6 +84,14 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
|
||||
*/
|
||||
protected abstract String getModulePrefix();
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.RepositoryConfigurationSource)
|
||||
*/
|
||||
public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSource source) {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -78,4 +78,14 @@ public interface RepositoryConfigurationSource {
|
||||
* @return
|
||||
*/
|
||||
Collection<BeanDefinition> getCandidates(ResourceLoader loader);
|
||||
|
||||
/**
|
||||
* Returns the value for the {@link String} attribute with the given name. The name is expected to be handed in
|
||||
* camel-case.
|
||||
*
|
||||
* @param name must not be {@literal null} or empty.
|
||||
* @return the attribute with the given name or {@literal null} if not configured or empty.
|
||||
* @since 1.8
|
||||
*/
|
||||
String getAttribute(String name);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -16,6 +16,8 @@
|
||||
package org.springframework.data.repository.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -42,6 +44,8 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
|
||||
private static final String REPOSITORY_FACTORY_BEAN_CLASS_NAME = "factory-class";
|
||||
private static final String CONSIDER_NESTED_REPOSITORIES = "consider-nested-repositories";
|
||||
|
||||
private static final Pattern CAMEL_CASE_PARTS = Pattern.compile("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");
|
||||
|
||||
private final Element element;
|
||||
private final ParserContext context;
|
||||
|
||||
@@ -161,4 +165,18 @@ public class XmlRepositoryConfigurationSource extends RepositoryConfigurationSou
|
||||
String attribute = getNullDefaultedAttribute(element, CONSIDER_NESTED_REPOSITORIES);
|
||||
return attribute != null && Boolean.parseBoolean(attribute);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationSource#getAttribute(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String getAttribute(String name) {
|
||||
|
||||
List<String> parts = Arrays.asList(CAMEL_CASE_PARTS.split(name));
|
||||
String xmlAttributeName = StringUtils.collectionToDelimitedString(parts, "-");
|
||||
String attribute = element.getAttribute(xmlAttributeName);
|
||||
|
||||
return StringUtils.hasText(attribute) ? attribute : null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user