at.spardat.xma.baserpc.RemoteData 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: RemoteData.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.baserpc;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import at.spardat.xma.serializer.XmaInput;
import at.spardat.xma.serializer.XmaOutput;
/**
* Models the data transferred via BaseRPCs. The main functionality in this class is
* its ability to serialize and deserialize the data.
*
* @author YSD
*/
public class RemoteData {
/**
* 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 the parameters to the XmaOutput.
*/
protected void externalize (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();
Object val = e.getValue();
boolean valIsCollection = (val instanceof UnserializableCollection);
// write the id of the parameter
o.writeShort("idParam", id);
// write the fact if the parameter is a collection
o.writeBoolean("isColl", valIsCollection);
if (valIsCollection) {
// write number of objects contained in the collection
Collection coll = ((UnserializableCollection)val).fCollection;
o.writeShort ("collSize", (short)coll.size());
Iterator iter2 = coll.iterator();
while (iter2.hasNext()) {
Object el = (Object) iter2.next();
// for each object, first write boolean if the object is null
o.writeBoolean ("isNull", el == null);
if (el != null) {
// if not null, write the object
o.writeObject ("obj", el);
}
}
} else {
o.writeObject("obj", e.getValue());
}
}
}
}
/**
* Reads parameters as written by externalizeParameters.
*/
protected void internalize (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. It cannot be null.
* @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);
}
/**
* Like setParameter, except that the provided collection is not
* serialized itself. Instead, only the elements of the collection are
* serialized. The corresponding get-call (getUnserializableCollection)
* must be used to retrieve the collection.
*/
public void setUnserializableCollection (int id, Collection parameter) {
setParameter (id, new UnserializableCollection (parameter));
}
/**
* For package internal usage without range checks on the ids.
*/
void setParameterInternal (int id, Object parameter) {
if (parameter == null) throw new IllegalArgumentException();
if (parameters_ == null) parameters_ = new HashMap();
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));
}
/**
* Retrieves a parameter that has been set via setUnserializableCollection. Please
* note that the type returned need not be the one provided via setUnserializableCollection.
* In fact, you cannot make any assumptions regarding the returned type beyond beeing a
* Collection.
*
* @return null if no such parameter
*/
public Collection getUnserializableCollection (int id) {
Object par = getParameter (id);
if (par == null) return null;
if (!(par instanceof UnserializableCollection)) return null;
return ((UnserializableCollection)par).fCollection;
}
/**
* Returns the number of parameters
*/
public int getParameterCount () {
if (parameters_ == null) return 0;
return parameters_.size();
}
/**
* Class to wrap a Collection to signal that it should not be treated conventially
* in the serialization process, but unfolded.
*/
private static class UnserializableCollection {
public UnserializableCollection (Collection c) {
fCollection = c;
}
public Collection fCollection;
}
}