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

at.spardat.xma.serializer.SerializerFactory Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

//@(#) $Id: SerializerFactory.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.serializer;

import at.spardat.xma.session.XMASession;

/**
 * Factory to create Serializers and Deserializers. An instance of 
 * SerializerFactoryClient or SerializerFactoryServer must be created.
 * 
 * @author YSD, 12.02.2005  
 */
public abstract class SerializerFactory {
    
    /**
     * Constant for binary
     */
    public static final String          SER_BINARY = "binary";
    
    /**
     * Constant for ascii
     */
    public static final String          SER_ASCII  = "ascii";
    
    /**
     * Constant for tagged ascii
     */
    public static final String          SER_TASCII = "tascii";
    
    /**
     * Property key
     */
    public static final String          PROP_KEY = "Serializer";
    
    /**
     * Overwritten sermode
     */
    private String                      fSerMode = null;
    
    /**
     * Key for system property
     */
    public static final String          SYS_PROP_KEY = "xma.Serializer";

    
    /**
     * Create a new Serializer with a given initial buffer size and returns it.
     * 
     * @param session may be null at the server
     */
    public Serializer createSerializer (XMASession session, int bufferSize) {
        String      what = getSerializationMode (session);
        if (what.equals (SER_TASCII)) return new TaggedAsciiSerializer (isAtServer(), bufferSize);
        else if (what.equals (SER_ASCII)) return new AsciiSerializer (isAtServer(), bufferSize);
        else return new BinarySerializer (isAtServer(), bufferSize);
    }
    
    /**
     * Creates a new Deserializer with input data (first len bytes are
     * taken into consideration).
     * 
     * @param session may be null at the server
     */
    public Deserializer createDeserializer (XMASession session, byte[] data, int offset, int len) {
        String      what = getSerializationMode (session);
        if (what.equals (SER_TASCII)) return new TaggedAsciiDeserializer (data, offset, len);
        else if (what.equals (SER_ASCII)) return new AsciiDeserializer (data, offset, len);
        else return new BinaryDeserializer (data, offset, len);
    }
    
    /**
     * Creates a new Deserializer with input data.
     * 
     * @param session XMASession. May be null at the server.
     * @param data input data
     */
    public Deserializer createDeserializer (XMASession session, byte[] data) {
        return createDeserializer (session, data, 0, data.length);
    }
    
    /**
     * Returns whether serialization mode is binary.
     * 
     * @param session may be null at the server
     */
    public boolean isModeBinary (XMASession session) {
        return getSerializationMode(session).equals(SER_BINARY);
    }
    
    /**
     * Returns one of the SER_-constants that determine the current
     * serialization mode.
     */
    public String getSerializationMode (XMASession session) {
        // if explicitely set then use the set value
        if (fSerMode != null) return fSerMode;
        // a set system-property overwrites all other settings
        String          sysProp = System.getProperty(SYS_PROP_KEY);
        if (sysProp != null) return sysProp;
        // let subclasses decide
        else return getSerializationMode0(session);
    }
    
    /**
     * Returns one of the SER_-constants that determine the current
     * serialization mode.
     */
    public abstract String getSerializationMode0 (XMASession session);
    
    /**
     * Indicates if this is executing at the server
     */
    public abstract boolean isAtServer ();
    
    /**
     * Allows to overwrite the properties and to force a particular mode
     */
    public void setSerializationMode (String what) {
        fSerMode = what;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy