Show LiteDescription.java syntax highlighted
/*
* Copyright (c) 2005
* Helsinki Institute of Physics
* see LICENSE file for details
*
* LiteDescription.java
* Created on Nov 14, 2003
*/
package fi.hip.gb.midlet.core;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import fi.hip.gb.bluetooth.util.Properties;
/**
* Stores the work description and knows how to
* serialize it.
*
* @author Juho Karppinen
*/
public class LiteDescription {
/** ID of the job */
private Long jobID = new Long(-1);
/** ID of the parent job */
private Long parentID = null;
/** name of the job */
private String jobName;
/** URL of the server, empty for automatic scheduling */
private String serviceURL;
/** URL of the JAR file with jar-scheme */
private String jarURL;
/** name of the agent class to be executed */
private String jobClass;
/** name of the method to be executed */
private String jobMethod;
/** sub descriptions (LiteDescription objects) */
private Vector subs = new Vector();
/** parameters */
private Vector parameters = new Vector();
/** flags key and value pairs */
private Properties flags = new Properties();
/** attachments name and value pairs*/
private Vector attachments = new Vector();
public LiteDescription(Long jobID) {
this.jobID = jobID;
}
/**
* @param jobID id of the job
* @param jobName the familiar name of the job
* @param serviceURL target for the job
* @param jarURL URL of the JAR file containing agent class, if
* not using jar-syntax it will be converted automatically
* @param jobClass name of the agent class
* @param jobMethod name of the method
*/
public LiteDescription(Long jobID,
String jobName,
String serviceURL,
String jarURL,
String jobClass,
String jobMethod) {
this(jobID);
this.jobName = jobName;
this.serviceURL = serviceURL;
// convert to JAR url
if(! jarURL.toLowerCase().startsWith("jar:")) {
jarURL = "jar:" + jarURL;
}
if(! jarURL.toLowerCase().endsWith("!/")) {
jarURL += "!/";
}
this.jarURL = jarURL;
this.jobClass = jobClass;
this.jobMethod = jobMethod;
}
/**
* Constructs a new sub-element.
* @param serviceURL target service
* @param methodName name of the method
*/
public LiteDescription(String serviceURL, String methodName) {
this.jobID = new Long(new Random().nextLong());
this.jobName = "";
this.serviceURL = serviceURL;
this.jarURL = "";
this.jobClass = "";
this.jobMethod = methodName;
}
/**
* Deserializer the description from byte array. This is
* reverse operation to {@link LiteDescription#serialize()}.
*
* @param data array of data arrays
*/
public LiteDescription(byte[] data) {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
try {
byte[][] array = new byte[dis.readInt()][];
for (int i = 0; i < data.length; i++) {
int size = dis.readInt();
dis.read(array[i], 0, size);
}
this.deserialize(array);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Deserializer the description from byte arrays. This is
* reverse operation to {@link LiteDescription#serialize(int)}.
*
* @param data array of data arrays
*/
public LiteDescription(byte[][] data) {
this.deserialize(data);
}
/**
* Gets the job ID
* @return ID of the job
*/
public Long getJobID() {
return this.jobID;
}
/**
* Sets the job ID
* @param jobID ID for the job
*/
public void setJobID(Long jobID) {
this.jobID = jobID;
}
/**
* Gets the parent ID.
* @return ID of the parent description
*/
public Long getParentID() {
return this.parentID;
}
/**
* Sets the parent ID.
* @param parentID ID of the parent description
*/
public void setParentID(Long parentID) {
this.parentID = parentID;
}
/**
* Sets the job name
* @param jobName name for the job
*/
public void setJobname(String jobName) {
this.jobName = jobName;
}
/**
* Gets the job name
* @return the name of the job
*/
public String getJobname() {
return this.jobName;
}
/**
* Gets the location of the service
* @return URL for the service
*/
public String getServiceURL() {
return this.serviceURL;
}
/**
* Sets the location of the service
* @param serviceURL URL for the service
*/
public void setServiceURL(String serviceURL) {
this.serviceURL = serviceURL;
}
/**
* Gets the location of the JAR file
* @return URL for the job jar file
*/
public String getJarURL() {
return this.jarURL;
}
/**
* Sets the location of the JAR file
* @param jarURL URL for the job jar file
*/
public void setJarURL(String jarURL) {
this.jarURL = jarURL;
}
/**
* Gets the classname
* @return name of the class
*/
public String getClassname() {
return this.jobClass;
}
/**
* Gets the method name
* @return name of the method
*/
public String getMethodname() {
return this.jobMethod;
}
/**
* Sets the method name
* @param methodName the name of the method
*/
public void setMethodname(String methodName) {
this.jobMethod = methodName;
}
/**
* Adds a service to the list of target servers.
* @param service complete service URL
*/
public void addSubElements(LiteDescription service) {
this.subs.addElement(service);
}
/**
* Gets the location of services.
* @return URL for the server
*/
public LiteDescription[] getSubElements() {
LiteDescription[] subArray = new LiteDescription[this.subs.size()];
for (int i = 0; i < subArray.length; i++) {
subArray[i] = (LiteDescription) this.subs.elementAt(i);
}
return subArray;
}
/**
* Adds parameter to the method call.
*
* @param parameter parameter
*/
public void addParameter(String parameter) {
parameters.addElement(parameter.getBytes());
}
/**
* Gets all parameters in this description
* @return an array containing all parameters
*/
public String[] getParameters() {
String[] parameterArray = new String[this.parameters.size()];
for(int i=0; i < parameterArray.length; i++) {
parameterArray[i] = new String((byte[])parameters.elementAt(i));
}
return parameterArray;
}
/**
* Sets the flags of this description.
* @param properties key/value pairs
* @return properties containing all key/value pairs, cannot be null.
*/
public void setFlags(Properties properties) {
this.flags = properties;
}
/**
* Gets the flags of this description. If no flags
* available, returns empty Properies object.
* @return properties containing all key/value pairs.
*/
public Properties getFlags() {
return this.flags;
}
/**
* Adds attachement, can be called as many times
* as wanted.
*
* @param filename name of the attachment
* @param data data of the attachment
*/
public void addAttachment(String filename, byte[] data) {
attachments.addElement(filename.getBytes());
attachments.addElement(data);
}
/**
* Gets the count of items on the description.
* Also used to clear the iterators, so after calling
* this serialize(index) starts over.
*
* @return number of elements
*/
public int getItems() {
this.flags.resetEnum();
return 6 + 3
+ this.subs.size()
+ this.parameters.size()
+ this.flags.size()*2
+ this.attachments.size();
}
public byte[] serialize() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
int count = this.getItems();
dos.writeInt(count);
for (int i = 0; i < count; i++) {
byte[] data = this.serialize(i);
dos.writeInt(data.length);
dos.write(data);
}
dos.close();
return baos.toByteArray();
} catch (Exception e) {
System.out.println("Failed to serialize wds: " + e.getMessage());
}
return null;
}
/**
* Serialize one element at the time
* @param index index which will be serialized
* @return byte array containg the data
*/
public byte[] serialize(int index) {
int counterStart = 6;
int subStart = counterStart + 3;
int paramStart = subStart + this.subs.size();
int flagStart = paramStart + this.parameters.size();
int attachmentStart = flagStart + this.flags.size()*2;
if(index == 0) {
String id = this.jobID.toString();
if(parentID != null)
id = parentID.toString() + "/" + id;
return id.getBytes();
} else if(index == 1) {
return this.jobName.getBytes();
} else if(index == 2) {
return this.serviceURL.getBytes();
} else if(index == 3) {
return this.jarURL.getBytes();
} else if(index == 4) {
return this.jobClass.getBytes();
} else if(index == 5) {
return this.jobMethod.getBytes();
} else if(index == counterStart) {
// number of sub elements
return Integer.toString(this.subs.size()).getBytes();
} else if(index == counterStart+1) {
// number of parameters
return Integer.toString(this.parameters.size()).getBytes();
} else if(index == counterStart+2) {
// number of flags
return Integer.toString(this.flags.size()).getBytes();
} else if(index < paramStart) {
// subelements
LiteDescription sub = (LiteDescription) this.subs.elementAt(index-subStart);
return sub.getMethodname().getBytes();
} else if(index < flagStart) {
// parameters
return (byte[]) this.parameters.elementAt(index-paramStart);
} else if(index < attachmentStart) {
// odd or even
System.out.println("flag " + (index-flagStart)
+ " even " + ((index-flagStart)/2*2 == (index-flagStart)));
if((index-flagStart)/2*2 == (index-flagStart))
return this.flags.nextKey().getBytes();
else
return this.flags.nextValue().getBytes();
} else {
// attachments
return (byte[]) this.attachments.elementAt(index - attachmentStart);
}
}
private void deserialize(byte[][] data) {
int index = 0;
String id = new String(data[index++]);
if(id.indexOf('/') != -1) {
// we have parent ID as well
this.parentID = new Long(Long.parseLong(id.substring(0, id.indexOf('/'))));
id = id.substring(id.indexOf('/')+1);
}
this.jobID = new Long(Long.parseLong(id));
this.jobName = new String(data[index++]);
this.serviceURL = new String(data[index++]);
this.jarURL = new String(data[index++]);
this.jobClass = new String(data[index++]);
this.jobMethod = new String(data[index++]);
int subCount = Integer.parseInt(new String(data[index++]));
int parameterCount = Integer.parseInt(new String(data[index++]));
int flagCount = Integer.parseInt(new String(data[index++]));
for(int i=0; i < subCount; i++) {
System.out.println("submethod " + new String(data[index + i]));
LiteDescription sub = new LiteDescription("", new String(data[index + i]));
this.subs.addElement(sub);
}
for(int i=0; i < parameterCount; i++) {
System.out.println("param " + new String(data[index + subCount + i]));
addParameter(new String(data[index + subCount + i]));
}
for(int i=0; i < flagCount; i++) {
String key = new String(data[index + subCount + parameterCount + i*2]);
String value = new String(data[index + 1 + subCount + parameterCount + i*2]);
this.flags.setProperty(key, value);
}
}
/**
* Print information inside description
* @return all information inside description
*/
public String toString() {
StringBuffer sb = new StringBuffer();
LiteDescription[] subArray = getSubElements();
for (int i = 0; i < subArray.length; i++) {
sb.append("sub[" + i + "]={" + subArray[i] + "}");
}
sb.append("jobname=" + getJobname());
sb.append(" class=" + getClassname());
sb.append(" method=" + getMethodname());
String[] paramArray = getParameters();
for (int i = 0; i < paramArray.length; i++) {
sb.append(" parameter[" + i + "]=" + paramArray[i]);
}
sb.append(" flags: " + flags.toString());
return sb.toString();
}
}
See more files for this project here