package org.jboss.fresh.shell.commands;
import org.apache.log4j.Logger;
import org.jboss.fresh.io.BufferObjectReader;
import org.jboss.fresh.io.BufferObjectWriter;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.StringTokenizer;
/**
* @author ales.justin@jboss.org
*/
public abstract class AbstractInvokeExe extends AbstractExecutable {
protected abstract Logger getLog();
protected abstract String getCmd();
// cat [PATHS]
public void process(String exename, String[] params) throws Exception {
getLog().debug("entered");
if (helpRequested()) {
PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
out.println("Usage: " + getCmd() + " [-ex] [-notx] <service name> <method sig>");
out.println(" --help : this help");
out.close();
getLog().debug("done");
return;
}
// zaklju�imo transakcijo �e je za�eta
// get mbean name
// get method
PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
if (params.length < 2) {
if (canThrowEx()) {
throw new Exception("Wrong number of parameters. Usage: " + getCmd() + " [-notx] <service name> <method sig>");
} else {
printUsage(out);
return;
}
}
String mbeanName = params[0];
String method = params[1];
boolean notx = false;
boolean wasActive = false;
if (params[0].equals("-notx")) {
notx = true;
mbeanName = params[1];
if (params.length < 3) {
if (canThrowEx()) {
throw new Exception("Wrong number of parameters. Usage: " + getCmd() + " [-notx] <srevice name> <method sig>");
} else {
printUsage(out);
return;
}
}
method = params[2];
}
String methodName;
String[] sig = null;
Object[] vals = null;
int pos = method.indexOf("(");
if (pos >= 0) {
methodName = method.substring(0, pos);
int epos = method.lastIndexOf(")");
String prs;
if (epos != -1) {
prs = method.substring(pos + 1, epos);
} else {
prs = method.substring(pos + 1);
}
LinkedList l = new LinkedList();
StringTokenizer st = new StringTokenizer(prs, ",");
while (st.hasMoreTokens()) {
l.add(st.nextToken());
}
sig = new String[l.size()];
Iterator it = l.iterator();
for (int i = 0; i < sig.length && it.hasNext(); i++) {
sig[i] = (String) it.next();
}
vals = new Object[sig.length];
} else {
methodName = method;
}
BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
BufferObjectReader oin = new BufferObjectReader(getStdIn());
if (vals != null) {
for (int i = 0; i < vals.length; i++) {
if (!oin.isFinished())
vals[i] = oin.readObject();
else if (canThrowEx()) {
throw new Exception("Not enough input objects on stdin.");
} else {
out.println("Not enough input objects on stdin.");
return;
}
}
}
/*
if (notx) {
// if in tx, do this in another thread - through remote
// or
if (TxSupport.isActive()) {
wasActive = true;
TxSupport.commit();
}
} */
try {
invoke(out, mbeanName, methodName, sig, vals, oout);
}
finally {
/* if (wasActive && !TxSupport.isActive()) {
TxSupport.begin();
} */
}
oout.close();
oin.close();
getLog().debug("done");
}
protected abstract void invoke(PrintWriter out, String mbeanName, String methodName, String[] sig, Object[] vals, BufferObjectWriter oout) throws Exception;
private void printUsage(PrintWriter pout) {
pout.println(" Usage: " + getCmd() + " [-notx] <service name> <method sig>");
pout.println();
}
}