Example usage for java.lang Runtime getRuntime

List of usage examples for java.lang Runtime getRuntime

Introduction

In this page you can find the example usage for java.lang Runtime getRuntime.

Prototype

public static Runtime getRuntime() 

Source Link

Document

Returns the runtime object associated with the current Java application.

Usage

From source file:net.bobah.mail.Dupes.java

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

    final CommandLineParser parser = new PosixParser();
    final Options options = new Options()
            .addOption("j", "threads", true, "number of parallel threads to use for analyzing")
            .addOption("hash", true,
                    "hash function to use, possible values: " + Arrays.toString(Hashes.values()))
            .addOption("dir", true, "add directory to search");
    final CommandLine cmdline = parser.parse(options, args);

    final int threads = Integer.valueOf(
            cmdline.getOptionValue("threads", String.valueOf(Runtime.getRuntime().availableProcessors())));
    final HashFunction hash = Hashes.valueOf(cmdline.getOptionValue("hash", "adler32")).hashfunc;
    final File[] dirs = Collections2
            .transform(Arrays.asList(cmdline.getOptionValues("dir")), new Function<String, File>() {
                @Override//from  ww  w .jav  a2  s  .c  om
                public File apply(String from) {
                    return new File(from);
                }
            }).toArray(new File[] {});

    log.info("hash: {}, threads: {}, dirs: {} in total", hash, threads, dirs.length);
    try {
        new Dupes(threads, hash, dirs).run();
    } finally {
        Utils.shutdownLogger();
    }
}

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  .jav  a  2s . co  m
 * @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:com.threadswarm.imagefeedarchiver.driver.CommandLineDriver.java

public static void main(String[] args) throws InterruptedException, ExecutionException, ParseException {
    // define available command-line options
    Options options = new Options();
    options.addOption("h", "help", false, "display usage information");
    options.addOption("u", "url", true, "RSS feed URL");
    options.addOption("a", "user-agent", true, "User-Agent header value to use when making HTTP requests");
    options.addOption("o", "output-directory", true, "output directory for downloaded images");
    options.addOption("t", "thread-count", true, "number of worker threads, defaults to cpu-count + 1");
    options.addOption("d", "delay", true, "delay between image downloads (in milliseconds)");
    options.addOption("p", "notrack", false, "tell websites that you don't wish to be tracked (DNT)");
    options.addOption("s", "https", false, "Rewrite image URLs to leverage SSL/TLS");

    CommandLineParser commandLineParser = new BasicParser();
    CommandLine commandLine = commandLineParser.parse(options, args);

    // print usage information if 'h'/'help' or no-args were given
    if (args.length == 0 || commandLine.hasOption("h")) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("java -jar ImageFeedArchiver.jar", options);
        return; //abort execution
    }//from w  w w.j  a v a 2  s. c  o  m

    URI rssFeedUri = null;
    if (commandLine.hasOption("u")) {
        String rssFeedUrlString = commandLine.getOptionValue("u");
        try {
            rssFeedUri = FeedUtils.getUriFromUrlString(rssFeedUrlString);
        } catch (MalformedURLException | URISyntaxException e) {
            LOGGER.error("The Feed URL you supplied was malformed or violated syntax rules.. exiting", e);
            System.exit(1);
        }
        LOGGER.info("Target RSS feed URL: {}", rssFeedUri);
    } else {
        throw new IllegalStateException("RSS feed URL was not specified!");
    }

    File outputDirectory = null;
    if (commandLine.hasOption("o")) {
        outputDirectory = new File(commandLine.getOptionValue("o"));
        if (!outputDirectory.isDirectory())
            throw new IllegalArgumentException("output directory must be a *directory*!");
        LOGGER.info("Using output directory: '{}'", outputDirectory);
    } else {
        throw new IllegalStateException("output directory was not specified!");
    }

    String userAgentString = null;
    if (commandLine.hasOption("a")) {
        userAgentString = commandLine.getOptionValue("a");
        LOGGER.info("Setting 'User-Agent' header value to '{}'", userAgentString);
    }

    int threadCount;
    if (commandLine.hasOption("t")) {
        threadCount = Integer.parseInt(commandLine.getOptionValue("t"));
    } else {
        threadCount = Runtime.getRuntime().availableProcessors() + 1;
    }
    LOGGER.info("Using {} worker threads", threadCount);

    long downloadDelay = 0;
    if (commandLine.hasOption("d")) {
        String downloadDelayString = commandLine.getOptionValue("d");
        downloadDelay = Long.parseLong(downloadDelayString);
    }
    LOGGER.info("Using a download-delay of {} milliseconds", downloadDelay);

    boolean doNotTrackRequested = commandLine.hasOption("p");

    boolean forceHttps = commandLine.hasOption("s");

    ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
    ((ConfigurableApplicationContext) context).registerShutdownHook();

    HttpClient httpClient = (HttpClient) context.getBean("httpClient");
    if (userAgentString != null)
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgentString);

    ProcessedRssItemDAO processedRssItemDAO = (ProcessedRssItemDAO) context.getBean("processedRssItemDAO");

    CommandLineDriver.Builder driverBuilder = new CommandLineDriver.Builder(rssFeedUri);
    driverBuilder.setDoNotTrackRequested(doNotTrackRequested).setOutputDirectory(outputDirectory)
            .setDownloadDelay(downloadDelay).setThreadCount(threadCount).setHttpClient(httpClient)
            .setForceHttps(forceHttps).setProcessedRssItemDAO(processedRssItemDAO);

    CommandLineDriver driver = driverBuilder.build();
    driver.run();
}

