/** * Main call. This will grab the command line arguments, run {@link #initialize()} * and then invoke {@link #doIt()}. * * @param args * @throws Throwable */ public void run(String[] args) throws Throwable { if (!getOptions(args)) { return; } run(); }
/** * Calls the {@link #initialize()} method and then runs the main {@link #doIt()} method. Call this if you set * the target and source in the constructor. * @throws Throwable */ public void run() throws Throwable { initialize(); doIt(); }
protected String readline() throws IOException { return getBufferedReader().readLine(); }
getOptions(); if (args.length == 0) { help(); return false; help(); return false; parseCommandLine(args); if (hasOption(HELP_OPTION, HELP_LONG_OPTION)) { help(); return false; setVerbose(false); if (hasOption(VERBOSE_OPTION, VERBOSE_LONG_OPTION)) { setVerbose(true); setDebugOn(false); if (hasOption(DEBUG_OPTION, DEBUG_LONG_OPTION)) { setDebugOn(true); if (hasOption(LOG_FILE_OPTION, LOG_FILE_LONG_OPTION)) { setLogfileName(getCommandLine().getOptionValue(LOG_FILE_LONG_OPTION)); if (hasOption(CONFIG_FILE_OPTION, CONFIG_FILE_LONG_OPTION)) { setConfigFile(getCommandLine().getOptionValue(CONFIG_FILE_OPTION));
/** * Sets up the configuration and runtime environment. This is called typically in the constructors before anything * else is called. Always call super on this method if you override it! At the end of this method, the configuration * node has been found and set for you to call in your {@link #getLoader()} method. */ public void initialize() throws Exception { // do not put logging messages (e.g. info) in this method since the actual // logger can't be loaded until the configuration file is found. All logging // messages before it is loaded will end up in the default log file, not // where the user wants them. if (getConfigFile() == null) { throw new MyConfigurationException("Error: no configuration file"); } if (getConfigFile().length() == 0) { throw new MyConfigurationException("Error:No configuration found."); } String cfgName = null; if (hasOption(CONFIG_NAME_OPTION, CONFIG_NAME_LONG_OPTION)) { cfgName = getCommandLine().getOptionValue(CONFIG_NAME_OPTION); } setConfigurationNode(ConfigUtil.findConfiguration(getConfigFile(), cfgName, getComponentName())); }
protected void parseCommandLine(String[] args) { CommandLineParser clp = new BasicParser(); try { cmd = clp.parse(getOptions(), args); } catch (UnrecognizedOptionException ux) { String err = "Error: unrecognized option + " + ux.getMessage() ; say(err); say("Invoke with -help for more"); if (isVerbose()) { ux.printStackTrace(); } throw new GeneralException(err, ux); } catch (ParseException e) { String err= "Error: could not parse a command line argument:" + e.getMessage(); say(err); if (isVerbose()) { e.printStackTrace(); } throw new GeneralException(err,e); } }
@Override protected Options getOptions() { Options options = super.getOptions(); options.addOption(CONFIG_NAME_OPTION, CONFIG_NAME_LONG_OPTION, true, "the name of the configuration. Omitting this means there is exactly one and to use that."); return options; }
/** * Converts command line arguments into integer action values. This makes writing switch * statements for choosing actions very easy. Pass in the short and long options, the * action that should be assigned and the command line. If the command option is present, * the action will be set. * <p/> * <p>The only supplied default is {@link #NO_ACTION} </p> * * @param flag * @param longFlag * @param action */ protected void checkAction(String flag, String longFlag, int action) { if (getCommandLine().hasOption(flag) || getCommandLine().hasOption(longFlag)) { if (getAction() != NO_ACTION) { throw new RuntimeException("Error: attempted to reset the action"); } setAction(action); } }
@Override public void initialize() throws Exception { super.initialize(); // Allow for over-riding the log file from the command line. if (getLogfileName().equals(DEFAULT_LOG_FILE)) { setMyLogger(getEnvironment().getMyLogger()); } else { File f = new File(getLogfileName()); info("Setting up environment, log file = " + f.getAbsolutePath()); } String cfgName = null; if (hasOption(CONFIG_NAME_OPTION, CONFIG_NAME_LONG_OPTION)) { cfgName = getCommandLine().getOptionValue(CONFIG_NAME_OPTION); } if (cfgName == null) { info("no named for a configuration given"); } else { info("getting named configuration \"" + cfgName + "\""); } try { List list = getConfigurationNode().getChildren("mail"); if (list.size() != 0) { mup = new MailUtilProvider((ConfigurationNode) list.get(0)); } } catch (Throwable t) { info("Did not initialize a mail notification environment:" + t.getMessage()); } serviceClient = ((AbstractClientLoader) getLoader()).createServiceClient(getClientEnvironment().getAccessTokenUri()); info("Done with bootstrap."); }
/** * Checks if the long or short form is part of the command line options. * * @param shortForm * @param longForm * @return */ protected boolean hasOption(String shortForm, String longForm) { return getCommandLine().hasOption(shortForm) || getCommandLine().hasOption(longForm); }
public AbstractEnvironment getEnvironment() throws Exception { if (environment == null) { environment = getLoader().load(); } return environment; }
@Override protected Options getOptions() { Options options = super.getOptions(); options.addOption(CONFIG_NAME_OPTION, CONFIG_NAME_LONG_OPTION, true, "the name of the configuration. " + "Omitting this means there is exactly one and to use that."); return options; } }
@Override protected Options getOptions() { Options options = super.getOptions(); options.addOption(SOURCE_CONFIG_FILE_OPTION, SOURCE_CONFIG_FILE_LONG_OPTION, true, "The full path to the source configuration file."); options.addOption(SOURCE_CONFIG_NAME_OPTION, SOURCE_CONFIG_NAME_LONG_OPTION, true, "The source server for the copy operation."); options.addOption(TARGET_CONFIG_FILE_OPTION, TARGET_CONFIG_FILE_LONG_OPTION, true, "The full path to the target configuration file."); options.addOption(TARGET_CONFIG_NAME_OPTION, TARGET_CONFIG_FILE_LONG_OPTION, true, "The target server for the operation."); return options; }