PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/xuggle/ferry/JNIEnv.java

https://github.com/eix128/xuggle-xuggler
Java | 225 lines | 118 code | 13 blank | 94 comment | 31 complexity | cc0cdba1cc82f3138a81cf8691fa755d MD5 | raw file
  1/*******************************************************************************
  2 * Copyright (c) 2012, Art Clarke.  All rights reserved.
  3 *  
  4 * This file is part of Xuggle-Xuggler-Main.
  5 *
  6 * Xuggle-Xuggler-Main is free software: you can redistribute it and/or modify
  7 * it under the terms of the GNU Lesser General Public License as published by
  8 * the Free Software Foundation, either version 3 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * Xuggle-Xuggler-Main is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU Lesser General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU Lesser General Public License
 17 * along with Xuggle-Xuggler-Main.  If not, see <http://www.gnu.org/licenses/>.
 18 *******************************************************************************/
 19package com.xuggle.ferry;
 20
 21/**
 22 * A set of utilities that can be used to find out
 23 * information about the Native environment that
 24 * we are running within.
 25 * @author aclarke
 26 *
 27 */
 28public class JNIEnv
 29{
 30  /**
 31   * The CPU architecture (i.e. chip architecture).
 32   * @author aclarke
 33   *
 34   */
 35  enum CPUArch{
 36    /** 32-bit systems such as the i386, i486, i586 or i686 */
 37    X86,
 38    /** 64-bit systems based on the x86 family */
 39    X86_64,
 40    /** 32-bit PowerPC based systems */
 41    PPC,
 42    /** 64-bit PowerPC based systems */
 43    PPC64,
 44    /** A chip architecture unknown to {@link Ferry}. */
 45    UNKNOWN,
 46  }
 47  /** The Operating system family */
 48  enum OSFamily {
 49    /** Linux family of operating systems */
 50    LINUX,
 51    /** Macintosh (darwin) family of operating systems */
 52    MAC,
 53    /** Microsoft Windows family of operating systems */
 54    WINDOWS,
 55    /** An unknown OS that {@link Ferry} knows nothing about */
 56    UNKNOWN,
 57  }
 58  /**
 59   * Our singleton.
 60   */
 61  private static final JNIEnv mEnv = new JNIEnv();
 62  
 63  private final CPUArch mCPUArch;
 64  private final OSFamily mOSFamily;
 65  
 66  /** Private. Only one {@link JNIEnv} is allowed per Java Virtual machine. */
 67  private JNIEnv() {
 68    mCPUArch = getCPUArch(System.getProperty("os.arch"));
 69    mOSFamily = getOSFamily(System.getProperty("os.name"));
 70  }
 71  
 72  /**
 73   * Get the (static) {@link JNIEnv} for this JVM.
 74   * @return the environment
 75   */
 76  public static JNIEnv getEnv() { return mEnv; }
 77
 78  /**
 79   * Get the CPU architecture based on the passed in javaCPUArch specifier.
 80   * 
 81   * The string should be of a format returned from {@link System#getProperty(String)}
 82   * for the property "os.arch".
 83   * 
 84   * @param javaCPU the string to parse
 85   * @return the CPU architecture
 86   * @see System#getProperty(String)
 87   */
 88  public static CPUArch getCPUArch(String javaCPU)
 89  {
 90    final CPUArch javaArch;
 91    final String javaCPUArch = javaCPU != null ? javaCPU.toLowerCase() : "";
 92    
 93    // first parse the java arch
 94    if (javaCPUArch.startsWith("x86_64") ||
 95        javaCPUArch.startsWith("amd64") ||
 96        javaCPUArch.startsWith("ia64")) {
 97      javaArch = CPUArch.X86_64;
 98    } else if (
 99        javaCPUArch.startsWith("ppc64") ||
100        javaCPUArch.startsWith("powerpc64")
101        ) {
102      javaArch = CPUArch.PPC64;
103    } else if (
104        javaCPUArch.startsWith("ppc") ||
105        javaCPUArch.startsWith("powerpc")
106        ) {
107      javaArch = CPUArch.PPC;
108    } else if (
109        javaCPUArch.contains("86")
110        )
111    {
112      javaArch = CPUArch.X86;
113    } else {
114      javaArch = CPUArch.UNKNOWN;
115    }
116    return javaArch;
117  }
118  
119  /**
120   * Return a CPUArch from parsing a GNU autoconf triple.
121   * 
122   * For example "x86_64-w64-mingw32" will return {@link JNIEnv.CPUArch#X86_64}
123   * and "ppc-apple-darwin" will return {@link JNIEnv.CPUArch#PPC}.
124   * 
125   * @param gnuString the GNU string
126   * @return the architecture
127   */
128  public static CPUArch getCPUArchFromGNUString(String gnuString)
129  {
130    final String nativeCpu = gnuString.toLowerCase();
131    final CPUArch nativeArch;
132    // then the native arch
133    if (nativeCpu.startsWith("x86_64") ||
134        nativeCpu.startsWith("amd64") ||
135        nativeCpu.startsWith("ia64"))
136      nativeArch = CPUArch.X86_64;
137    else if (
138        nativeCpu.startsWith("ppc64") ||
139        nativeCpu.startsWith("powerpc64")
140        )
141      nativeArch = CPUArch.PPC64;
142    else if (
143        nativeCpu.startsWith("ppc") ||
144        nativeCpu.startsWith("powerpc")
145        )
146      nativeArch = CPUArch.PPC;
147    else if (
148        nativeCpu.contains("86")
149        )
150      nativeArch = CPUArch.X86;
151    else
152      nativeArch = CPUArch.UNKNOWN;
153 
154    return nativeArch;
155  }
156  
157  /**
158   * Get the OSFamily based on the passed in osName specifier.
159   * 
160   * The string should be of a format returned from {@link System#getProperty(String)}
161   * for the property "os.name".
162   * 
163   * @param osName the string to parse
164   * @return the OSFamily
165   * @see System#getProperty(String)
166   */
167  public static OSFamily getOSFamily(String osName)
168  {
169    final OSFamily retval;
170    
171    if (osName != null && osName.length() > 0)
172    {
173      if (osName.startsWith("Windows"))
174        retval = OSFamily.WINDOWS;
175      else if (osName.startsWith("Mac"))
176        retval = OSFamily.MAC;
177      else if (osName.startsWith("Linux"))
178        retval = OSFamily.LINUX;
179      else
180        retval = OSFamily.UNKNOWN;
181    } else
182      retval = OSFamily.UNKNOWN;
183    return retval;
184  }
185  
186  /**
187   * Return an OS Family from parsing a GNU autoconf triple.
188   * 
189   * For example "x86_64-w64-mingw32" will return {@link JNIEnv.OSFamily#WINDOWS}
190   * and "ppc-apple-darwin" will return {@link JNIEnv.OSFamily#MAC}.
191   * 
192   * @param gnuString the GNU string
193   * @return the OSFamily
194   */
195  public static OSFamily getOSFamilyFromGNUString(String gnuString)
196  {
197    final String nativeOs = (gnuString != null ? gnuString.toLowerCase() : "");
198    final OSFamily retval;
199    if (nativeOs.startsWith("mingw") || nativeOs.startsWith("cygwin"))
200      retval = OSFamily.WINDOWS;
201    else if (nativeOs.startsWith("darwin"))
202      retval = OSFamily.MAC;
203    else if (nativeOs.startsWith("linux"))
204      retval = OSFamily.LINUX;
205    else
206      retval = OSFamily.UNKNOWN;
207    return retval;
208  }
209
210  /**
211   * @return the {@link JNIEnv.CPUArch} of this Java Virtual Machine.
212   */
213  public CPUArch getCPUArch()
214  {
215    return mCPUArch;
216  }
217
218  /**
219   * @return the {@link JNIEnv.OSFamily} of this Java Virtual Machine.
220   */
221  public OSFamily getOSFamily()
222  {
223    return mOSFamily;
224  }
225}