From source file:jsdp.app.inventory.univariate.CapacitatedStochasticLotSizing.java

public static void main(String args[]) {

    boolean simulate = true;

    /*******************************************************************
     * Problem parameters/*  www. j  a va  2s . c om*/
     */
    double fixedOrderingCost = 40;
    double proportionalOrderingCost = 0;
    double holdingCost = 1;
    double penaltyCost = 2;
    double maxOrderQuantity = 50;

    double[] meanDemand = { 20, 50, 20, 10, 20, 50 };
    double coefficientOfVariation = 0.2;
    double truncationQuantile = 0.99;

    // Random variables

    Distribution[] distributions = IntStream.iterate(0, i -> i + 1).limit(meanDemand.length)
            .mapToObj(i -> new NormalDist(meanDemand[i], meanDemand[i] * coefficientOfVariation))
            //.mapToObj(i -> new PoissonDist(meanDemand[i]))
            .toArray(Distribution[]::new);
    double[] supportLB = IntStream.iterate(0, i -> i + 1).limit(meanDemand.length)
            .mapToDouble(i -> distributions[i].inverseF(1 - truncationQuantile)).toArray();
    double[] supportUB = IntStream.iterate(0, i -> i + 1).limit(meanDemand.length)
            .mapToDouble(i -> distributions[i].inverseF(truncationQuantile)).toArray();
    double initialInventory = 0;

    /*******************************************************************
     * Model definition
     */

    // State space

    double stepSize = 1; //Stepsize must be 1 for discrete distributions
    double minState = -250;
    double maxState = 250;
    StateImpl.setStateBoundaries(stepSize, minState, maxState);

    // Actions

    Function<State, ArrayList<Action>> buildActionList = s -> {
        StateImpl state = (StateImpl) s;
        ArrayList<Action> feasibleActions = new ArrayList<Action>();
        for (double i = state.getInitialState(); i <= StateImpl.getMaxState()
                && i <= state.getInitialState() + maxOrderQuantity; i += StateImpl.getStepSize()) {
            feasibleActions.add(new ActionImpl(state, i - state.getInitialState()));
        }
        return feasibleActions;
    };

    Function<State, Action> idempotentAction = s -> new ActionImpl(s, 0);

    // Immediate Value Function

    ImmediateValueFunction<State, Action, Double> immediateValueFunction = (initialState, action,
            finalState) -> {
        ActionImpl a = (ActionImpl) action;
        StateImpl fs = (StateImpl) finalState;
        double orderingCost = a.getAction() > 0 ? (fixedOrderingCost + a.getAction() * proportionalOrderingCost)
                : 0;
        double holdingAndPenaltyCost = holdingCost * Math.max(fs.getInitialState(), 0)
                + penaltyCost * Math.max(-fs.getInitialState(), 0);
        return orderingCost + holdingAndPenaltyCost;
    };

    // Random Outcome Function

    RandomOutcomeFunction<State, Action, Double> randomOutcomeFunction = (initialState, action, finalState) -> {
        double realizedDemand = ((StateImpl) initialState).getInitialState() + ((ActionImpl) action).getAction()
                - ((StateImpl) finalState).getInitialState();
        return realizedDemand;
    };

    /*******************************************************************
     * Solve
     */

    // Sampling scheme

    SamplingScheme samplingScheme = SamplingScheme.NONE;
    int maxSampleSize = 200;
    double reductionFactorPerStage = 1;

    // Value Function Processing Method: backward recursion
    double discountFactor = 1.0;
    BackwardRecursionImpl recursion = new BackwardRecursionImpl(OptimisationDirection.MIN, distributions,
            supportLB, supportUB, immediateValueFunction, randomOutcomeFunction, buildActionList,
            idempotentAction, discountFactor, samplingScheme, maxSampleSize, reductionFactorPerStage,
            HashType.HASHTABLE);

    System.out.println("--------------Backward recursion--------------");
    recursion.runBackwardRecursionMonitoring();
    System.out.println();
    double ETC = recursion.getExpectedCost(initialInventory);
    StateDescriptorImpl initialState = new StateDescriptorImpl(0, initialInventory);
    double action = recursion.getOptimalAction(initialState).getAction();
    long percent = recursion.getMonitoringInterfaceBackward().getPercentCPU();
    System.out.println(
            "Expected total cost (assuming an initial inventory level " + initialInventory + "): " + ETC);
    System.out.println("Optimal initial action: " + action);
    System.out.println("Time elapsed: " + recursion.getMonitoringInterfaceBackward().getTime());
    System.out
            .println("Cpu usage: " + percent + "% (" + Runtime.getRuntime().availableProcessors() + " cores)");
    System.out.println();

    /*******************************************************************
     * Charting
     */
    System.out.println("--------------Charting--------------");
    int targetPeriod = 0; //If targetPeriod > 0 then no sampling!
    plotOptimalPolicyAction(targetPeriod, recursion); //Plot optimal policy action
    BackwardRecursionImpl recursionPlot = new BackwardRecursionImpl(OptimisationDirection.MIN, distributions,
            supportLB, supportUB, immediateValueFunction, randomOutcomeFunction, buildActionList,
            idempotentAction, discountFactor, samplingScheme, maxSampleSize, reductionFactorPerStage,
            HashType.HASHTABLE);
    plotOptimalPolicyCost(targetPeriod, recursionPlot); //Plot optimal policy cost      
    System.out.println();

    /*******************************************************************
     * Simulation
     */
    System.out.println("--------------Simulation--------------");
    double confidence = 0.95; //Simulation confidence level 
    double errorTolerance = 0.0001; //Simulation error threshold

    if (simulate && samplingScheme == SamplingScheme.NONE)
        simulate(distributions, fixedOrderingCost, holdingCost, penaltyCost, proportionalOrderingCost,
                initialInventory, recursion, confidence, errorTolerance);
    else {
        if (!simulate)
            System.out.println("Simulation disabled.");
        if (samplingScheme != SamplingScheme.NONE)
            System.out.println(
                    "Cannot simulate a sampled solution, please disable sampling: set samplingScheme == SamplingScheme.NONE.");
    }
}

