From c1dc81995334532d2671fe94d5052d0974fb0a44 Mon Sep 17 00:00:00 2001 From: Paul Speed Date: Sat, 2 May 2015 00:35:15 -0400 Subject: [PATCH] Added the ability to put the serializer registry in "read only" mode. Modified the SerializerRegistrationMessage to put the serializer registry into read only after it compiles the message so that the server won't accidentally register messages after they've been compiled. --- .../SerializerRegistrationsMessage.java | 4 +++- .../jme3/network/serializing/Serializer.java | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/jme3-networking/src/main/java/com/jme3/network/message/SerializerRegistrationsMessage.java b/jme3-networking/src/main/java/com/jme3/network/message/SerializerRegistrationsMessage.java index da7f2a7cf..9c3896475 100644 --- a/jme3-networking/src/main/java/com/jme3/network/message/SerializerRegistrationsMessage.java +++ b/jme3-networking/src/main/java/com/jme3/network/message/SerializerRegistrationsMessage.java @@ -127,7 +127,9 @@ public class SerializerRegistrationsMessage extends AbstractMessage { } compiled = list.toArray(new Registration[list.size()]); - INSTANCE = new SerializerRegistrationsMessage(compiled); + INSTANCE = new SerializerRegistrationsMessage(compiled); + + Serializer.setReadOnly(true); } public void registerAll() { diff --git a/jme3-networking/src/main/java/com/jme3/network/serializing/Serializer.java b/jme3-networking/src/main/java/com/jme3/network/serializing/Serializer.java index a7d20da54..d4c054403 100644 --- a/jme3-networking/src/main/java/com/jme3/network/serializing/Serializer.java +++ b/jme3-networking/src/main/java/com/jme3/network/serializing/Serializer.java @@ -71,6 +71,8 @@ public abstract class Serializer { private static boolean strictRegistration = true; + private static volatile boolean locked = false; + // Registers the classes we already have serializers for. static { @@ -168,6 +170,20 @@ public abstract class Serializer { return nextAvailableId--; } + /** + * Can put the registry in a read-only state such that additional attempts + * to register classes will fail. This can be used by servers to lock the + * registry to avoid accidentally registering classes after a full registry + * set has been compiled. + */ + public static void setReadOnly( boolean b ) { + locked = b; + } + + public static boolean isReadOnly() { + return locked; + } + /** * Directly registers a class for a specific ID. Generally, use the regular * registerClass() method. This method is intended for framework code that might @@ -175,7 +191,11 @@ public abstract class Serializer { */ public static SerializerRegistration registerClassForId( short id, Class cls, Serializer serializer ) { - SerializerRegistration reg = new SerializerRegistration(serializer, cls, id); + if( locked ) { + throw new RuntimeException("Serializer registry locked trying to register class:" + cls); + } + + SerializerRegistration reg = new SerializerRegistration(serializer, cls, id); idRegistrations.put(id, reg); classRegistrations.put(cls, reg);