DATACMNS-333 - Added Jackson 2 support to repository populators.

Added necessary infrastructure to use Jackson 2 with repository populators. Expose a jackson2-populator XML element in the namespace to setup a Jackson2 based repository populator.
This commit is contained in:
Oliver Gierke
2013-05-29 17:51:39 +02:00
parent cd2ea03928
commit e0dda2a4f0
10 changed files with 283 additions and 7 deletions

View File

@@ -36,5 +36,6 @@ public class RepositoryNameSpaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("unmarshaller-populator", PARSER);
registerBeanDefinitionParser("jackson-populator", PARSER);
registerBeanDefinitionParser("jackson2-populator", PARSER);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2013 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.
@@ -15,9 +15,12 @@
*/
package org.springframework.data.repository.config;
import java.util.Arrays;
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.repository.init.Jackson2RepositoryPopulatorFactoryBean;
import org.springframework.data.repository.init.JacksonRepositoryPopulatorFactoryBean;
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
import org.springframework.util.StringUtils;
@@ -38,8 +41,16 @@ public class ResourceReaderRepositoryPopulatorBeanDefinitionParser extends Abstr
protected String getBeanClassName(Element element) {
String name = element.getLocalName();
return "unmarshaller-populator".equals(name) ? UnmarshallerRepositoryPopulatorFactoryBean.class.getName()
: JacksonRepositoryPopulatorFactoryBean.class.getName();
if ("unmarshaller-populator".equals(name)) {
return UnmarshallerRepositoryPopulatorFactoryBean.class.getName();
} else if ("jackson-populator".equals(name)) {
return JacksonRepositoryPopulatorFactoryBean.class.getName();
} else if ("jackson2-populator".equals(name)) {
return Jackson2RepositoryPopulatorFactoryBean.class.getName();
}
throw new IllegalStateException("Unsupported populator type " + name + "!");
}
/*
@@ -55,7 +66,7 @@ public class ResourceReaderRepositoryPopulatorBeanDefinitionParser extends Abstr
if ("unmarshaller-populator".equals(localName)) {
parseXmlPopulator(element, builder);
} else if ("jackson-populator".equals(localName)) {
} else if (Arrays.asList("jackson-populator", "jackson2-populator").contains(localName)) {
parseJsonPopulator(element, builder);
}
}
@@ -66,7 +77,7 @@ public class ResourceReaderRepositoryPopulatorBeanDefinitionParser extends Abstr
* @param element
* @param builder
*/
private void parseJsonPopulator(Element element, BeanDefinitionBuilder builder) {
private static void parseJsonPopulator(Element element, BeanDefinitionBuilder builder) {
String objectMapperRef = element.getAttribute("object-mapper-ref");
@@ -81,7 +92,7 @@ public class ResourceReaderRepositoryPopulatorBeanDefinitionParser extends Abstr
* @param element
* @param builder
*/
private void parseXmlPopulator(Element element, BeanDefinitionBuilder builder) {
private static void parseXmlPopulator(Element element, BeanDefinitionBuilder builder) {
String unmarshallerRefName = element.getAttribute("unmarshaller-ref");

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2013 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.repository.init;
import org.springframework.beans.factory.FactoryBean;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* {@link FactoryBean} to set up a {@link ResourceReaderRepositoryPopulator} with a {@link Jackson2ResourceReader}.
*
* @author Oliver Gierke
* @since 1.6
*/
public class Jackson2RepositoryPopulatorFactoryBean extends AbstractRepositoryPopulatorFactoryBean {
private ObjectMapper mapper;
/**
* Configures the {@link ObjectMapper} to be used.
*
* @param mapper
*/
public void setMapper(ObjectMapper mapper) {
this.mapper = mapper;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.init.AbstractRepositoryPopulatorFactoryBean#getResourceReader()
*/
@Override
protected ResourceReader getResourceReader() {
return new Jackson2ResourceReader(mapper);
}
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2013 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.repository.init;
import static com.fasterxml.jackson.databind.DeserializationFeature.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* A {@link ResourceReader} using Jackson to read JSON into objects.
*
* @author Oliver Gierke
* @since 1.6
*/
public class Jackson2ResourceReader implements ResourceReader {
private static final String DEFAULT_TYPE_KEY = "_class";
private static final ObjectMapper DEFAULT_MAPPER = new ObjectMapper();
static {
DEFAULT_MAPPER.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
}
private final ObjectMapper mapper;
private String typeKey = DEFAULT_TYPE_KEY;
/**
* Creates a new {@link Jackson2ResourceReader}.
*/
public Jackson2ResourceReader() {
this(DEFAULT_MAPPER);
}
/**
* Creates a new {@link Jackson2ResourceReader} using the given {@link ObjectMapper}.
*
* @param mapper
*/
public Jackson2ResourceReader(ObjectMapper mapper) {
this.mapper = mapper == null ? DEFAULT_MAPPER : mapper;
}
/**
* Configures the JSON document's key to lookup the type to instantiate the object. Defaults to
* {@link Jackson2ResourceReader#DEFAULT_TYPE_KEY}.
*
* @param typeKey
*/
public void setTypeKey(String typeKey) {
this.typeKey = typeKey;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.init.ResourceReader#readFrom(org.springframework.core.io.Resource, java.lang.ClassLoader)
*/
public Object readFrom(Resource resource, ClassLoader classLoader) throws Exception {
InputStream stream = resource.getInputStream();
JsonNode node = mapper.reader(JsonNode.class).readTree(stream);
if (node.isArray()) {
Iterator<JsonNode> elements = node.elements();
List<Object> result = new ArrayList<Object>();
while (elements.hasNext()) {
JsonNode element = elements.next();
result.add(readSingle(element, classLoader));
}
return result;
}
return readSingle(node, classLoader);
}
/**
* Reads the given {@link JsonNode} into an instance of the type encoded in it using the configured type key.
*
* @param node must not be {@literal null}.
* @param classLoader
* @return
*/
private Object readSingle(JsonNode node, ClassLoader classLoader) throws IOException {
JsonNode typeNode = node.findValue(typeKey);
String typeName = typeNode == null ? null : typeNode.asText();
Class<?> type = ClassUtils.resolveClassName(typeName, classLoader);
return mapper.reader(type).readValue(node);
}
}