Example usage for java.io File exists

List of usage examples for java.io File exists

Introduction

In this page you can find the example usage for java.io File exists.

Prototype

public boolean exists() 

Source Link

Document

Tests whether the file or directory denoted by this abstract pathname exists.

Usage

From source file:es.tid.fiware.fiwareconnectors.cygnus.nodes.CygnusApplication.java

/**
 * Main application to be run when this CygnusApplication is invoked. The only differences with the original one
 * are the CygnusApplication is used instead of the Application one, and the Management Interface port option in
 * the command line./* w w w  . j a  v a2 s .c om*/
 * @param args
 */
public static void main(String[] args) {
    try {
        Options options = new Options();

        Option option = new Option("n", "name", true, "the name of this agent");
        option.setRequired(true);
        options.addOption(option);

        option = new Option("f", "conf-file", true, "specify a conf file");
        option.setRequired(true);
        options.addOption(option);

        option = new Option(null, "no-reload-conf", false, "do not reload " + "conf file if changed");
        options.addOption(option);

        option = new Option("h", "help", false, "display help text");
        options.addOption(option);

        option = new Option("p", "mgmt-if-port", true, "the management interface port");
        option.setRequired(false);
        options.addOption(option);

        CommandLineParser parser = new GnuParser();
        CommandLine commandLine = parser.parse(options, args);

        File configurationFile = new File(commandLine.getOptionValue('f'));
        String agentName = commandLine.getOptionValue('n');
        boolean reload = !commandLine.hasOption("no-reload-conf");

        if (commandLine.hasOption('h')) {
            new HelpFormatter().printHelp("flume-ng agent", options, true);
            return;
        } // if

        int mgmtIfPort = 8081; // default value

        if (commandLine.hasOption('p')) {
            mgmtIfPort = new Integer(commandLine.getOptionValue('p')).intValue();
        } // if

        // the following is to ensure that by default the agent will fail on startup if the file does not exist

        if (!configurationFile.exists()) {
            // if command line invocation, then need to fail fast
            if (System.getProperty(Constants.SYSPROP_CALLED_FROM_SERVICE) == null) {
                String path = configurationFile.getPath();

                try {
                    path = configurationFile.getCanonicalPath();
                } catch (IOException ex) {
                    logger.error("Failed to read canonical path for file: " + path, ex);
                } // try catch

                throw new ParseException("The specified configuration file does not exist: " + path);
            } // if
        } // if

        List<LifecycleAware> components = Lists.newArrayList();
        CygnusApplication application;

        if (reload) {
            EventBus eventBus = new EventBus(agentName + "-event-bus");
            PollingPropertiesFileConfigurationProvider configurationProvider = new PollingPropertiesFileConfigurationProvider(
                    agentName, configurationFile, eventBus, 30);
            components.add(configurationProvider);
            application = new CygnusApplication(components, mgmtIfPort);
            eventBus.register(application);
        } else {
            PropertiesFileConfigurationProvider configurationProvider = new PropertiesFileConfigurationProvider(
                    agentName, configurationFile);
            application = new CygnusApplication(mgmtIfPort);
            application.handleConfigurationEvent(configurationProvider.getConfiguration());
        } // if else

        application.start();

        final CygnusApplication appReference = application;
        Runtime.getRuntime().addShutdownHook(new Thread("agent-shutdown-hook") {
            @Override
            public void run() {
                appReference.stop();
            } // run
        });
    } catch (Exception e) {
        logger.error("A fatal error occurred while running. Exception follows.", e);
    } // try catch
}

From source file:es.upm.dit.xsdinferencer.XSDInferencer.java

/**
 * Main method, executed when the tool is invoked as a standalone application
 * @param args an array with all the arguments passed to the application
 * @throws XSDConfigurationException if there is a problem regarding the configuration
 * @throws IOException if there is an I/O problem while reading the input XML files or writing the output files
 * @throws JDOMException if there is any problem while parsing the input XML files
 *//*from   w  ww  .  j a v  a 2s .  c o m*/
public static void main(String[] args) throws Exception {
    if (Arrays.asList(args).contains("--help")) {
        printHelp();
        System.exit(0);
    }
    try {
        XSDInferencer inferencer = new XSDInferencer();

        Results results = inferencer.inferSchema(args);

        Map<String, String> xsdsAsXMLStrings = results.getXSDsAsStrings();
        Map<String, String> jsonsAsStrings = results.getJsonSchemasAsStrings();
        Map<String, String> schemasAsStrings = xsdsAsXMLStrings != null ? xsdsAsXMLStrings : jsonsAsStrings;
        Map<String, String> statisticsDocumentsAsXMLStrings = results.getStatisticsAsStrings();
        File outputDirectory = null;
        for (int i = 0; i < args.length; i++) {
            if (!args[i].equalsIgnoreCase("--" + KEY_OUTPUT_DIRECTORY))
                continue;
            if (args[i + 1].startsWith("--") || i == args.length - 1)
                throw new IllegalArgumentException("Output directory parameter bad specified");
            outputDirectory = new File(args[i + 1]);
            if (!outputDirectory.exists())
                throw new FileNotFoundException("Output directory not found.");
            if (!outputDirectory.isDirectory())
                throw new NotDirectoryException(outputDirectory.getPath());
        }
        if (outputDirectory != null) {
            System.out.println("Writing results to " + outputDirectory.getAbsolutePath());

            for (String name : schemasAsStrings.keySet()) {
                File currentOutpuFile = new File(outputDirectory, name);
                FileOutputStream fOs = new FileOutputStream(currentOutpuFile);
                BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(fOs, Charsets.UTF_8));
                bWriter.write(schemasAsStrings.get(name));
                bWriter.flush();
                bWriter.close();
            }
            if (statisticsDocumentsAsXMLStrings != null) {
                for (String name : statisticsDocumentsAsXMLStrings.keySet()) {
                    File currentOutpuFile = new File(outputDirectory, name);
                    FileWriter fWriter = new FileWriter(currentOutpuFile);
                    BufferedWriter bWriter = new BufferedWriter(fWriter);
                    bWriter.write(statisticsDocumentsAsXMLStrings.get(name));
                    bWriter.flush();
                    bWriter.close();
                }
            }
            System.out.println("Results written");
        } else {
            for (String name : schemasAsStrings.keySet()) {
                System.out.println(name + ":");
                System.out.println(schemasAsStrings.get(name));
                System.out.println();
            }
            if (statisticsDocumentsAsXMLStrings != null) {
                for (String name : statisticsDocumentsAsXMLStrings.keySet()) {
                    System.out.println(name + ":");
                    System.out.println(statisticsDocumentsAsXMLStrings.get(name));
                    System.out.println();
                }
            }
        }
    } catch (XSDInferencerException e) {
        System.err.println();
        System.err.println("Error at inference proccess: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:ar.edu.taco.TacoMain.java

/**
 * @param args/*  w  ww  . j a  v  a 2  s  .  c  om*/
 */
@SuppressWarnings({ "static-access" })
public static void main(String[] args) {
    @SuppressWarnings("unused")
    int loopUnrolling = 3;

    String tacoVersion = getManifestAttribute(Attributes.Name.IMPLEMENTATION_VERSION);
    String tacoCreatedBy = getManifestAttribute(new Name("Created-By"));

    System.out.println("TACO: Taco static analysis tool.");
    System.out.println("Created By: " + tacoCreatedBy);
    System.out.println("Version: " + tacoVersion);
    System.out.println("");
    System.out.println("");

    Option helpOption = new Option("h", "help", false, "print this message");
    Option versionOption = new Option("v", "version", false, "shows version");

    Option configFileOption = OptionBuilder.withArgName("path").withLongOpt("configFile").hasArg()
            .withDescription("set the configuration file").create("cf");
    Option classToCheckOption = OptionBuilder.withArgName("classname").withLongOpt("classToCheck").hasArg()
            .withDescription("set the class to be checked").create('c');
    Option methodToCheckOption = OptionBuilder.withArgName("methodname").withLongOpt("methodToCheck").hasArg()
            .withDescription("set the method to be checked").create('m');
    Option dependenciesOption = OptionBuilder.withArgName("classname").withLongOpt("dependencies").hasArgs()
            .withDescription("additional sources to be parsed").create('d');
    Option relevantClassesOption = OptionBuilder.withArgName("classname").withLongOpt("relevantClasses")
            .hasArgs().withDescription("Set the relevant classes to be used").create("rd");
    Option loopsOptions = OptionBuilder.withArgName("integer").withLongOpt("unroll").hasArg()
            .withDescription("set number of loop unrollings").create('u');
    Option bitOptions = OptionBuilder.withArgName("integer").withLongOpt("width").hasArg()
            .withDescription("set bit width").create('w');
    Option instOptions = OptionBuilder.withArgName("integer").withLongOpt("bound").hasArg()
            .withDescription("set class bound").create('b');
    Option skolemizeOption = OptionBuilder.withLongOpt("skolemize")
            .withDescription("set whether or not skolemize").create("sk");
    Option simulateOption = OptionBuilder.withLongOpt("simulate")
            .withDescription("run method instead of checking").create("r");
    Option modularReasoningOption = OptionBuilder.withLongOpt("modularReasoning")
            .withDescription("check method using modular reasoning").create("mr");
    Option relevancyAnalysisOption = OptionBuilder.withLongOpt("relevancyAnalysis")
            .withDescription("calculate the needed relevantClasses").create("ra");
    Option scopeRestrictionOption = OptionBuilder.withLongOpt("scopeRestriction")
            .withDescription("restrict signature scope to value set in -b option").create("sr");
    /*
     * Option noVerifyOption = OptionBuilder.withLongOpt(
     * "noVerify").withDescription(
     * "builds output but does not invoke verification engine").create(
     * "nv");
     */
    Options options = new Options();
    options.addOption(helpOption);
    options.addOption(versionOption);
    options.addOption(configFileOption);
    options.addOption(classToCheckOption);
    options.addOption(methodToCheckOption);
    options.addOption(dependenciesOption);
    options.addOption(relevantClassesOption);
    options.addOption(loopsOptions);
    options.addOption(bitOptions);
    options.addOption(instOptions);
    options.addOption(skolemizeOption);
    options.addOption(simulateOption);
    options.addOption(modularReasoningOption);
    options.addOption(relevancyAnalysisOption);
    options.addOption(scopeRestrictionOption);
    // options.addOption(noVerifyOption)

    String configFileArgument = null;
    Properties overridingProperties = new Properties();
    TacoCustomScope tacoScope = new TacoCustomScope();

    // create the parser
    CommandLineParser parser = new PosixParser();

    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);

        // help
        if (line.hasOption(helpOption.getOpt())) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(120, CMD, HEADER, options, FOOTER, true);
            return;
        }

        // version
        if (line.hasOption(versionOption.getOpt())) {
            System.out.println(FOOTER);
            System.out.println("");
            return;
        }

        // Configuration file
        if (line.hasOption(configFileOption.getOpt())) {
            configFileArgument = line.getOptionValue(configFileOption.getOpt());
        }

        // class to check
        if (line.hasOption(classToCheckOption.getOpt())) {
            overridingProperties.put(TacoConfigurator.CLASS_TO_CHECK_FIELD,
                    line.getOptionValue(classToCheckOption.getOpt()));
        }

        // method to check
        if (line.hasOption(methodToCheckOption.getOpt())) {
            String methodtoCheck = line.getOptionValue(methodToCheckOption.getOpt());

            if (!methodtoCheck.matches("^[A-Za-z0-9_-]+_[0-9]")) {
                methodtoCheck = methodtoCheck + "_0";
            }
            overridingProperties.put(TacoConfigurator.METHOD_TO_CHECK_FIELD, methodtoCheck);
        }

        // Dependencies classes
        if (line.hasOption(dependenciesOption.getOpt())) {
            String dependenciesClasses = "";
            for (String aDependencyClass : line.getOptionValues(dependenciesOption.getOpt())) {
                dependenciesClasses += aDependencyClass;
            }
            overridingProperties.put(TacoConfigurator.CLASSES_FIELD, dependenciesClasses);
        }

        // Relevant classes
        if (line.hasOption(relevantClassesOption.getOpt())) {
            String relevantClasses = "";
            for (String aRelevantClass : line.getOptionValues(relevantClassesOption.getOpt())) {
                relevantClasses += aRelevantClass;
            }
            overridingProperties.put(TacoConfigurator.RELEVANT_CLASSES, relevantClasses);
        }

        // Loop unrolling
        if (line.hasOption(loopsOptions.getOpt())) {
            loopUnrolling = Integer.parseInt(line.getOptionValue(loopsOptions.getOpt()));
        }

        // Int bitwidth
        if (line.hasOption(bitOptions.getOpt())) {
            String alloy_bitwidth_str = line.getOptionValue(bitOptions.getOpt());
            overridingProperties.put(TacoConfigurator.BITWIDTH, alloy_bitwidth_str);
            int alloy_bitwidth = new Integer(alloy_bitwidth_str);
            tacoScope.setAlloyBitwidth(alloy_bitwidth);
        }

        // instances scope
        if (line.hasOption(instOptions.getOpt())) {
            String assertionsArguments = "for " + line.getOptionValue(instOptions.getOpt());
            overridingProperties.put(TacoConfigurator.ASSERTION_ARGUMENTS, assertionsArguments);
        }

        // Skolemize
        if (line.hasOption(skolemizeOption.getOpt())) {
            overridingProperties.put(TacoConfigurator.SKOLEMIZE_INSTANCE_INVARIANT, false);
            overridingProperties.put(TacoConfigurator.SKOLEMIZE_INSTANCE_ABSTRACTION, false);
        }

        // Simulation
        if (line.hasOption(simulateOption.getOpt())) {
            overridingProperties.put(TacoConfigurator.INCLUDE_SIMULATION_PROGRAM_DECLARATION, true);
            overridingProperties.put(TacoConfigurator.GENERATE_CHECK, false);
            overridingProperties.put(TacoConfigurator.GENERATE_RUN, false);
        }

        // Modular Reasoning
        if (line.hasOption(modularReasoningOption.getOpt())) {
            overridingProperties.put(TacoConfigurator.MODULAR_REASONING, true);
        }

        // Relevancy Analysis
        if (line.hasOption(relevancyAnalysisOption.getOpt())) {
            overridingProperties.put(TacoConfigurator.RELEVANCY_ANALYSIS, true);
        }

    } catch (ParseException e) {
        System.err.println("Command line parsing failed: " + e.getMessage());
    }

    try {

        System.out.println("****** Starting Taco (version. " + tacoVersion + ") ****** ");
        System.out.println("");

        File file = new File("config/log4j.xml");
        if (file.exists()) {
            DOMConfigurator.configure("config/log4j.xml");
        } else {
            System.err.println("log4j:WARN File config/log4j.xml not found");
        }

        TacoMain main = new TacoMain();

        // BUILD TacoScope 

        //
        main.run(configFileArgument, overridingProperties);

    } catch (IllegalArgumentException e) {
        System.err.println("Error found:");
        System.err.println(e.getMessage());
    } catch (MethodToCheckNotFoundException e) {
        System.err.println("Error found:");
        System.err.println("Method to check was not found. Please verify config file, or add -m option");
    } catch (TacoException e) {
        System.err.println("Error found:");
        System.err.println(e.getMessage());
    }
}

