Example usage for org.apache.commons.lang SystemUtils OS_NAME

List of usage examples for org.apache.commons.lang SystemUtils OS_NAME

Introduction

In this page you can find the example usage for org.apache.commons.lang SystemUtils OS_NAME.

Prototype

String OS_NAME

To view the source code for org.apache.commons.lang SystemUtils OS_NAME.

Click Source Link

Document

The os.name System Property.

Usage

From source file:com.amazonaws.services.kinesis.producer.KinesisProducer.java

private void extractBinaries() {
    synchronized (EXTRACT_BIN_MUTEX) {
        final List<File> watchFiles = new ArrayList<>(2);
        String os = SystemUtils.OS_NAME;
        if (SystemUtils.IS_OS_WINDOWS) {
            os = "windows";
        } else if (SystemUtils.IS_OS_LINUX) {
            os = "linux";
        } else if (SystemUtils.IS_OS_MAC_OSX) {
            os = "osx";
        } else {// ww w . j  a  va 2  s.  c  o  m
            throw new RuntimeException("Your operation system is not supported (" + os
                    + "), the KPL only supports Linux, OSX and Windows");
        }

        String root = "amazon-kinesis-producer-native-binaries";
        String tmpDir = config.getTempDirectory();
        if (tmpDir.trim().length() == 0) {
            tmpDir = System.getProperty("java.io.tmpdir");
        }
        tmpDir = Paths.get(tmpDir, root).toString();
        pathToTmpDir = tmpDir;

        String binPath = config.getNativeExecutable();
        if (binPath != null && !binPath.trim().isEmpty()) {
            pathToExecutable = binPath.trim();
            log.warn("Using non-default native binary at " + pathToExecutable);
            pathToLibDir = "";
        } else {
            log.info("Extracting binaries to " + tmpDir);
            try {
                File tmpDirFile = new File(tmpDir);
                if (!tmpDirFile.exists() && !tmpDirFile.mkdirs()) {
                    throw new IOException("Could not create tmp dir " + tmpDir);
                }

                String extension = os.equals("windows") ? ".exe" : "";
                String executableName = "kinesis_producer" + extension;
                byte[] bin = IOUtils.toByteArray(this.getClass().getClassLoader()
                        .getResourceAsStream(root + "/" + os + "/" + executableName));
                MessageDigest md = MessageDigest.getInstance("SHA1");
                String mdHex = DatatypeConverter.printHexBinary(md.digest(bin)).toLowerCase();

                pathToExecutable = Paths.get(pathToTmpDir, "kinesis_producer_" + mdHex + extension).toString();
                File extracted = new File(pathToExecutable);
                watchFiles.add(extracted);
                if (extracted.exists()) {
                    try (FileInputStream fis = new FileInputStream(extracted);
                            FileLock lock = fis.getChannel().lock(0, Long.MAX_VALUE, true)) {
                        boolean contentEqual = false;
                        if (extracted.length() == bin.length) {
                            byte[] existingBin = IOUtils.toByteArray(new FileInputStream(extracted));
                            contentEqual = Arrays.equals(bin, existingBin);
                        }
                        if (!contentEqual) {
                            throw new SecurityException("The contents of the binary "
                                    + extracted.getAbsolutePath() + " is not what it's expected to be.");
                        }
                    }
                } else {
                    try (FileOutputStream fos = new FileOutputStream(extracted);
                            FileLock lock = fos.getChannel().lock()) {
                        IOUtils.write(bin, fos);
                    }
                    extracted.setExecutable(true);
                }

                String certFileName = "b204d74a.0";
                File certFile = new File(pathToTmpDir, certFileName);
                if (!certFile.exists()) {
                    try (FileOutputStream fos = new FileOutputStream(certFile);
                            FileLock lock = fos.getChannel().lock()) {
                        byte[] certs = IOUtils.toByteArray(this.getClass().getClassLoader()
                                .getResourceAsStream("cacerts/" + certFileName));
                        IOUtils.write(certs, fos);
                    }
                }

                watchFiles.add(certFile);
                pathToLibDir = pathToTmpDir;
                FileAgeManager.instance().registerFiles(watchFiles);
            } catch (Exception e) {
                throw new RuntimeException("Could not copy native binaries to temp directory " + tmpDir, e);
            }

        }
    }
}

From source file:net.ymate.platform.commons.util.SystemEnvUtils.java

/**
 * ?????/*from   www  .  jav  a 2  s. c  om*/
 */
public static void initSystemEnvs() {
    Process p = null;
    try {
        if (SystemUtils.IS_OS_WINDOWS) {
            p = Runtime.getRuntime().exec("cmd /c set");
        } else if (SystemUtils.IS_OS_UNIX) {
            p = Runtime.getRuntime().exec("/bin/sh -c set");
        } else {
            _LOG.warn("Unknown os.name=" + SystemUtils.OS_NAME);
            SYSTEM_ENV_MAP.clear();
        }
        if (p != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                int i = line.indexOf("=");
                if (i > -1) {
                    String key = line.substring(0, i);
                    String value = line.substring(i + 1);
                    SYSTEM_ENV_MAP.put(key, value);
                }
            }
        }
    } catch (IOException e) {
        _LOG.warn(RuntimeUtils.unwrapThrow(e));
    }
}

From source file:net.ymate.platform.core.util.RuntimeUtils.java

/**
 * ?????// w  w w.  j  av a 2 s.c om
 */
public static void initSystemEnvs() {
    Process p = null;
    BufferedReader br = null;
    try {
        if (SystemUtils.IS_OS_WINDOWS) {
            p = Runtime.getRuntime().exec("cmd /c set");
        } else if (SystemUtils.IS_OS_UNIX) {
            p = Runtime.getRuntime().exec("/bin/sh -c set");
        } else {
            _LOG.warn("Unknown os.name=" + SystemUtils.OS_NAME);
            SYSTEM_ENV_MAP.clear();
        }
        if (p != null) {
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                int i = line.indexOf('=');
                if (i > -1) {
                    String key = line.substring(0, i);
                    String value = line.substring(i + 1);
                    SYSTEM_ENV_MAP.put(key, value);
                }
            }
        }
    } catch (IOException e) {
        _LOG.warn(RuntimeUtils.unwrapThrow(e));
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                _LOG.warn("", e);
            }
        }
        if (p != null) {
            p.destroy();
        }
    }
}

From source file:org.alfresco.sync.DesktopSyncAbstract.java

@AfterSuite(alwaysRun = true)
public void closeWebDrone() {
    //        // Delete the site before close
    if (SystemUtils.OS_NAME.contains("Windows")) {
        removeAccount();/*from   w ww .  j a  v  a 2 s . co m*/
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Closing web drone");
    }
    // Close the browser
    if (drone != null) {
        drone.quit();
        drone = null;
    }
}

From source file:org.alfresco.sync.DesktopSyncTest.java

@BeforeSuite(alwaysRun = true)
public void initialSetup() {
    try {/*  w  w  w.  j a  v a  2 s .  com*/
        setupContext();
        drone = getWebDrone();
        userInfo = new String[] { username, password };

        try {
            new File(SCREENSHOTS_FOLDER).mkdir();
        } catch (Exception e) {
            logger.error("Cannot initialize SCREENSTHOT FOLDER: " + SCREENSHOTS_FOLDER);
        }
        // // Site creation for windows
        if (SystemUtils.OS_NAME.contains("Windows")) {
            initialSiteSetUp();
        }
    } catch (Exception e) {
        logger.error("Failed to load Bean context in :" + this.getClass(), e);
    }
}

From source file:org.apache.cocoon.generation.StatusGenerator.java

