All Downloads are FREE. Search and download functionalities are using the official Maven repository.

automated.os_operations.Services Maven / Gradle / Ivy

The newest version!
package automated.os_operations;

import java.io.*;
import java.net.ServerSocket;

/**
 * Created by Ismail on 2/23/2018.
 * This class contains methods related to operations
 * that made on OS level (like: change, stop or start services)
 */
public class Services {

    /*************** Class Variables Section ***************/

    /*************** Class Methods Section ***************/
    // This method to check if a specific port running and in use
    // or there is a service reserve it and return Boolean value
    // as status of port usage status
    public static Boolean checkPortInUse(int portNo) {
        // Define Boolean variable
        Boolean isServerRunning = false;
        // Checking if port in use
        try {// Initialize ServerSocket and assign the port for
            ServerSocket serverSocket = new ServerSocket(portNo);
            // Try to close assigned ServerSocket
        } catch (IOException e) {// In case port in use will throw exception
            isServerRunning = true;
        }// Return result of isServerRunning variable
        return isServerRunning;
    }

    /* This method still not fully implemented */
    // This method to check if a specific program installed on
    // current machine and return Boolean value of program status
    // is installed or not
    public static Boolean checkProgramExists(String programCommand) {
        // Define Program exist Boolean variable
        Boolean isProgramExists = false;
        try {// Check if program is installed or not
            // Define ProcessBuilder variable and assign command
            ProcessBuilder processBuilder = new ProcessBuilder(programCommand);
            // Define Process and start ProcessBuilder with assigned command
            Process process = processBuilder.start();
            // Wait until Process object execution ends
            process.waitFor();
            // Check command execution success
            // Need a way to verify the program is installed
        } catch (Throwable e) {
            // Do action here
        }
        // Return result of isProgramExists variable
        return isProgramExists;
    }

    /* This method still not implemented */
    // This method to run specific process and
    // return process result object
    public static Process runProcess(String processCommand) {
        Process process = null;
        return process;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy