use of com.ibm.java.diagnostics.utils.commands.ICommand in project openj9 by eclipse.
the class DTFJContext method refresh.
/**
* Refresh this context.
*/
public void refresh() {
// used to detect command name clashes
ArrayList<String> names = new ArrayList<String>();
commands.clear();
loadedPlugins.clear();
failedPlugins.clear();
// these are the only hardwired commands that are always available within any context
addGlobalCommandsToContext();
PluginManager pluginManager = PluginManagerLocator.getManager();
DTFJPluginManager manager = new DTFJPluginManager(pluginManager);
try {
pluginManager.refreshSearchPath();
pluginManager.scanForClassFiles();
} catch (CommandException e) {
// may catch a CommandException when loading plugins
logger.log(Level.FINE, "Problem loading DTFJ View plugins: " + e.getMessage());
logger.log(Level.FINEST, "Problem loading DTFJ View plugins: ", e);
}
ClassLoader thisloader = this.getClass().getClassLoader();
if (thisloader == null) {
thisloader = ClassLoader.getSystemClassLoader();
}
LocalPriorityClassloader loader = new LocalPriorityClassloader(pluginManager.getClasspath(), thisloader);
for (PluginConfig config : manager.getPlugins(apiLevel, hasImage, (rt != null))) {
try {
ICommand cmd = config.newInstance(loader);
loadedPlugins.add(config);
for (String cmdname : cmd.getCommandNames()) {
// check for a name space clash i.e. two plugins could potentially be invoked by a single command
if (names.contains(cmdname)) {
logger.fine("Name space clash detected for " + config.getClassName() + ". The command name " + cmdname + " is already in use");
} else {
names.add(cmdname);
}
}
commands.add(cmd);
} catch (CommandException e) {
failedPlugins.add(config);
logger.log(Level.FINE, "Failed to create a new plugin instance for " + config.getClassName());
logger.log(Level.FINEST, "Failed to create a new plugin instance: ", e);
}
}
}
use of com.ibm.java.diagnostics.utils.commands.ICommand in project openj9 by eclipse.
the class PluginConfig method newInstance.
public ICommand newInstance(ClassLoader loader) throws CommandException {
ClassInfo info = entry.getData();
try {
Class<?> clazz = Class.forName(info.getClassname(), true, loader);
Object instance = clazz.newInstance();
if (instance instanceof BaseCommand) {
((BaseCommand) instance).setConfig(this);
}
return (ICommand) instance;
} catch (Throwable e) {
// have to use Throwable here to catch things like class not found errors
t = e;
throw new CommandException("Failed to create a new instance of command " + info.getClassname(), e);
}
}
use of com.ibm.java.diagnostics.utils.commands.ICommand in project openj9 by eclipse.
the class HelpCommand method printHelpSummary.
private void printHelpSummary() {
SortedSet<String> helpTable = new TreeSet<String>();
String contextHelp = String.format(COMMAND_FORMAT, "context", "[ID|asid ID]", "switch to the selected context");
helpTable.add(contextHelp);
for (ICommand thisCommand : ctx.getCommands()) {
for (String thisEntry : thisCommand.getCommandDescriptions()) {
if (!thisEntry.endsWith("\n")) {
thisEntry += "\n";
}
helpTable.add(thisEntry);
}
}
for (String entry : helpTable) {
out.print(entry);
}
}
use of com.ibm.java.diagnostics.utils.commands.ICommand in project openj9 by eclipse.
the class CombinedContext method initJdmpviewCommand.
/*
* Commands / plugins that inherit from the BaseJdmpviewCommand class need to have
* their initialisation methods fired, which validates the context type and sets
* up various instance level fields. Commands that directly implement the DTFJ
* plugin interfaces will be ignored.
*/
private boolean initJdmpviewCommand(CommandParser parser, PrintStream out) throws CommandException {
for (ICommand command : commands) {
if (command.recognises(parser.getCommand(), this)) {
try {
/*
* This work-around is needed because the BaseJdmpviewCommand is loaded in both the
* CombinedContext classloader and in the classloader which is running the command. This
* means that a simple instanceof check will not work
* i.e. (command instanceof BaseJdmpviewCommand) fails as it's comparing the base command from
* two different classloaders. Solution is to use the same classloader as the command
* to get the BaseJdmpviewCommand class and then use reflection to invoke it.
*/
Class<?> base = command.getClass().getClassLoader().loadClass(BaseJdmpviewCommand.class.getName());
if (base.isAssignableFrom(command.getClass())) {
Method method = base.getMethod("initCommand", new Class<?>[] { String.class, String[].class, IContext.class, PrintStream.class });
Object result = method.invoke(command, parser.getCommand(), parser.getArguments(), this, out);
return (Boolean) result;
}
} catch (Exception e) {
out.println("Error initialising command : " + parser.getOriginalLine() + " (" + e.getMessage() + ")");
logger.log(Level.FINE, "Error initialising command : " + parser.getOriginalLine(), e);
}
}
}
return false;
}
Aggregations