All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.javakaffee.web.msm.serializer.kryo.CGLibProxySerializerFactory Maven / Gradle / Ivy

There is a newer version: 2.3.2
Show newest version
/*
 * Copyright 2010 Martin Grotzke
 *
 * 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 de.javakaffee.web.msm.serializer.kryo;

import com.esotericsoftware.kryo.ClassResolver;
import com.esotericsoftware.kryo.Kryo;

import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.util.DefaultClassResolver;
import de.javakaffee.kryoserializers.cglib.CGLibProxySerializer;
import de.javakaffee.kryoserializers.cglib.CGLibProxySerializer.CGLibProxyMarker;
import org.objenesis.strategy.StdInstantiatorStrategy;

/**
 * A {@link SerializerFactory} that creates {@link CGLibProxySerializer} instances. Additionally
 * as {@link KryoCustomization} it registers a {@link CGLibProxySerializer} for the
 * {@link CGLibProxyMarker} class.
 * 
 * @author Martin Grotzke
 */
public class CGLibProxySerializerFactory implements KryoCustomization, SerializerFactory, KryoBuilderConfiguration {

    private final Kryo kryo;

    /**
     * Needed for instantiation as KryoBuilderConfiguration.
     */
    public CGLibProxySerializerFactory() {
        this(null);
    }

    public CGLibProxySerializerFactory(Kryo kryo) {
        this.kryo = kryo;
    }

    @Override
    public KryoBuilder configure(KryoBuilder kryoBuilder) {
        return kryoBuilder
                .withClassResolver(createClassResolver())
                .withInstantiatorStrategy(
                        new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    }

    @Override
    public void customize( final Kryo kryo ) {
        kryo.register( CGLibProxySerializer.CGLibProxyMarker.class, new CGLibProxySerializer() );
    }

    @Override
    public Serializer newSerializer(Class type) {
        if ( CGLibProxySerializer.canSerialize( type ) ) {
            return kryo.getSerializer(CGLibProxySerializer.CGLibProxyMarker.class);
        }
        return null;
    }

    protected ClassResolver createClassResolver() {
        return new CGLibProxyClassResolver();
    }

    static class CGLibProxyClassResolver extends DefaultClassResolver {
        @Override
        protected Class getTypeByName(final String className) {
            if (className.indexOf(CGLibProxySerializer.DEFAULT_NAMING_MARKER) > 0) {
                return CGLibProxyMarker.class;
            }
            return super.getTypeByName(className);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy