Search in sources :

Example 1 with ICommand

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);
        }
    }
}
Also used : PluginManager(com.ibm.java.diagnostics.utils.plugins.PluginManager) DTFJPluginManager(com.ibm.java.diagnostics.utils.plugins.DTFJPluginManager) PluginConfig(com.ibm.java.diagnostics.utils.plugins.PluginConfig) ICommand(com.ibm.java.diagnostics.utils.commands.ICommand) DTFJPluginManager(com.ibm.java.diagnostics.utils.plugins.DTFJPluginManager) LocalPriorityClassloader(com.ibm.java.diagnostics.utils.plugins.LocalPriorityClassloader) ArrayList(java.util.ArrayList) CommandException(com.ibm.java.diagnostics.utils.commands.CommandException)

Example 2 with ICommand

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);
    }
}
Also used : BaseCommand(com.ibm.java.diagnostics.utils.commands.BaseCommand) ICommand(com.ibm.java.diagnostics.utils.commands.ICommand) CommandException(com.ibm.java.diagnostics.utils.commands.CommandException)

Example 3 with ICommand

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);
    }
}
Also used : TreeSet(java.util.TreeSet) ICommand(com.ibm.java.diagnostics.utils.commands.ICommand)

Example 4 with ICommand

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;
}
Also used : ICommand(com.ibm.java.diagnostics.utils.commands.ICommand) Method(java.lang.reflect.Method) BaseJdmpviewCommand(com.ibm.jvm.dtfjview.commands.BaseJdmpviewCommand) URISyntaxException(java.net.URISyntaxException) CommandException(com.ibm.java.diagnostics.utils.commands.CommandException) ParseException(java.text.ParseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CorruptDataException(com.ibm.dtfj.image.CorruptDataException)

Aggregations

ICommand (com.ibm.java.diagnostics.utils.commands.ICommand)4 CommandException (com.ibm.java.diagnostics.utils.commands.CommandException)3 CorruptDataException (com.ibm.dtfj.image.CorruptDataException)1 BaseCommand (com.ibm.java.diagnostics.utils.commands.BaseCommand)1 DTFJPluginManager (com.ibm.java.diagnostics.utils.plugins.DTFJPluginManager)1 LocalPriorityClassloader (com.ibm.java.diagnostics.utils.plugins.LocalPriorityClassloader)1 PluginConfig (com.ibm.java.diagnostics.utils.plugins.PluginConfig)1 PluginManager (com.ibm.java.diagnostics.utils.plugins.PluginManager)1 BaseJdmpviewCommand (com.ibm.jvm.dtfjview.commands.BaseJdmpviewCommand)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1