Class Serialization

java.lang.Object
org.drasyl.node.handler.serialization.Serialization

public class Serialization extends Object
Holds a Map with all available Serializers and the classes each serializer should be used for. This class is used by MessageSerializer to (de)serialize message objects when communicating with remote nodes.

Each serializer is applied for objects of the assigned class, all subclasses, and all implementations.

Each received message includes the class name of the content. Normally, we would have to invoke the method Class.forName(String) to load the associated class to find a serializer for the class, any subclass, or implementation. However, this method invocation poses a security risk because we cannot control the class name received from the remote node. For this reason, when adding a Serializer, the classpath is scanned in advance for subclasses and implementations. These results will then later used when a message should be deserialized. This approach allows us to load only classes with valid Serializers.

See Also:
  • Field Details

    • NULL_SERIALIZER

      protected static final NullSerializer NULL_SERIALIZER
  • Constructor Details

  • Method Details

    • buildInheritanceGraph

      public static void buildInheritanceGraph()
      Rebuilds the inheritance graph, but keeps the old values.
    • findSerializerFor

      public Serializer findSerializerFor(String clazzName)
      Returns the configured Serializer for the given clazzName. The configured Serializer is used if the configured class `isAssignableFrom` from the clazz, i.e. the configured class is a super class or implemented interface. In case of ambiguity it is primarily using the most specific configured class, and secondly the entry configured first.
      Parameters:
      clazzName - name of class for which a serializer should be searched for
      Returns:
      serializer for given clazz or null if nothing found
    • addSerializer

      public void addSerializer(Class<?> clazz, Serializer serializer)
      Adds a serializer as serializer for objects of type clazz.
      Parameters:
      clazz - class the serializer should be used for
      serializer - the serializer
    • removeSerializer

      public void removeSerializer(Class<?> clazz)
      Removes all serializers for objects of type clazz.
      Parameters:
      clazz - class the serializer should be removed for
    • removeSerializer

      public void removeSerializer(Serializer serializer)
      Removes given serializer from
      Parameters:
      serializer - the serializer that should be removed
    • noop

      public static void noop()
      This method does nothing. However, it triggers the loading of the inheritance graph and thus allows the caller to eagerly load the inheritance graph.