SGF-758 - Exclude Java and Spring Framework Types when using MappingPdxSerializer.

This commit is contained in:
John Blum
2018-06-10 22:34:38 -07:00
parent 1e17a6031f
commit e1c167a0d5
2 changed files with 33 additions and 199 deletions

View File

@@ -16,7 +16,9 @@
package org.springframework.data.gemfire.mapping;
import static org.springframework.data.gemfire.mapping.MappingPdxSerializer.ExcludeComGemstoneGemFireTypesFilter.EXCLUDE_COM_GEMSTONE_GEMFIRE_TYPES;
import static org.springframework.data.gemfire.mapping.MappingPdxSerializer.ExcludeJavaTypesFilter.EXCLUDE_JAVA_TYPES;
import static org.springframework.data.gemfire.mapping.MappingPdxSerializer.ExcludeNullTypesFilter.EXCLUDE_NULL_TYPES;
import static org.springframework.data.gemfire.mapping.MappingPdxSerializer.ExcludeOrgSpringFrameworkTypesFilter.EXCLUDE_ORG_SPRING_FRAMEWORK_TYPES;
import java.util.Collections;
import java.util.Map;
@@ -190,9 +192,12 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
private Map<?, PdxSerializer> customPdxSerializers;
private Filter typeFilters = EXCLUDE_NULL_TYPES.and(EXCLUDE_COM_GEMSTONE_GEMFIRE_TYPES);
private Filter typeFilters = EXCLUDE_NULL_TYPES
.and(EXCLUDE_COM_GEMSTONE_GEMFIRE_TYPES)
.and(EXCLUDE_JAVA_TYPES)
.and(EXCLUDE_ORG_SPRING_FRAMEWORK_TYPES);
// TODO: decide what to do with this; SpELContext is not used
// TODO: remove? SpELContext is not used
private SpELContext spelContext;
/**
@@ -371,7 +376,6 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
this.entityInstantiators = new EntityInstantiators(gemfireInstantiators);
}
/* (non-Javadoc) */
protected EntityInstantiators getGemfireInstantiators() {
return this.entityInstantiators;
}
@@ -666,6 +670,18 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
}
}
public static class ExcludeJavaTypesFilter extends org.springframework.data.gemfire.util.AbstractFilter<Class<?>> {
public static final Filter<Class<?>> EXCLUDE_JAVA_TYPES = new ExcludeJavaTypesFilter();
protected static final String JAVA_PACKAGE_NAME = "java";
@Override
public boolean accept(Class<?> type) {
return type != null && !type.getPackage().getName().startsWith(JAVA_PACKAGE_NAME);
}
}
public static class ExcludeNullTypesFilter extends org.springframework.data.gemfire.util.AbstractFilter<Class<?>> {
public static final Filter<Class<?>> EXCLUDE_NULL_TYPES = new ExcludeNullTypesFilter();
@@ -675,4 +691,18 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
return type != null;
}
}
public static class ExcludeOrgSpringFrameworkTypesFilter
extends org.springframework.data.gemfire.util.AbstractFilter<Class<?>> {
public static final Filter<Class<?>> EXCLUDE_ORG_SPRING_FRAMEWORK_TYPES =
new ExcludeOrgSpringFrameworkTypesFilter();
protected static final String ORG_SPRING_FRAMEWORK_PACKAGE_NAME = "org.springframework";
@Override
public boolean accept(Class<?> type) {
return type != null && !type.getPackage().getName().startsWith(ORG_SPRING_FRAMEWORK_PACKAGE_NAME);
}
}
}

View File

@@ -1,196 +0,0 @@
/*
* 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.gemfire.mapping;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import com.gemstone.gemfire.DataSerializable;
import com.gemstone.gemfire.Instantiator;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.gemfire.repository.sample.Address;
import org.springframework.data.gemfire.repository.sample.Person;
/**
* Integration tests for {@link MappingPdxSerializer}.
*
* @author Oliver Gierke
* @author John Blum
*/
public class MappingPdxSerializerIntegrationTest {
static Region<Object, Object> region;
static Cache cache;
@BeforeClass
public static void setUp() {
MappingPdxSerializer serializer = new MappingPdxSerializer(new GemfireMappingContext(),
new DefaultConversionService());
cache = new CacheFactory()
.set("name", MappingPdxSerializerIntegrationTest.class.getSimpleName())
.set("mcast-port", "0")
.set("log-level", "warning")
.setPdxSerializer(serializer)
.setPdxPersistent(true)
.create();
region = cache.createRegionFactory()
.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE)
.create("foo");
}
@AfterClass
@SuppressWarnings("all")
public static void tearDown() {
try {
cache.close();
}
catch (Exception ignore) {
}
finally {
for (String name : new File(".").list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("BACKUP");
}
})) {
new File(name).delete();
}
}
}
@Test
public void serializeAndDeserializeCorrectly() {
Address address = new Address();
address.zipCode = "01234";
address.city = "London";
Person person = new Person(1L, "Oliver", "Gierke");
person.address = address;
region.put(1L, person);
Object result = region.get(1L);
assertThat(result instanceof Person, is(true));
Person reference = person;
assertThat(reference.getFirstname(), is(person.getFirstname()));
assertThat(reference.getLastname(), is(person.getLastname()));
assertThat(reference.address, is(person.address));
}
@Test
public void serializeAndDeserializeCorrectlyWithDataSerializable() {
Address address = new Address();
address.zipCode = "01234";
address.city = "London";
PersonWithDataSerializableProperty person = new PersonWithDataSerializableProperty(2L, "Oliver", "Gierke", new DataSerializableProperty("foo"));
person.address = address;
region.put(2L, person);
Object result = region.get(2L);
assertThat(result instanceof PersonWithDataSerializableProperty, is(true));
PersonWithDataSerializableProperty reference = person;
assertThat(reference.getFirstname(), is(person.getFirstname()));
assertThat(reference.getLastname(), is(person.getLastname()));
assertThat(reference.address, is(person.address));
assertThat(reference.dsProperty.getValue(),is("foo"));
}
@SuppressWarnings("serial")
public static class PersonWithDataSerializableProperty extends Person {
private DataSerializableProperty dsProperty;
public PersonWithDataSerializableProperty(Long id, String firstname,
String lastname, DataSerializableProperty dsProperty) {
super(id, firstname, lastname);
this.dsProperty = dsProperty;
}
public DataSerializableProperty getDataSerializableProperty() {
return this.dsProperty;
}
}
@SuppressWarnings("serial")
public static class DataSerializableProperty implements DataSerializable {
static {
registerInstantiator();
}
private static void registerInstantiator() {
try {
Instantiator.register(new Instantiator(DataSerializableProperty.class,101) {
public DataSerializable newInstance() {
return new DataSerializableProperty("");
}
});
}
catch (IllegalStateException ignore) {
// thrown when already registered
}
}
private String value;
public DataSerializableProperty(String value) {
this.value = value;
}
@Override
public void fromData(DataInput dataInput) throws IOException,
ClassNotFoundException {
value = dataInput.readUTF();
}
@Override
public void toData(DataOutput dataOutput) throws IOException {
dataOutput.writeUTF(value);
}
public String getValue() {
return this.value;
}
}
}