remove ptql shell command

This commit is contained in:
Doug MacEachern 2007-04-16 01:14:56 +00:00
parent 2bb854483f
commit 3ffa2335c3
5 changed files with 1 additions and 354 deletions

View File

@ -1,192 +0,0 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.sigar.cmd;
import java.util.ArrayList;
import java.util.Map;
import java.lang.reflect.Method;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.util.GetlineCompleter;
import org.hyperic.sigar.ptql.ProcessQueryBuilder;
import org.hyperic.sigar.shell.CollectionCompleter;
import org.hyperic.sigar.shell.MultiwordShellCommand;
import org.hyperic.sigar.shell.ShellBase;
import org.hyperic.sigar.shell.ShellCommandExecException;
import org.hyperic.sigar.shell.ShellCommandInitException;
import org.hyperic.sigar.shell.ShellCommandHandler;
import org.hyperic.sigar.shell.ShellCommandUsageException;
/**
* Run process table queries.
* @see org.hyperic.sigar.ptql.ProcessQueryBuilder
*/
public class PTQL
extends MultiwordShellCommand
implements GetlineCompleter {
private Shell shell;
private Ps ps;
private GetlineCompleter m_completer;
private Map methods;
private PTQL() { }
public PTQL(Shell shell) {
this.shell = shell;
this.ps = new Ps(this.shell);
this.methods = ProcessQueryBuilder.getMethods();
this.m_completer =
new CollectionCompleter(shell, methods.keySet());
}
public String complete(String line) {
int ix = line.indexOf(".");
if (ix == -1) {
line = this.m_completer.complete(line);
if (!line.endsWith(".")) {
if (this.methods.get(line) != null) {
return line + ".";
}
}
return line;
}
String attrClass = line.substring(0, ix);
String attr = line.substring(ix+1, line.length());
Method method = (Method)this.methods.get(attrClass);
if (method == null) {
return line;
}
Class subtype = method.getReturnType();
boolean isSigarClass = ProcessQueryBuilder.isSigarClass(subtype);
int ix2 = attr.indexOf(".");
if (ix2 != -1) {
method = null;
String op = attr.substring(ix2+1, attr.length());
attr = attr.substring(0, ix2);
if (isSigarClass) {
try {
method =
subtype.getMethod("get" + attr,
ProcessQueryBuilder.NOPARAM);
} catch (NoSuchMethodException e) { }
}
final Method m = method;
GetlineCompleter completer =
new CollectionCompleter(this.shell,
ProcessQueryBuilder.
getMethodOpNames(m));
String partial = completer.complete(op);
String result = attrClass + "." + attr + "." + partial;
if (partial.length() == 2) {
result += "=";
}
return result;
}
if (isSigarClass) {
final ArrayList possible = new ArrayList();
Method[] submethods = subtype.getDeclaredMethods();
for (int i=0; i<submethods.length; i++) {
Method m = submethods[i];
if (m.getName().startsWith("get")) {
possible.add(m.getName().substring(3));
}
}
GetlineCompleter completer =
new CollectionCompleter(this.shell, possible);
String partial = completer.complete(attr);
String result = attrClass + "." + partial;
if (possible.contains(partial)) {
result += ".";
}
return result;
}
return line;
}
public void init(String commandName, ShellBase shell)
throws ShellCommandInitException
{
ShellCommandHandler handler;
super.init(commandName, shell);
handler = new ProcessQueryGenerate(this.shell);
registerSubHandler("generate", handler);
}
public String getSyntaxArgs() {
return "<query>";
}
public String getUsageShort() {
return "Run process table query";
}
public void processCommand(String[] args)
throws ShellCommandUsageException, ShellCommandExecException
{
if (args.length > 0) {
if (getSubHandler(args[0]) != null) {
super.processCommand(args);
return;
}
}
if (args.length != 1) {
throw new ShellCommandUsageException(getSyntax());
}
long[] pids;
try {
pids = this.shell.findPids(args);
} catch (NumberFormatException e) {
throw new ShellCommandUsageException(getSyntax());
} catch (SigarException e) {
throw new ShellCommandExecException(e.getMessage());
}
for (int i=0; i<pids.length; i++) {
try {
this.ps.output(pids[i]);
} catch (SigarException e) {
throw new ShellCommandExecException(e.getMessage());
}
}
this.ps.flush();
}
}

View File

@ -1,73 +0,0 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.sigar.cmd;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.ptql.ProcessQueryGenerator;
public class ProcessQueryGenerate extends SigarCommandBase {
public ProcessQueryGenerate(Shell shell) {
super(shell);
}
public ProcessQueryGenerate() {
super();
}
public boolean validateArgs(String[] args) {
return true;
}
public void output(String[] args) throws SigarException {
ProcessQueryGenerator generator =
new ProcessQueryGenerator(this.proxy);
long[] pids;
if (args.length > 0) {
pids = this.shell.findPids(args);
}
else {
pids = this.proxy.getProcList();
}
for (int i=0; i<pids.length; i++) {
long pid = pids[i];
String query = generator.generate(pid);
if (query != null) {
println(query);
}
else {
this.err.println("failed to narrow query for " + pid +
" (" +
this.proxy.getProcState(pid).getName() +
")");
}
}
flush();
}
public static void main(String[] args) throws Exception {
new ProcessQueryGenerate().processCommand(args);
}
}

View File

@ -109,10 +109,6 @@ public class Shell extends ShellBase {
//requires junit.jar
registerCommandHandler("test", new SigarTestRunner(this));
} catch (NoClassDefFoundError e) { }
try {
//requires bcel-5.1.jar
registerCommandHandler("ptql", new PTQL(this));
} catch (NoClassDefFoundError e) { }
}
public void processCommand(ShellCommandHandler handler, String args[])

View File

@ -205,8 +205,7 @@ public abstract class SigarCommandBase
return line;
}
return
((GetlineCompleter)this.shell.getHandler("ptql")).complete(line);
return line; //XXX bring back ptql completion
}
public String complete(String line) {

View File

@ -1,83 +0,0 @@
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.hyperic.sigar.ptql;
import org.hyperic.sigar.ProcCred;
import org.hyperic.sigar.ProcState;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarProxy;
public class ProcessQueryGenerator {
private ProcessFinder finder;
private SigarProxy sigar;
public ProcessQueryGenerator(SigarProxy sigar) {
this.sigar = sigar;
this.finder = new ProcessFinder(sigar);
}
public String generate(long pid)
throws SigarException {
StringBuffer query = new StringBuffer();
ProcState state = sigar.getProcState(pid);
query.append("State.Name.eq=" + state.getName());
if (this.finder.find(query).length == 1) {
return query.toString();
}
try {
ProcCred cred = sigar.getProcCred(pid);
query.append(",").append("Cred.Uid.eq=" + cred.getUid());
query.append(",").append("Cred.Gid.eq=" + cred.getGid());
if (this.finder.find(query).length == 1) {
return query.toString();
}
} catch (SigarException e) {
}
try {
String[] args = sigar.getProcArgs(pid);
for (int i=args.length-1; i>=0; i--) {
int j;
//common case for java apps, last arg is the classname
//use -1 for query since number of args may change,
//but last arg is always the classname.
if (i == args.length-1) {
j = -1;
}
else {
j = i;
}
query.append(",").append("Args." + j + ".eq=" + args[i]);
if (this.finder.find(query).length == 1) {
return query.toString();
}
}
} catch (SigarException e) {
}
return null;
}
}