Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Extract JSAP handling into its own class.
Browse files Browse the repository at this point in the history
  • Loading branch information
tparker-usgs committed Jul 13, 2015
1 parent b469bd6 commit 7f16479
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<description>USGS Swarm Build File</description>

<property name="package" value="gov.usgs.volcanoes.pensive"/>
<property name="version" value="1.1.8"/>
<property name="version" value="1.2"/>
<property name="jarfile" value="pensive-${version}.jar" />
<property name="classpath" location="../USGS/contrib/jtransforms-2.4.jar;../USGS/lib/swarm.jar;../USGS/lib/plot.jar;../USGS/lib/util.jar;../USGS/contrib/freemarker.jar;../USGS/contrib/JSAP-2.1.jar;../USGS/contrib/slf4j-api-1.7.1.jar;../USGS/contrib/slf4j-log4j12-1.7.1.jar" />
<property name="source" location="." />
Expand Down
47 changes: 47 additions & 0 deletions src/gov/usgs/volcanoes/pensive/Args.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package gov.usgs.volcanoes.pensive;

import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Parameter;
import com.martiansoftware.jsap.SimpleJSAP;
import com.martiansoftware.jsap.Switch;
import com.martiansoftware.jsap.UnflaggedOption;

public class Args extends SimpleJSAP {

public static final String DEFAULT_CONFIG_FILENAME = "pensive.config";
public static final String PROGRAM_NAME = "java -jar net.stash.pensive.Pensive";
public static final String EXPLANATION_PREFACE = "I am the Pensive server";

private static final String EXPLANATION = "\n";

private static final Parameter[] PARAMETERS = new Parameter[] {
new Switch("create-config", 'c', "create-config",
"Create an example config file in the curent working directory."),
new Switch("verbose", 'v', "verbose", "Verbose logging."),
new UnflaggedOption("config-filename", JSAP.STRING_PARSER, DEFAULT_CONFIG_FILENAME, JSAP.NOT_REQUIRED,
JSAP.NOT_GREEDY, "The config file name.") };

private JSAPResult config;
public final boolean createConfig;
public final String configFileName;
public final boolean verbose;

public Args(String[] args) throws JSAPException {
super(PROGRAM_NAME, EXPLANATION, PARAMETERS);
config = parse(args);
if (messagePrinted()) {
// The following error message is useful for catching the case
// when args are missing, but help isn't printed.
if (!config.getBoolean("help"))
System.err.println("Try using the --help flag.");

System.exit(1);
}

createConfig = config.getBoolean("create-config");
configFileName = config.getString("config-filename");
verbose = config.getBoolean("verbose");
}
}
61 changes: 11 additions & 50 deletions src/gov/usgs/volcanoes/pensive/Pensive.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
package gov.usgs.volcanoes.pensive;

import gov.usgs.util.ConfigFile;
import gov.usgs.util.Log;
import gov.usgs.util.Util;
import gov.usgs.volcanoes.pensive.page.Page;
import gov.usgs.volcanoes.pensive.plot.SubnetPlotter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.LogManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Parameter;
import com.martiansoftware.jsap.SimpleJSAP;
import com.martiansoftware.jsap.Switch;
import com.martiansoftware.jsap.UnflaggedOption;
import com.martiansoftware.jsap.JSAPException;

/**
* An application to produce a continuous collection of subnet spectrograms.
Expand Down Expand Up @@ -56,19 +47,6 @@ public class Pensive {
/** One plot scheduler per wave server */
private Map<String, PlotScheduler> plotScheduler;

// JSAP related stuff.
public static final String JSAP_PROGRAM_NAME = "java -jar net.stash.pensive.Pensive";
public static final String JSAP_EXPLANATION_PREFACE = "I am the Pensive server";

private static final String DEFAULT_JSAP_EXPLANATION = "\n";

private static final Parameter[] DEFAULT_JSAP_PARAMETERS = new Parameter[] {
new Switch("create-config", 'c', "create-config",
"Create an example config file in the curent working directory."),
new Switch("verbose", 'v', "verbose", "Verbose logging."),
new UnflaggedOption("configFilename", JSAP.STRING_PARSER, DEFAULT_CONFIG_FILENAME, JSAP.NOT_REQUIRED,
JSAP.NOT_GREEDY, "The config file name.") };

/**
* Class constructor
*
Expand Down Expand Up @@ -190,28 +168,6 @@ private void schedulePlots() {
}
}

public static JSAPResult getArguments(String[] args) {
JSAPResult config = null;
try {
SimpleJSAP jsap = new SimpleJSAP(JSAP_PROGRAM_NAME, JSAP_EXPLANATION_PREFACE + DEFAULT_JSAP_EXPLANATION,
DEFAULT_JSAP_PARAMETERS);

config = jsap.parse(args);
if (jsap.messagePrinted()) {
// The following error message is useful for catching the case
// when args are missing, but help isn't printed.
if (!config.getBoolean("help"))
System.err.println("Try using the --help flag.");

System.exit(1);
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
return config;
}

public static void createConfig() throws IOException {

InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("pensive-example.config");
Expand Down Expand Up @@ -249,9 +205,15 @@ public static String getVersion() {
* @param args
*/
public static void main(String[] args) {
JSAPResult config = getArguments(args);
Args config = null;
try {
config = new Args(args);
} catch (JSAPException e1) {
System.err.println("Couldn't parse command line. Try using the --help flag.");
System.exit(1);
}

if (config.getBoolean("create-config")) {
if (config.createConfig) {
try {
LOGGER.warn("Creating example config " + DEFAULT_CONFIG_FILENAME);
Pensive.createConfig();
Expand All @@ -261,10 +223,9 @@ public static void main(String[] args) {
System.exit(0);
}

String fn = config.getString("configFilename");
ConfigFile cf = new ConfigFile(fn);
ConfigFile cf = new ConfigFile(config.configFileName);
if (!cf.wasSuccessfullyRead()) {
LOGGER.warn("Can't parse config file " + fn + ". Try using the --help flag.");
LOGGER.warn("Can't parse config file " + config.configFileName + ". Try using the --help flag.");
System.exit(1);
}

Expand Down

0 comments on commit 7f16479

Please sign in to comment.