From source file:com.diversityarrays.kdxplore.trials.TrialSelectionDialog.java

static public void main(String[] args) {

    Transformer<BackgroundRunner, TrialSearchOptionsPanel> factory = new Transformer<BackgroundRunner, TrialSearchOptionsPanel>() {
        @Override/*from   w  ww.j a v  a 2 s  .  c o m*/
        public TrialSearchOptionsPanel transform(BackgroundRunner backgroundRunner) {
            return TrialSelectionSearchOptionsPanel.create(backgroundRunner);
        }
    };

    boolean test = false;
    if (test) {
        @SuppressWarnings("unchecked")
        TrialSelectionDialog tsd = new TrialSelectionDialog(null, "Test Tree", null, Collections.EMPTY_LIST, //$NON-NLS-1$
                factory);
        //         tsd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        tsd.setVisible(true);
    } else {

        File propertiesFile = new File(System.getProperty("user.home"), //$NON-NLS-1$
                "LoginDialog.properties"); //$NON-NLS-1$
        //.getBundle("LoginDialog"); //, Locale.getDefault());

        ResourceBundle bundle = null;

        if (propertiesFile.exists()) {
            try {
                bundle = new PropertyResourceBundle(new FileReader(propertiesFile));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.exit(1);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }

        Preferences loginPreferences = KdxplorePreferences.getInstance().getPreferences();
        //         Preferences loginPreferences = Preferences.userNodeForPackage(KdxConstants.class);

        LoginDialog ld = new LoginDialog(null, "Login Please", loginPreferences, bundle); //$NON-NLS-1$
        ld.setVisible(true);
        DALClient client = ld.getDALClient();
        if (client != null) {
            @SuppressWarnings("unchecked")
            TrialSelectionDialog tsd = new TrialSelectionDialog(null, "Test Tree", client, //$NON-NLS-1$
                    Collections.EMPTY_LIST, factory);
            //            tsd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            tsd.setVisible(true);
            if (tsd.trialRecords == null) {
                System.out.println("Cancelled !"); //$NON-NLS-1$
            } else {
                System.out.println(tsd.trialRecords.length + " chosen: "); //$NON-NLS-1$
                for (TrialPlus tr : tsd.trialRecords) {
                    System.out.println("\t" + tr.getTrialId() + ": " + tr.getTrialName()); //$NON-NLS-1$ //$NON-NLS-2$
                }
                System.out.println("- - -"); //$NON-NLS-1$
            }
        }
    }
    System.exit(0);
}

From source file:edu.wpi.khufnagle.webimagemanager.WebImageManager.java

/**
 * Defines information for the lighthouses, then runs the
 * photograph-collection process.//from ww w. java  2  s .  c  o  m
 * @param args Command-line arguments for this program (not used in this
 *        implementation)
 */
// Auto-boxing done "on the fly" to show progress of downloading images
@SuppressWarnings("boxing")
public static void main(final String[] args) {
    final long startTime = System.nanoTime();
    System.out.println("***BEGIN PHOTO TRANSFER PROCESS***");

    // Add data for lighthouses (next 375 lines or so)
    final List<LighthouseInfo> lighthouseData = new ArrayList<LighthouseInfo>();
    /*
     * lighthouseData.add(new LighthouseInfo("Statue of Liberty", 40.689348,
     * -74.044726));
     */// Statue of Liberty = 2080 photos w/out restrictions
    lighthouseData.add(new LighthouseInfo("Portland Head Light", 43.623104, -70.207867));
    lighthouseData.add(new LighthouseInfo("Pemaquid Point Light", 43.836970, -69.505997));
    lighthouseData.add(new LighthouseInfo("Five Mile Point (New Haven Harbor) Light", 41.248958, -72.903766));
    lighthouseData.add(new LighthouseInfo("Cape Neddick (Nubble) Light", 43.165211, -70.591102));
    lighthouseData.add(new LighthouseInfo("Portland Breakwater Light", 43.655516, -70.234813));
    lighthouseData.add(new LighthouseInfo("Beavertail Light", 41.449368, -71.399372));
    lighthouseData.add(new LighthouseInfo("Bass Harbor Head Light", 44.221976, -68.337214));
    lighthouseData.add(new LighthouseInfo("Nobska Point Light", 41.515792, -70.655116));
    lighthouseData.add(new LighthouseInfo("Spring Point Ledge Light", 43.652108, -70.223922));
    lighthouseData.add(new LighthouseInfo("Gay Head Light", 41.348450, -70.834956));
    lighthouseData.add(new LighthouseInfo("Derby Wharf Light", 42.516566, -70.883536));
    lighthouseData.add(new LighthouseInfo("Rockland Breakwater Light", 44.104006, -69.077453));
    lighthouseData.add(new LighthouseInfo("Sandy Neck Light", 41.722647, -70.280927));
    lighthouseData.add(new LighthouseInfo("Marblehead Light", 42.505411, -70.833708));
    lighthouseData.add(new LighthouseInfo("Portsmouth Harbor Light", 43.071061, -70.708634));
    lighthouseData.add(new LighthouseInfo("Highland Light", 42.039122, -70.062025));
    lighthouseData.add(new LighthouseInfo("Cape Elizabeth Light", 43.566058, -70.200042));
    lighthouseData.add(new LighthouseInfo("Marshall Point Light", 43.917406, -69.261222));
    lighthouseData.add(new LighthouseInfo("Chatham Light", 41.671407, -69.949884));
    lighthouseData.add(new LighthouseInfo("Block Island Southeast Light", 41.153412, -71.552117));
    lighthouseData.add(new LighthouseInfo("Edgartown Light", 41.390863, -70.503057));
    lighthouseData.add(new LighthouseInfo("Watch Hill Light", 41.303884, -71.858575));
    lighthouseData.add(new LighthouseInfo("Nauset Light", 41.858305, -69.951631));
    lighthouseData
            .add(new LighthouseInfo("Fayerweather Island (Black Rock Harbor) Light", 41.142380, -73.217409));
    lighthouseData.add(new LighthouseInfo("Owls Head Light", 44.092138, -69.044105));
    lighthouseData.add(new LighthouseInfo("Point Judith Light", 41.361035, -71.481402));
    lighthouseData.add(new LighthouseInfo("Sankaty Head Light", 41.284379, -69.966244));
    lighthouseData.add(new LighthouseInfo("Eastern Point Light", 42.580229, -70.664537));
    lighthouseData.add(new LighthouseInfo("Fort Pickering Light", 42.526473, -70.866465));
    lighthouseData.add(new LighthouseInfo("Wood Island Light", 43.456788, -70.328976));
    lighthouseData.add(new LighthouseInfo("Stonington Harbor Light", 41.328780, -71.905486));
    lighthouseData.add(new LighthouseInfo("West Quoddy Head Light", 44.815073, -66.950742));
    lighthouseData.add(new LighthouseInfo("Fort Point Light", 44.467265, -68.811717));
    lighthouseData.add(new LighthouseInfo("Annisquam Light", 42.661874, -70.681488));
    lighthouseData.add(new LighthouseInfo("Newport Harbor Light", 41.493299, -71.327038));
    lighthouseData.add(new LighthouseInfo("Long Point Light", 42.033117, -70.168651));
    lighthouseData.add(new LighthouseInfo("Castle Hill Light", 41.462116, -71.362919));
    lighthouseData.add(new LighthouseInfo("Brant Point Light", 41.289918, -70.090287));
    lighthouseData.add(new LighthouseInfo("Stratford Point Light", 41.151984, -73.103276));
    lighthouseData.add(new LighthouseInfo("Boston Light", 42.327925, -70.890101));
    lighthouseData.add(new LighthouseInfo("Lynde Point Light", 41.271452, -72.343142));
    lighthouseData.add(new LighthouseInfo("Scituate Light", 42.204748, -70.715814));
    lighthouseData.add(new LighthouseInfo("Prospect Harbor Light", 44.403285, -68.012922));
    lighthouseData.add(new LighthouseInfo("Wood End Light", 42.021223, -70.193502));
    lighthouseData.add(new LighthouseInfo("Rose Island Light", 41.495477, -71.342742));
    lighthouseData.add(new LighthouseInfo("Saybrook Breakwater Light", 41.263158, -72.342813));
    lighthouseData.add(new LighthouseInfo("Great Point Light", 41.390096, -70.048234));
    lighthouseData.add(new LighthouseInfo("Cape Poge Light", 41.418798, -70.451923));
    lighthouseData.add(new LighthouseInfo("Monhegan Light", 43.764779, -69.316204));
    lighthouseData.add(new LighthouseInfo("Hendricks Head Light", 43.822589, -69.689761));
    lighthouseData.add(new LighthouseInfo("Egg Rock Light", 44.354050, -68.138166));
    lighthouseData.add(new LighthouseInfo("New London Ledge Light", 41.305826, -72.077448));
    lighthouseData.add(new LighthouseInfo("Avery Point Lighthouse", 41.315245, -72.063579));
    lighthouseData.add(new LighthouseInfo("Palmers Island Light", 41.626936, -70.909109));
    lighthouseData.add(new LighthouseInfo("Cuckolds Light", 43.779663, -69.649982));
    lighthouseData.add(new LighthouseInfo("Gull Rocks Light", 41.502451, -71.333140));
    lighthouseData.add(new LighthouseInfo("Goat Island Light", 43.357826, -70.425109));
    lighthouseData.add(new LighthouseInfo("East Chop Light", 41.470245, -70.567439));
    lighthouseData.add(new LighthouseInfo("Neds Point Light", 41.650859, -70.795638));
    lighthouseData.add(new LighthouseInfo("Sakonnet Point Light", 41.453090, -71.202382));
    lighthouseData.add(new LighthouseInfo("Narrows (Bug) Light", 42.323137, -70.919158));
    lighthouseData.add(new LighthouseInfo("Plum Island Light", 42.815119, -70.818981));
    lighthouseData.add(new LighthouseInfo("Block Island North Light", 41.227639, -71.575811));
    lighthouseData.add(new LighthouseInfo("Mount Desert Rock Light", 43.968582, -68.128306));
    lighthouseData.add(new LighthouseInfo("Duxbury Pier Light", 41.987375, -70.648498));
    lighthouseData.add(new LighthouseInfo("Long Island Head Light", 42.330197, -70.957712));
    lighthouseData.add(new LighthouseInfo("Prudence Island Light", 41.605881, -71.303535));
    lighthouseData.add(new LighthouseInfo("Plum Beach Light", 41.530248, -71.405202));
    lighthouseData.add(new LighthouseInfo("Doubling Point Light", 43.882503, -69.806792));
    lighthouseData.add(new LighthouseInfo("Dice Head Light", 44.382732, -68.819022));
    lighthouseData.add(new LighthouseInfo("Ram Island Ledge Light", 43.631457, -70.187366));
    lighthouseData.add(new LighthouseInfo("New London Harbor Light", 41.316619, -72.089743));
    lighthouseData.add(new LighthouseInfo("Lime Rock Light", 41.477536, -71.325924));
    lighthouseData.add(new LighthouseInfo("Ten Pound Island Light", 42.601865, -70.665556));
    lighthouseData.add(new LighthouseInfo("Bristol Ferry Light", 41.642842, -71.260319));
    lighthouseData.add(new LighthouseInfo("Musselbed Shoals Light", 41.636261, -71.259958));
    lighthouseData.add(new LighthouseInfo("Conimicut Light", 41.716969, -71.345106));
    lighthouseData.add(new LighthouseInfo("Tongue Point Light", 41.166590, -73.177497));
    lighthouseData.add(new LighthouseInfo("Bass River Light", 41.651746, -70.169473));
    lighthouseData.add(new LighthouseInfo("Hospital Point Light", 42.546413, -70.856164));
    lighthouseData.add(new LighthouseInfo("Newburyport Range Light", 42.811524, -70.864838));
    lighthouseData.add(new LighthouseInfo("Dutch Island Light", 41.496702, -71.404299));
    lighthouseData.add(new LighthouseInfo("Heron Neck Light", 44.025216, -68.861966));
    lighthouseData.add(new LighthouseInfo("Pumpkin Island Light", 44.309166, -68.742876));
    lighthouseData.add(new LighthouseInfo("Whaleback Light", 43.058744, -70.696306));
    lighthouseData.add(new LighthouseInfo("Hyannis Harbor Light", 41.636267, -70.288439));
    lighthouseData.add(new LighthouseInfo("Stage Harbor Light", 41.658692, -69.983689));
    lighthouseData.add(new LighthouseInfo("Lovells Island Range Light", 42.332440, -70.930214));
    lighthouseData.add(new LighthouseInfo("Hog Island Shoal Light", 41.632338, -71.273198));
    lighthouseData.add(new LighthouseInfo("Ram Island Light", 43.803935, -69.599349));
    lighthouseData.add(new LighthouseInfo("Bridgeport Harbor Light", 41.156718, -73.179950));
    lighthouseData.add(new LighthouseInfo("Straitsmouth Island Light", 42.662236, -70.588157));
    lighthouseData.add(new LighthouseInfo("Squirrel Point Light", 43.816520, -69.802402));
    lighthouseData.add(new LighthouseInfo("Mayos Beach Light", 41.930755, -70.032097));
    lighthouseData.add(new LighthouseInfo("Race Point Light", 42.062314, -70.243084));
    lighthouseData.add(new LighthouseInfo("Point Gammon Light", 41.609647, -70.266196));
    lighthouseData.add(new LighthouseInfo("Wings Neck Light", 41.680235, -70.661250));
    lighthouseData.add(new LighthouseInfo("West Chop Light", 41.480806, -70.599796));
    lighthouseData.add(new LighthouseInfo("Bird Island Light", 41.669295, -70.717341));
    lighthouseData.add(new LighthouseInfo("Clarks Point Light", 41.593176, -70.901416));
    lighthouseData.add(new LighthouseInfo("Thacher Island Light", 42.639168, -70.574759));
    lighthouseData.add(new LighthouseInfo("White Island Light", 42.967228, -70.623249));
    lighthouseData.add(new LighthouseInfo("Wickford Harbor Light", 41.572618, -71.436831));
    lighthouseData.add(new LighthouseInfo("Whale Rock Light", 41.444597, -71.423584));
    lighthouseData.add(new LighthouseInfo("Burnt Island Light", 43.825133, -69.640262));
    lighthouseData.add(new LighthouseInfo("Rockland Harbor Southwest Light", 44.082720, -69.096310));
    lighthouseData.add(new LighthouseInfo("Saddleback Ledge Light", 44.014232, -68.726461));
    lighthouseData.add(new LighthouseInfo("Grindle Point Light", 44.281451, -68.942967));
    lighthouseData.add(new LighthouseInfo("Winter Harbor Light", 44.361421, -68.087742));
    lighthouseData.add(new LighthouseInfo("Peck's Ledge Light", 41.077298, -73.369811));
    lighthouseData.add(new LighthouseInfo("Sheffield Island Light", 41.048251, -73.419931));
    lighthouseData.add(new LighthouseInfo("Whitlocks Mill Light", 45.162793, -67.227395));
    lighthouseData.add(new LighthouseInfo("Boon Island Light", 43.121183, -70.475845));
    lighthouseData.add(new LighthouseInfo("Southwest Ledge Light", 41.234443, -72.912092));
    lighthouseData.add(new LighthouseInfo("Broad Sound Channel Inner Range Light", 42.326933, -70.984649));
    lighthouseData.add(new LighthouseInfo("Spectacle Island Light", 42.326898, -70.984772));
    lighthouseData.add(new LighthouseInfo("Deer Island Light", 42.339836, -70.954525));
    lighthouseData.add(new LighthouseInfo("Nayatt Point Light", 41.725120, -71.338926));
    lighthouseData.add(new LighthouseInfo("Doubling Point Range Lights", 43.882860, -69.795652));
    lighthouseData.add(new LighthouseInfo("Burkehaven Light", 43.371669, -72.065869));
    lighthouseData.add(new LighthouseInfo("Loon Island Light", 43.392123, -72.059977));
    lighthouseData.add(new LighthouseInfo("Curtis Island Light", 44.201372, -69.048865));
    lighthouseData.add(new LighthouseInfo("Butler Flats Light", 41.603775, -70.894556));
    lighthouseData.add(new LighthouseInfo("Graves Light", 42.365098, -70.869191));
    lighthouseData.add(new LighthouseInfo("Stamford Harbor Light", 41.013643, -73.542577));
    lighthouseData.add(new LighthouseInfo("Billingsgate Light", 41.871624, -70.068982));
    lighthouseData.add(new LighthouseInfo("Monomoy Point Light", 41.559310, -69.993650));
    lighthouseData.add(new LighthouseInfo("Bishop & Clerks Light", 41.574154, -70.249963));
    lighthouseData.add(new LighthouseInfo("Plymouth Light", 42.003737, -70.600565));
    lighthouseData.add(new LighthouseInfo("Cleveland Ledge Light", 41.630927, -70.694201));
    lighthouseData.add(new LighthouseInfo("Tarpaulin Cove Light", 41.468822, -70.757514));
    lighthouseData.add(new LighthouseInfo("Minots Ledge Light", 42.269678, -70.759136));
    lighthouseData.add(new LighthouseInfo("Dumpling Rock Light", 41.538167, -70.921427));
    lighthouseData.add(new LighthouseInfo("Bakers Island Light", 42.536470, -70.785995));
    lighthouseData.add(new LighthouseInfo("Cuttyhunk Light", 41.414391, -70.949558));
    lighthouseData.add(new LighthouseInfo("Egg Rock Light", 42.433346, -70.897386));
    lighthouseData.add(new LighthouseInfo("Ipswich Range Light", 42.685360, -70.766128));
    lighthouseData.add(new LighthouseInfo("Borden Flats Light", 41.704450, -71.174395));
    lighthouseData.add(new LighthouseInfo("Bullocks Point Light", 41.737740, -71.364179));
    lighthouseData.add(new LighthouseInfo("Pomham Rocks Light", 41.777618, -71.369594));
    lighthouseData.add(new LighthouseInfo("Sabin Point Light", 41.762010, -71.374234));
    lighthouseData.add(new LighthouseInfo("Fuller Rock Light", 41.794055, -71.379720));
    lighthouseData.add(new LighthouseInfo("Gould Island Light", 41.537826, -71.344804));
    lighthouseData.add(new LighthouseInfo("Warwick Light", 41.667111, -71.378413));
    lighthouseData.add(new LighthouseInfo("Sassafras Point Light", 41.802496, -71.390272));
    lighthouseData.add(new LighthouseInfo("Conanicut Light", 41.573484, -71.371767));
    lighthouseData.add(new LighthouseInfo("Poplar Point Light", 41.571053, -71.439189));
    lighthouseData.add(new LighthouseInfo("Halfway Rock Light", 43.655873, -70.037402));
    lighthouseData.add(new LighthouseInfo("Seguin Island Light", 43.707554, -69.758118));
    lighthouseData.add(new LighthouseInfo("Pond Island Light", 43.740031, -69.770273));
    lighthouseData.add(new LighthouseInfo("Perkins Island Light", 43.786764, -69.785256));
    lighthouseData.add(new LighthouseInfo("Latimer Reef Light", 41.304503, -71.933292));
    lighthouseData.add(new LighthouseInfo("Morgan Point Light", 41.316669, -71.989327));
    lighthouseData.add(new LighthouseInfo("Franklin Island Light", 43.892184, -69.374842));
    lighthouseData.add(new LighthouseInfo("Matinicus Rock Light", 43.783605, -68.854898));
    lighthouseData.add(new LighthouseInfo("Tenants Harbor Light", 43.961107, -69.184877));
    lighthouseData.add(new LighthouseInfo("Whitehead Light", 43.978706, -69.124285));
    lighthouseData.add(new LighthouseInfo("Two Bush Island Light", 43.964239, -69.073942));
    lighthouseData.add(new LighthouseInfo("Indian Island Light", 44.165470, -69.061004));
    lighthouseData.add(new LighthouseInfo("Browns Head Light", 44.111774, -68.909482));
    lighthouseData.add(new LighthouseInfo("Goose Rocks Light", 44.135394, -68.830526));
    lighthouseData.add(new LighthouseInfo("Sperry Light", 41.221221, -72.423110));
    lighthouseData.add(new LighthouseInfo("Isle au Haut Light", 44.064733, -68.651339));
    lighthouseData.add(new LighthouseInfo("Deer Island Thorofare Light", 44.134338, -68.703202));
    lighthouseData.add(new LighthouseInfo("Herrick Cove Light", 43.411136, -72.041706));
    lighthouseData.add(new LighthouseInfo("Eagle Island Light", 44.217634, -68.767743));
    lighthouseData.add(new LighthouseInfo("Burnt Coat Harbor Light", 44.134176, -68.447258));
    lighthouseData.add(new LighthouseInfo("Faulkner's Island Light", 41.211612, -72.655088));
    lighthouseData.add(new LighthouseInfo("Blue Hill Bay Light", 44.248746, -68.497880));
    lighthouseData.add(new LighthouseInfo("Great Duck Island Light", 44.142193, -68.245836));
    lighthouseData.add(new LighthouseInfo("Bear Island Light", 44.283485, -68.269858));
    lighthouseData.add(new LighthouseInfo("Baker Island Light", 44.241266, -68.198923));
    lighthouseData.add(new LighthouseInfo("Crabtree Ledge Light", 44.475613, -68.199383));
    lighthouseData.add(new LighthouseInfo("Statford Shoal Light", 41.059557, -73.101394));
    lighthouseData.add(new LighthouseInfo("Petit Manan Light", 44.367574, -67.864129));
    lighthouseData.add(new LighthouseInfo("Penfield Reef Light", 41.117101, -73.222070));
    lighthouseData.add(new LighthouseInfo("Narraguagus Light", 44.462467, -67.837844));
    lighthouseData.add(new LighthouseInfo("Nash Island Light", 44.464305, -67.747299));
    lighthouseData.add(new LighthouseInfo("Moose Peak Light", 44.474244, -67.533471));
    lighthouseData.add(new LighthouseInfo("Green's Ledge Light", 41.041551, -73.443974));
    lighthouseData.add(new LighthouseInfo("Libby Island Light", 44.568236, -67.367339));
    lighthouseData.add(new LighthouseInfo("Great Captain Island Light", 40.982478, -73.623706));
    lighthouseData.add(new LighthouseInfo("Avery Rock Light", 44.654358, -67.344137));
    lighthouseData.add(new LighthouseInfo("Little River Light", 44.650873, -67.192325));
    lighthouseData.add(new LighthouseInfo("Lubec Channel Light", 44.841955, -66.976731));
    lighthouseData.add(new LighthouseInfo("St. Croix River Light", 45.128762, -67.133594));

    /*
     * "Clean out" photo directories before beginning photo transfer process.
     */
    final File photosDir = new File("photos");
    final File[] photoLighthouseDirsToDelete = photosDir.listFiles();
    if (photoLighthouseDirsToDelete != null) {
        for (final File photoLighthouseDir : photoLighthouseDirsToDelete) {
            // Use Apache Commons IO (again) to recursively delete the directory
            // and all of the files within it
            if (photoLighthouseDir.exists() && photoLighthouseDir.isDirectory()) {
                try {
                    FileUtils.deleteDirectory(photoLighthouseDir);
                    System.out.println("Deleted directory \"" + photoLighthouseDir + "\" successfully.");
                } catch (final IOException ioe) {
                    System.err.println(
                            "Could not delete directory: \"" + photoLighthouseDir + "\" successfully!");
                }
            }
        }
    }

    // Keep track of elapsed time
    long estimatedTime = System.nanoTime() - startTime;
    String elapsedTime = WebImageManager.calculateElapsedTime(estimatedTime);

    System.out.println("Estimated elapsed time: " + elapsedTime + ".");

    System.out.println();

    /*
     * Keep track of total number of photographs transferred from Flickr
     * websites to disks across _all_ lighthouses
     */
    int totalNumPhotosTransferred = 0;

    /*
     * Keep track of total number of photographs that _should_ be transferred
     * from Flickr for _all_ lighthouses
     */
    int totalNumPhotos = 0;

    for (final LighthouseInfo lighthousePieceOfData : lighthouseData) {
        System.out.println("Processing photos of " + lighthousePieceOfData.getName() + "...");

        /*
         * URL for accessing Flickr APIs. For a given lighthouse, this URL
         * provides an XML file in response that lists information about every
         * geotagged, Creative Commons-enabled photograph for that lighthouse
         * on Flickr.
         */
        // GET Parameter Explanation:
        // method - Use the "search photos" method for the Flickr APIs
        //
        // api_key - A unique key that I use to get the results
        //
        // text - Find all lighthouses whose title, tags, or description
        // contains the word "lighthouse"
        //
        // license - Find all photos with a Creative Commons license _except_
        // those that do not allow for modification on my part
        //
        // content_type - Find photos only (no videos)
        //
        // has_geo - Implicitly set to true; implies that all photos are
        // geotagged
        //
        // lat - The latitude of the center of the "search circle"
        //
        // lon - The longitude of the center of the "search circle"
        //
        // radius - The radius of the "search circle," in _kilometers_ (NOT
        // miles)
        //
        // extras - Also include a URL to the "raw" photo (small version)
        final String inputURLText = "http://ycpi.api.flickr.com/services/rest/?" + "method=flickr.photos.search"
                + "&api_key=3ea8366b020383eb91f170c6f41748f5" + "&text=lighthouse" + "&license=1,2,4,5,7"
                + "&content_type=1" + "&has_geo" + "&lat=" + lighthousePieceOfData.getLatitude() + "&lon="
                + lighthousePieceOfData.getLongitude() + "&radius=1" + "&extras=url_s";
        // Output file where XML web response will be stored temporarily
        final String outputFileName = "output.xml";

        /*
         * Convert the name of the lighthouse to a "computer friendly" version
         * with all lower-case letters and underscores replacing spaces,
         * apostrophes, and parenthesis
         */
        String lighthouseName = lighthousePieceOfData.getName();
        lighthouseName = lighthouseName.toLowerCase();
        lighthouseName = lighthouseName.replace(' ', '_');
        lighthouseName = lighthouseName.replace('\'', '_');
        lighthouseName = lighthouseName.replace('(', '_');
        lighthouseName = lighthouseName.replace(')', '_');

        // Will contain the textual links to each "raw" photo website
        Set<String> rawPhotoURLs = new HashSet<String>();

        // Make sure file for XML output does not exist at first
        // (don't want to use an old, incorrect version accidentally)
        final File outputXMLFile = new File(outputFileName);
        if (outputXMLFile.exists()) {
            outputXMLFile.delete();
        }
        System.out.println("Cleaned output XML file containing photo URLs on disk successfully.");

        /*
         * Access the list of photographs for a given lighthouse and copy them
         * to the XML file on disk
         */
        final WebDataExtractor extractor = new WebDataExtractor(inputURLText, outputFileName);
        System.out.println("Looking for XML file containing lighthosue photo information...");
        extractor.transferURLToFile();
        System.out.println("Found XML file containing lighthouse photo URLs.");

        /*
         * Object for extracting the "raw" URLs from each piece of photo data
         * in the XML file
         */
        final XMLParser parser = new FlickrXMLOutputParser(outputFileName);

        // Complete the extraction process
        rawPhotoURLs = parser.parseFile("//photo/@url_s");

        final int numPhotos = rawPhotoURLs.size();
        totalNumPhotos += numPhotos;
        int i = 0; // Counter for keeping track of progress

        /*
         * Keep track of photos transferred successfully (which might be less
         * than the total number of photos defined int the XML output from
         * Flickr, especially if connection issues occur
         */
        int numPhotosTransferred = 0;
        for (final String photoURL : rawPhotoURLs) {
            System.out.print("Transferring photos...");
            i++;

            /*
             * Go to a website containing a "raw" JPEG image file and save it
             * accordingly on disk in the photo folder corresponding to the
             * lighthouse name
             */
            final WebDataExtractor rawPhotoExtractor = new WebDataExtractor(photoURL,
                    "photos/" + lighthouseName + "/lighthouse_photo_" + Integer.toString(i) + ".jpg");
            final boolean transferSuccessful = rawPhotoExtractor.transferURLToFile();
            if (transferSuccessful) {
                numPhotosTransferred++;
            }

            // Simple progress tracker
            System.out.printf("%d of %d (%.1f%%) complete.\n", i, numPhotos, i * 1.0 / numPhotos * 100.0);
        }

        // Indicate number of photos successfully transferred to disk
        if (numPhotosTransferred == numPhotos && numPhotos > 0) {
            System.out.println("All photos transferred to disk successfully!");
        } else if (numPhotos == 0) {
            System.out.println("It appears there are no photos available for this lighthouse...");
        } else if (numPhotosTransferred == 1 && numPhotos > 1) {
            System.out.println("1 photo transferred to disk successfully.");
        } else if (numPhotosTransferred == 1 && numPhotos == 1) {
            System.out.println("The photo transferred to disk successfully!");
        } else {
            System.out.println(numPhotosTransferred + " photos transferred to disk successfully.");
        }

        // Keep track of elapsed time
        estimatedTime = System.nanoTime() - startTime;
        elapsedTime = WebImageManager.calculateElapsedTime(estimatedTime);
        System.out.println("Estimated elapsed time: " + elapsedTime + ".");

        // Add extra line in between lighthouses in output stream
        System.out.println();

        /*
         * Keep track of total number of photos transferred so far across
         * _all_lighthouses
         */
        totalNumPhotosTransferred += numPhotosTransferred;

    }

    // Display "grand" total (which is hopefully greater than 0)
    System.out.println("***GRAND TOTAL: " + totalNumPhotosTransferred + " OF " + totalNumPhotos
            + " PHOTOS TRANSFERRED SUCCESSFULLY***");
    estimatedTime = System.nanoTime() - startTime;
    elapsedTime = WebImageManager.calculateElapsedTime(estimatedTime);
    System.out.println("TOTAL ELAPSED TIME: " + elapsedTime.toUpperCase());
}

From source file:com.aerospike.load.AerospikeLoad.java

public static void main(String[] args) throws IOException {

    Thread statPrinter = new Thread(new PrintStat(counters));
    try {/* w  w w  .  j  a  v  a  2  s.co  m*/
        log.info("Aerospike loader started");
        Options options = new Options();
        options.addOption("h", "host", true, "Server hostname (default: localhost)");
        options.addOption("p", "port", true, "Server port (default: 3000)");
        options.addOption("n", "namespace", true, "Namespace (default: test)");
        options.addOption("s", "set", true, "Set name. (default: null)");
        options.addOption("c", "config", true, "Column definition file name");
        options.addOption("wt", "write-threads", true,
                "Number of writer threads (default: Number of cores * 5)");
        options.addOption("rt", "read-threads", true,
                "Number of reader threads (default: Number of cores * 1)");
        options.addOption("l", "rw-throttle", true, "Throttling of reader to writer(default: 10k) ");
        options.addOption("tt", "transaction-timeout", true,
                "write transaction timeout in miliseconds(default: No timeout)");
        options.addOption("et", "expiration-time", true,
                "Expiration time of records in seconds (default: never expire)");
        options.addOption("T", "timezone", true,
                "Timezone of source where data dump is taken (default: local timezone)");
        options.addOption("ec", "abort-error-count", true, "Error count to abort (default: 0)");
        options.addOption("wa", "write-action", true, "Write action if key already exists (default: update)");
        options.addOption("v", "verbose", false, "Logging all");
        options.addOption("u", "usage", false, "Print usage.");

        CommandLineParser parser = new PosixParser();
        CommandLine cl = parser.parse(options, args, false);

        if (args.length == 0 || cl.hasOption("u")) {
            logUsage(options);
            return;
        }

        if (cl.hasOption("l")) {
            rwThrottle = Integer.parseInt(cl.getOptionValue("l"));
        } else {
            rwThrottle = Constants.READLOAD;
        }
        // Get all command line options
        params = Utils.parseParameters(cl);

        //Get client instance
        AerospikeClient client = new AerospikeClient(params.host, params.port);
        if (!client.isConnected()) {
            log.error("Client is not able to connect:" + params.host + ":" + params.port);
            return;
        }

        if (params.verbose) {
            log.setLevel(Level.DEBUG);
        }

        // Get available processors to calculate default number of threads
        int cpus = Runtime.getRuntime().availableProcessors();
        nWriterThreads = cpus * scaleFactor;
        nReaderThreads = cpus;

        // Get writer thread count
        if (cl.hasOption("wt")) {
            nWriterThreads = Integer.parseInt(cl.getOptionValue("wt"));
            nWriterThreads = (nWriterThreads > 0
                    ? (nWriterThreads > Constants.MAX_THREADS ? Constants.MAX_THREADS : nWriterThreads)
                    : 1);
            log.debug("Using writer Threads: " + nWriterThreads);
        }
        writerPool = Executors.newFixedThreadPool(nWriterThreads);

        // Get reader thread count
        if (cl.hasOption("rt")) {
            nReaderThreads = Integer.parseInt(cl.getOptionValue("rt"));
            nReaderThreads = (nReaderThreads > 0
                    ? (nReaderThreads > Constants.MAX_THREADS ? Constants.MAX_THREADS : nReaderThreads)
                    : 1);
            log.debug("Using reader Threads: " + nReaderThreads);
        }

        String columnDefinitionFileName = cl.getOptionValue("c", null);

        log.debug("Column definition files/directory: " + columnDefinitionFileName);
        if (columnDefinitionFileName == null) {
            log.error("Column definition files/directory not specified. use -c <file name>");
            return;
        }

        File columnDefinitionFile = new File(columnDefinitionFileName);
        if (!columnDefinitionFile.exists()) {
            log.error("Column definition files/directory does not exist: "
                    + Utils.getFileName(columnDefinitionFileName));
            return;
        }

        // Get data file list
        String[] files = cl.getArgs();
        if (files.length == 0) {
            log.error("No data file Specified: add <file/dir name> to end of the command ");
            return;
        }
        List<String> allFileNames = new ArrayList<String>();
        allFileNames = Utils.getFileNames(files);
        if (allFileNames.size() == 0) {
            log.error("Given datafiles/directory does not exist");
            return;
        }
        for (int i = 0; i < allFileNames.size(); i++) {
            log.debug("File names:" + Utils.getFileName(allFileNames.get(i)));
            File file = new File(allFileNames.get(i));
            counters.write.recordTotal = counters.write.recordTotal + file.length();
        }

        //remove column definition file from list
        allFileNames.remove(columnDefinitionFileName);

        log.info("Number of data files:" + allFileNames.size());

        /**
         * Process column definition file to get meta data and bin mapping.
         */
        metadataColumnDefs = new ArrayList<ColumnDefinition>();
        binColumnDefs = new ArrayList<ColumnDefinition>();
        metadataConfigs = new HashMap<String, String>();

        if (Parser.processJSONColumnDefinitions(columnDefinitionFile, metadataConfigs, metadataColumnDefs,
                binColumnDefs, params)) {
            log.info("Config file processed.");
        } else {
            throw new Exception("Config file parsing Error");
        }

        // Add metadata of config to parameters
        String metadata;
        if ((metadata = metadataConfigs.get(Constants.INPUT_TYPE)) != null) {
            params.fileType = metadata;
            if (params.fileType.equals(Constants.CSV_FILE)) {

                // Version check
                metadata = metadataConfigs.get(Constants.VERSION);
                String[] vNumber = metadata.split("\\.");
                int v1 = Integer.parseInt(vNumber[0]);
                int v2 = Integer.parseInt(vNumber[1]);
                if ((v1 <= Constants.MajorV) && (v2 <= Constants.MinorV)) {
                    log.debug("Config version used:" + metadata);
                } else
                    throw new Exception("\"" + Constants.VERSION + ":" + metadata + "\" is not Supported");

                // Set delimiter 
                if ((metadata = metadataConfigs.get(Constants.DELIMITER)) != null && metadata.length() == 1) {
                    params.delimiter = metadata.charAt(0);
                } else {
                    log.warn("\"" + Constants.DELIMITER + ":" + metadata
                            + "\" is not properly specified in config file. Default is ','");
                }

                if ((metadata = metadataConfigs.get(Constants.IGNORE_FIRST_LINE)) != null) {
                    params.ignoreFirstLine = metadata.equals("true");
                } else {
                    log.warn("\"" + Constants.IGNORE_FIRST_LINE + ":" + metadata
                            + "\" is not properly specified in config file. Default is false");
                }

                if ((metadata = metadataConfigs.get(Constants.COLUMNS)) != null) {
                    counters.write.colTotal = Integer.parseInt(metadata);
                } else {
                    throw new Exception("\"" + Constants.COLUMNS + ":" + metadata
                            + "\" is not properly specified in config file");
                }
            } else {
                throw new Exception("\"" + params.fileType + "\" is not supported in config file");
            }
        } else {
            throw new Exception("\"" + Constants.INPUT_TYPE + "\" is not specified in config file");
        }

        // add config input to column definitions
        if (params.fileType.equals(Constants.CSV_FILE)) {
            List<String> binName = null;
            if (params.ignoreFirstLine) {
                String line;
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(new FileInputStream(allFileNames.get(0)), "UTF8"));
                if ((line = br.readLine()) != null) {
                    binName = Parser.getCSVRawColumns(line, params.delimiter);
                    br.close();
                    if (binName.size() != counters.write.colTotal) {
                        throw new Exception("Number of column in config file and datafile are mismatch."
                                + " Datafile: " + Utils.getFileName(allFileNames.get(0)) + " Configfile: "
                                + Utils.getFileName(columnDefinitionFileName));
                    }
                }
            }

            //update columndefs for metadata
            for (int i = 0; i < metadataColumnDefs.size(); i++) {
                if (metadataColumnDefs.get(i).staticValue) {

                } else {
                    if (metadataColumnDefs.get(i).binValuePos < 0) {
                        if (metadataColumnDefs.get(i).columnName == null) {
                            if (metadataColumnDefs.get(i).jsonPath == null) {
                                log.error("dynamic metadata having improper info"
                                        + metadataColumnDefs.toString()); //TODO
                            } else {
                                //TODO check for json_path   
                            }
                        } else {
                            if (params.ignoreFirstLine) {
                                if (binName.indexOf(metadataColumnDefs.get(i).binValueHeader) != -1) {
                                    metadataColumnDefs.get(i).binValuePos = binName
                                            .indexOf(metadataColumnDefs.get(i).binValueHeader);
                                } else {
                                    throw new Exception("binName missing in data file:"
                                            + metadataColumnDefs.get(i).binValueHeader);
                                }
                            }
                        }
                    } else {
                        if (params.ignoreFirstLine)
                            metadataColumnDefs.get(i).binValueHeader = binName
                                    .get(metadataColumnDefs.get(i).binValuePos);
                    }
                }
                if ((!metadataColumnDefs.get(i).staticValue) && (metadataColumnDefs.get(i).binValuePos < 0)) {
                    throw new Exception("Information for bin mapping is missing in config file:"
                            + metadataColumnDefs.get(i));
                }

                if (metadataColumnDefs.get(i).srcType == null) {
                    throw new Exception(
                            "Source data type is not properly mentioned:" + metadataColumnDefs.get(i));
                }

                if (metadataColumnDefs.get(i).binNameHeader == Constants.SET
                        && !metadataColumnDefs.get(i).srcType.equals(SrcColumnType.STRING)) {
                    throw new Exception("Set name should be string type:" + metadataColumnDefs.get(i));
                }

                if (metadataColumnDefs.get(i).binNameHeader.equalsIgnoreCase(Constants.SET)
                        && params.set != null) {
                    throw new Exception(
                            "Set name is given both in config file and commandline. Provide only once.");
                }
            }

            //update columndefs for bins
            for (int i = 0; i < binColumnDefs.size(); i++) {
                if (binColumnDefs.get(i).staticName) {

                } else {
                    if (binColumnDefs.get(i).binNamePos < 0) {
                        if (binColumnDefs.get(i).columnName == null) {
                            if (binColumnDefs.get(i).jsonPath == null) {
                                log.error("dynamic bin having improper info"); //TODO
                            } else {
                                //TODO check for json_path
                            }
                        } else {
                            if (params.ignoreFirstLine) {
                                if (binName.indexOf(binColumnDefs.get(i).binNameHeader) != -1) {
                                    binColumnDefs.get(i).binNamePos = binName
                                            .indexOf(binColumnDefs.get(i).binNameHeader);
                                } else {
                                    throw new Exception("binName missing in data file:"
                                            + binColumnDefs.get(i).binNameHeader);
                                }
                            }
                        }
                    } else {
                        if (params.ignoreFirstLine)
                            binColumnDefs.get(i).binNameHeader = binName.get(binColumnDefs.get(i).binNamePos);
                    }
                }

                if (binColumnDefs.get(i).staticValue) {

                } else {
                    if (binColumnDefs.get(i).binValuePos < 0) {
                        if (binColumnDefs.get(i).columnName == null) {
                            if (binColumnDefs.get(i).jsonPath == null) {
                                log.error("dynamic bin having improper info"); //TODO
                            } else {
                                //TODO check for json_path
                            }
                        } else {
                            if (params.ignoreFirstLine) {
                                if (binName.contains(binColumnDefs.get(i).binValueHeader)) {
                                    binColumnDefs.get(i).binValuePos = binName
                                            .indexOf(binColumnDefs.get(i).binValueHeader);
                                } else if (!binColumnDefs.get(i).binValueHeader.toLowerCase()
                                        .equals(Constants.SYSTEM_TIME)) {
                                    throw new Exception("Wrong column name mentioned in config file:"
                                            + binColumnDefs.get(i).binValueHeader);
                                }
                            }
                        }
                    } else {
                        if (params.ignoreFirstLine)
                            binColumnDefs.get(i).binValueHeader = binName.get(binColumnDefs.get(i).binValuePos);
                    }

                    //check for missing entries in config file
                    if (binColumnDefs.get(i).binValuePos < 0 && binColumnDefs.get(i).binValueHeader == null) {
                        throw new Exception("Information missing(Value header or bin mapping) in config file:"
                                + binColumnDefs.get(i));
                    }

                    //check for proper data type in config file.
                    if (binColumnDefs.get(i).srcType == null) {
                        throw new Exception(
                                "Source data type is not properly mentioned:" + binColumnDefs.get(i));
                    }

                    //check for valid destination type
                    if ((binColumnDefs.get(i).srcType.equals(SrcColumnType.TIMESTAMP)
                            || binColumnDefs.get(i).srcType.equals(SrcColumnType.BLOB))
                            && binColumnDefs.get(i).dstType == null) {
                        throw new Exception("Destination type is not mentioned: " + binColumnDefs.get(i));
                    }

                    //check for encoding
                    if (binColumnDefs.get(i).dstType != null && binColumnDefs.get(i).encoding == null) {
                        throw new Exception(
                                "Encoding is not given for src-dst type conversion:" + binColumnDefs.get(i));
                    }

                    //check for valid encoding
                    if (binColumnDefs.get(i).srcType.equals(SrcColumnType.BLOB)
                            && !binColumnDefs.get(i).encoding.equals(Constants.HEX_ENCODING)) {
                        throw new Exception("Wrong encoding for blob data:" + binColumnDefs.get(i));
                    }
                }

                //Check static bin name mapped to dynamic bin value
                if ((binColumnDefs.get(i).binNamePos == binColumnDefs.get(i).binValuePos)
                        && (binColumnDefs.get(i).binNamePos != -1)) {
                    throw new Exception("Static bin name mapped to dynamic bin value:" + binColumnDefs.get(i));
                }

                //check for missing entries in config file
                if (binColumnDefs.get(i).binNameHeader == null
                        && binColumnDefs.get(i).binNameHeader.length() > Constants.BIN_NAME_LENGTH) {
                    throw new Exception("Information missing binName or large binName in config file:"
                            + binColumnDefs.get(i));
                }
            }
        }

        log.info(params.toString());
        log.debug("MetadataConfig:" + metadataColumnDefs);
        log.debug("BinColumnDefs:" + binColumnDefs);

        // Start PrintStat thread
        statPrinter.start();

        // Reader pool size
        ExecutorService readerPool = Executors.newFixedThreadPool(
                nReaderThreads > allFileNames.size() ? allFileNames.size() : nReaderThreads);
        log.info("Reader pool size : " + nReaderThreads);

        // Submit all tasks to writer threadpool.
        for (String aFile : allFileNames) {
            log.debug("Submitting task for: " + aFile);
            readerPool.submit(new AerospikeLoad(aFile, client, params));
        }

        // Wait for reader pool to complete
        readerPool.shutdown();
        log.info("Shutdown down reader thread pool");

        while (!readerPool.isTerminated())
            ;
        //readerPool.awaitTermination(20, TimeUnit.MINUTES);
        log.info("Reader thread pool terminated");

        // Wait for writer pool to complete after getting all tasks from reader pool
        writerPool.shutdown();
        log.info("Shutdown down writer thread pool");

        while (!writerPool.isTerminated())
            ;
        log.info("Writer thread pool terminated");

        // Print final statistic of aerospike-loader.
        log.info("Final Statistics of importer: (Succesfull Writes = " + counters.write.writeCount.get() + ", "
                + "Errors="
                + (counters.write.writeErrors.get() + counters.write.readErrors.get()
                        + counters.write.processingErrors.get())
                + "(" + (counters.write.writeErrors.get()) + "-Write," + counters.write.readErrors.get()
                + "-Read," + counters.write.processingErrors.get() + "-Processing)");
    } catch (Exception e) {
        log.error(e);
        if (log.isDebugEnabled()) {
            e.printStackTrace();
        }
    } finally {
        // Stop statistic printer thread.
        statPrinter.interrupt();
        log.info("Aerospike loader completed");
    }
}

