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

com.alachisoft.ncache.serialization.standard.FormatterServices Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * @(#)FormatterServices.java	1.0
 *
 * Created on September 18, 2008, 12:59 PM
 *
 * Copyright 2008 NeXtreme Innovations, Inc. All rights reserved.
 * "NeXtreme Innovations" PROPRIETARY/CONFIDENTIAL. Use is subject
 * to license terms.
 */
package com.alachisoft.ncache.serialization.standard;

import com.alachisoft.ncache.serialization.core.io.ICompactSerializable;
import com.alachisoft.ncache.serialization.core.io.InternalCompactSerializable;
import com.alachisoft.ncache.serialization.core.io.TypeSurrogateSelector;
import com.alachisoft.ncache.serialization.core.io.surrogates.NCacheArgumentException;
import com.alachisoft.ncache.serialization.core.io.surrogates.SerializationSurrogate;
import com.alachisoft.ncache.serialization.standard.io.TypeSurrogateSelectorImpl;
import com.alachisoft.ncache.serialization.standard.io.surrogates.*;
import com.alachisoft.ncache.serialization.util.SerializationUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * FormatterServices class.
 *
 * @version 1.0, September 18, 2008
 */
public class FormatterServices {

    static HashMap _attributeOrder = new HashMap();
    private static FormatterServices msDefault;
    private TypeSurrogateSelector mSurrogateSelector;
    private String _cacheContext;

    /**
     * Creates a new instance of FormatterServices
     */
    public FormatterServices() {
        mSurrogateSelector = TypeSurrogateSelectorImpl.getDefault();
    }

    /**
     * Creates a new instance of FormatterServices
     *
     * @param selector
     */
    public FormatterServices(TypeSurrogateSelector selector) {
        if (selector == null) {
            throw new NullPointerException();
        }
        this.mSurrogateSelector = selector;
    }

    /*
     * Returns the default type surrogate selector Object
     */
    public static FormatterServices getDefault() {
        if (FormatterServices.msDefault == null) {
            FormatterServices.msDefault = new FormatterServices();
        }
        return FormatterServices.msDefault;
    }

    public static HashMap getAttributeOrder() {
        return _attributeOrder;
    }

    public static void setAttributeOrder(HashMap attrib) {
        _attributeOrder = attrib;
    }

    public void SetCacheContext(String context) {
        this._cacheContext = context;
    }

    /*
     * Returns the default type surrogate selector Object
     */
    public TypeSurrogateSelector getSurrogateSelector() {
        return this.mSurrogateSelector;
    }

    /*
     * Returns the default type surrogate selector Object
     */
    public void setSurrogateSelector(TypeSurrogateSelector selector) {
        if (selector == null) {
            throw new NullPointerException();
        }
        this.mSurrogateSelector = selector;
    }

    /*
     * Registers a type that implements  with the system. If the type is an array of s appropriate surrogates for arrays
     * and the element type are also registered.
     *
     * Else the class is sent to be dynimcally serialized
     *
     * type that implements  specified HARD handle for type If  is null  If the  is already registered or when
     * no appropriate surrogate is found for the specified . 
     */
    public void registerKnownType(Class cls, short typeHandle, short subHandle, String cacheContext, boolean portable, HashMap nonCompactFieldsMap)
            throws NCacheArgumentException {
        if (cls == null) {
            throw new NullPointerException("cls");
        }
        if (mSurrogateSelector.getSurrogateForType(cls, true, cacheContext) != null) {
            throw new IllegalArgumentException(PackageResources.Surrogates_AlreadyRegistered);
        }

        SerializationSurrogate surrogate = null;
        try {
            surrogate = selectBestSurrogate(cls, portable, subHandle, nonCompactFieldsMap, false);
        } catch (Exception exception) {
            throw new NCacheArgumentException(exception.getMessage());
        }
        if (surrogate == null) {
            throw new IllegalArgumentException(PackageResources.Surrogates_NotFound);
        }

        this.mSurrogateSelector.register(surrogate, typeHandle, surrogate.getSubHandle(), cacheContext, portable);
    }

    public void register(Map type, HashMap attributeOrder, String cacheContext, HashMap portable) throws NCacheArgumentException {
        this._cacheContext = cacheContext;
        setAttributeOrder(attributeOrder);

        Iterator it = type.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();

            HashMap handleNonCompactFieldMap = (HashMap) pairs.getValue();
            Short typeHandle = (Short) handleNonCompactFieldMap.get("handle");

            HashMap nonCompactFieldsMap = (HashMap) handleNonCompactFieldMap.get("non-compact-fields");

            boolean isPortable = false;
            if (portable != null && portable.get((Short) typeHandle) != null) {
                isPortable = (Boolean) portable.get((Short) typeHandle);
            }
            short subHandle = 0;
            if (isPortable) {
                subHandle = SerializationUtil.GetSubTypeHandle(cacheContext, Short.toString((Short) typeHandle), (Class) pairs.getKey());
            }

            registerKnownType((Class) pairs.getKey(), (Short) typeHandle, subHandle, cacheContext, isPortable, nonCompactFieldsMap);
        }
    }

    /*
     * Registers a type that implements  with the system. If the type is an array of s appropriate surrogates for arrays
     * and the element type are also registered.  type that implements  If
     *  is null.   If the  is already registered or when no appropriate surrogate is
     * found for the specified . 
     */
    public void registerKnownType(Class cls)
            throws NCacheArgumentException {
        if (cls == null) {
            throw new NullPointerException("cls");
        }
        if (this.mSurrogateSelector.getSurrogateForType(cls, true, _cacheContext) != null) {
            throw new IllegalArgumentException(PackageResources.Surrogates_AlreadyRegistered);
        }

        SerializationSurrogate surrogate = null;
        try {
            //portable is kept true to keep things working as if Portable types are registerd
            surrogate = selectBestSurrogate(cls, false, (short) 0, null, false);
        } catch (Exception exception) {
            throw new NCacheArgumentException(exception.getMessage());
        }
        if (surrogate == null) {
            throw new IllegalArgumentException(PackageResources.Surrogates_NotFound);
        }

        this.mSurrogateSelector.register(surrogate);
    }

    /**
     * Registers a type that implements  with the system. If the type is an array of s appropriate surrogates for arrays
     * and the element type are also registered.  type that implements  If
     *  is null.   If the  is already registered or when no appropriate surrogate is
     * found for the specified . 
     *
     * @param cls    Class to register
     * @param handle the handle to which the class is to be registered, if the handle already exists IllegalArgumentException will be thrown "Surrogates_AlreadyRegistered"
     * @throws NCacheArgumentException when class is null or this handle is already taken or any exception arrives from selecting the best surrogate
     */
    public void registerKnownTypes(Class cls, short handle) throws NCacheArgumentException {
        registerKnownTypes(cls, handle, true);
    }

    /**
     * Registers a type that implements  with the system. If the type is an array of s appropriate surrogates for arrays
     * and the element type are also registered.  type that implements  If
     *  is null.   If the  is already registered or when no appropriate surrogate is
     * found for the specified . 
     *
     * @param cls    Class to register
     * @param handle the handle to which the class is to be registered, if the handle already exists IllegalArgumentException will be thrown "Surrogates_AlreadyRegistered"
     * @throws NCacheArgumentException when class is null or this handle is already taken or any exception arrives from selecting the best surrogate
     */
    public void registerNonVersionCompatibleCompactType(Class cls, short handle) throws NCacheArgumentException {
        registerKnownTypes(cls, handle, false);
    }

    private void registerKnownTypes(Class cls, short handle, boolean versionCompatible) throws NCacheArgumentException {
        if (cls == null) {
            throw new NullPointerException("cls");
        }
        if (this.mSurrogateSelector.getSurrogateForType(cls, true, _cacheContext) != null) {
            SerializationSurrogate surrogate = this.mSurrogateSelector.getSurrogateForTypeHandle(handle, _cacheContext);
            if (surrogate != null && surrogate.getRealClass().equals(cls)) {
                return;
            }
            throw new IllegalArgumentException(PackageResources.Surrogates_AlreadyRegistered);
        }

        SerializationSurrogate surrogate = null;
        try {
            //portable is kept true to keep things working as if Portable types are registerd
            surrogate = selectBestSurrogate(cls, false, (short) 0, null, versionCompatible);
        } catch (Exception exception) {
            throw new NCacheArgumentException(exception.getMessage());
        }
        if (surrogate == null) {
            throw new IllegalArgumentException(PackageResources.Surrogates_NotFound);
        }

        this.mSurrogateSelector.register(surrogate, handle);
    }

    /*
     *
     *  from the system.  the specified type
     */

    /**
     * Unregisters the surrogate for the specified type that implements
     *
     * @param cls          Class type
     * @param cacheContext name of the cache
     * @param removeCache  set to true if all the registered types with respect to this cache is to be removed, param class can be any/null for this
     */
    public void unregisterKnownType(Class cls, String cacheContext, boolean removeCache) {
        if (cls == null && !removeCache) {
            throw new NullPointerException("cls");
        }

        SerializationSurrogate surrogate = null;

        if (!removeCache) {
            surrogate = mSurrogateSelector.getSurrogateForType(cls, true, _cacheContext);
        }

        this.mSurrogateSelector.unregister(surrogate, cacheContext, removeCache);
    }

    /*
     * Finds and returns the best surrogate responsible for serializing the specified   the given , whose
     * surrogate is returned instance of 
     */
    private SerializationSurrogate selectBestSurrogate(Class cls, boolean portable, short subHandle, HashMap nonCompactFieldsMap, boolean versionCompatible) throws Exception {
        SerializationSurrogate surrogate = null;

        if (Throwable.class.isAssignableFrom(cls)) {
            surrogate = new ThrowableSerializationSurrogate(cls);
        } else if (cls.isArray()) {
            surrogate = new GenericArraySerializationSurrogate(cls);
        } else if (ICompactSerializable.class.isAssignableFrom(cls)) {
            if (versionCompatible)
                surrogate = new VersionCompatibleXExternalizableSerializationSurrogate(cls);
            else
                surrogate = new XExternalizableSerializationSurrogate(cls);
        } else if (InternalCompactSerializable.class.isAssignableFrom(cls)) {
            if (versionCompatible)
                surrogate = new VersionCompatibleInternalCompactSerializableSurrogate(cls);
            else
                surrogate = new InternalCompactSerializableSurrogate(cls);
        } else if (ICompactSerializable.class.isAssignableFrom(cls)) {
            surrogate = new ExternalizableSerializationSurrogate(cls);
        } else if (Enum.class.isAssignableFrom(cls)) {
            surrogate = new EnumSerializationSurrogate(cls);
        }
//        else if (Collection.class.isAssignableFrom(cls))
        else // If all else fails create a Dynamic Surrogate
        {
            surrogate = DynamicSurrogateBuilder.createTypeSurrogate(mSurrogateSelector, cls, getAttributeOrder(), this._cacheContext, portable, subHandle, nonCompactFieldsMap);
        }
        return surrogate;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy