aboutsummaryrefslogtreecommitdiff
path: root/src/org/gnunet/util/Program.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/gnunet/util/Program.java')
-rw-r--r--src/org/gnunet/util/Program.java48
1 files changed, 33 insertions, 15 deletions
diff --git a/src/org/gnunet/util/Program.java b/src/org/gnunet/util/Program.java
index a5cd147..1c7f1b9 100644
--- a/src/org/gnunet/util/Program.java
+++ b/src/org/gnunet/util/Program.java
@@ -21,8 +21,8 @@
21package org.gnunet.util; 21package org.gnunet.util;
22 22
23import org.apache.log4j.*; 23import org.apache.log4j.*;
24import org.gnunet.util.getopt.Option; 24import org.gnunet.util.getopt.Argument;
25import org.gnunet.util.getopt.OptionAction; 25import org.gnunet.util.getopt.ArgumentAction;
26import org.gnunet.util.getopt.Parser; 26import org.gnunet.util.getopt.Parser;
27import org.slf4j.Logger; 27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory; 28import org.slf4j.LoggerFactory;
@@ -33,6 +33,8 @@ import java.io.IOException;
33/** 33/**
34 * Program is the entry point class for everything that uses gnunet services or APIs. 34 * Program is the entry point class for everything that uses gnunet services or APIs.
35 * 35 *
36 * Also specifies the default command line arguments using the org.gnunet.util.getopt annotations.
37 *
36 * @see Service 38 * @see Service
37 */ 39 */
38public abstract class Program { 40public abstract class Program {
@@ -42,33 +44,33 @@ public abstract class Program {
42 44
43 protected final Configuration cfg = new Configuration(); 45 protected final Configuration cfg = new Configuration();
44 46
45 @Option(shortname = "c", longname = "config", 47 @Argument(shortname = "c", longname = "config",
46 description = "Path of the configuration file", 48 description = "Path of the configuration file",
47 argumentName = "FILENAME", 49 argumentName = "FILENAME",
48 action = OptionAction.STORE_STRING) 50 action = ArgumentAction.STORE_STRING)
49 public String cfgFileName; 51 public String cfgFileName;
50 52
51 @Option(shortname = "h", longname = "help", 53 @Argument(shortname = "h", longname = "help",
52 description = "print this help message", 54 description = "print this help message",
53 action = OptionAction.SET) 55 action = ArgumentAction.SET)
54 public boolean printHelp; 56 public boolean printHelp;
55 57
56 @Option(shortname = "v", longname = "version", 58 @Argument(shortname = "v", longname = "version",
57 description = "print version", 59 description = "print version",
58 action = OptionAction.SET) 60 action = ArgumentAction.SET)
59 public boolean showVersion; 61 public boolean showVersion;
60 62
61 63
62 @Option(shortname = "L", longname = "log", 64 @Argument(shortname = "L", longname = "log",
63 description = "configure logging to use LOGLEVEL", 65 description = "configure logging to use LOGLEVEL",
64 argumentName = "LOGLEVEL", 66 argumentName = "LOGLEVEL",
65 action = OptionAction.STORE_STRING) 67 action = ArgumentAction.STORE_STRING)
66 public String logLevel; 68 public String logLevel;
67 69
68 @Option(shortname = "l", longname = "logfile", 70 @Argument(shortname = "l", longname = "logfile",
69 description = "configure logging to write logs to LOGFILE", 71 description = "configure logging to write logs to LOGFILE",
70 argumentName = "LOGFILE", 72 argumentName = "LOGFILE",
71 action = OptionAction.STORE_STRING) 73 action = ArgumentAction.STORE_STRING)
72 public String logFile; 74 public String logFile;
73 75
74 76
@@ -79,7 +81,7 @@ public abstract class Program {
79 81
80 /** 82 /**
81 * A program with the desired environment for a gnunet utility. 83 * A program with the desired environment for a gnunet utility.
82 * While executing the scheduler is guaranteed to run, command arguments are parsed, 84 * While executing, the scheduler is guaranteed to run, command arguments are parsed,
83 * the default configuration is loaded and the DNS Resolver is initialized. 85 * the default configuration is loaded and the DNS Resolver is initialized.
84 * 86 *
85 * @param args array of command line arguments to parse. used to automatically load additional settings 87 * @param args array of command line arguments to parse. used to automatically load additional settings
@@ -94,6 +96,12 @@ public abstract class Program {
94 */ 96 */
95 } 97 }
96 98
99 /**
100 * Configure logging with the given log level and log file.
101 *
102 * @param logLevel one of DEBUG,INFO,WARN,ERROR,OFF
103 * @param logFile logfile, absolute or relative to the current working directory
104 */
97 public static void configureLogging(String logLevel, String logFile) { 105 public static void configureLogging(String logLevel, String logFile) {
98 org.apache.log4j.Logger rootLogger = LogManager.getRootLogger(); 106 org.apache.log4j.Logger rootLogger = LogManager.getRootLogger();
99 107
@@ -155,7 +163,7 @@ public abstract class Program {
155 /** 163 /**
156 * Start the Program as the initial task of the Scheduler. 164 * Start the Program as the initial task of the Scheduler.
157 */ 165 */
158 public void start() { 166 public final void start() {
159 Parser optParser = new Parser(this); 167 Parser optParser = new Parser(this);
160 unprocessedArgs = optParser.parse(args); 168 unprocessedArgs = optParser.parse(args);
161 169
@@ -177,13 +185,23 @@ public abstract class Program {
177 } else { 185 } else {
178 Scheduler.run(new Scheduler.Task() { 186 Scheduler.run(new Scheduler.Task() {
179 public void run(Scheduler.RunContext c) { 187 public void run(Scheduler.RunContext c) {
180 Program.this.run(); 188 Program.this.runHook();
181 } 189 }
182 }); 190 });
183 } 191 }
184 } 192 }
185 193
186 /** 194 /**
195 * Overridden by specializations of Program, like Service.
196 *
197 * Allows for start() to be final.
198 */
199 /* package-private */
200 void runHook() {
201 run();
202 }
203
204 /**
187 * Override to implement the behavior of the Program. 205 * Override to implement the behavior of the Program.
188 */ 206 */
189 public abstract void run(); 207 public abstract void run();