From source file:com.servoy.extensions.plugins.http.HttpProvider.java

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        System.out.println("Use: ContentFetcher mainurl contenturl destdir"); //$NON-NLS-1$
        System.out.println(/*w ww .j  a v  a2 s  .c  o m*/
                "Example: ContentFetcher http://site.com http://site.com/dir[0-2]/image_A[001-040].jpg c:/temp"); //$NON-NLS-1$
        System.out.println(
                "Result: accessing http://site.com for cookie, reading http://site.com/dir1/image_A004.jpg writing c:/temp/dir_1_image_A004.jpg"); //$NON-NLS-1$
    } else {
        String url = args[1];
        String destdir = args[2];

        List parts = new ArrayList();
        int dir_from = 0;
        int dir_to = 0;
        int dir_fill = 0;
        int from = 0;
        int to = 0;
        int fill = 0;

        StringTokenizer tk = new StringTokenizer(url, "[]", true); //$NON-NLS-1$
        boolean hasDir = (tk.countTokens() > 5);
        boolean inDir = hasDir;
        System.out.println("hasDir " + hasDir); //$NON-NLS-1$
        boolean inTag = false;
        while (tk.hasMoreTokens()) {
            String token = tk.nextToken();
            if (token.equals("[")) //$NON-NLS-1$
            {
                inTag = true;
                continue;
            }
            if (token.equals("]")) //$NON-NLS-1$
            {
                inTag = false;
                if (inDir)
                    inDir = false;
                continue;
            }
            if (inTag) {
                int idx = token.indexOf('-');
                String s_from = token.substring(0, idx);
                int a_from = new Integer(s_from).intValue();
                int a_fill = s_from.length();
                int a_to = new Integer(token.substring(idx + 1)).intValue();
                if (inDir) {
                    dir_from = a_from;
                    dir_to = a_to;
                    dir_fill = a_fill;
                } else {
                    from = a_from;
                    to = a_to;
                    fill = a_fill;
                }
            } else {
                parts.add(token);
            }
        }

        DefaultHttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
        HttpGet main = new HttpGet(args[0]);
        HttpResponse res = client.execute(main);
        ;
        int main_rs = res.getStatusLine().getStatusCode();
        if (main_rs != 200) {
            System.out.println("main page retrieval failed " + main_rs); //$NON-NLS-1$
            return;
        }

        for (int d = dir_from; d <= dir_to; d++) {
            String dir_number = "" + d; //$NON-NLS-1$
            if (dir_fill > 1) {
                dir_number = "000000" + d; //$NON-NLS-1$
                int dir_digits = (int) (Math.log(fill) / Math.log(10));
                System.out.println("dir_digits " + dir_digits); //$NON-NLS-1$
                dir_number = dir_number.substring(dir_number.length() - (dir_fill - dir_digits),
                        dir_number.length());
            }
            for (int i = from; i <= to; i++) {
                try {
                    String number = "" + i; //$NON-NLS-1$
                    if (fill > 1) {
                        number = "000000" + i; //$NON-NLS-1$
                        int digits = (int) (Math.log(fill) / Math.log(10));
                        System.out.println("digits " + digits); //$NON-NLS-1$
                        number = number.substring(number.length() - (fill - digits), number.length());
                    }
                    int part = 0;
                    StringBuffer surl = new StringBuffer((String) parts.get(part++));
                    if (hasDir) {
                        surl.append(dir_number);
                        surl.append(parts.get(part++));
                    }
                    surl.append(number);
                    surl.append(parts.get(part++));
                    System.out.println("reading url " + surl); //$NON-NLS-1$

                    int indx = surl.toString().lastIndexOf('/');
                    StringBuffer sfile = new StringBuffer(destdir);
                    sfile.append("\\"); //$NON-NLS-1$
                    if (hasDir) {
                        sfile.append("dir_"); //$NON-NLS-1$
                        sfile.append(dir_number);
                        sfile.append("_"); //$NON-NLS-1$
                    }
                    sfile.append(surl.toString().substring(indx + 1));
                    File file = new File(sfile.toString());
                    if (file.exists()) {
                        file = new File("" + System.currentTimeMillis() + sfile.toString());
                    }
                    System.out.println("write file " + file.getAbsolutePath()); //$NON-NLS-1$

                    //                  URL iurl = createURLFromString(surl.toString());
                    HttpGet get = new HttpGet(surl.toString());

                    HttpResponse response = client.execute(get);
                    int result = response.getStatusLine().getStatusCode();
                    System.out.println("page http result " + result); //$NON-NLS-1$
                    if (result == 200) {
                        InputStream is = response.getEntity().getContent();
                        FileOutputStream fos = new FileOutputStream(file);
                        Utils.streamCopy(is, fos);
                        fos.close();
                    }
                } catch (Exception e) {
                    System.err.println(e);
                }
            }
        }
    }
}

