[阅读: 502] 2005-10-04 11:04:11
/*
* Created on Jun 6, 2005
*
* This class provide the basic action which run on a
* certain account for a certain date. The baseline script
* will be probided by a resource bundle.
*/
package com.bnpparibas.command.action;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import com.tibco.tibrv.TibrvMsg;
import com.tibco.tibrv.TibrvException;
import com.bnpparibas.utils.rv.RvSerilizable;
/**
* @author Alex SUO
*
* This class provide an abstraction about the underlying script.
* Each action instance represents a set of script to be run for
* a certain type of task, for example, copy a file, run PNLC,
* and so on.
*
* This class has no hard-coded script. The script will be written
* in configuation files. And the available types of commands will
* also be in configuation files. When the client start, it sends
* an message to the server and the server will respond with the
* set of actions available.
*/
public abstract class Action implements RvSerilizable {
/** The action label for storing in the tibco message. */
public static String TYPE = "ACTION_TYPE";
/** The parameter label for storing in the tibco message. */
public static String PARAMS = "ACTION_PARAM";
/** The type string of the action. */
public static String type = null;
/**
* The HashMap holding all the special parameters for
* the action, if any.
*/
protected HashMap params = new HashMap();
/**
* Validate if the parameters are enough for the class.
* @return true if the parameters are enough and the
* class can be executed, false otherwise.
*/
public boolean validate() {
return true;
}
/**
* Get the type string of the action class.
* @return The type string of the action class.
*/
public abstract String getType();
/**
* Set the parameter for a key.
* @param key The key of the parameter.
* @param value The value of the parameter.
*/
public void setParam(Object key, Object value) {
params.put(key, value);
}
/**
* Get the parameter by a key.
* @param key The key of the parameter.
* @return The parameter for the key.
*/
public Object getParam(Object key) {
return params.get(key);
}
/**
* The abstract method for executing the action.
*
* Each subclass of action should implement the action behavior
* for itself. The parameters of the action should be readily
* available in the <code>params</code> HashMap.
*
* @throws Exception If any during action execution.
*/
public abstract void execute() throws Exception;
/**
* Implement the generic method for the interface.
* @param message The message to write the object in.
* @throws TibrvException If any in writing the message;
* @see RvSerilizable#rvWriteObject(TibrvMsg)
*/
public void rvWriteObject(TibrvMsg message) throws Exception {
message.add(TYPE, getType());
//write the parameters
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream(outBytes);
outStream.writeObject(params);
outStream.close();
byte array[] = outBytes.toByteArray();
message.add(PARAMS, array);
}
/**
* Implement the generic method for the interface.
* @param message The message to read the object from.
* @throws TibrvException if any in reading the message.
* @see RvSerilizable#rvReadObejct(TibrvMsg)
*/
public void rvReadObejct(TibrvMsg message) throws Exception {
byte array[] = (byte[])message.get(PARAMS);
if(array == null) {
throw new Exception("Error: PARAMS object is null.");
} else {
ByteArrayInputStream inBytes = new ByteArrayInputStream(array);
ObjectInputStream inStream = new ObjectInputStream(inBytes);
params = (HashMap)inStream.readObject();
return;
}
}
}