decodes.util.CmdLineArgs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendcs Show documentation
Show all versions of opendcs Show documentation
A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.
The newest version!
package decodes.util;
import java.io.IOException;
import java.io.File;
import java.util.Map;
import java.util.Properties;
import decodes.gui.TopFrame;
import decodes.launcher.Profile;
import ilex.cmdline.*;
import ilex.util.EnvExpander;
import ilex.util.JavaLoggerAdapter;
import ilex.util.Logger;
import ilex.util.FileLogger;
/**
Extends the ilex.cmdline.StdAppSettings class to handle arguments that
are common to most DECODES programs.
*/
public class CmdLineArgs
extends StdAppSettings
{
// Add DECODES-specific setting declarations here...
/** Log file argument (-l) */
protected StringToken log_arg;
/** properties file argument (-P), converted to a {@link decodes.launcher.Profile} */
private Profile profile;
/** Application Define argument (-D) */
private StringToken define_arg;
private BooleanToken forwardLogArg;
// Used by parent programs that spawn multiple apps. The parent sets this to
// tell parseArgs to skip the logger and properties initialization.
private boolean noInit = false;
//No filter option token.
public StringToken NoCompFilterToken;
/** Properties set explicitly on the command line. */
private Properties cmdLineProps;
/** default constructor */
public CmdLineArgs()
{
this(true, "util.log");
}
/**
Explicit constructor.
@param isNetworkApp True if this is a network-aware application
@param defaultLogFile Initialize log file name
*/
public CmdLineArgs(boolean isNetworkApp, String defaultLogFile)
{
super(isNetworkApp);
// Construct DECODES-specific setting & call addToken for each one.
log_arg = new StringToken(
"l", "log-file", "", TokenOptions.optSwitch, defaultLogFile);
define_arg = new StringToken(
"D", "Env-Define", "",
TokenOptions.optSwitch|TokenOptions.optMultiple, "");
// NoCompFilterToken = new StringToken("L",
// "Disable Computation List filter (default=on)", "",
// TokenOptions.optSwitch, "true");
// addToken(NoCompFilterToken);
forwardLogArg = new BooleanToken("FL", "Forward javax.logging logger to application log.", "",
TokenOptions.optSwitch, false);
addToken(log_arg);
addToken(define_arg);
addToken(forwardLogArg);
cmdLineProps = new Properties();
}
/** @return log file name, either default, or as specified in argumnet */
public String getLogFile()
{
String s = log_arg.getValue();
if (s == null || s.length() == 0)
return null;
return s;
}
public void setDefaultLogFile(String f)
{
log_arg.setDefaultValue(f);
}
// /** @return log Nofiltertoken option value, either default(true), or as specified in argument */
// public String getNoCompFilterToken()
// {
// String s = NoCompFilterToken.getValue();
// if (s == null || s.length() == 0)
// return null;
// return s;
// }
//
/**
Parses the command line argument and fills in internal variables.
@param args the arguments.
*/
public void parseArgs(String args[])
{
super.parseArgs(args);
/*
The user can set system properties on the command line with multiple
-Dname=value arguments. The following puts each setting into
System.properties so that they are available globally.
Each 'name' is converted to upper case before putting in the
properties set. So retrieve the property by upper-case name only.
*/
for(int i=0; i 0)
{
String procname = Logger.instance().getProcName();
try
{
Logger.setLogger(new FileLogger(procname, fn,100*1024*1024)); // 100 MegaBytes
}
catch(IOException e)
{
System.err.println("Cannot open log file '" + fn + "': " + e);
System.exit(1);
}
}
DecodesSettings settings = DecodesSettings.instance();
// if -P arg supplied, use it, else look in install dir.
final String propFileName = super.getPropertiesFile();
if (propFileName != null && propFileName.length() > 0)
{
profile = Profile.getProfile(new File(EnvExpander.expand(propFileName)));
}
else // the default profile is always at index 0 in Profile.getProfiles
{
profile = Profile.getDefaultProfile();
}
File propFile = profile.getFile();
if (!propFile.canRead())
{
String msg = "Cannot read properties file '" + propFile.getPath()
+ "' -P ARGUMENT PARSING or retrieval of default properties FAILED!";
Logger.instance().fatal(msg);
System.err.println(msg);
throw new IllegalArgumentException(msg);
}
if (profile.isProfile())
{
String profileName = profile.getName();
TopFrame.profileName = profileName; // don't include the full path that may be there.
if (!profileName.equalsIgnoreCase("user") && !profileName.equalsIgnoreCase("decodes"))
{
settings.setProfileName(profileName);
}
}
//Load the decodes.properties
if (!settings.isLoaded())
{
try
{
settings.loadFromProfile(profile);
}
catch(Exception ex)
{
Logger.instance().failure("Cannot load decodes properties from '" + propFileName + "': " + ex);
}
}
// Userdir is needed to support multi-user installations under unix/linux.
// If the property is not set, just copy DCSTOOL_HOME.
// That is, assume this is a single-user or windows installation.
String userDir = System.getProperty("DCSTOOL_USERDIR");
if (userDir == null)
{
System.setProperty("DCSTOOL_USERDIR", System.getProperty("DCSTOOL_HOME"));
}
// Set debug level.
int dl = getDebugLevel();
if (dl > 0)
Logger.instance().setMinLogPriority(
dl == 1 ? Logger.E_DEBUG1 :
dl == 2 ? Logger.E_DEBUG2 : Logger.E_DEBUG3);
if (DecodesSettings.instance().fontAdjust != 0)
{
for (Map.Entry