Skip to content

Instantly share code, notes, and snippets.

@bastengao
Created December 8, 2013 04:50
Show Gist options
  • Select an option

  • Save bastengao/7853455 to your computer and use it in GitHub Desktop.

Select an option

Save bastengao/7853455 to your computer and use it in GitHub Desktop.

Revisions

  1. bastengao created this gist Dec 8, 2013.
    38 changes: 38 additions & 0 deletions OsCheck.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    /**
    * helper class to check the operating system this Java VM runs in
    * http://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java
    * compare to http://svn.terracotta.org/svn/tc/dso/tags/2.6.4/code/base/common/src/com/tc/util/runtime/Os.java
    * http://www.docjar.com/html/api/org/apache/commons/lang/SystemUtils.java.html
    */
    public final class OsCheck {
    /**
    * types of Operating Systems
    */
    public enum OSType {
    Windows, MacOS, Linux, Other
    }

    protected static OSType detectedOS;

    /**
    * detected the operating system from the os.name System property and cache
    * the result
    *
    * @returns - the operating system detected
    */
    public static OSType getOperatingSystemType() {
    if (detectedOS == null) {
    String OS = System.getProperty("os.name", "generic").toLowerCase();
    if (OS.indexOf("win") >= 0) {
    detectedOS = OSType.Windows;
    } else if ((OS.indexOf("mac") >= 0) || (OS.indexOf("darwin") >= 0)) {
    detectedOS = OSType.MacOS;
    } else if (OS.indexOf("nux") >= 0) {
    detectedOS = OSType.Linux;
    } else {
    detectedOS = OSType.Other;
    }
    }
    return detectedOS;
    }
    }
    33 changes: 33 additions & 0 deletions SigarUtil.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import com.google.common.io.Resources;
    import org.hyperic.sigar.Sigar;

    import java.io.File;
    import java.io.IOException;

    /**
    * @author gaohui
    * @date 13-11-27 19:43
    */
    public class SigarUtil {

    public final static Sigar sigar = initSigar();

    private static Sigar initSigar() {
    try {
    String file = Resources.getResource("sigar/.sigar_shellrc").getFile();
    File classPath = new File(file).getParentFile();

    String path = System.getProperty("java.library.path");
    if (OsCheck.getOperatingSystemType() == OsCheck.OSType.Windows) {
    path += ";" + classPath.getCanonicalPath();
    } else {
    path += ":" + classPath.getCanonicalPath();
    }
    System.setProperty("java.library.path", path);

    return new Sigar();
    } catch (Exception e) {
    return null;
    }
    }
    }