From 6d5788dfb7b3dec227837fca4ed38c13799f83ca Mon Sep 17 00:00:00 2001 From: kohsuke Date: Tue, 2 Sep 2008 00:35:39 +0000 Subject: [PATCH] using args4j to better support options git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11920 71c3de6d-444a-0410-be80-ed276b4c234a --- jnlp-agent/pom.xml | 25 +++ .../src/main/java/hudson/jnlp/Main.java | 147 ++++++++++++------ 2 files changed, 123 insertions(+), 49 deletions(-) diff --git a/jnlp-agent/pom.xml b/jnlp-agent/pom.xml index 057d043e1e..829017cfd2 100644 --- a/jnlp-agent/pom.xml +++ b/jnlp-agent/pom.xml @@ -35,6 +35,25 @@ + + org.jvnet.maven-antrun-extended-plugin + maven-antrun-extended-plugin + + + + process-classes + + run + + + + + + + + + + @@ -45,5 +64,11 @@ ${project.version} compile + + args4j + args4j + 2.0.9 + provided + \ No newline at end of file diff --git a/jnlp-agent/src/main/java/hudson/jnlp/Main.java b/jnlp-agent/src/main/java/hudson/jnlp/Main.java index 3b78c8e955..2bb1ae030d 100644 --- a/jnlp-agent/src/main/java/hudson/jnlp/Main.java +++ b/jnlp-agent/src/main/java/hudson/jnlp/Main.java @@ -1,16 +1,42 @@ package hudson.jnlp; +import org.kohsuke.args4j.Option; +import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.CmdLineException; + import javax.swing.SwingUtilities; import javax.swing.JOptionPane; import java.io.StringWriter; import java.io.PrintWriter; import java.util.logging.Logger; +import java.util.List; +import java.util.ArrayList; /** + * Entry point to JNLP slave agent. + * * @author Kohsuke Kawaguchi */ public class Main { - private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); + + @Option(name="-tunnel",metaVar="HOST:PORT", + usage="Connect to the specified host and port, instead of connecting directly to Hudson." + + "Useful when connection to Hudson needs to be tunneled.") + public String tunnel; + + @Option(name="-headless", + usage="Run in headless mode, without GUI") + public boolean headlessMode = Boolean.getBoolean("hudson.agent.headless") + || Boolean.getBoolean("hudson.webstart.headless"); + + /** + * 4 mandatory parameters. + * Host name (deprecated), Hudson URL, secret key, and slave name. + */ + @Argument + public final List args = new ArrayList(); + public static void main(String[] args) { // see http://forum.java.sun.com/thread.jspa?threadID=706976&tstart=0 // not sure if this is the cause, but attempting to fix @@ -18,57 +44,80 @@ public class Main { // by overwriting the security manager. System.setSecurityManager(null); - boolean headlessMode = Boolean.getBoolean("hudson.agent.headless") - || Boolean.getBoolean("hudson.webstart.headless"); - - if (headlessMode) { - mainHeadless(args); - } else { - mainGui(args); + Main m = new Main(); + CmdLineParser p = new CmdLineParser(m); + try { + p.parseArgument(); + if(m.args.size()!=4) + throw new CmdLineException("four arguments required"); + } catch (CmdLineException e) { + System.err.println(e.getMessage()); + System.err.println("java -jar jnlp-agent.jar [options...] "); + p.printUsage(System.err); + return; } + + m.main(); } - private static void mainGui(String[] args) { - GUI.setUILookAndFeel(); - final MainDialog frame = new MainDialog(); - frame.setVisible(true); - - Engine engine = new Engine(new Listener() { - public void status(final String msg) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - frame.status(msg); - } - }); - } - - public void error(final Throwable t) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - StringWriter sw = new StringWriter(); - t.printStackTrace(new PrintWriter(sw)); - JOptionPane.showMessageDialog( - frame,sw.toString(),"Error", - JOptionPane.ERROR_MESSAGE); - System.exit(-1); - } - }); - } - }, args[0], args[1], args[2], args[3]); + + public void main() { + Engine engine = new Engine( + headlessMode ? new CuiListener() : new GuiListener(), + args.get(0), args.get(1), args.get(2), args.get(3)); engine.start(); } - - public static void mainHeadless(String[] args) { - LOGGER.info("Hudson agent is running in headless mode."); - Engine engine = new Engine(new Listener() { - public void status(final String msg) { - LOGGER.info(msg); - } - - public void error(final Throwable t) { - LOGGER.severe(t.getMessage()); - System.exit(-1); - } - }, args[0], args[1], args[2], args[3]); - engine.start(); + + /** + * {@link Listener} implementation that shows GUI. + */ + private static final class GuiListener implements Listener { + private final MainDialog frame; + + public GuiListener() { + GUI.setUILookAndFeel(); + frame = new MainDialog(); + frame.setVisible(true); + } + + public void status(final String msg) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + frame.status(msg); + } + }); + } + + public void error(final Throwable t) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + StringWriter sw = new StringWriter(); + t.printStackTrace(new PrintWriter(sw)); + JOptionPane.showMessageDialog( + frame,sw.toString(),"Error", + JOptionPane.ERROR_MESSAGE); + System.exit(-1); + } + }); + } } + + /** + * {@link Listener} implementation that sends output to {@link Logger}. + */ + private static final class CuiListener implements Listener { + private CuiListener() { + LOGGER.info("Hudson agent is running in headless mode."); + } + + public void status(final String msg) { + LOGGER.info(msg); + } + + public void error(final Throwable t) { + LOGGER.severe(t.getMessage()); + System.exit(-1); + } + } + + private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); } -- GitLab