From source file:com.thimbleware.jmemcached.Main.java

public static void main(String[] args) throws Exception {
    // look for external log4j.properties

    // setup command line options
    Options options = new Options();
    options.addOption("h", "help", false, "print this help screen");
    options.addOption("bl", "block-store", false, "use external (from JVM) heap");
    options.addOption("f", "mapped-file", false, "use external (from JVM) heap through a memory mapped file");
    options.addOption("bs", "block-size", true,
            "block size (in bytes) for external memory mapped file allocator.  default is 8 bytes");
    options.addOption("i", "idle", true, "disconnect after idle <x> seconds");
    options.addOption("p", "port", true, "port to listen on");
    options.addOption("m", "memory", true,
            "max memory to use; in bytes, specify K, kb, M, GB for larger units");
    options.addOption("c", "ceiling", true,
            "ceiling memory to use; in bytes, specify K, kb, M, GB for larger units");
    options.addOption("l", "listen", true, "Address to listen on");
    options.addOption("s", "size", true, "max items");
    options.addOption("b", "binary", false, "binary protocol mode");
    options.addOption("V", false, "Show version number");
    options.addOption("v", false, "verbose (show commands)");

    // read command line options
    CommandLineParser parser = new PosixParser();
    CommandLine cmdline = parser.parse(options, args);

    if (cmdline.hasOption("help") || cmdline.hasOption("h")) {
        System.out.println("Memcached Version " + MemCacheDaemon.memcachedVersion);
        System.out.println("http://thimbleware.com/projects/memcached\n");

        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar memcached.jar", options);
        return;/* w  ww.  j  av  a 2s .  c om*/
    }

    if (cmdline.hasOption("V")) {
        System.out.println("Memcached Version " + MemCacheDaemon.memcachedVersion);
        return;
    }

    int port = 11211;
    if (cmdline.hasOption("p")) {
        port = Integer.parseInt(cmdline.getOptionValue("p"));
    } else if (cmdline.hasOption("port")) {
        port = Integer.parseInt(cmdline.getOptionValue("port"));
    }

    InetSocketAddress addr = new InetSocketAddress(port);
    if (cmdline.hasOption("l")) {
        addr = new InetSocketAddress(cmdline.getOptionValue("l"), port);
    } else if (cmdline.hasOption("listen")) {
        addr = new InetSocketAddress(cmdline.getOptionValue("listen"), port);
    }

    int max_size = 1000000;
    if (cmdline.hasOption("s"))
        max_size = (int) Bytes.valueOf(cmdline.getOptionValue("s")).bytes();
    else if (cmdline.hasOption("size"))
        max_size = (int) Bytes.valueOf(cmdline.getOptionValue("size")).bytes();

    System.out.println("Setting max cache elements to " + String.valueOf(max_size));

    int idle = -1;
    if (cmdline.hasOption("i")) {
        idle = Integer.parseInt(cmdline.getOptionValue("i"));
    } else if (cmdline.hasOption("idle")) {
        idle = Integer.parseInt(cmdline.getOptionValue("idle"));
    }

    boolean memoryMapped = false;
    if (cmdline.hasOption("f")) {
        memoryMapped = true;
    } else if (cmdline.hasOption("mapped-file")) {
        memoryMapped = true;
    }

    boolean blockStore = false;
    if (cmdline.hasOption("bl")) {
        blockStore = true;
    } else if (cmdline.hasOption("block-store")) {
        blockStore = true;
    }

    boolean verbose = false;
    if (cmdline.hasOption("v")) {
        verbose = true;
    }

    long ceiling;
    if (cmdline.hasOption("c")) {
        ceiling = Bytes.valueOf(cmdline.getOptionValue("c")).bytes();
        System.out.println("Setting ceiling memory size to " + Bytes.bytes(ceiling).megabytes() + "M");
    } else if (cmdline.hasOption("ceiling")) {
        ceiling = Bytes.valueOf(cmdline.getOptionValue("ceiling")).bytes();
        System.out.println("Setting ceiling memory size to " + Bytes.bytes(ceiling).megabytes() + "M");
    } else if (!memoryMapped) {
        ceiling = 1024000;
        System.out.println(
                "Setting ceiling memory size to default limit of " + Bytes.bytes(ceiling).megabytes() + "M");
    } else {
        System.out
                .println("ERROR : ceiling memory size mandatory when external memory mapped file is specified");

        return;
    }

    boolean binary = false;
    if (cmdline.hasOption("b")) {
        binary = true;
    }

    int blockSize = 8;
    if (!memoryMapped && (cmdline.hasOption("bs") || cmdline.hasOption("block-size"))) {
        System.out.println(
                "WARN : block size option is only valid for memory mapped external heap storage; ignoring");
    } else if (cmdline.hasOption("bs")) {
        blockSize = Integer.parseInt(cmdline.getOptionValue("bs"));
    } else if (cmdline.hasOption("block-size")) {
        blockSize = Integer.parseInt(cmdline.getOptionValue("block-size"));
    }

    long maxBytes;
    if (cmdline.hasOption("m")) {
        maxBytes = Bytes.valueOf(cmdline.getOptionValue("m")).bytes();
        System.out.println("Setting max memory size to " + Bytes.bytes(maxBytes).gigabytes() + "GB");
    } else if (cmdline.hasOption("memory")) {
        maxBytes = Bytes.valueOf(cmdline.getOptionValue("memory")).bytes();
        System.out.println("Setting max memory size to " + Bytes.bytes(maxBytes).gigabytes() + "GB");
    } else if (!memoryMapped) {
        maxBytes = Runtime.getRuntime().maxMemory();
        System.out
                .println("Setting max memory size to JVM limit of " + Bytes.bytes(maxBytes).gigabytes() + "GB");
    } else {
        System.out.println(
                "ERROR : max memory size and ceiling size are mandatory when external memory mapped file is specified");
        return;
    }

    if (!memoryMapped && !blockStore && maxBytes > Runtime.getRuntime().maxMemory()) {
        System.out.println("ERROR : JVM heap size is not big enough. use '-Xmx"
                + String.valueOf(maxBytes / 1024000) + "m' java argument before the '-jar' option.");
        return;
    } else if ((memoryMapped || !blockStore) && maxBytes > Integer.MAX_VALUE) {
        System.out.println(
                "ERROR : when external memory mapped, memory size may not exceed the size of Integer.MAX_VALUE ("
                        + Bytes.bytes(Integer.MAX_VALUE).gigabytes() + "GB");
        return;
    }

    // create daemon and start it
    final MemCacheDaemon<LocalCacheElement> daemon = new MemCacheDaemon<LocalCacheElement>();

    CacheStorage<Key, LocalCacheElement> storage;
    if (blockStore) {
        BlockStoreFactory blockStoreFactory = ByteBufferBlockStore.getFactory();

        storage = new BlockStorageCacheStorage(8, (int) ceiling, blockSize, maxBytes, max_size,
                blockStoreFactory);
    } else if (memoryMapped) {
        BlockStoreFactory blockStoreFactory = MemoryMappedBlockStore.getFactory();

        storage = new BlockStorageCacheStorage(8, (int) ceiling, blockSize, maxBytes, max_size,
                blockStoreFactory);
    } else {
        storage = ConcurrentLinkedHashMap.create(ConcurrentLinkedHashMap.EvictionPolicy.FIFO, max_size,
                maxBytes);
    }

    daemon.setCache(new CacheImpl(storage));
    daemon.setBinary(binary);
    daemon.setAddr(addr);
    daemon.setIdleTime(idle);
    daemon.setVerbose(verbose);
    daemon.start();

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            if (daemon.isRunning())
                daemon.stop();
        }
    }));
}

