001: /*
002: * Created on Apr 13, 2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl.util;
008:
009: import java.io.BufferedReader;
010: import java.io.InputStreamReader;
011: import java.util.ArrayList;
012: import java.util.HashMap;
013:
014: import org.xdev.base.xssl.XSSLAction;
015: import org.xdev.base.xssl.XSSLActionIterator;
016: import org.xdev.base.xssl.XSSLReturn;
017:
018: /**
019: * @author AYegorov
020: *
021: * To change the template for this generated type comment go to
022: * Window>Preferences>Java>Code Generation>Code and Comments
023: */
024: public class ExternalCommand extends XSSLActionIterator {
025:
026: private String[] args = null;
027:
028: private String command = null;
029:
030: private int count = 0;
031:
032: private int argCount = 0;
033:
034: private Object returnObject = null;
035:
036: private ArrayList commands = new ArrayList();
037:
038: /**
039: * @param id
040: */
041: public ExternalCommand(String id) {
042: super (id);
043: }
044:
045: /**
046: * @param id
047: * @param properties
048: */
049: public ExternalCommand(String id, HashMap properties) {
050: super (id, properties);
051: }
052:
053: /* (non-Javadoc)
054: * @see org.xdev.base.xssl.XSSLActionIterator#invoke(org.xdev.base.xssl.XSSLAction)
055: */
056: protected void invoke(XSSLAction component) throws Exception {
057:
058: if (component instanceof XSSLReturn) {
059:
060: Object obj = ((XSSLReturn) component).getObjectValue();
061:
062: if (component.getBooleanProperty("is-command")) {
063: commands.add(obj);
064: } else {
065: args[count++] = obj != null ? obj.toString() : "";
066: }
067: }
068: }
069:
070: /* (non-Javadoc)
071: * @see org.xdev.base.xssl.XSSLAction#set()
072: */
073: protected void set() throws Exception {
074:
075: this .argCount = this .getCount();
076:
077: if (this .hasProperty("command")) {
078: this .command = this .getProperty("command");
079: } else {
080: XSSLReturn cmd = (XSSLReturn) this .getElement(0);
081:
082: if (cmd.execute(this )) {
083: this .command = cmd.getObjectValue().toString();
084: }
085:
086: argCount--;
087: }
088:
089: args = new String[argCount];
090: }
091:
092: public int setIterationIndex() {
093: return (this .iterationIndex = this .getCount() - this .argCount);
094: }
095:
096: protected void invoke() throws Exception {
097: super .invoke();
098:
099: Process p = Runtime.getRuntime().exec(this .command, this .args);
100:
101: BufferedReader read = new BufferedReader(new InputStreamReader(
102: p.getInputStream()));
103:
104: StringBuffer buffer = new StringBuffer();
105:
106: String line = null;
107:
108: while ((line = read.readLine()) != null) {
109: buffer.append(line + "\n");
110: }
111:
112: this .returnObject = buffer.toString();
113: }
114:
115: public Object getObjectValue() {
116: return this.returnObject;
117: }
118: }
|