Platform.java

/*
 * Created on 2011/11/11
 * Copyright (C) 2011 Koga Laboratory. All rights reserved.
 *
 */
package org.mklab.cga.platform;

import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Ryota
 * @version 1.0, 2011/11/11
 */
public final class Platform {

  /** Singleton instance */
  private final static Platform INSTANCE = new Platform();

  /**
   * Get singleton instance.
   * 
   * @return instance
   */
  public static Platform getInstance() {
    return INSTANCE;
  }

  /** Set names */
  public final static Map<String, String> OS_NAMES = new HashMap<String, String>() {

    /** */
    private static final long serialVersionUID = 1187720077606710151L;

    {
      put("Mac OS X", DARWIN); //$NON-NLS-1$
    }
  };

  /** Set names */
  public final static Map<String, String> ARCH_NAMES = new HashMap<String,String>() {

    /** */
    private static final long serialVersionUID = -4151598783211168327L;

    {
      put("x86", "i386"); //$NON-NLS-1$ //$NON-NLS-2$
    }
  };

  /** Set platform architecture */
  public final static String ARCH = setArchitecture();
  /** Set platform operating system */
  public final static String OS = setOperatingSystem();
  /** Set platform Java virtual machine */
  public final static String JVM = setJavaVirtualMachine();

  private final static String DARWIN = "darwin"; //$NON-NLS-1$
  private final static String WINDOWS = "windows"; //$NON-NLS-1$
  private final static String LINUX = "linux"; //$NON-NLS-1$
  private final static String FREEBSD = "freebsd"; //$NON-NLS-1$
  private final static String OPENBSD = "openbsd"; //$NON-NLS-1$
  private final static String SOLARIS = "solaris"; //$NON-NLS-1$
  private final static String OPENVMS = "openvms"; //$NON-NLS-1$
  private final static String GCJ = "GNU libgcj"; //$NON-NLS-1$

  private static String setArchitecture() {
    final String arch = getProperty("os.arch", "unknown").toLowerCase(); //$NON-NLS-1$ //$NON-NLS-2$
    for (final String s : ARCH_NAMES.keySet()) {
      if (s.equalsIgnoreCase(arch)) {
        return ARCH_NAMES.get(s);
      }
    }
    return arch;
  }

  private static String setOperatingSystem() {
    final String osname = getProperty("os.name", "unknown").toLowerCase(); //$NON-NLS-1$ //$NON-NLS-2$
    for (final String s : OS_NAMES.keySet()) {
      if (s.equalsIgnoreCase(osname)) {
        return OS_NAMES.get(s);
      }
    }
    if (osname.startsWith("windows")) { //$NON-NLS-1$
      return WINDOWS;
    }
    return osname;
  }

  private static String setJavaVirtualMachine() {
    return getProperty("java.vm.name", "unknown"); //$NON-NLS-1$ //$NON-NLS-2$
  }

  /** */
  public final static boolean IS_WINDOWS = WINDOWS.equals(OS);
  /** */
  public final static boolean IS_MAC = DARWIN.equals(OS);
  /** */
  public final static boolean IS_FREEBSD = FREEBSD.equals(OS);
  /** */
  public final static boolean IS_OPENBSD = OPENBSD.equals(OS);
  /** */
  public final static boolean IS_LINUX = LINUX.equals(OS);
  /** */
  public final static boolean IS_SOLARIS = SOLARIS.equals(OS);
  /** */
  public final static boolean IS_BSD = IS_MAC || IS_FREEBSD || IS_OPENBSD;
  /** */
  public final static boolean IS_OPENVMS = OPENVMS.equals(OS);
  /** */
  public final static String NAME = String.format("%s-%s", ARCH, OS); //$NON-NLS-1$
  /** */
  public final static int BIG_ENDIAN = 4321;
  /** */
  public final static int LITTLE_ENDIAN = 1234;
  /** */
  public final static int BYTE_ORDER = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN) ? BIG_ENDIAN : LITTLE_ENDIAN;
  /** */
  public final static boolean IS_GCJ = JVM.equals(GCJ);
  /** */
  public final static boolean IS_JVM_32 = !JVM.contains("64-Bit"); //$NON-NLS-1$

  /**
   * An extension over <code>System.getProperty</code> method. Handles security restrictions, and returns the default value if the access to the property is restricted.
   * 
   * @param property The system property name.
   * @param defValue The default value.
   * @return The value of the system property, or the default value.
   */
  public static String getProperty(String property, String defValue) {
    try {
      return System.getProperty(property, defValue);
    } catch (SecurityException se) {
      return defValue;
    }
  }
}