com.acmutv.ontoqa.tool.runtime.RuntimeManager.java Source code

Java tutorial

Introduction

Here is the source code for com.acmutv.ontoqa.tool.runtime.RuntimeManager.java

Source

/*
  The MIT License (MIT)
    
  Copyright (c) 2016 Antonella Botte, Giacomo Marciani and Debora Partigianoni
    
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
    
    
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
    
    
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
 */

package com.acmutv.ontoqa.tool.runtime;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * This class realizes the app lifecycle services.
 * @author Antonella Botte {@literal <abotte@acm.org>}
 * @author Giacomo Marciani {@literal <gmarciani@acm.org>}
 * @author Debora Partigianoni {@literal <dpartigianoni@acm.org>}
 * @since 1.0
 */
public class RuntimeManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeManager.class);

    /**
     * Registers atexit runnables as JVM shutdown hooks.
     * @param hooks atexit runnables.
     * @see Runtime
     */
    public static void registerShutdownHooks(Runnable... hooks) {
        Runtime runtime = Runtime.getRuntime();
        for (Runnable hook : hooks) {
            runtime.addShutdownHook(new Thread(hook));
            LOGGER.trace("Registered shutdown hook {}", hook.getClass().getName());
        }
    }

    /**
     * Registers a periodic task.
     * @param task the task to execute.
     * @param delay the delay to first execution.
     * @param period the period between executions.
     * @param timeout the time to interruption.
     * @param unit the time unit.
     */
    private static void registerPeriodic(Runnable task, long delay, long period, long timeout, TimeUnit unit) {
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        final ScheduledFuture<?> handler = scheduler.scheduleAtFixedRate(task, delay, period, unit);

        if (timeout > 0) {
            Runnable interrupt = () -> handler.cancel(true);
            scheduler.schedule(interrupt, timeout, TimeUnit.SECONDS);
        }
    }

    /**
     * Executes the given command and arguments.
     * @param command The command to execute. Arguments must be given as a separated strings.
     *                E.g.: BashExecutor.runCommand("ls", "-la") or BashExecutor.runCommand("ls", "-l", "-a")
     * @return The command output as a string.
     * @throws IOException when error in process generation or output.
     */
    public static String runCommand(String... command) throws IOException {
        LOGGER.trace("command={}", Arrays.asList(command));
        String out;
        ProcessBuilder pb = new ProcessBuilder(command);
        Process p = pb.start();
        try (InputStream in = p.getInputStream()) {
            out = IOUtils.toString(in, Charset.defaultCharset());
        }
        return out.trim();
    }

    /**
     * A method that runs a command on the command line of the host and captures the result from the console
     * @param command command
     * @return output of the executed system command
     */
    public static String runCmd(String command) {
        StringBuilder cmdOutput = new StringBuilder();
        String s;

        try {
            Process p = Runtime.getRuntime().exec(command);
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            while ((s = stdInput.readLine()) != null) {
                cmdOutput.append(s).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return cmdOutput.toString();
    }

    /**
     * Retrieves the local number of cores.
     * @return the number of cores.
     */
    public static int getCores() {
        return Runtime.getRuntime().availableProcessors();
    }

    /**
     * Returns the current JVM name.
     * @return the JVM name.
     */
    public static String getJvmName() {
        return ManagementFactory.getRuntimeMXBean().getName();
    }

}