private void genVMStatus() throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    startGroup("VM");

    // BEGIN ClassPath
    String classpath = SystemUtils.JAVA_CLASS_PATH;
    if (classpath != null) {
        List paths = new ArrayList();
        StringTokenizer tokenizer = new StringTokenizer(classpath, SystemUtils.PATH_SEPARATOR);
        while (tokenizer.hasMoreTokens()) {
            paths.add(tokenizer.nextToken());
        }//from ww w .  j a va 2s.c o m
        addMultilineValue("classpath", paths);
    }
    // END ClassPath

    // BEGIN CONTEXT CLASSPATH
    String contextClassPath = null;
    try {
        contextClassPath = (String) this.context.get(Constants.CONTEXT_CLASSPATH);
    } catch (ContextException e) {
        // we ignore this
    }
    if (contextClassPath != null) {
        List paths = new ArrayList();
        StringTokenizer tokenizer = new StringTokenizer(contextClassPath, File.pathSeparator);
        while (tokenizer.hasMoreTokens()) {
            paths.add(tokenizer.nextToken());
        }
        addMultilineValue("context-classpath", paths);
    }
    // END CONTEXT CLASSPATH

    // BEGIN Memory status
    startGroup("Memory");
    final long totalMemory = Runtime.getRuntime().totalMemory();
    final long freeMemory = Runtime.getRuntime().freeMemory();
    addValue("total", String.valueOf(totalMemory));
    addValue("used", String.valueOf(totalMemory - freeMemory));
    addValue("free", String.valueOf(freeMemory));
    endGroup();
    // END Memory status

    // BEGIN JRE
    startGroup("JRE");
    addValue("version", SystemUtils.JAVA_VERSION);
    atts.clear();
    // qName = prefix + ':' + localName
    atts.addAttribute(XLINK_NS, "type", XLINK_PREFIX + ":type", "CDATA", "simple");
    atts.addAttribute(XLINK_NS, "href", XLINK_PREFIX + ":href", "CDATA", SystemUtils.JAVA_VENDOR_URL);
    addValue("java-vendor", SystemUtils.JAVA_VENDOR, atts);
    endGroup();
    // END JRE

    // BEGIN Operating system
    startGroup("Operating System");
    addValue("name", SystemUtils.OS_NAME);
    addValue("architecture", SystemUtils.OS_ARCH);
    addValue("version", SystemUtils.OS_VERSION);
    endGroup();
    // END operating system

    // BEGIN Cache
    if (this.storeJanitor != null) {
        startGroup("Store Janitor");

        // For each element in StoreJanitor
        Iterator i = this.storeJanitor.iterator();
        while (i.hasNext()) {
            Store store = (Store) i.next();
            startGroup(
                    store.getClass().getName() + " (hash = 0x" + Integer.toHexString(store.hashCode()) + ")");
            int size = 0;
            int empty = 0;
            atts.clear();
            atts.addAttribute(NAMESPACE, "name", "name", "CDATA", "cached");
            super.contentHandler.startElement(NAMESPACE, "value", "value", atts);

            atts.clear();
            Enumeration e = store.keys();
            while (e.hasMoreElements()) {
                size++;
                Object key = e.nextElement();
                Object val = store.get(key);
                String line;
                if (val == null) {
                    empty++;
                } else {
                    line = key + " (class: " + val.getClass().getName() + ")";
                    super.contentHandler.startElement(NAMESPACE, "line", "line", atts);
                    super.contentHandler.characters(line.toCharArray(), 0, line.length());
                    super.contentHandler.endElement(NAMESPACE, "line", "line");
                }
            }
            if (size == 0) {
                super.contentHandler.startElement(NAMESPACE, "line", "line", atts);
                String value = "[empty]";
                super.contentHandler.characters(value.toCharArray(), 0, value.length());
                super.contentHandler.endElement(NAMESPACE, "line", "line");
            }
            super.contentHandler.endElement(NAMESPACE, "value", "value");

            addValue("size", String.valueOf(size) + " items in cache (" + empty + " are empty)");
            endGroup();
        }
        endGroup();
    }

    if (this.storePersistent != null) {
        startGroup(storePersistent.getClass().getName() + " (hash = 0x"
                + Integer.toHexString(storePersistent.hashCode()) + ")");
        int size = 0;
        int empty = 0;
        atts.clear();
        atts.addAttribute(NAMESPACE, "name", "name", "CDATA", "cached");
        super.contentHandler.startElement(NAMESPACE, "value", "value", atts);

        atts.clear();
        Enumeration e = this.storePersistent.keys();
        while (e.hasMoreElements()) {
            size++;
            Object key = e.nextElement();
            Object val = storePersistent.get(key);
            String line;
            if (val == null) {
                empty++;
            } else {
                line = key + " (class: " + val.getClass().getName() + ")";
                super.contentHandler.startElement(NAMESPACE, "line", "line", atts);
                super.contentHandler.characters(line.toCharArray(), 0, line.length());
                super.contentHandler.endElement(NAMESPACE, "line", "line");
            }
        }
        if (size == 0) {
            super.contentHandler.startElement(NAMESPACE, "line", "line", atts);
            String value = "[empty]";
            super.contentHandler.characters(value.toCharArray(), 0, value.length());
            super.contentHandler.endElement(NAMESPACE, "line", "line");
        }
        super.contentHandler.endElement(NAMESPACE, "value", "value");

        addValue("size", size + " items in cache (" + empty + " are empty)");
        endGroup();
    }
    // END Cache

    endGroup();
}

From source file:org.apache.cocoon.webservices.system.System.java

/**
 * <code>getOperatingSystem</code> returns the host operating system
 *
 * @return host operating system/*from   w  ww. j a  v a  2s .  c om*/
 */
public String getOperatingSystem() {
    return SystemUtils.OS_NAME;
}

From source file:org.cleartk.util.PlatformDetection.java

public PlatformDetection() {
    // resolve OS
    if (SystemUtils.IS_OS_WINDOWS) {
        this.os = OS_WINDOWS;
    } else if (SystemUtils.IS_OS_MAC_OSX) {
        this.os = OS_OSX;
    } else if (SystemUtils.IS_OS_SOLARIS) {
        this.os = OS_SOLARIS;
    } else if (SystemUtils.IS_OS_LINUX) {
        this.os = OS_LINUX;
    } else {/*  www.j ava2s . co m*/
        throw new IllegalArgumentException("Unknown operating system " + SystemUtils.OS_NAME);
    }

    // resolve architecture
    Map<String, String> archMap = new HashMap<String, String>();
    archMap.put("x86", ARCH_X86_32);
    archMap.put("i386", ARCH_X86_32);
    archMap.put("i486", ARCH_X86_32);
    archMap.put("i586", ARCH_X86_32);
    archMap.put("i686", ARCH_X86_32);
    archMap.put("x86_64", ARCH_X86_64);
    archMap.put("amd64", ARCH_X86_64);
    archMap.put("powerpc", ARCH_PPC);
    this.arch = archMap.get(SystemUtils.OS_ARCH);
    if (this.arch == null) {
        throw new IllegalArgumentException("Unknown architecture " + SystemUtils.OS_ARCH);
    }
}

From source file:org.mule.transport.sftp.AbstractSftpTestCase.java

protected void recursiveDeleteInLocalFilesystem(File parent) throws IOException {
    // If this file is a directory then first delete all its children
    if (parent.isDirectory()) {
        for (File child : parent.listFiles()) {
            recursiveDeleteInLocalFilesystem(child);
        }/*from w w  w  . j a v  a 2 s  .  c  om*/
    }

    // Now delete this file, but first check write permissions on its parent...
    File parentParent = parent.getParentFile();

    if (!parentParent.canWrite()) {
        // setWritable is only available on JDK6 and beyond
        //if (!parentParent.setWritable(true))
        //throw new IOException("Failed to set readonly-folder: " + parentParent + " to writeable");
        // FIXME DZ: since setWritable doesnt exist on jdk5, need to detect os to make dir writable
        if (SystemUtils.IS_OS_WINDOWS) {
            Runtime.getRuntime().exec("attrib -r /D" + parentParent.getAbsolutePath());
        } else if (SystemUtils.IS_OS_UNIX || SystemUtils.IS_OS_LINUX) {
            Runtime.getRuntime().exec("chmod +w " + parentParent.getAbsolutePath());
        } else {
            throw new IOException(
                    "This test is not supported on your detected platform : " + SystemUtils.OS_NAME);
        }
    }

    if (parent.exists()) {
        // FIXME DZ: since setWritable doesnt exist on jdk5, need to detect os to make dir writable
        if (SystemUtils.IS_OS_WINDOWS) {
            Runtime.getRuntime().exec("attrib -r /D" + parent.getAbsolutePath());
        } else if (SystemUtils.IS_OS_UNIX || SystemUtils.IS_OS_LINUX) {
            Runtime.getRuntime().exec("chmod +w " + parent.getAbsolutePath());
        } else {
            throw new IOException(
                    "This test is not supported on your detected platform : " + SystemUtils.OS_NAME);
        }
        if (!parent.delete())
            throw new IOException("Failed to delete folder: " + parent);
    }
}

From source file:org.zeroturnaround.exec.test.InputRedirectTest.java

@Test
public void testRedirectInput() throws Exception {
    String binTrue;//  w  ww . j  a  v  a  2 s. co  m
    if (SystemUtils.IS_OS_LINUX) {
        binTrue = "/bin/true";
    } else if (SystemUtils.IS_OS_MAC_OSX) {
        binTrue = "/usr/bin/true";
    } else {
        Assume.assumeTrue("Unsupported OS " + SystemUtils.OS_NAME, false);
        return; // Skip this test
    }

    // We need to put something in the buffer
    ByteArrayInputStream bais = new ByteArrayInputStream("foo".getBytes());
    ProcessExecutor exec = new ProcessExecutor().command(binTrue);
    // Test that we don't get IOException: Stream closed
    int exit = exec.redirectInput(bais).readOutput(true).execute().getExitValue();
    log.debug("Exit: {}", exit);
}