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
John Blum
 
 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;
 		this. = Collections.emptyMap();
 	}

Creates a new MappingPdxSerializer using the default GemfireMappingContext and DefaultConversionService.
 
 	public MappingPdxSerializer() {
 	}
 
 	/*
 	 * (non-Javadoc)
 	 *
 	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(
 	 * 	org.springframework.context.ApplicationContext)
 	 */
 	public void setApplicationContext(ApplicationContext applicationContextthrows BeansException {
 		this. = new SpELContext(applicationContext);
 	}
 
 	/* (non-Javadoc) */
	}

Configures custom PDX serializers to use for specific class types.

Parameters:
customSerializers a mapping of domain object class types and their corresponding PDX serializer.
	public void setCustomSerializers(Map<Class<?>, PdxSerializer> customSerializers) {
		Assert.notNull(customSerializers);
		this. = customSerializers;
	}
	/* (non-Javadoc) */
	protected Map<Class<?>, PdxSerializer> getCustomSerializers() {
		return Collections.unmodifiableMap();
	}

Configures the EntityInstantiators used to create the instances read by this PdxSerializer.

Parameters:
gemfireInstantiators must not be null.
	public void setGemfireInstantiators(Map<Class<?>, EntityInstantiatorgemfireInstantiators) {
		Assert.notNull(gemfireInstantiators);
		this. = new EntityInstantiators(gemfireInstantiators);
	}
	/* (non-Javadoc) */
	}
	/* (non-Javadoc) */
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gemstone.gemfire.pdx.PdxSerializer#fromData(java.lang.Class,
	 * com.gemstone.gemfire.pdx.PdxReader)
	 */
	public Object fromData(Class<?> typefinal PdxReader reader) {
		final GemfirePersistentEntity<?> entity = getPersistentEntity(type);
		Object instance = getInstantiatorFor(entity).createInstance(entity,
				new GemfirePropertyValueProvider(reader), null));
		final BeanWrapper<Objectwrapper = BeanWrapper.create(instancegetConversionService());
			public void doWithPersistentProperty(GemfirePersistentProperty persistentProperty) {
				if (entity.isConstructorArgument(persistentProperty)) {
					return;
				}
				PdxSerializer customSerializer = getCustomSerializer(persistentProperty.getType()); 
				Object value;
				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<Objectwrapper = BeanWrapper.create(valuegetConversionService());
			@SuppressWarnings("unchecked")
			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(String.format("Could not write value for property %1$s",
						persistentProperty), e);
				}
			}
		});
		GemfirePersistentProperty idProperty = entity.getIdProperty();
		if (idProperty != null) {
			writer.markIdentityField(idProperty.getName());
		}
		return true;
	}

Looks up and returns a custom PdxSerializer based on the class type of the object to (de)serialize.

Parameters:
type the Class type of the object to (de)serialize.
Returns:
a "custom" PdxSerializer for the given class type or null if no custom PdxSerializer for the given class type was registered.
See also:
getCustomSerializers()
com.gemstone.gemfire.pdx.PdxSerializer
	protected PdxSerializer getCustomSerializer(Class<?> type) {
		return getCustomSerializers().get(type);
	}

Looks up and returns an EntityInstantiator to construct and initialize an instance of the object defined by the given PersistentEntity (meta-data).

Parameters:
entity the PersistentEntity object used to lookup the custom EntityInstantiator.
Returns:
an EntityInstantiator for the given PersistentEntity.
See also:
org.springframework.data.convert.EntityInstantiator
org.springframework.data.mapping.PersistentEntity
	}

Looks up and returns the PersistentEntity meta-data for the given entity class type.

Parameters:
entityType the Class type of the actual persistent entity, application domain object class.
Returns:
the PersistentEntity meta-data for the given entity class type.
See also:
getMappingContext()
org.springframework.data.gemfire.mapping.GemfirePersistentEntity
	protected GemfirePersistentEntity<?> getPersistentEntity(Class<?> entityType) {
	}
New to GrepCode? Check out our FAQ X