From source file:de.prozesskraft.pkraft.Startinstance.java

public static void main(String[] args) throws org.apache.commons.cli.ParseException, IOException {

    /*----------------------------
      get options from ini-file/*from   w w w.  ja  v a2 s . co  m*/
    ----------------------------*/
    java.io.File inifile = new java.io.File(WhereAmI.getInstallDirectoryAbsolutePath(Startinstance.class) + "/"
            + "../etc/pkraft-startinstance.ini");

    if (inifile.exists()) {
        try {
            ini = new Ini(inifile);
        } catch (InvalidFileFormatException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } else {
        System.err.println("ini file does not exist: " + inifile.getAbsolutePath());
        System.exit(1);
    }

    /*----------------------------
      create boolean options
    ----------------------------*/
    Option ohelp = new Option("help", "print this message");
    Option ov = new Option("v", "prints version and build-date");

    /*----------------------------
      create argument options
    ----------------------------*/
    Option obasedir = OptionBuilder.withArgName("DIR").hasArg()
            .withDescription("[optional, default .] base directory where instance shourd run.")
            //            .isRequired()
            .create("basedir");

    Option odefinition = OptionBuilder.withArgName("FILE").hasArg()
            .withDescription("[optional] definition file of the process you want to start an instance from.")
            //            .isRequired()
            .create("definition");

    Option onostart = OptionBuilder.withArgName("")
            //            .hasArg()
            .withDescription(
                    "[optional] oppresses the start of the instance. (only create the process-instance)")
            //            .isRequired()
            .create("nostart");

    Option opdomain = OptionBuilder.withArgName("STRING").hasArg()
            .withDescription("[optional] domain of the process (mandatory if you omit -definition)")
            //            .isRequired()
            .create("pdomain");

    Option opname = OptionBuilder.withArgName("STRING").hasArg().withDescription(
            "[optional] name of the process you want to start an instance from (mandatory if you omit -definition)")
            //            .isRequired()
            .create("pname");

    Option opversion = OptionBuilder.withArgName("STRING").hasArg().withDescription(
            "[optional] version of the process you want to start an instance from (mandatory if you omit -definition)")
            //            .isRequired()
            .create("pversion");

    Option ocommitfile = OptionBuilder.withArgName("KEY=FILE; FILE").hasArg()
            .withDescription("[optional] this file will be committed as file. omit KEY= if KEY==FILENAME.")
            //            .isRequired()
            .create("commitfile");

    Option ocommitvariable = OptionBuilder.withArgName("KEY=VALUE; VALUE").hasArg()
            .withDescription("[optional] this string will be committed as a variable. omit KEY= if KEY==VALUE")
            //            .isRequired()
            .create("commitvariable");

    Option ocommitfiledummy = OptionBuilder.withArgName("KEY=FILE; FILE").hasArg().withDescription(
            "[optional] use this parameter like --commitfile. the file will not be checked against the process interface and therefore allows to commit files which are not expected by the process definition. use this parameter only for test purposes e.g. to commit dummy output files for accelerated tests of complex processes or the like.")
            //            .isRequired()
            .create("commitfiledummy");

    Option ocommitvariabledummy = OptionBuilder.withArgName("KEY=VALUE; VALUE").hasArg().withDescription(
            "[optional] use this parameter like --commitvariable. the variable will not be checked against the process interface and therefore allows to commit variables which are not expected by the process definition. use this parameter only for test purposes e.g. to commit dummy output variables for accelerated tests of complex processes or the like.")
            //            .isRequired()
            .create("commitvariabledummy");

    /*----------------------------
      create options object
    ----------------------------*/
    Options options = new Options();

    options.addOption(ohelp);
    options.addOption(ov);
    options.addOption(obasedir);
    options.addOption(odefinition);
    options.addOption(onostart);
    options.addOption(opname);
    options.addOption(opversion);
    options.addOption(ocommitfile);
    options.addOption(ocommitvariable);
    options.addOption(ocommitfiledummy);
    options.addOption(ocommitvariabledummy);

    /*----------------------------
      create the parser
    ----------------------------*/
    CommandLineParser parser = new GnuParser();
    // parse the command line arguments
    commandline = parser.parse(options, args);

    /*----------------------------
      usage/help
    ----------------------------*/
    if (commandline.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("startinstance", options);
        System.exit(0);
    }

    if (commandline.hasOption("v")) {
        System.out.println("author:  alexander.vogel@caegroup.de");
        System.out.println("version: [% version %]");
        System.out.println("date:    [% date %]");
        System.exit(0);
    }
    /*----------------------------
      ueberpruefen ob eine schlechte kombination von parametern angegeben wurde
    ----------------------------*/
    if (!(commandline.hasOption("definition")) && (!(commandline.hasOption("pname"))
            || !(commandline.hasOption("pversion")) || !(commandline.hasOption("pdomain")))) {
        System.err.println("option -definition or the options -pname & -pversion & -pdomain are mandatory");
        exiter();
    }

    if ((commandline.hasOption("definition") && ((commandline.hasOption("pversion"))
            || (commandline.hasOption("pname")) || (commandline.hasOption("pdomain"))))) {
        System.err.println("you must not use option -definition with -pversion or -pname or -pdomain");
        exiter();
    }

    /*----------------------------
      die lizenz ueberpruefen und ggf abbrechen
    ----------------------------*/

    // check for valid license
    ArrayList<String> allPortAtHost = new ArrayList<String>();
    allPortAtHost.add(ini.get("license-server", "license-server-1"));
    allPortAtHost.add(ini.get("license-server", "license-server-2"));
    allPortAtHost.add(ini.get("license-server", "license-server-3"));

    MyLicense lic = new MyLicense(allPortAtHost, "1", "user-edition", "0.1");

    // lizenz-logging ausgeben
    for (String actLine : (ArrayList<String>) lic.getLog()) {
        System.err.println(actLine);
    }

    // abbruch, wenn lizenz nicht valide
    if (!lic.isValid()) {
        System.exit(1);
    }

    /*----------------------------
      die eigentliche business logic
    ----------------------------*/

    Process p1 = new Process();
    String pathToDefinition = "";

    if (commandline.hasOption("definition")) {
        pathToDefinition = commandline.getOptionValue("definition");
    } else if (commandline.hasOption("pname") && commandline.hasOption("pversion")
            && commandline.hasOption("pdomain")) {
        pathToDefinition.replaceAll("/+$", "");
        pathToDefinition = ini.get("process", "domain-installation-directory") + "/"
                + commandline.getOptionValue("pdomain") + "/" + commandline.getOptionValue("pname") + "/"
                + commandline.getOptionValue("pversion") + "/process.xml";
    } else {
        System.err.println("option -definition or the options -pname & -pversion & -pdomain are mandatory");
        exiter();
    }

    // check ob das ermittelte oder uebergebene xml-file ueberhaupt existiert
    java.io.File xmlDefinition = new java.io.File(pathToDefinition);
    if (!(xmlDefinition.exists()) || !(xmlDefinition.isFile())) {
        System.err.println("process definition does not exist: " + pathToDefinition);
        exiter();
    }

    p1.setInfilexml(xmlDefinition.getCanonicalPath());
    Process p2 = null;

    try {
        p2 = p1.readXml();
    } catch (JAXBException e1) {
        System.err.println(e1.getMessage());
    }

    // das processBinary vorneweg schon mal erstellen, da es sein kann das das committen laenger dauert und ein pradar-attend den neuen prozess schon mal aufnehmen will
    // root-verzeichnis erstellen
    if (commandline.hasOption("basedir")) {
        p2.setBaseDir(commandline.getOptionValue("basedir"));
    }

    p2.makeRootdir();

    // den pfad fuers binary setzen
    p2.setOutfilebinary(p2.getRootdir() + "/process.pmb");

    System.err.println("info: writing process instance " + p2.getOutfilebinary());

    // binary schreiben
    p2.writeBinary();

    // step, an den die commits gehen, soll 'root' sein.
    Step stepRoot = p2.getRootStep();

    // committen von files (ueber einen glob)
    if (commandline.hasOption("commitfile")) {
        for (String actOptionCommitfile : commandline.getOptionValues("commitfile")) {
            String[] parts = actOptionCommitfile.split("=");
            File userFile = new File();

            if (parts.length == 1) {
                userFile.setKey(new java.io.File(parts[0]).getName());
                userFile.setGlob(parts[0]);
            } else if (parts.length == 2) {
                userFile.setKey(parts[0]);
                userFile.setGlob(parts[1]);
            } else {
                System.err.println("error in option -commitfile " + actOptionCommitfile);
                exiter();
            }

            // die auf der kommandozeile uebergebenen Informationen sollen in die vorhandenen commits im rootStep gemappt werden
            // alle vorhandenen commits in step root durchgehen und dem passenden file zuordnen
            for (Commit actCommit : stepRoot.getCommit()) {
                // alle files des aktuellen commits
                for (File actFile : actCommit.getFile()) {
                    if (actFile.getKey().equals(userFile.getKey())) {
                        // wenn actFile schon ein valider eintrag ist, dann soll ein klon befuellt werden
                        if (actFile.getGlob() != null) {
                            // wenn die maximale erlaubte anzahl noch nicht erreicht ist
                            if (actCommit.getFile(actFile.getKey()).size() < actFile.getMaxoccur()) {
                                File newFile = actFile.clone();
                                newFile.setGlob(userFile.getGlob());
                                System.err.println("entering file into commit '" + actCommit.getName() + "' ("
                                        + newFile.getKey() + "=" + newFile.getGlob() + ")");
                                actCommit.addFile(newFile);
                                break;
                            } else {
                                System.err.println("fatal: you only may commit " + actFile.getMaxoccur() + " "
                                        + actFile.getKey() + "-files into commit " + actCommit.getName());
                                exiter();
                            }
                        }
                        // ansonsten das bereits vorhandene file im commit mit den daten befuellen
                        else {
                            actFile.setGlob(userFile.getGlob());
                            actFile.setGlobdir(p2.getBaseDir());
                            System.err.println("entering file into commit '" + actCommit.getName() + "' ("
                                    + actFile.getKey() + "=" + actFile.getGlob() + ")");
                            break;
                        }
                    }
                }
            }
        }
    }

    // committen von files (ueber einen glob)
    if (commandline.hasOption("commitfiledummy")) {
        // diese files werden nicht in bestehende commits der prozessdefinition eingetragen, sondern in ein spezielles commit
        Commit commitFiledummy = new Commit();
        commitFiledummy.setName("fileDummy");
        stepRoot.addCommit(commitFiledummy);

        for (String actOptionCommitfiledummy : commandline.getOptionValues("commitfiledummy")) {
            String[] parts = actOptionCommitfiledummy.split("=");
            File userFile = new File();
            commitFiledummy.addFile(userFile);

            if (parts.length == 1) {
                userFile.setKey(new java.io.File(parts[0]).getName());
                userFile.setGlob(parts[0]);
            } else if (parts.length == 2) {
                userFile.setKey(parts[0]);
                userFile.setGlob(parts[1]);
            } else {
                System.err.println("error in option -commitfiledummy " + actOptionCommitfiledummy);
                exiter();
            }
            userFile.setGlobdir(p2.getBaseDir());
            System.err.println("entering (dummy-)file into commit '" + commitFiledummy.getName() + "' ("
                    + userFile.getKey() + "=" + userFile.getGlob() + ")");
        }
    }

    if (commandline.hasOption("commitvariable")) {
        for (String actOptionCommitvariable : commandline.getOptionValues("commitvariable")) {
            if (actOptionCommitvariable.matches(".+=.+")) {
                String[] parts = actOptionCommitvariable.split("=");
                Variable userVariable = new Variable();

                if (parts.length == 1) {
                    userVariable.setKey("default");
                    userVariable.setValue(parts[0]);
                } else if (parts.length == 2) {
                    userVariable.setKey(parts[0]);
                    userVariable.setValue(parts[1]);
                } else {
                    System.err.println("error in option -commitvariable");
                    exiter();
                }
                //                  commit.addVariable(variable);

                // die auf der kommandozeile uebergebenen Informationen sollen in die vorhandenen commits im rootStep gemappt werden
                // alle vorhandenen commits in step root durchgehen und dem passenden file zuordnen
                for (Commit actCommit : stepRoot.getCommit()) {
                    // alle files des aktuellen commits
                    for (Variable actVariable : actCommit.getVariable()) {
                        if (actVariable.getKey().equals(userVariable.getKey())) {
                            // wenn actFile schon ein valider eintrag ist, dann soll ein klon befuellt werden
                            if (actVariable.getGlob() != null) {
                                // wenn die maximale erlaubte anzahl noch nicht erreicht ist
                                if (actCommit.getVariable(actVariable.getKey()).size() < actVariable
                                        .getMaxoccur()) {
                                    Variable newVariable = actVariable.clone();
                                    newVariable.setValue(userVariable.getValue());
                                    System.err.println("entering variable into commit '" + actCommit.getName()
                                            + "' (" + newVariable.getKey() + "=" + newVariable.getValue()
                                            + ")");
                                    actCommit.addVariable(newVariable);
                                    break;
                                } else {
                                    System.err.println("fatal: you only may commit " + actVariable.getMaxoccur()
                                            + " " + actVariable.getKey() + "-variable(s) into commit "
                                            + actCommit.getName());
                                    exiter();
                                }
                            }
                            // ansonsten das bereits vorhandene file im commit mit den daten befuellen
                            else {
                                actVariable.setValue(userVariable.getValue());
                                System.err.println("entering variable into commit '" + actCommit.getName()
                                        + "' (" + actVariable.getKey() + "=" + actVariable.getValue() + ")");
                                break;
                            }
                        }
                    }
                }
            } else {
                System.err.println("-commitvariable " + actOptionCommitvariable
                        + " does not match pattern \"NAME=VALUE\".");
                exiter();
            }

        }
    }

    if (commandline.hasOption("commitvariabledummy")) {
        // diese files werden nicht in bestehende commits der prozessdefinition eingetragen, sondern in ein spezielles commit
        Commit commitVariabledummy = new Commit();
        commitVariabledummy.setName("variableDummy");
        stepRoot.addCommit(commitVariabledummy);

        for (String actOptionCommitvariabledummy : commandline.getOptionValues("commitvariabledummy")) {
            String[] parts = actOptionCommitvariabledummy.split("=");
            Variable userVariable = new Variable();
            commitVariabledummy.addVariable(userVariable);

            if (parts.length == 1) {
                userVariable.setKey(parts[0]);
                userVariable.setValue(parts[0]);
            } else if (parts.length == 2) {
                userVariable.setKey(parts[0]);
                userVariable.setValue(parts[1]);
            } else {
                System.err.println("error in option -commitvariabledummy");
                exiter();
            }

            System.err.println("entering variable into commit '" + commitVariabledummy.getName() + "' ("
                    + userVariable.getKey() + "=" + userVariable.getValue() + ")");
        }
    }

    //      if (commandline.hasOption("basedir"))
    //      {
    //         p2.setBaseDir(commandline.getOptionValue("basedir"));
    //      }

    //         commit.doIt();
    stepRoot.commit();

    // root-verzeichnis erstellen
    p2.makeRootdir();

    // den pfad fuers binary setzen
    //      p2.setOutfilebinary(p2.getRootdir() + "/process.pmb");

    System.err.println("info: writing process instance " + p2.getOutfilebinary());

    // binary schreiben
    p2.writeBinary();

    try {
        Thread.sleep(1500, 0);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //         Runtime.getRuntime().exec("process-manager -help");

    // starten nur, falls es nicht abgewaehlt wurde
    if (!commandline.hasOption("nostart")) {
        System.err.println("info: starting processmanager for instance " + p2.getOutfilebinary());
        String aufrufString = ini.get("apps", "pkraft-manager") + " -instance " + p2.getOutfilebinary();
        System.err.println("calling: " + aufrufString);

        ArrayList<String> processSyscallWithArgs = new ArrayList<String>(
                Arrays.asList(aufrufString.split(" ")));

        ProcessBuilder pb = new ProcessBuilder(processSyscallWithArgs);

        //      ProcessBuilder pb = new ProcessBuilder("processmanager -instance "+p2.getOutfilebinary());
        //      Map<String,String> env = pb.environment();
        //      String path = env.get("PATH");
        //      System.out.println("PATH: "+path);
        //      
        java.lang.Process p = pb.start();
        System.err.println("pid: " + p.hashCode());
    } else {
        System.err.println("info: NOT starting processmanager for instance " + p2.getOutfilebinary());
    }

}

From source file:edu.stanford.muse.launcher.Main.java

public static void main(String args[]) throws Exception {

    // set javawebstart.version to a dummy value if not already set (might happen when running with java -jar from cmd line)
    // exit.jsp doesn't allow us to showdown unless this prop is set
    if (System.getProperty("javawebstart.version") == null)
        System.setProperty("javawebstart.version", "UNKNOWN");

    final int TIMEOUT_SECS = 60;
    if (args.length > 0) {
        out.print(args.length + " argument(s): ");
        for (int i = 0; i < args.length; i++)
            out.print(args[i] + " ");
        out.println();//from  w  ww.java 2s  .c  o  m
    }

    Options options = getOpt();
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Muse batch mode", options);
        return;
    }

    boolean debug = false;
    if (cmd.hasOption("debug")) {
        URL url = ClassLoader.getSystemResource("log4j.properties.debug");
        out.println("Loading logging configuration from url: " + url);
        PropertyConfigurator.configure(url);
        debug = true;
    } else if (cmd.hasOption("debug-address-book")) {
        URL url = ClassLoader.getSystemResource("log4j.properties.debug.ab");
        out.println("Loading logging configuration from url: " + url);
        PropertyConfigurator.configure(url);
        debug = false;
    } else if (cmd.hasOption("debug-groups")) {
        URL url = ClassLoader.getSystemResource("log4j.properties.debug.groups");
        out.println("Loading logging configuration from url: " + url);
        PropertyConfigurator.configure(url);
        debug = false;
    }

    if (cmd.hasOption("no-browser-open"))
        browserOpen = false;

    if (cmd.hasOption("port")) {
        String portStr = cmd.getOptionValue('p');
        try {
            PORT = Integer.parseInt(portStr);
            String mesg = " Running on port: " + PORT;
            out.println(mesg);
        } catch (NumberFormatException nfe) {
            out.println("invalid port number " + portStr);
        }
    }

    if (cmd.hasOption("start-page"))
        startPage = cmd.getOptionValue("start-page");
    if (cmd.hasOption("base-dir"))
        baseDir = cmd.getOptionValue("base-dir");
    if (cmd.hasOption("search-mode"))
        searchMode = true;
    if (cmd.hasOption("amuse-mode"))
        amuseMode = true;

    System.setSecurityManager(null); // this is important
    WebAppContext webapp0 = null; // deployWarAt("root.war", "/"); // for redirecting
    String path = "/muse";
    WebAppContext webapp1 = deployWarAt("muse.war", path);
    if (webapp1 == null) {
        System.err.println("Aborting... no webapp");
        return;
    }

    // if in any debug mode, turn blurring off
    if (debug)
        webapp1.setAttribute("noblur", true);

    // we set this and its read by JSPHelper within the webapp 
    System.setProperty("muse.container", "jetty");

    // need to copy crossdomain.xml file for
    String tmp = System.getProperty("java.io.tmpdir");
    final URL url = Main.class.getClassLoader().getResource("crossdomain.xml");
    try {
        InputStream is = url.openStream();
        String file = tmp + File.separatorChar + "crossdomain.xml";
        copy_stream_to_file(is, file);
    } catch (Exception e) {
        System.err.println("Aborting..." + e);
        return;
    }
    server = new Server(PORT);
    ResourceHandler resource_handler = new ResourceHandler();
    //        resource_handler.setWelcomeFiles(new String[]{ "index.html" });
    resource_handler.setResourceBase(tmp);

    // set the header buffer size in the connectors, default is a ridiculous 4K, which causes failures any time there is
    // is a large request, such as selecting a few hundred folders. (even for posts!)
    // usually there is only one SocketConnector, so we just put the setHeaderBufferSize in a loop.
    Connector conns[] = server.getConnectors();
    for (Connector conn : conns) {
        int NEW_BUFSIZE = 1000000;
        // out.println ("Connector " + conn + " buffer size is " + conn.getHeaderBufferSize() + " setting to " + NEW_BUFSIZE);
        conn.setHeaderBufferSize(NEW_BUFSIZE);
    }

    BASE_URL = "http://localhost:" + PORT + path;
    String MUSE_CHECK_URL = BASE_URL + "/js/muse.js"; // for quick check of existing muse or successful start up. BASE_URL may take some time to run and may not always be available now that we set dirAllowed to false and public mode does not serve /muse.
    String debugFile = tmp + File.separatorChar + "debug.txt";

    HandlerList hl = new HandlerList();
    if (webapp0 != null)
        hl.setHandlers(new Handler[] { webapp1, webapp0, resource_handler });
    else
        hl.setHandlers(new Handler[] { webapp1, resource_handler });
    out.println("Starting up Muse on the local computer at " + BASE_URL + ", "
            + formatDateLong(new GregorianCalendar()));
    out.println("***For troubleshooting information, see this file: " + debugFile + "***\n");
    out.println("Current directory = " + System.getProperty("user.dir") + ", home directory = "
            + System.getProperty("user.home"));
    out.println("Memory status at the beginning: " + getMemoryStats());
    if (Runtime.getRuntime().maxMemory() / MB < 512)
        aggressiveWarn(
                "You are probably running Muse without enough memory. \nIf you launched Muse from the command line, you can increase memory with an option like java -Xmx1g",
                2000);
    server.setHandler(hl);

    // handle frequent error of user trying to launch another server when its already on
    // server.start() usually takes a few seconds to return
    // after that it takes a few seconds for the webapp to deploy
    // ignore any exceptions along the way and assume not if we can't prove it is alive
    boolean urlAlive = false;
    try {
        urlAlive = isURLAlive(MUSE_CHECK_URL);
    } catch (Exception e) {
        out.println("Exception: e");
        e.printStackTrace(out);
    }

    boolean disableStart = false;
    if (urlAlive) {
        out.println("Oh! Muse is already running at the URL: " + BASE_URL + ", will have to kill it!");
        killRunningServer(BASE_URL);
        Thread.sleep(3000);
        try {
            urlAlive = isURLAlive(MUSE_CHECK_URL);
        } catch (Exception e) {
            out.println("Exception: e");
            e.printStackTrace(out);
        }
        if (!urlAlive)
            out.println("Good. Kill succeeded, will restart");
        else {
            String message = "Previously running Muse still alive despite attempt to kill it, disabling fresh restart!\n";
            message += "If you just want to use the previous instance of Muse, please go to http://localhost:9099/muse\n";
            message += "\nTo kill this instance, please go to your computer's task manager and kill running java or javaw processes.\nThen try launching Muse again.\n";
            aggressiveWarn(message, 2000);
            return;
        }
    }
    //        else
    //          out.println ("Muse not already alive at URL: ..." + URL);

    if (!disableStart) {
        out.println("Starting Muse at URL: ..." + BASE_URL);
        try {
            server.start();
        } catch (BindException be) {
            out.println("port busy, but webapp not alive: " + BASE_URL + "\n" + be);
            throw new RuntimeException("Error: Port in use (Please kill Muse if its already running!)\n" + be);
        }
    }

    //      webapp1.start(); -- not needed
    PrintStream debugOut1 = System.err;
    try {
        File f = new File(debugFile);
        if (f.exists())
            f.delete(); // particular problem on windows :-(
        debugOut1 = new PrintStream(new FileOutputStream(debugFile), false, "UTF-8");
    } catch (IOException ioe) {
        System.err.println("Warning: failed to delete debug file " + debugFile + " : " + ioe);
    }

    final PrintStream debugOut = debugOut1;

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            try {
                server.stop();
                server.destroy();
                debugOut.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }));

    //        InfoFrame frame = new InfoFrame();
    //        frame.doShow();

    boolean success = waitTillPageAlive(MUSE_CHECK_URL, TIMEOUT_SECS);
    //        frame.updateText ("Opening a browser window");

    if (success) {
        // best effort to start shutdown thread
        //           out.println ("Starting Muse shutdown listener at port " + JettyShutdownThread.SHUTDOWN_PORT);

        try {
            int shutdownPort = PORT + 1; // shut down port is arbitrarily set to port + 1. it is ASSUMED to be free. 

            new JettyShutdownThread(server, shutdownPort).start();
            out.println("Listening for Muse shutdown message on port " + shutdownPort);
        } catch (Exception e) {
            out.println(
                    "Unable to start shutdown listener, you will have to stop the server manually using Cmd-Q on Mac OS or kill javaw processes on Windows");
        }

        try {
            setupSystemTrayIcon();
        } catch (Exception e) {
            out.println("Unable to setup system tray icon: " + e);
            e.printStackTrace(out);
        }

        // open browser window
        if (browserOpen) {
            preferredBrowser = null;
            // launch a browser here
            try {

                String link;
                if (System.getProperty("muse.mode.public") != null)
                    link = "http://localhost:" + PORT + "/muse/archives/";
                else
                    link = "http://localhost:" + PORT + "/muse/index.jsp";

                if (searchMode) {
                    String u = "http://localhost:" + PORT + "/muse/search";
                    out.println("Launching URL in browser: " + u);
                    link += "?mode=search";
                } else if (amuseMode) {
                    String u = "http://localhost:" + PORT + "/muse/amuse.jsp";
                    out.println("Launching URL in browser: " + u);
                    link = u;
                } else if (startPage != null) {
                    // startPage has to be absolute
                    link = "http://localhost:" + PORT + "/muse/" + startPage;
                }

                if (baseDir != null)
                    link = link + "?cacheDir=" + baseDir; // typically this is used when starting from command line. note: still using name, cacheDir

                out.println("Launching URL in browser: " + link);
                launchBrowser(link);

            } catch (Exception e) {
                out.println(
                        "Warning: Unable to launch browser due to exception (use the -n option to prevent Muse from trying to launch a browser):");
                e.printStackTrace(out);
            }
        }

        if (!cmd.hasOption("no-shutdown")) {
            // arrange to kill Muse after a period of time, we don't want the server to run forever

            // i clearly have too much time on my hands right now...
            long secs = KILL_AFTER_MILLIS / 1000;
            long hh = secs / 3600;
            long mm = (secs % 3600) / 60;
            long ss = secs % (60);
            out.print("Muse will shut down automatically after ");
            if (hh != 0)
                out.print(hh + " hours ");
            if (mm != 0 || (hh != 0 && ss != 0))
                out.print(mm + " minutes");
            if (ss != 0)
                out.print(ss + " seconds");
            out.println();

            Timer timer = new Timer();
            TimerTask tt = new ShutdownTimerTask();
            timer.schedule(tt, KILL_AFTER_MILLIS);
        }
    } else {
        out.println("\n\n\nSORRY!!! UNABLE TO DEPLOY WEBAPP, EXITING\n\n\n");
        //          frame.updateText("Sorry, looks like we are having trouble starting the jetty server\n");
    }

    savedSystemOut = out;
    savedSystemErr = System.err;
    System.setOut(debugOut);
    System.setErr(debugOut);
}