From source file:io.anserini.index.IndexTweetsUpdatePlace.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(new Option(HELP_OPTION, "show help"));
    options.addOption(new Option(OPTIMIZE_OPTION, "merge indexes into a single segment"));
    options.addOption(new Option(STORE_TERM_VECTORS_OPTION, "store term vectors"));

    options.addOption(OptionBuilder.withArgName("collection").hasArg()
            .withDescription("source collection directory").create(COLLECTION_OPTION));
    options.addOption(//  w  ww.ja v a2s.co m
            OptionBuilder.withArgName("dir").hasArg().withDescription("index location").create(INDEX_OPTION));
    options.addOption(OptionBuilder.withArgName("file").hasArg().withDescription("file with deleted tweetids")
            .create(DELETES_OPTION));
    options.addOption(OptionBuilder.withArgName("id").hasArg().withDescription("max id").create(MAX_ID_OPTION));

    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }

    if (cmdline.hasOption(HELP_OPTION) || !cmdline.hasOption(COLLECTION_OPTION)
            || !cmdline.hasOption(INDEX_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(IndexTweetsUpdatePlace.class.getName(), options);
        System.exit(-1);
    }

    String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION);
    String indexPath = cmdline.getOptionValue(INDEX_OPTION);

    System.out.println(collectionPath + " " + indexPath);

    LOG.info("collection: " + collectionPath);
    LOG.info("index: " + indexPath);

    long startTime = System.currentTimeMillis();
    File file = new File(collectionPath);
    if (!file.exists()) {
        System.err.println("Error: " + file + " does not exist!");
        System.exit(-1);
    }

    final FieldType textOptions = new FieldType();
    textOptions.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
    textOptions.setStored(true);
    textOptions.setTokenized(true);
    if (cmdline.hasOption(STORE_TERM_VECTORS_OPTION)) {
        textOptions.setStoreTermVectors(true);

    }

    final StatusStream stream = new JsonStatusCorpusReader(file);

    final Directory dir = new SimpleFSDirectory(Paths.get(cmdline.getOptionValue(INDEX_OPTION)));
    final IndexWriterConfig config = new IndexWriterConfig(ANALYZER);

    config.setOpenMode(IndexWriterConfig.OpenMode.APPEND);

    final IndexWriter writer = new IndexWriter(dir, config);
    System.out.print("Original # of docs " + writer.numDocs());
    int updateCount = 0;

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {

            try {
                stream.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                dir.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Shutting down");

        }
    });
    int cnt = 0;
    Status status;
    try {
        while ((status = stream.next()) != null) {

            if (status.getPlace() != null) {

                //               Query q = NumericRangeQuery.newLongRange(TweetStreamReader.StatusField.ID.name, status.getId(),
                //                     status.getId(), true, true);
                //               System.out.print("Deleting docCount="+writer.numDocs());
                //               writer.deleteDocuments(q);
                //               writer.commit();
                //               System.out.print(" Deleted docCount="+writer.numDocs());

                Document doc = new Document();
                doc.add(new LongField(StatusField.ID.name, status.getId(), Field.Store.YES));
                doc.add(new LongField(StatusField.EPOCH.name, status.getEpoch(), Field.Store.YES));
                doc.add(new TextField(StatusField.SCREEN_NAME.name, status.getScreenname(), Store.YES));

                doc.add(new Field(StatusField.TEXT.name, status.getText(), textOptions));

                doc.add(new IntField(StatusField.FRIENDS_COUNT.name, status.getFollowersCount(), Store.YES));
                doc.add(new IntField(StatusField.FOLLOWERS_COUNT.name, status.getFriendsCount(), Store.YES));
                doc.add(new IntField(StatusField.STATUSES_COUNT.name, status.getStatusesCount(), Store.YES));
                doc.add(new DoubleField(StatusField.LONGITUDE.name, status.getLongitude(), Store.YES));
                doc.add(new DoubleField(StatusField.LATITUDE.name, status.getlatitude(), Store.YES));
                doc.add(new StringField(StatusField.PLACE.name, status.getPlace(), Store.YES));
                long inReplyToStatusId = status.getInReplyToStatusId();
                if (inReplyToStatusId > 0) {
                    doc.add(new LongField(StatusField.IN_REPLY_TO_STATUS_ID.name, inReplyToStatusId,
                            Field.Store.YES));
                    doc.add(new LongField(StatusField.IN_REPLY_TO_USER_ID.name, status.getInReplyToUserId(),
                            Field.Store.YES));
                }

                String lang = status.getLang();
                if (!lang.equals("unknown")) {
                    doc.add(new TextField(StatusField.LANG.name, status.getLang(), Store.YES));
                }

                long retweetStatusId = status.getRetweetedStatusId();
                if (retweetStatusId > 0) {
                    doc.add(new LongField(StatusField.RETWEETED_STATUS_ID.name, retweetStatusId,
                            Field.Store.YES));
                    doc.add(new LongField(StatusField.RETWEETED_USER_ID.name, status.getRetweetedUserId(),
                            Field.Store.YES));
                    doc.add(new IntField(StatusField.RETWEET_COUNT.name, status.getRetweetCount(), Store.YES));
                    if (status.getRetweetCount() < 0 || status.getRetweetedStatusId() < 0) {
                        LOG.warn("Error parsing retweet fields of " + status.getId());
                    }
                }

                long id = status.getId();
                BytesRefBuilder brb = new BytesRefBuilder();
                NumericUtils.longToPrefixCodedBytes(id, 0, brb);
                Term term = new Term(StatusField.ID.name, brb.get());
                writer.updateDocument(term, doc);

                //               writer.addDocument(doc);

                updateCount += 1;

                if (updateCount % 10000 == 0) {

                    LOG.info(updateCount + " statuses updated");
                    writer.commit();
                    System.out.println("Updated docCount=" + writer.numDocs());
                }

            }

        }

        LOG.info("Total elapsed time: " + (System.currentTimeMillis() - startTime) + "ms");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        writer.close();
        dir.close();
        stream.close();
    }
}

