Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 java.util.Map;
 
 
 import  com.gemstone.gemfire.pdx.PdxReader;
 import  com.gemstone.gemfire.pdx.PdxSerializer;
 import  com.gemstone.gemfire.pdx.PdxWriter;

PdxSerializer implementation that uses a GemfireMappingContext to read and write entities.

Author(s):
Oliver Gierke
David Turanski
 
 public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAware {
 
 
 
 	
 	private Map<Class<?>,PdxSerializer> customSerializers;
 
 	private SpELContext context;

Creates a new MappingPdxSerializer using the given GemfireMappingContext and ConversionService.

Parameters:
mappingContext must not be null.
conversionService must not be null.
 
 	public MappingPdxSerializer(GemfireMappingContext mappingContextConversionService conversionService) {
 
 		Assert.notNull(mappingContext);
 		Assert.notNull(conversionService);
 
 		this. = mappingContext;
 		this. = conversionService;
 	}

Creates a new MappingPdxSerializer using the default GemfireMappingContext and DefaultConversionService.
 
 	public MappingPdxSerializer() {
 	}

Configures the EntityInstantiators to be used to create the instances to be read.

Parameters:
gemfireInstantiators must not be null.
 
 	public void setGemfireInstantiators(Map<Class<?>, EntityInstantiatorgemfireInstantiators) {
 		Assert.notNull(gemfireInstantiators);
 		this. = new EntityInstantiators(gemfireInstantiators);
 	}

Configures custom pdx serializers to use for specific types

Parameters:
customSerializers
 
 	public void setCustomSerializers(Map<Class<?>, PdxSerializer> customSerializers) {
		this. = customSerializers;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.context.ApplicationContextAware#setApplicationContext
	 * (org.springframework.context.ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext applicationContextthrows BeansException {
		this. = new SpELContext(applicationContext);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gemstone.gemfire.pdx.PdxSerializer#fromData(java.lang.Class,
	 * com.gemstone.gemfire.pdx.PdxReader)
	 */
	public Object fromData(Class<?> typefinal PdxReader reader) {
		GemfirePropertyValueProvider propertyValueProvider = new GemfirePropertyValueProvider(reader);
				entitypropertyValueProvidernull);
 
		Object instance = instantiator.createInstance(entityprovider);
		final BeanWrapper<PersistentEntity<Object, ?>, Objectwrapper = BeanWrapper
			public void doWithPersistentProperty(GemfirePersistentProperty persistentProperty) {
				if (entity.isConstructorArgument(persistentProperty)) {
					return;
				}
				PdxSerializer customSerializer = getCustomSerializer(persistentProperty.getType()); 
				Object value = null;
				if (customSerializer != null) {
					value = customSerializer.fromData(persistentProperty.getType(), reader);
else {
					value = reader.readField(persistentProperty.getName());
				}
				try {
					wrapper.setProperty(persistentPropertyvalue);
				}
				catch (Exception e) {
					throw new MappingException("Could not read value " + value.toString(), e);
				}
			}
		});
		return wrapper.getBean();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gemstone.gemfire.pdx.PdxSerializer#toData(java.lang.Object,
	 * com.gemstone.gemfire.pdx.PdxWriter)
	 */
	public boolean toData(Object valuefinal PdxWriter writer) {
		final BeanWrapper<PersistentEntity<Object, ?>, Objectwrapper = BeanWrapper.create(value);
			@SuppressWarnings({ "unchecked""rawtypes" })
			public void doWithPersistentProperty(GemfirePersistentProperty persistentProperty) {
				try {
					Object propertyValue = wrapper.getProperty(persistentProperty);
					PdxSerializer customSerializer = getCustomSerializer(persistentProperty.getType());
					if (customSerializer != null) {
						customSerializer.toData(propertyValuewriter);
else {
						writer.writeField(persistentProperty.getName(), propertyValue, (ClasspersistentProperty.getType());
				}
				catch (Exception e) {
					throw new MappingException("Could not write value for property " + persistentProperty.toString(), e);
				}
			}
		});
		GemfirePersistentProperty idProperty = entity.getIdProperty();
		if (idProperty != null) {
			writer.markIdentityField(idProperty.getName());
		}
		return true;
	}
	private PdxSerializer getCustomSerializer(Class<?> clazz) {
		return  == null ? null : .get(clazz);
	}
New to GrepCode? Check out our FAQ X