DATACMNS-266 - Use new common Maven build infrastructure.

Simplified project setup to be a single module build again. Using Spring Data Build parent POM to simplify project setup. See https://github.com/SpringSource/spring-data-build#spring-data-build-infrastructure
This commit is contained in:
Oliver Gierke
2013-01-11 12:13:47 +01:00
parent c908d0e023
commit ac256f9921
375 changed files with 215 additions and 3092 deletions

View File

@@ -0,0 +1,85 @@
/*
* Copyright 2012 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.config;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.data.auditing.AuditingHandler;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* {@link BeanDefinitionParser} that parses an {@link AuditingHandler} {@link BeanDefinition}
*
* @author Oliver Gierke
* @since 1.5
*/
public class AuditingHandlerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
private static final String AUDITOR_AWARE_REF = "auditor-aware-ref";
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#getBeanClass(org.w3c.dom.Element)
*/
@Override
protected Class<?> getBeanClass(Element element) {
return AuditingHandler.class;
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#shouldGenerateId()
*/
@Override
protected boolean shouldGenerateId() {
return true;
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#doParse(org.w3c.dom.Element, org.springframework.beans.factory.support.BeanDefinitionBuilder)
*/
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String auditorAwareRef = element.getAttribute(AUDITOR_AWARE_REF);
if (StringUtils.hasText(auditorAwareRef)) {
builder.addPropertyValue("auditorAware", createLazyInitTargetSourceBeanDefinition(auditorAwareRef));
}
ParsingUtils.setPropertyValue(builder, element, "set-dates", "dateTimeForNow");
ParsingUtils.setPropertyReference(builder, element, "date-time-provider-ref", "dateTimeProvider");
}
private BeanDefinition createLazyInitTargetSourceBeanDefinition(String auditorAwareRef) {
BeanDefinitionBuilder targetSourceBuilder = rootBeanDefinition(LazyInitTargetSource.class);
targetSourceBuilder.addPropertyValue("targetBeanName", auditorAwareRef);
BeanDefinitionBuilder builder = rootBeanDefinition(ProxyFactoryBean.class);
builder.addPropertyValue("targetSource", targetSourceBuilder.getBeanDefinition());
return builder.getBeanDefinition();
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright 2012 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.config;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* Helper to create {@link BeanComponentDefinition} more easily.
*
* @author Oliver Gierke
*/
public class BeanComponentDefinitionBuilder {
private final Element defaultSource;
private final ParserContext context;
/**
* Creates a new {@link BeanComponentDefinitionBuilder} using the given {@link Element} as default source and the
* given {@link ParserContext}.
*
* @param defaultSource must not be {@literal null}.
* @param context must not be {@literal null}.
*/
public BeanComponentDefinitionBuilder(Element defaultSource, ParserContext context) {
Assert.notNull(defaultSource);
Assert.notNull(context);
this.defaultSource = defaultSource;
this.context = context;
}
/**
* Creates a {@link BeanComponentDefinition} from the given {@link BeanDefinitionBuilder}. Will generate a bean name.
*
* @param builder must not be {@literal null}.
* @return
*/
public BeanComponentDefinition getComponent(BeanDefinitionBuilder builder) {
Assert.notNull(builder);
AbstractBeanDefinition definition = builder.getRawBeanDefinition();
String name = BeanDefinitionReaderUtils.generateBeanName(definition, context.getRegistry(), context.isNested());
return getComponent(builder, name);
}
/**
* Creates a {@link BeanComponentDefinition} from the given {@link BeanDefinitionBuilder} and inspects the backing
* {@link Element}s id attribute for a name. It will use this one if found or the given fallback if not.
*
* @param builder must not be {@literal null}.
* @param fallback must not be {@literal null} or empty.
* @return
*/
public BeanComponentDefinition getComponentIdButFallback(BeanDefinitionBuilder builder, String fallback) {
Assert.hasText(fallback);
String id = defaultSource.getAttribute("id");
return getComponent(builder, StringUtils.hasText(id) ? id : fallback);
}
/**
* Creates a {@link BeanComponentDefinition} from the given {@link BeanDefinitionBuilder} using the given name.
*
* @param builder must not be {@literal null}.
* @param name must not be {@literal null} or empty.
* @return
*/
public BeanComponentDefinition getComponent(BeanDefinitionBuilder builder, String name) {
return getComponent(builder, name, defaultSource);
}
/**
* Creates a new {@link BeanComponentDefinition} from the given {@link BeanDefinitionBuilder} using the given name and
* raw source object.
*
* @param builder must not be {@literal null}.
* @param name must not be {@literal null}.
* @param rawSource
* @return
*/
public BeanComponentDefinition getComponent(BeanDefinitionBuilder builder, String name, Object rawSource) {
Assert.notNull(builder);
Assert.hasText(name);
AbstractBeanDefinition definition = builder.getRawBeanDefinition();
definition.setSource(context.extractSource(rawSource));
return new BeanComponentDefinition(definition, name);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2012 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.config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
/**
* {@link AuditingHandlerBeanDefinitionParser} that will register am {@link IsNewAwareAuditingHandler}. Needs to get the
* bean id of the
*
* @author Oliver Gierke
*/
public class IsNewAwareAuditingHandlerBeanDefinitionParser extends AuditingHandlerBeanDefinitionParser {
private final String isNewStrategyFactoryBeanId;
/**
* Creates a new {@link IsNewAwareAuditingHandlerBeanDefinitionParser}.
*
* @param isNewStrategyFactoryBeanId must not be {@literal null} or empty.
*/
public IsNewAwareAuditingHandlerBeanDefinitionParser(String isNewStrategyFactoryBeanId) {
Assert.hasText(isNewStrategyFactoryBeanId);
this.isNewStrategyFactoryBeanId = isNewStrategyFactoryBeanId;
}
/*
* (non-Javadoc)
* @see org.springframework.data.config.AuditingHandlerBeanDefinitionParser#getBeanClass(org.w3c.dom.Element)
*/
@Override
protected Class<?> getBeanClass(Element element) {
return IsNewAwareAuditingHandler.class;
}
/*
* (non-Javadoc)
* @see org.springframework.data.config.AuditingHandlerBeanDefinitionParser#doParse(org.w3c.dom.Element, org.springframework.beans.factory.support.BeanDefinitionBuilder)
*/
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
builder.addConstructorArgReference(isNewStrategyFactoryBeanId);
super.doParse(element, builder);
}
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright 2011-2012 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.config;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* Utility methods for {@link BeanDefinitionParser} implementations.
*
* @author Oliver Gierke
*/
public abstract class ParsingUtils {
private ParsingUtils() {
}
/**
* Configures a property value for the given property name reading the attribute of the given name from the given
* {@link Element} if the attribute is configured.
*
* @param builder must not be {@literal null}.
* @param element must not be {@literal null}.
* @param attrName must not be {@literal null} or empty.
* @param propertyName must not be {@literal null} or empty.
*/
public static void setPropertyValue(BeanDefinitionBuilder builder, Element element, String attrName,
String propertyName) {
Assert.notNull(builder, "BeanDefinitionBuilder must not be null!");
Assert.notNull(element, "Element must not be null!");
Assert.hasText(attrName, "Attribute name must not be null!");
Assert.hasText(propertyName, "Property name must not be null!");
String attr = element.getAttribute(attrName);
if (StringUtils.hasText(attr)) {
builder.addPropertyValue(propertyName, attr);
}
}
/**
* Sets the property with the given attribute name on the given {@link BeanDefinitionBuilder} to the value of the
* attribute with the given name if the attribute is configured.
*
* @param builder must not be {@literal null}.
* @param element must not be {@literal null}.
* @param attribute must not be {@literal null} or empty.
*/
public static void setPropertyValue(BeanDefinitionBuilder builder, Element element, String attribute) {
setPropertyValue(builder, element, attribute, attribute);
}
/**
* Configures a bean property reference with the value of the attribute of the given name if it is configured.
*
* @param builder must not be {@literal null}.
* @param element must not be {@literal null}.
* @param attribute must not be {@literal null} or empty.
* @param property must not be {@literal null}or empty.
*/
public static void setPropertyReference(BeanDefinitionBuilder builder, Element element, String attribute,
String property) {
Assert.notNull(builder, "BeanDefinitionBuilder must not be null!");
Assert.notNull(element, "Element must not be null!");
Assert.hasText(attribute, "Attribute name must not be null!");
Assert.hasText(property, "Property name must not be null!");
String value = element.getAttribute(attribute);
if (StringUtils.hasText(value)) {
builder.addPropertyReference(property, value);
}
}
/**
* Returns the {@link BeanDefinition} built by the given {@link BeanDefinitionBuilder} enriched with source
* information derived from the given {@link Element}.
*
* @param builder must not be {@literal null}.
* @param context must not be {@literal null}.
* @param element must not be {@literal null}.
* @return
*/
public static AbstractBeanDefinition getSourceBeanDefinition(BeanDefinitionBuilder builder, ParserContext context,
Element element) {
Assert.notNull(element, "Element must not be null!");
Assert.notNull(context, "ParserContext must not be null!");
return getSourceBeanDefinition(builder, context.extractSource(element));
}
/**
* Returns the {@link AbstractBeanDefinition} built by the given builder with the given extracted source applied.
*
* @param builder must not be {@literal null}.
* @param source
* @return
*/
public static AbstractBeanDefinition getSourceBeanDefinition(BeanDefinitionBuilder builder, Object source) {
Assert.notNull(builder, "Builder must not be null!");
AbstractBeanDefinition definition = builder.getRawBeanDefinition();
definition.setSource(source);
return definition;
}
}

View File

@@ -0,0 +1,239 @@
/*
* Copyright 2010-2012 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.config;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.HashSet;
import java.util.regex.Pattern;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.parsing.ReaderContext;
import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AspectJTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.RegexPatternTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Parser to populate the given {@link ClassPathScanningCandidateComponentProvider} with {@link TypeFilter}s parsed from
* the given {@link Element}'s children.
*
* @author Oliver Gierke
*/
public class TypeFilterParser {
private static final String FILTER_TYPE_ATTRIBUTE = "type";
private static final String FILTER_EXPRESSION_ATTRIBUTE = "expression";
private final ReaderContext readerContext;
private final ClassLoader classLoader;
/**
* Creates a new {@link TypeFilterParser} with the given {@link ReaderContext}.
*
* @param readerContext must not be {@literal null}.
*/
public TypeFilterParser(XmlReaderContext readerContext) {
this(readerContext, readerContext.getResourceLoader().getClassLoader());
}
/**
* Constructor to ease testing as {@link XmlReaderContext#getBeanClassLoader()} is final and thus cannot be mocked
* easily.
*
* @param readerContext must not be {@literal null}.
* @param classLoader must not be {@literal null}.
*/
TypeFilterParser(ReaderContext readerContext, ClassLoader classLoader) {
Assert.notNull(readerContext, "ReaderContext must not be null!");
Assert.notNull(classLoader, "ClassLoader must not be null!");
this.readerContext = readerContext;
this.classLoader = classLoader;
}
/**
* Returns all {@link TypeFilter} declared in nested elements of the given {@link Element}. Allows to selectively
* retrieve including or excluding filters based on the given {@link Type}.
*
* @param element must not be {@literal null}.
* @param type must not be {@literal null}.
* @return
*/
public Iterable<TypeFilter> parseTypeFilters(Element element, Type type) {
NodeList nodeList = element.getChildNodes();
Collection<TypeFilter> filters = new HashSet<TypeFilter>();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element childElement = type.getElement(node);
if (childElement != null) {
try {
filters.add(createTypeFilter(childElement, classLoader));
} catch (RuntimeException e) {
readerContext.error(e.getMessage(), readerContext.extractSource(element), e.getCause());
}
}
}
return filters;
}
/**
* Createsa a {@link TypeFilter} instance from the given {@link Element} and {@link ClassLoader}.
*
* @param element must not be {@literal null}.
* @param classLoader must not be {@literal null}.
* @return
*/
protected TypeFilter createTypeFilter(Element element, ClassLoader classLoader) {
String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE);
String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE);
try {
FilterType filter = FilterType.fromString(filterType);
return filter.getFilter(expression, classLoader);
} catch (ClassNotFoundException ex) {
throw new FatalBeanException("Type filter class not found: " + expression, ex);
}
}
/**
* Enum representing all the filter types available for {@code include} and {@code exclude} elements. This acts as
* factory for {@link TypeFilter} instances.
*
* @author Oliver Gierke
* @see #getFilter(String, ClassLoader)
*/
private static enum FilterType {
ANNOTATION {
@Override
@SuppressWarnings("unchecked")
public TypeFilter getFilter(String expression, ClassLoader classLoader) throws ClassNotFoundException {
return new AnnotationTypeFilter((Class<Annotation>) classLoader.loadClass(expression));
}
},
ASSIGNABLE {
@Override
public TypeFilter getFilter(String expression, ClassLoader classLoader) throws ClassNotFoundException {
return new AssignableTypeFilter(classLoader.loadClass(expression));
}
},
ASPECTJ {
@Override
public TypeFilter getFilter(String expression, ClassLoader classLoader) {
return new AspectJTypeFilter(expression, classLoader);
}
},
REGEX {
@Override
public TypeFilter getFilter(String expression, ClassLoader classLoader) {
return new RegexPatternTypeFilter(Pattern.compile(expression));
}
},
CUSTOM {
@Override
public TypeFilter getFilter(String expression, ClassLoader classLoader) throws ClassNotFoundException {
Class<?> filterClass = classLoader.loadClass(expression);
if (!TypeFilter.class.isAssignableFrom(filterClass)) {
throw new IllegalArgumentException("Class is not assignable to [" + TypeFilter.class.getName() + "]: "
+ expression);
}
return (TypeFilter) BeanUtils.instantiateClass(filterClass);
}
};
/**
* Returns the {@link TypeFilter} for the given expression and {@link ClassLoader}.
*
* @param expression
* @param classLoader
* @return
* @throws ClassNotFoundException
*/
abstract TypeFilter getFilter(String expression, ClassLoader classLoader) throws ClassNotFoundException;
/**
* Returns the {@link FilterType} for the given type as {@link String}.
*
* @param typeString
* @return
* @throws IllegalArgumentException if no {@link FilterType} could be found for the given argument.
*/
static FilterType fromString(String typeString) {
for (FilterType filter : FilterType.values()) {
if (filter.name().equalsIgnoreCase(typeString)) {
return filter;
}
}
throw new IllegalArgumentException("Unsupported filter type: " + typeString);
}
}
public static enum Type {
INCLUDE("include-filter"), EXCLUDE("exclude-filter");
private String elementName;
private Type(String elementName) {
this.elementName = elementName;
}
/**
* Returns the {@link Element} if the given {@link Node} is an {@link Element} and it's name equals the one of the
* type.
*
* @param node
* @return
*/
Element getElement(Node node) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
String localName = node.getLocalName();
if (elementName.equals(localName)) {
return (Element) node;
}
}
return null;
}
}
}