从命令行控制Netbeans:从Shell脚本附加调试器

时间:2010-12-16 10:40:19

标签: netbeans netbeans-6.9 netbeans-plugins

我正在使用监控远程服务器的守护程序脚本。当远程服务器启动时,我希望Netbeans自动将其调试器连接到远程服务器。

是否可以从命令行控制此行为? 输入像

这样的东西
netbeans --attach-debugger 192.168.178.34:9009

在终端内做那个?或者我有什么方法可以访问Netbeans内部的东西? (直到现在,我只是Netbeans的“用户”所以我不知道内部以及如何很好地访问它们)

或者我必须写一个Netbeans插件才能做到这一点吗?如果是的话,你能给我一个很好的起点来添加这个功能吗?

1 个答案:

答案 0 :(得分:3)

好了,因为没有从命令行附加Debugger的选项,我在NB-mailinglist的this blog entrythis thread的帮助下写了一个Netbeans插件。现在我可以从命令行调用我的插件动作。

因此,构建一个简单的NetBeans模块,其中包含2个重要的类。 这是获取命令行参数并将它们转发给我的Action的类:

import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.swing.Action;
import org.netbeans.api.sendopts.CommandException;
import org.netbeans.spi.sendopts.Env;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.netbeans.spi.sendopts.Option;
import org.openide.ErrorManager;
import org.openide.cookies.InstanceCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.util.lookup.ServiceProvider;
import org.openide.windows.WindowManager;

@ServiceProvider(service = OptionProcessor.class)
public class TriggerActionCommandLine extends OptionProcessor {

    //Here we specify "runAction" as the new key in the command,
    //but it could be any other string you like, of course:
    private static Option action = Option.requiredArgument(Option.NO_SHORT_NAME, "debug");

    private static final Logger logger = Logger.getLogger(AttachDebugger.class.getName());

    @Override
    public Set<org.netbeans.spi.sendopts.Option> getOptions() {
        return Collections.singleton(action);
    }

    @Override
    protected void process(Env env, Map<Option, String[]> values) throws CommandException {
        final String[] args = (String[]) values.get(action);
        if (args.length > 0) {
            //Set the value to be the first argument from the command line,
            //i.e., this is "GreetAction", for example:
            final String ip = args[0];
            //Wait until the UI is constructed,
            //otherwise you will fail to retrieve your action:
            WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
                @Override
                public void run() {
                    //Then find & perform the action: 
                    Action a = findAction(AttachDebugger.ACTION_NAME);
                    // forward IP address to Action
                    ActionEvent e = new ActionEvent(this, 1, ip);
                    a.actionPerformed(e);
                }
            });
        }
    }

    public Action findAction(String actionName) {
        FileObject myActionsFolder = FileUtil.getConfigFile("Actions/PSFActions");
        FileObject[] myActionsFolderKids = myActionsFolder.getChildren();
        for (FileObject fileObject : myActionsFolderKids) {
            logger.info(fileObject.getName());
            //Probably want to make this more robust,
            //but the point is that here we find a particular Action:
            if (fileObject.getName().contains(actionName)) {
                try {
                    DataObject dob = DataObject.find(fileObject);
                    InstanceCookie ic = dob.getLookup().lookup(InstanceCookie.class);
                    if (ic != null) {
                        Object instance = ic.instanceCreate();
                        if (instance instanceof Action) {
                            Action a = (Action) instance;
                            return a;
                        }
                    }
                } catch (Exception e) {
                    ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
                    return null;
                }
            }
        }
        return null;
    }

}

这是我的插件操作,它将调试器附加到给定的远程地址:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.debugger.jpda.DebuggerStartException;
import org.netbeans.api.debugger.jpda.JPDADebugger;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.python.util.PythonInterpreter;

@ActionID(category = "PSFActions", id = "de.mackaz.AttachDebugger")
@ActionRegistration(displayName = "#CTL_AttachDebuggerAction")
@ActionReferences({
    @ActionReference(path = "Menu/Tools", position = 1800, separatorBefore = 1750, separatorAfter = 1850)
})
public final class AttachDebugger implements ActionListener {

    private static final Logger logger = Logger.getLogger(AttachDebugger.class.getName());

    public static final String ACTION_NAME="AttachDebugger";

    @Override
    public void actionPerformed(ActionEvent e) {
        String ip;
        if (!e.getActionCommand().contains("Attach Debugger")) {
            ip = e.getActionCommand();
        } else {
            ip = lookupIP();
        }
        try {
            logger.log(Level.INFO, "Attaching Debugger to IP {0}", ip);
            JPDADebugger.attach(
                    ip,
                    9009,
                    new Object[]{null});
        } catch (DebuggerStartException ex) {
            int msgType = NotifyDescriptor.ERROR_MESSAGE;
            String msg = "Failed to connect debugger to remote IP " + ip;
            NotifyDescriptor errorDescriptor = new NotifyDescriptor.Message(msg, msgType);
            DialogDisplayer.getDefault().notify(errorDescriptor);
        }
    }
}

现在我可以通过调用netbeans/bin/netbeans --debug 192.168.178.79

将Netbeans调试器附加到特定地址