Java Code Examples for com.esotericsoftware.kryo.Kryo#setClassLoader()

The following examples show how to use com.esotericsoftware.kryo.Kryo#setClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KryoCodec.java    From redisson with Apache License 2.0 6 votes vote down vote up
/**
 * Sub classes can customize the Kryo instance by overriding this method
 *
 * @return create Kryo instance
 */
protected Kryo createInstance(List<Class<?>> classes, ClassLoader classLoader) {
    Kryo kryo = new Kryo();
    if (classLoader != null) {
        kryo.setClassLoader(classLoader);
    }
    kryo.setReferences(false);
    for (Class<?> clazz : classes) {
        kryo.register(clazz);
    }
    return kryo;
}
 
Example 2
Source File: KryoSerialization.java    From JPPF with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserialize(final InputStream is) throws Exception {
  final Kryo kryo = pool.get();
  final ClassLoader cl = Thread.currentThread().getContextClassLoader();
  if (cl != null)  kryo.setClassLoader(cl);
  try {
    final Input in = new Input(is);
    return kryo.readClassAndObject(in);
  } finally {
    pool.put(kryo);
  }
}
 
Example 3
Source File: SerializationUtils.java    From hudi with Apache License 2.0 5 votes vote down vote up
public Kryo newKryo() {

      Kryo kryo = new Kryo();
      // ensure that kryo doesn't fail if classes are not registered with kryo.
      kryo.setRegistrationRequired(false);
      // This would be used for object initialization if nothing else works out.
      kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
      // Handle cases where we may have an odd classloader setup like with libjars
      // for hadoop
      kryo.setClassLoader(Thread.currentThread().getContextClassLoader());
      return kryo;
    }
 
Example 4
Source File: Namespace.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a Kryo instance.
 *
 * @return Kryo instance
 */
@Override
public Kryo create() {
  LOGGER.trace("Creating Kryo instance for {}", this);
  Kryo kryo = new Kryo();
  kryo.setClassLoader(classLoader);
  kryo.setRegistrationRequired(registrationRequired);

  // If compatible serialization is enabled, override the default serializer.
  if (compatible) {
    kryo.setDefaultSerializer(CompatibleFieldSerializer::new);
  }

  // TODO rethink whether we want to use StdInstantiatorStrategy
  kryo.setInstantiatorStrategy(
      new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));

  for (RegistrationBlock block : registeredBlocks) {
    int id = block.begin();
    if (id == FLOATING_ID) {
      id = kryo.getNextRegistrationId();
    }
    for (Pair<Class<?>[], Serializer<?>> entry : block.types()) {
      register(kryo, entry.getLeft(), entry.getRight(), id++);
    }
  }
  return kryo;
}
 
Example 5
Source File: Kryo5Codec.java    From redisson with Apache License 2.0 5 votes vote down vote up
protected Kryo createKryo(ClassLoader classLoader) {
    Kryo kryo = new Kryo();
    if (classLoader != null) {
        kryo.setClassLoader(classLoader);
    }
    kryo.setRegistrationRequired(false);
    kryo.setReferences(false);
    kryo.addDefaultSerializer(Throwable.class, new JavaSerializer());
    return kryo;
}
 
Example 6
Source File: KryoSerialization.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected Kryo newKryoInstance() {
    Kryo kryo = new Kryo(new DefaultClassResolver(), new CubaMapReferenceResolver());
    kryo.setInstantiatorStrategy(new CubaInstantiatorStrategy());
    if (onlySerializable) {
        kryo.setDefaultSerializer(CubaFieldSerializer.class);
    }

    //To work properly must itself be loaded by the application classloader (i.e. by classloader capable of loading
    //all the other application classes). For web application it means placing this class inside webapp folder.
    kryo.setClassLoader(KryoSerialization.class.getClassLoader());

    //noinspection ArraysAsListWithZeroOrOneArgument
    kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
    kryo.register(Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer());
    kryo.register(Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer());
    kryo.register(Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer());
    kryo.register(Collections.singletonList("").getClass(), new CollectionsSingletonListSerializer());
    kryo.register(Collections.singleton("").getClass(), new CollectionsSingletonSetSerializer());
    kryo.register(Collections.singletonMap("", "").getClass(), new CollectionsSingletonMapSerializer());
    kryo.register(BitSet.class, new BitSetSerializer());
    kryo.register(GregorianCalendar.class, new GregorianCalendarSerializer());
    kryo.register(InvocationHandler.class, new JdkProxySerializer());
    kryo.register(EnumSet.class, new EnumSetSerializer());
    kryo.register(TreeSet.class, new DefaultSerializers.TreeSetSerializer());
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);
    kryo.register(Pattern.class, new RegexSerializer());

    kryo.register(CGLibProxySerializer.CGLibProxyMarker.class, new CGLibProxySerializer());
    ImmutableListSerializer.registerSerializers(kryo);
    ImmutableSetSerializer.registerSerializers(kryo);
    ImmutableMapSerializer.registerSerializers(kryo);
    ImmutableMultimapSerializer.registerSerializers(kryo);
    kryo.register(IndirectList.class, new IndirectContainerSerializer());
    kryo.register(IndirectMap.class, new IndirectContainerSerializer());
    kryo.register(IndirectSet.class, new IndirectContainerSerializer());

    kryo.register(org.eclipse.persistence.indirection.IndirectList.class, new IndirectContainerSerializer());
    kryo.register(org.eclipse.persistence.indirection.IndirectMap.class, new IndirectContainerSerializer());
    kryo.register(org.eclipse.persistence.indirection.IndirectSet.class, new IndirectContainerSerializer());

    //classes with custom serialization methods
    kryo.register(HashMultimap.class, new CubaJavaSerializer());
    kryo.register(ArrayListMultimap.class, new CubaJavaSerializer());
    kryo.register(MetaClassImpl.class, new CubaJavaSerializer());
    kryo.register(MetaPropertyImpl.class, new CubaJavaSerializer());
    kryo.register(UnitOfWorkQueryValueHolder.class, new UnitOfWorkQueryValueHolderSerializer(kryo));

    kryo.addDefaultSerializer(Collection.class, new CubaCollectionSerializer());

    registerEntitySerializer(kryo);

    return kryo;
}