Skip to content

Make driver-, session- and transaction config serializable. #1082

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

Merged
merged 12 commits into from
Nov 19, 2021
9 changes: 7 additions & 2 deletions driver/src/main/java/org/neo4j/driver/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.neo4j.driver;

import java.io.File;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -64,8 +65,10 @@
* @since 1.0
*/
@Immutable
public class Config
public class Config implements Serializable
{
private static final long serialVersionUID = -4496545746399601108L;

private static final Config EMPTY = builder().build();

/** User defined logging */
Expand Down Expand Up @@ -780,8 +783,10 @@ public Config build()
/**
* Control how the driver determines if it can trust the encryption certificates provided by the Neo4j instance it is connected to.
*/
public static class TrustStrategy
public static class TrustStrategy implements Serializable
{
private static final long serialVersionUID = -1631888096243987740L;

/**
* The trust strategy that the driver supports
*/
Expand Down
13 changes: 8 additions & 5 deletions driver/src/main/java/org/neo4j/driver/SessionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.reactivestreams.Subscription;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -33,14 +34,16 @@
/**
* The session configurations used to configure a session.
*/
public class SessionConfig
public class SessionConfig implements Serializable
{
private static final long serialVersionUID = 5773462156979050657L;

private static final SessionConfig EMPTY = builder().build();

private final Iterable<Bookmark> bookmarks;
private final AccessMode defaultAccessMode;
private final String database;
private final Optional<Long> fetchSize;
private final Long fetchSize;
private final String impersonatedUser;

private SessionConfig( Builder builder )
Expand Down Expand Up @@ -123,7 +126,7 @@ public Optional<String> database()
*/
public Optional<Long> fetchSize()
{
return fetchSize;
return Optional.ofNullable( fetchSize );
}

/**
Expand Down Expand Up @@ -170,7 +173,7 @@ public String toString()
*/
public static class Builder
{
private Optional<Long> fetchSize = Optional.empty();
private Long fetchSize = null;
private Iterable<Bookmark> bookmarks = null;
private AccessMode defaultAccessMode = AccessMode.WRITE;
private String database = null;
Expand Down Expand Up @@ -278,7 +281,7 @@ public Builder withDatabase( String database )
*/
public Builder withFetchSize( long size )
{
this.fetchSize = Optional.of( assertValidFetchSize( size ) );
this.fetchSize = assertValidFetchSize( size );
return this;
}

Expand Down
33 changes: 27 additions & 6 deletions driver/src/main/java/org/neo4j/driver/TransactionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
package org.neo4j.driver;

import java.io.Serializable;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -61,17 +63,22 @@
*
* @see Session
*/
public class TransactionConfig
public class TransactionConfig implements Serializable
{
private static final long serialVersionUID = -7954949878657177280L;

private static final TransactionConfig EMPTY = builder().build();

private final Duration timeout;
private final Map<String,Value> metadata;
private final Map<String,Object> metadata;

// Values are not serializable, hence, we keep a transient volatile map of them around
private transient volatile Map<String, Value> convertedMetadata;

private TransactionConfig( Builder builder )
{
this.timeout = builder.timeout;
this.metadata = unmodifiableMap( builder.metadata );
this.metadata = builder.metadata;
}

/**
Expand Down Expand Up @@ -111,7 +118,20 @@ public Duration timeout()
*/
public Map<String,Value> metadata()
{
return metadata;
Map<String,Value> result = this.convertedMetadata;
if ( result == null )
{
synchronized ( this )
{
result = this.convertedMetadata;
if ( result == null )
{
this.convertedMetadata = unmodifiableMap( Extract.mapOfValues( this.metadata ) );
result = this.convertedMetadata;
}
}
}
return result;
}

/**
Expand Down Expand Up @@ -161,7 +181,7 @@ public String toString()
public static class Builder
{
private Duration timeout;
private Map<String,Value> metadata = emptyMap();
private Map<String,Object> metadata = emptyMap();

private Builder()
{
Expand Down Expand Up @@ -202,7 +222,8 @@ public Builder withTimeout( Duration timeout )
public Builder withMetadata( Map<String,Object> metadata )
{
requireNonNull( metadata, "Transaction metadata should not be null" );
this.metadata = Extract.mapOfValues( metadata );
metadata.values().forEach( Extract::assertParameter ); // Just assert valid parameters but don't create a value map yet
this.metadata = new HashMap<>( metadata ); // Create a defensive copy
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.neo4j.driver.internal;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
Expand All @@ -28,8 +29,10 @@

import static java.util.Objects.requireNonNull;

public final class InternalBookmark implements Bookmark
public final class InternalBookmark implements Bookmark, Serializable
{
private static final long serialVersionUID = 8196096018245038950L;

private static final InternalBookmark EMPTY = new InternalBookmark( Collections.emptySet() );

private final Set<String> values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.neo4j.driver.internal;

import java.io.IOException;
import java.io.Serializable;
import java.security.GeneralSecurityException;

import org.neo4j.driver.Config;
Expand All @@ -30,8 +31,10 @@
import static org.neo4j.driver.internal.Scheme.isSecurityScheme;
import static org.neo4j.driver.internal.security.SecurityPlanImpl.insecure;

public class SecuritySettings
public class SecuritySettings implements Serializable
{
private static final long serialVersionUID = 4494615367164106576L;

private static final boolean DEFAULT_ENCRYPTED = false;
private static final Config.TrustStrategy DEFAULT_TRUST_STRATEGY = Config.TrustStrategy.trustSystemCertificates();
private static final SecuritySettings DEFAULT = new SecuritySettings( DEFAULT_ENCRYPTED, DEFAULT_TRUST_STRATEGY );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.neo4j.driver.internal.logging;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.time.LocalDateTime;
import java.util.Objects;
Expand All @@ -39,8 +40,10 @@
*
* @see Logging#console(Level)
*/
public class ConsoleLogging implements Logging
public class ConsoleLogging implements Logging, Serializable
{
private static final long serialVersionUID = 9205935204074879150L;

private final Level level;

public ConsoleLogging( Level level )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
*/
package org.neo4j.driver.internal.logging;

import java.io.Serializable;

import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;

public class DevNullLogging implements Logging
public class DevNullLogging implements Logging, Serializable
{
private static final long serialVersionUID = -2632752338512373821L;

public static final Logging DEV_NULL_LOGGING = new DevNullLogging();

private DevNullLogging()
Expand All @@ -34,4 +38,14 @@ public Logger getLog( String name )
{
return DevNullLogger.DEV_NULL_LOGGER;
}

// Don't remove that apparently unused method.
// It is involved during deserialization after readObject on the new object.
// The returned value replaces the object read.
// An enum would be preferable, but would not be API compatible.
// Reference: https://docs.oracle.com/en/java/javase/17/docs/specs/serialization/input.html#the-readresolve-method andJoshua Bloch, Effective Java 3rd edition
private Object readResolve()
{
return DEV_NULL_LOGGING;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.neo4j.driver.internal.logging;

import java.io.Serializable;
import java.util.logging.Level;

import org.neo4j.driver.Logger;
Expand All @@ -29,8 +30,10 @@
*
* @see Logging#javaUtilLogging(Level)
*/
public class JULogging implements Logging
public class JULogging implements Logging, Serializable
{
private static final long serialVersionUID = -1145576859241657833L;

private final Level loggingLevel;

public JULogging( Level loggingLevel )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.slf4j.LoggerFactory;

import java.io.Serializable;

import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;

Expand All @@ -29,8 +31,10 @@
*
* @see Logging#slf4j()
*/
public class Slf4jLogging implements Logging
public class Slf4jLogging implements Logging, Serializable
{
private static final long serialVersionUID = 4120390028025944991L;

@Override
public Logger getLog( String name )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
*/
package org.neo4j.driver.internal.retry;

public final class RetrySettings
import java.io.Serializable;

public final class RetrySettings implements Serializable
{
private static final long serialVersionUID = -2895062473220745239L;

public static final RetrySettings DEFAULT =
new RetrySettings( ExponentialBackoffRetryLogic.DEFAULT_MAX_RETRY_TIME_MS );

Expand Down
Loading