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

at.spardat.xma.rpc.RemoteData Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show 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: RemoteData.java 7307 2011-03-07 09:35:22Z blichtl $
package at.spardat.xma.rpc;

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

import at.spardat.xma.serializer.XmaInput;
import at.spardat.xma.serializer.XmaOutput;

/**
 * This class acts as value object for data to be transferred between
 * XMA client and XMA server in RPCs.
 *
 * @author YSD, 26.05.2003 10:39:58
 */
class RemoteData {

    /**
     * The version of the XMA runtime that produces an serialized output stream
     * via externalize (excluding the bootstrap runtime).
     * This must be the second attribute serialized to react on different
     * versions when deserializing.
     */
    protected int               runtimeVersion_ = at.spardat.xma.util.Version.getVersionNumber();


    /**
     * The serial change number. See class {@link at.spardat.xma.component.Component}.
     * -1 means that the SCN is unknown. An unknown SCN may be sent from server to client
     * in the case of some early time processing errors.
     */
    protected short             serverChangeNumber_ = -1;

    /**
     * The stream of page modifications. Never null if this is a RemoteCallData.
     * May be null if this is a RemoteReplyData.
     */
    protected byte[]            pageDeltas_;

    /**
     * Collection of parameters. Keys are Shorts (ids) and values are objects.
     * May be null if no parameters are provided. The ids of programmer supplied
     * parameters must be greater than or equal to zero. XMA runtime parameters
     * have a negative id.
     */
    private HashMap             parameters_;


    /**
     * Write all except the parameters to the XmaOutput.
     */
    protected void externalizeExclParameter (XmaOutput o) throws IOException {
        o.writeInt ("rtversion", runtimeVersion_);
        o.writeShort ("scn", serverChangeNumber_);
        o.writeBoolean ("ynModels", pageDeltas_ != null);
        if (pageDeltas_ != null) {
            o.writeSerializedBytes("models", pageDeltas_);
        }
    }

    /**
     * Write the parameters to the ObjectOutput.
     */
    protected void externalizeParameters (XmaOutput o) throws IOException {
        short           numParameters = (parameters_ == null ? 0 : (short) parameters_.size());
        o.writeShort ("numParams", numParameters);
        if (numParameters > 0) {
            Iterator    iter = parameters_.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry e = (Map.Entry) iter.next();
                short id = ((Short)e.getKey()).shortValue();
                o.writeShort ("pId", id);
                // warning: the following line will throw IOExceptions if there is something
                // that cannot be serialized. This may be a common exception if user supplied
                // parameters are used and they are not Serializable.
                o.writeObject ("pValue", e.getValue());
            }
        }
    }

    /**
     * Reads data as written by externalizeExclParameter
     */
    protected void internalizeExclParameter (XmaInput in) throws IOException {
        runtimeVersion_ = in.readInt();
        serverChangeNumber_ = in.readShort();
        boolean             ynModels = in.readBoolean();
        if (ynModels) {
            pageDeltas_ = in.readSerializedBytes();
        }
    }

    /**
     * Reads parameters as written by externalizeParameters.
     */
    protected void internalizeParameters (XmaInput in) throws IOException, ClassNotFoundException {
        parameters_ = new HashMap();
        short           numParameters = in.readShort();
        for (int i=0; ijava.io.Serializable
     *                   and must be successfully be serialized. Setting it null removes the current
     *                   value of the given parameter.
     * @throws IllegalArgumentException if any before mentioned condition is violated.
     */
    public void setParameter (int id, Object parameter) {
        if (id < 0 || id > 127) {
            throw new IllegalArgumentException();
        }
        setParameterInternal (id, parameter);
    }

    /**
     * For package internal usage without range checks on the ids.
     */
    void setParameterInternal (int id, Object parameter) {
        if (parameters_ == null) {
            if (parameter == null) {
                return; // nothing there and nothing to do
            }
            parameters_ = new HashMap();
        }
        if (parameter == null) {
            // remove if already exists
            parameters_.remove(new Short((short)id));
            if (parameters_.isEmpty()) {
                parameters_ = null;
            }
        } else {
            parameters_.put(new Short((short)id), parameter);
        }
    }

    /**
     * Retrieves a parameter for a given id.
     *
     * @param id the id that has been used in setParameter.
     * @return the same object set via setParameter or null if there
     *          is no parameter with the given id.
     */
    public Object getParameter (int id) {
        if (parameters_ == null) {
            return null;
        }
        return parameters_.get(new Short((short)id));
    }

    /**
     * Returns the number of parameters
     */
    public int getParameterCount () {
        if (parameters_ == null) {
            return 0;
        }
        return parameters_.size();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy