提交 6d5788df 编写于 作者: K kohsuke

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
上级 41ee4ea9
......@@ -35,6 +35,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
<artifactId>maven-antrun-extended-plugin</artifactId>
<executions>
<execution>
<!-- rejar args4j contents -->
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<resolveArtifact artifactId="args4j" property="args4j.jar" />
<unjar src="${args4j.jar}" dest="target/classes" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
......@@ -45,5 +64,11 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
<version>2.0.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
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<String> args = new ArrayList<String>();
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...] <host> <hudson URL> <secret key> <slave name>");
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());
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册