From source file:io.anserini.index.UserPostFrequencyDistribution.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(new Option(HELP_OPTION, "show help"));

    options.addOption(new Option(STORE_TERM_VECTORS_OPTION, "store term vectors"));

    options.addOption(OptionBuilder.withArgName("collection").hasArg()
            .withDescription("source collection directory").create(COLLECTION_OPTION));
    options.addOption(OptionBuilder.withArgName("property").hasArg()
            .withDescription("source collection directory").create("property"));
    options.addOption(OptionBuilder.withArgName("collection_pattern").hasArg()
            .withDescription("source collection directory").create("collection_pattern"));

    CommandLine cmdline = null;/*  w ww. j av a  2 s. co  m*/
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }

    if (cmdline.hasOption(HELP_OPTION) || !cmdline.hasOption(COLLECTION_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(UserPostFrequencyDistribution.class.getName(), options);
        System.exit(-1);
    }

    String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION);

    final FieldType textOptions = new FieldType();
    textOptions.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
    textOptions.setStored(true);
    textOptions.setTokenized(true);
    textOptions.setStoreTermVectors(true);

    LOG.info("collection: " + collectionPath);
    LOG.info("collection_pattern " + cmdline.getOptionValue("collection_pattern"));
    LOG.info("property " + cmdline.getOptionValue("property"));
    LongOpenHashSet deletes = null;

    long startTime = System.currentTimeMillis();
    File file = new File(collectionPath);
    if (!file.exists()) {
        System.err.println("Error: " + file + " does not exist!");
        System.exit(-1);
    }

    final JsonStatusCorpusReader stream = new JsonStatusCorpusReader(file,
            cmdline.getOptionValue("collection_pattern"));

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {

            try {

                stream.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            ;

            System.out.println("# of users indexed this round: " + userIndexedCount);

            System.out.println("Shutting down");

        }
    });
    Status status;
    boolean readerNotInitialized = true;

    try {
        Properties prop = new Properties();
        while ((status = stream.next()) != null) {

            // try{
            // status = DataObjectFactory.createStatus(s);
            // if (status==null||status.getText() == null) {
            // continue;
            // }}catch(Exception e){
            //
            // }
            //

            boolean pittsburghRelated = false;
            try {

                if (Math.abs(status.getLongitude() - pittsburghLongitude) < 0.05d
                        && Math.abs(status.getlatitude() - pittsburghLatitude) < 0.05d)
                    pittsburghRelated = true;
            } catch (Exception e) {

            }
            try {
                if (status.getPlace().contains("Pittsburgh, PA"))
                    pittsburghRelated = true;
            } catch (Exception e) {

            }
            try {
                if (status.getUserLocation().contains("Pittsburgh, PA"))
                    pittsburghRelated = true;
            } catch (Exception e) {

            }

            try {
                if (status.getText().contains("Pittsburgh"))
                    pittsburghRelated = true;
            } catch (Exception e) {

            }

            if (pittsburghRelated) {

                int previousPostCount = 0;

                if (prop.containsKey(String.valueOf(status.getUserid()))) {
                    previousPostCount = Integer
                            .valueOf(prop.getProperty(String.valueOf(status.getUserid())).split(" ")[1]);
                }

                prop.setProperty(String.valueOf(status.getUserid()),
                        String.valueOf(status.getStatusesCount()) + " " + (1 + previousPostCount));
                if (prop.size() > 0 && prop.size() % 1000 == 0) {
                    Runtime runtime = Runtime.getRuntime();
                    runtime.gc();
                    System.out.println("Property size " + prop.size() + "Memory used:  "
                            + ((runtime.totalMemory() - runtime.freeMemory()) / (1024L * 1024L)) + " MB\n");
                }
                OutputStream output = new FileOutputStream(cmdline.getOptionValue("property"), false);
                prop.store(output, null);
                output.close();

            }
        }
        //         prop.store(output, null);
        LOG.info(String.format("Total of %s statuses added", userIndexedCount));
        LOG.info("Total elapsed time: " + (System.currentTimeMillis() - startTime) + "ms");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        stream.close();
    }
}

