Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
*
* 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 com.vmware.xenon.common.serialization;
import java.net.URI;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.UUID;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Kryo.DefaultInstantiatorStrategy;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.MapSerializer;
import com.esotericsoftware.kryo.serializers.VersionFieldSerializer;
import org.objenesis.strategy.StdInstantiatorStrategy;
import com.vmware.xenon.common.ServiceDocument;
import com.vmware.xenon.common.Utils;
public final class KryoSerializers {
private static final String PROPERTY_KRYO_HANDLE_BUILTIN_COLLECTIONS
= Utils.PROPERTY_NAME_PREFIX + "kryo.handleBuiltInCollections";
private static boolean KRYO_HANDLE_BUILTIN_COLLECTIONS = true;
static {
String v = System.getProperty(PROPERTY_KRYO_HANDLE_BUILTIN_COLLECTIONS);
if (v != null) {
KRYO_HANDLE_BUILTIN_COLLECTIONS = Boolean.valueOf(v);
}
}
/**
* Binary serialization thread local instances that track object references
*/
public static class KryoForObjectThreadLocal extends ThreadLocal {
@Override
protected Kryo initialValue() {
return KryoSerializers.create(true);
}
}
/**
* Binary serialization thread local instances that do not track object references, used
* for document and operation body serialization
*/
public static class KryoForDocumentThreadLocal extends ThreadLocal {
@Override
protected Kryo initialValue() {
return KryoSerializers.create(false);
}
}
private KryoSerializers() {
}
private static final ThreadLocal kryoForObjectPerThread = new KryoForObjectThreadLocal();
private static final ThreadLocal kryoForDocumentPerThread = new KryoForDocumentThreadLocal();
private static ThreadLocal kryoForObjectPerThreadCustom;
private static ThreadLocal kryoForDocumentPerThreadCustom;
public static final long THREAD_LOCAL_BUFFER_LIMIT_BYTES = 1024 * 1024;
private static final int DEFAULT_BUFFER_SIZE_BYTES = 4096;
private static final BufferThreadLocal bufferPerThread = new BufferThreadLocal();
public static Kryo create(boolean isObjectSerializer) {
Kryo k = new Kryo();
// handle classes with missing default constructors
k.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
// supports addition of fields if the @since annotation is used
k.setDefaultSerializer(VersionFieldSerializer.class);
// Custom serializers for Java 8 date/time
k.addDefaultSerializer(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE);
k.addDefaultSerializer(Instant.class, InstantSerializer.INSTANCE);
k.addDefaultSerializer(ZoneId.class, ZoneIdSerializer.INSTANCE);
// Add non-cloning serializers for all immutable types bellow
k.addDefaultSerializer(UUID.class, UUIDSerializer.INSTANCE);
k.addDefaultSerializer(URI.class, URISerializer.INSTANCE);
k.addDefaultSerializer(ByteBuffer.class, ByteBufferSerializer.INSTANCE);
if (KRYO_HANDLE_BUILTIN_COLLECTIONS) {
configureJdkCollections(k);
}
if (!isObjectSerializer) {
// For performance reasons, and to avoid memory use, assume documents do not
// require object graph serialization with duplicate or recursive references
k.setReferences(false);
k.setCopyReferences(false);
// documentSerialized must nullify certain fields.
k.setDefaultSerializer(FieldNullifyingVersionFieldSerializer.class);
} else {
// To avoid monotonic increase of memory use, due to reference tracking, we must
// reset after each use.
k.setAutoReset(true);
}
return k;
}
private static void configureJdkCollections(Kryo kryo) {
// write singleton as arraylists of size 1
CollectionSerializer emptyOrSingletonSerializer = new CollectionSerializer() {
@SuppressWarnings("rawtypes")
@Override
protected Collection> create(Kryo kryo, Input input, Class type) {
return newCollection(type);
}
@SuppressWarnings("rawtypes")
private Collection> newCollection(Class origType) {
if (NavigableSet.class.isAssignableFrom(origType)) {
return new TreeSet<>();
} else if (Set.class.isAssignableFrom(origType)) {
return new HashSet<>();
} else {
return new ArrayList<>(1);
}
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
protected Collection> createCopy(Kryo kryo, Collection original) {
return newCollection((Class) original.getClass());
}
};
MapSerializer emptyOrSingletonMapSerializer = new MapSerializer(){
@SuppressWarnings("rawtypes")
@Override
protected Map, ?> create(Kryo kryo, Input input, Class