From source file:com.adobe.aem.demomachine.gui.AemDemo.java

public static void main(String[] args) {

    String demoMachineRootFolder = null;

    // Command line options for this tool
    Options options = new Options();
    options.addOption("f", true, "Path to Demo Machine root folder");
    CommandLineParser parser = new BasicParser();
    try {//from   www  . ja v a2 s  .  co m
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("f")) {
            demoMachineRootFolder = cmd.getOptionValue("f");
        }

    } catch (ParseException ex) {

        logger.error(ex.getMessage());

    }

    // Let's grab the version number for the core Maven file
    String mavenFilePath = (demoMachineRootFolder != null ? demoMachineRootFolder
            : System.getProperty("user.dir")) + File.separator + "java" + File.separator + "core"
            + File.separator + "pom.xml";
    File mavenFile = new File(mavenFilePath);
    if (mavenFile.exists() && !mavenFile.isDirectory()) {

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document;
            document = builder.parse(mavenFile);
            NodeList list = document.getElementsByTagName("version");
            if (list != null && list.getLength() > 0) {
                aemDemoMachineVersion = list.item(0).getFirstChild().getNodeValue();
            }

        } catch (Exception e) {
            logger.error("Can't parse Maven pom.xml file");
        }

    }

    // Let's check if we have a valid build.xml file to work with...
    String buildFilePath = (demoMachineRootFolder != null ? demoMachineRootFolder
            : System.getProperty("user.dir")) + File.separator + "build.xml";
    logger.debug("Trying to load build file from " + buildFilePath);
    buildFile = new File(buildFilePath);
    if (buildFile.exists() && !buildFile.isDirectory()) {

        // Launching the main window
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    UIManager.getLookAndFeelDefaults().put("defaultFont", new Font("Arial", Font.BOLD, 14));
                    AemDemo window = new AemDemo();
                    window.frameMain.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    } else {

        logger.error("No valid build.xml file to work with");
        System.exit(-1);

    }
}