From source file:com.github.brandtg.switchboard.FileLogAggregator.java

/** Main. */
public static void main(String[] args) throws Exception {
    Options opts = new Options();
    opts.addOption("h", "help", false, "Prints help message");
    opts.addOption("f", "file", true, "File to output aggregated logs to");
    opts.addOption("s", "separator", true, "Line separator in log");
    CommandLine cli = new GnuParser().parse(opts, args);

    if (cli.getArgs().length == 0 || cli.hasOption("help")) {
        new HelpFormatter().printHelp("usage: [opts] sourceHost:port ...", opts);
        System.exit(1);/*  ww  w  .  j  av  a  2s  . c o  m*/
    }

    // Parse sources
    Set<InetSocketAddress> sources = new HashSet<>();
    for (int i = 0; i < cli.getArgs().length; i++) {
        String[] tokens = cli.getArgs()[i].split(":");
        sources.add(new InetSocketAddress(tokens[0], Integer.valueOf(tokens[1])));
    }

    // Parse output stream
    OutputStream outputStream;
    if (cli.hasOption("file")) {
        outputStream = new FileOutputStream(cli.getOptionValue("file"));
    } else {
        outputStream = System.out;
    }

    // Separator
    String separator = cli.getOptionValue("separator", "\n");
    if (separator.length() != 1) {
        throw new IllegalArgumentException("Separator must only be 1 character");
    }

    final FileLogAggregator fileLogAggregator = new FileLogAggregator(sources, separator.charAt(0),
            outputStream);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                fileLogAggregator.stop();
            } catch (Exception e) {
                LOG.error("Error when stopping log aggregator", e);
            }
        }
    });

    fileLogAggregator.start();
}

