Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datagraph 311: Serialization of Query Results and Entities #124

Merged
merged 4 commits into from Aug 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,6 +22,10 @@

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
@Deprecated
/**
* @deprecated replaced by {@link QueryResult}
*/
public @interface MapResult {
String value() default "";
}
@@ -0,0 +1,34 @@
/**
* Copyright 2011 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.neo4j.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to mark either a POJO or interface as being able to hold the results of a
* SDN based query.
*
* @author Nicki Watt
* @since 06.08.2013
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface QueryResult {

}
Expand Up @@ -17,27 +17,45 @@
package org.springframework.data.neo4j.fieldaccess;

import org.springframework.data.neo4j.core.EntityState;
import org.springframework.data.neo4j.mapping.MappingPolicy;
import org.springframework.data.neo4j.mapping.ManagedEntity;
import org.springframework.data.neo4j.mapping.MappingPolicy;
import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty;
import org.springframework.data.neo4j.support.DoReturn;
import org.springframework.data.neo4j.support.Neo4jTemplate;

import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

/**
* This class provides a mechanism for managing and controlling access to
* a Set based field on a SDN managed entity. The associated field typically
* serves as a container for all the references to some other SDN entity(s).
*
* @param <T>
*/
public class ManagedFieldAccessorSet<T> extends AbstractSet<T> {
private final Object entity;
final Set<T> delegate;
private final Neo4jPersistentProperty property;
private final Neo4jTemplate ctx;
private final FieldAccessor fieldAccessor;
private final MappingPolicy mappingPolicy;
public class ManagedFieldAccessorSet<T> extends AbstractSet<T> implements Serializable {

private static final long serialVersionUID = 1L;

private Object writeReplace() {
return new SerializationProxy<T>(this);
}

private void readObject(ObjectInputStream ois) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}

private final transient Object entity;
final transient Set<T> delegate;
private final transient Neo4jPersistentProperty property;
private final transient Neo4jTemplate ctx;
private final transient FieldAccessor fieldAccessor;
private final transient MappingPolicy mappingPolicy;

@SuppressWarnings("unchecked")
public ManagedFieldAccessorSet(final Object entity, final Object newVal, final Neo4jPersistentProperty property, Neo4jTemplate ctx, FieldAccessor fieldAccessor, final MappingPolicy mappingPolicy) {
Expand Down Expand Up @@ -141,4 +159,26 @@ public void clear() {
delegate.clear();
update();
}
}

/**
* Implementation of the Serialization Proxy Pattern (ref Item 78
* of Effective Java - 2nd edition)
* @param <T> Type of the underlying class being stored in the Set.
*/
private static class SerializationProxy<T> implements Serializable {

private static final long serialVersionUID = 1L;
private Set<T> delegateSet;

SerializationProxy(ManagedFieldAccessorSet<T> managedFieldAccessorSet) {
this.delegateSet = managedFieldAccessorSet.delegate;
}

private Object readResolve() {
return delegateSet;
}

}

}

Expand Up @@ -16,25 +16,40 @@
package org.springframework.data.neo4j.fieldaccess;

import org.springframework.data.neo4j.core.EntityState;
import org.springframework.data.neo4j.mapping.MappingPolicy;
import org.springframework.data.neo4j.mapping.ManagedEntity;
import org.springframework.data.neo4j.mapping.MappingPolicy;
import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty;
import org.springframework.data.neo4j.support.DoReturn;
import org.springframework.data.neo4j.support.Neo4jTemplate;

import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Map;

/**
* Updates the entity containing such a ManagedPrefixedDynamicProperties when some property is added, changed or
* deleted.
*/
public class ManagedPrefixedDynamicProperties extends PrefixedDynamicProperties {
private final Object entity;
private final Neo4jTemplate template;
private final FieldAccessor fieldAccessor;
private final Neo4jPersistentProperty property;
private boolean isNode;
private MappingPolicy mappingPolicy;
public class ManagedPrefixedDynamicProperties extends PrefixedDynamicProperties implements Serializable
{

private static final long serialVersionUID = 1L;

private Object writeReplace() {
return new SerializationProxy(this);
}

private void readObject(ObjectInputStream ois) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}

private transient final Object entity;
private transient final Neo4jTemplate template;
private transient final FieldAccessor fieldAccessor;
private transient final Neo4jPersistentProperty property;
private transient boolean isNode;
private transient MappingPolicy mappingPolicy;

public ManagedPrefixedDynamicProperties(String prefix, final Neo4jPersistentProperty property, final Object entity, Neo4jTemplate template, FieldAccessor fieldAccessor, final MappingPolicy mappingPolicy) {
this(prefix,10,property,entity, template,fieldAccessor, mappingPolicy);
Expand Down Expand Up @@ -102,4 +117,28 @@ private Object updateValueWithState(EntityState entityState) {
property.setValue(entity, newValue);
return newValue;
}

/**
* Implementation of the Serialization Proxy Pattern (ref Item 78
* of Effective Java - 2nd edition)
* @param <T> Type of the underlying class being stored in the Set.
*/
private static class SerializationProxy<T> implements Serializable {

private static final long serialVersionUID = 1L;
private Map actualMapContent;
private String prefix;

SerializationProxy(ManagedPrefixedDynamicProperties prefixedDynamicProperties) {
this.actualMapContent = prefixedDynamicProperties.asMap();
this.prefix = prefixedDynamicProperties.prefix;
}

private Object readResolve() {
PrefixedDynamicProperties val = new PrefixedDynamicProperties(prefix);
val.setPropertiesFrom(actualMapContent);
return val;
}

}
}
Expand Up @@ -15,6 +15,9 @@
*/
package org.springframework.data.neo4j.fieldaccess;

import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -27,9 +30,20 @@
* <p>
* The methods *PrefixedProperty() allow to access the prefixed property key/values pairs directly.
*/
public class PrefixedDynamicProperties implements DynamicProperties {
private final Map<String, Object> map;
protected final String prefix;
public class PrefixedDynamicProperties implements DynamicProperties , Serializable {

private static final long serialVersionUID = 1L;

private Object writeReplace() {
return new SerializationProxy(this);
}

private void readObject(ObjectInputStream ois) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}

private transient final Map<String, Object> map;
protected final transient String prefix;

/**
* Handles key prefixing
Expand Down Expand Up @@ -291,4 +305,28 @@ public boolean equals(Object obj) {
}
return true;
}

/**
* Implementation of the Serialization Proxy Pattern (ref Item 78
* of Effective Java - 2nd edition)
* @param <T> Type of the underlying class being stored in the Set.
*/
private static class SerializationProxy<T> implements Serializable {

private static final long serialVersionUID = 1L;
private Map actualMapContent;
private String prefix;

SerializationProxy(PrefixedDynamicProperties prefixedDynamicProperties) {
this.actualMapContent = prefixedDynamicProperties.map;
this.prefix = prefixedDynamicProperties.prefix;
}

private Object readResolve() {
PrefixedDynamicProperties val = new PrefixedDynamicProperties(prefix);
val.setPropertiesFrom(actualMapContent);
return val;
}

}
}
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.neo4j.mapping;

import java.io.Serializable;
import java.util.*;

import static java.util.Arrays.asList;
Expand All @@ -32,7 +33,10 @@ enum Option {
boolean shouldLoad();
MappingPolicy combineWith(MappingPolicy mappingPolicy);

public class DefaultMappingPolicy implements MappingPolicy {
public class DefaultMappingPolicy implements MappingPolicy , Serializable {

private static final long serialVersionUID = 1L;

private Set<Option> options;

public DefaultMappingPolicy(Option... options) {
Expand Down