From source file:com.splout.db.dnode.DNode.java

public static void main(String[] args) throws Exception {
    SploutConfiguration config;//  w w w.j  a va2s.  c o  m
    if (args.length == 1) {
        // config root
        config = SploutConfiguration.get(args[0]);
    } else {
        config = SploutConfiguration.get();
    }
    final DNode dnode = new DNode(config, new DNodeHandler());
    // Add shutdown hook
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                log.info("Shutdown hook called - trying to gently stop DNode ...");
                dnode.stop();
            } catch (Throwable e) {
                log.error("Error in ShutdownHook", e);
            }
        }
    });
    dnode.init();
}

From source file:com.edduarte.protbox.Protbox.java

public static void main(String... args) {

    // activate debug / verbose mode
    if (args.length != 0) {
        List<String> argsList = Arrays.asList(args);
        if (argsList.contains("-v")) {
            Constants.verbose = true;/* w w  w  . j a  v  a 2  s .c  om*/
        }
    }

    // use System's look and feel
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
        // If the System's look and feel is not obtainable, continue execution with JRE look and feel
    }

    // check this is a single instance
    try {
        new ServerSocket(1882);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null,
                "Another instance of Protbox is already running.\n" + "Please close the other instance first.",
                "Protbox already running", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }

    // check if System Tray is supported by this operative system
    if (!SystemTray.isSupported()) {
        JOptionPane.showMessageDialog(null,
                "Your operative system does not support system tray functionality.\n"
                        + "Please try running Protbox on another operative system.",
                "System tray not supported", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }

    // add PKCS11 providers
    FileFilter fileFilter = new AndFileFilter(new WildcardFileFilter(Lists.newArrayList("*.config")),
            HiddenFileFilter.VISIBLE);

    File[] providersConfigFiles = new File(Constants.PROVIDERS_DIR).listFiles(fileFilter);

    if (providersConfigFiles != null) {
        for (File f : providersConfigFiles) {
            try {
                List<String> lines = FileUtils.readLines(f);
                String aliasLine = lines.stream().filter(line -> line.contains("alias")).findFirst().get();
                lines.remove(aliasLine);
                String alias = aliasLine.split("=")[1].trim();

                StringBuilder sb = new StringBuilder();
                for (String s : lines) {
                    sb.append(s);
                    sb.append("\n");
                }

                Provider p = new SunPKCS11(new ReaderInputStream(new StringReader(sb.toString())));
                Security.addProvider(p);

                pkcs11Providers.put(p.getName(), alias);

            } catch (IOException | ProviderException ex) {
                if (ex.getMessage().equals("Initialization failed")) {
                    ex.printStackTrace();

                    String s = "The following error occurred:\n" + ex.getCause().getMessage()
                            + "\n\nIn addition, make sure you have "
                            + "an available smart card reader connected before opening the application.";
                    JTextArea textArea = new JTextArea(s);
                    textArea.setColumns(60);
                    textArea.setLineWrap(true);
                    textArea.setWrapStyleWord(true);
                    textArea.setSize(textArea.getPreferredSize().width, 1);

                    JOptionPane.showMessageDialog(null, textArea, "Error loading PKCS11 provider",
                            JOptionPane.ERROR_MESSAGE);
                    System.exit(1);
                } else {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null,
                            "Error while setting up PKCS11 provider from configuration file " + f.getName()
                                    + ".\n" + ex.getMessage(),
                            "Error loading PKCS11 provider", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }

    // adds a shutdown hook to save instantiated directories into files when the application is being closed
    Runtime.getRuntime().addShutdownHook(new Thread(Protbox::exit));

    // get system tray and run tray applet
    tray = SystemTray.getSystemTray();
    SwingUtilities.invokeLater(() -> {

        if (Constants.verbose) {
            logger.info("Starting application");
        }

        //Start a new TrayApplet object
        trayApplet = TrayApplet.getInstance();
    });

    // prompts the user to choose which provider to use
    ProviderListWindow.showWindow(Protbox.pkcs11Providers.keySet(), providerName -> {

        // loads eID token
        eIDTokenLoadingWindow.showPrompt(providerName, (returnedUser, returnedCertificateData) -> {
            user = returnedUser;
            certificateData = returnedCertificateData;

            // gets a password to use on the saved registry files (for loading and saving)
            final AtomicReference<Consumer<SecretKey>> consumerHolder = new AtomicReference<>(null);
            consumerHolder.set(password -> {
                registriesPasswordKey = password;
                try {
                    // if there are serialized files, load them if they can be decoded by this user's private key
                    final List<SavedRegistry> serializedDirectories = new ArrayList<>();
                    if (Constants.verbose) {
                        logger.info("Reading serialized registry files...");
                    }

                    File[] registryFileList = new File(Constants.REGISTRIES_DIR).listFiles();
                    if (registryFileList != null) {
                        for (File f : registryFileList) {
                            if (f.isFile()) {
                                byte[] data = FileUtils.readFileToByteArray(f);
                                try {
                                    Cipher cipher = Cipher.getInstance("AES");
                                    cipher.init(Cipher.DECRYPT_MODE, registriesPasswordKey);
                                    byte[] registryDecryptedData = cipher.doFinal(data);
                                    serializedDirectories.add(new SavedRegistry(f, registryDecryptedData));
                                } catch (GeneralSecurityException ex) {
                                    if (Constants.verbose) {
                                        logger.info("Inserted Password does not correspond to " + f.getName());
                                    }
                                }
                            }
                        }
                    }

                    // if there were no serialized directories, show NewDirectory window to configure the first folder
                    if (serializedDirectories.isEmpty() || registryFileList == null) {
                        if (Constants.verbose) {
                            logger.info("No registry files were found: running app as first time!");
                        }
                        NewRegistryWindow.start(true);

                    } else { // there were serialized directories
                        loadRegistry(serializedDirectories);
                        trayApplet.repaint();
                        showTrayApplet();
                    }

                } catch (AWTException | IOException | GeneralSecurityException | ReflectiveOperationException
                        | ProtboxException ex) {

                    JOptionPane.showMessageDialog(null,
                            "The inserted password was invalid! Please try another one!", "Invalid password!",
                            JOptionPane.ERROR_MESSAGE);
                    insertPassword(consumerHolder.get());
                }
            });
            insertPassword(consumerHolder.get());
        });
    });
}