Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2008 JRuby project Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.ext.ffi;
 
 import org.jruby.Ruby;
 
 public class Platform {
     private static final java.util.Locale LOCALE = ...;
     public static final CPU_TYPE CPU = determineCPU();
     public static final OS_TYPE OS = determineOS();
 
     public static final String NAME =  + "-" + ;
     public static final String LIBPREFIX =  == . ? "" : "lib";
     public static final String LIBSUFFIX = determineLibExt();
     public static final String LIBC = determineLibC();
     
     public static final int BIG_ENDIAN = 4321;
     public static final int LITTLE_ENDIAN = 1234;
     public static final int BYTE_ORDER = ByteOrder.nativeOrder().equals(.) ?  : ;
 
 
     protected final int addressSizelongSize;
     private final long addressMask;
     protected final Pattern libPattern;
     private final int javaVersionMajor;
 
     public enum OS_TYPE {
         DARWIN,
         FREEBSD,
         NETBSD,
         OPENBSD,
         LINUX,
         SOLARIS,
         AIX,
         WINDOWS,
 
         UNKNOWN;
         @Override
         public String toString() { return name().toLowerCase(); }
     }
 
     public enum CPU_TYPE {
         I386,
         X86_64,
         POWERPC,
         POWERPC64,
         SPARC,
         SPARCV9,
         S390X,
         ARM,
         UNKNOWN;
         @Override
         public String toString() { return name().toLowerCase(); }
     }
 
     private static final class SingletonHolder {
         private static final Platform PLATFORM = determinePlatform(determineOS());
     }
 
     private static final OS_TYPE determineOS() {
         String osName = System.getProperty("os.name").split(" ")[0];
         if (startsWithIgnoreCase(osName"mac") || startsWithIgnoreCase(osName"darwin")) {
             return .;
         } else if (startsWithIgnoreCase(osName"sunos") || startsWithIgnoreCase(osName"solaris")) {
            return .;
        }
        for (OS_TYPE os : OS_TYPE.values()) {
            if (startsWithIgnoreCase(osNameos.toString())) {
                return os;
            }
        }
        return .;
    }
    private static final Platform determinePlatform(OS_TYPE os) {
        switch (os) {
            case :
                return new Darwin();
            case :
                return new Linux();
            case :
                return new AIX();
            case :
                return new Windows();
            case :
                return new Unsupported(os);
            default:
                return new Default(os);
        }
    }
    private static final CPU_TYPE determineCPU() {
        String archString = System.getProperty("os.arch").toLowerCase();
        if ("x86".equals(archString) || "i386".equals(archString) || "i86pc".equals(archString)) {
            return .;
        } else if ("x86_64".equals(archString) || "amd64".equals(archString)) {
            return .;
        } else if ("ppc".equals(archString) || "powerpc".equals(archString)) {
            return .;
        } else if ("ppc64".equals(archString)) {
            return .;
        } else if ("sparc".equals(archString)) {
            return .;
        } else if ("sparcv9".equals(archString)) {
            return .;
        } else if ("s390x".equals(archString)) {
            return .;
        } else if ("arm".equals(archString)) {
            return .;
        } else if ("universal".equals(archString)) {
            // OS X OpenJDK7 builds report "universal" right now
            String bits = SafePropertyAccessor.getProperty("sun.arch.data.model");
            if ("32".equals(bits)) {
                System.setProperty("os.arch""i386");
                return .;
            } else if ("64".equals(bits)) {
                System.setProperty("os.arch""x86_64");
                return .;
            }
        }
        return .;
    }
    private static final String determineLibC() {
        switch () {
            case :
                return "msvcrt.dll";
            case :
                return "libc.so.6";
            case :
                if (Integer.getInteger("sun.arch.data.model") == 32) {
                    return "libc.a(shr.o)";
                } else {
                    return "libc.a(shr_64.o)";
                }
            default:
                return  + "c." + ;
        }
    }
    private static final String determineLibExt() {
        switch () {
            case :
                return "dll";
            case :
                return "a";
            case :
                return "dylib";
            default:
                return "so";
        }
    }
    protected Platform(OS_TYPE os) {
        int dataModel = Integer.getInteger("sun.arch.data.model");
        if (dataModel != 32 && dataModel != 64) {
            switch () {
                case :
                case :
                case :
                    dataModel = 32;
                    break;
                case :
                case :
                case :
                case :
                    dataModel = 64;
                    break;
                default:
                    dataModel = 0;
            }
        }
         = dataModel;
         =  == 32 ? 0xffffffffL : 0xffffffffffffffffL;
         = os == . ? 32 : // Windows is LLP64
        String libpattern = null;
        switch (os) {
            case :
                libpattern = ".*\\.dll$";
                break;
            case :
                libpattern = "lib.*\\.(dylib|jnilib)$";
                break;
            case :
                libpattern = "lib.*\\.a$";
                break;
            default:
                libpattern = "lib.*\\.so.*$";
                break;
        }
         = Pattern.compile(libpattern);
        int version = 5;
        try {
            String versionString = System.getProperty("java.version");
            if (versionString != null) {
                String[] v = versionString.split("\\.");
                version = Integer.valueOf(v[1]);
            }
        } catch (Exception ex) {
            version = 0;
        }
         = version;
    }

    
Gets the current Platform

Returns:
The current platform.
    public static final Platform getPlatform() {
        return .;
    }

    
Gets the current Operating System.

Returns:
A OS value representing the current Operating System.
    public final OS_TYPE getOS() {
        return ;
    }

    
Gets the current processor architecture the JVM is running on.

Returns:
A CPU value representing the current processor architecture.
    public final CPU_TYPE getCPU() {
        return ;
    }

    
Gets the version of the Java Virtual Machine (JVM) jffi is running on.

Returns:
A number representing the java version. e.g. 5 for java 1.5, 6 for java 1.6
    public final int getJavaMajorVersion() {
        return ;
    }
    public final boolean isBSD() {
        return  == . ||  == . ||  == . ||  == .;
    }
    public final boolean isUnix() {
        return  != .;
    }
    public final boolean isSupported() {
        return  != . 
                &&  != .
                && ( == 32 ||  == 64)
                &&  >= 5;
    }
    public static void createPlatformModule(Ruby runtimeRubyModule ffi) {
        RubyModule module = ffi.defineModuleUnder("Platform");
        Platform platform = Platform.getPlatform();
        OS_TYPE os = platform.getOS();
        module.defineConstant("ADDRESS_SIZE"runtime.newFixnum(platform.addressSize));
        module.defineConstant("LONG_SIZE"runtime.newFixnum(platform.longSize));
        module.defineConstant("OS"runtime.newString(.toString()));
        module.defineConstant("ARCH"runtime.newString(platform.getCPU().toString()));
        module.defineConstant("NAME"runtime.newString(platform.getName()));
        module.defineConstant("IS_WINDOWS"runtime.newBoolean(os == .));
        module.defineConstant("IS_BSD"runtime.newBoolean(platform.isBSD()));
        module.defineConstant("IS_FREEBSD"runtime.newBoolean(os == .));
        module.defineConstant("IS_OPENBSD"runtime.newBoolean(os == .));
        module.defineConstant("IS_SOLARIS"runtime.newBoolean(os == .));
        module.defineConstant("IS_LINUX"runtime.newBoolean(os == .));
        module.defineConstant("IS_MAC"runtime.newBoolean(os == .));
        module.defineConstant("LIBC"runtime.newString());
        module.defineConstant("LIBPREFIX"runtime.newString());
        module.defineConstant("LIBSUFFIX"runtime.newString());
        module.defineConstant("BYTE_ORDER"runtime.newFixnum());
        module.defineConstant("BIG_ENDIAN"runtime.newFixnum());
        module.defineConstant("LITTLE_ENDIAN"runtime.newFixnum());
        module.defineAnnotatedMethods(Platform.class);
    }
    @JRubyMethod(name = "windows?", module=true)
    public static IRubyObject windows_p(ThreadContext contextIRubyObject recv) {
        return context.runtime.newBoolean( == .);
    }
    @JRubyMethod(name = "mac?", module=true)
    public static IRubyObject mac_p(ThreadContext contextIRubyObject recv) {
        return context.runtime.newBoolean( == .);
    }
    @JRubyMethod(name = "unix?", module=true)
    public static IRubyObject unix_p(ThreadContext contextIRubyObject recv) {
        return context.runtime.newBoolean(Platform.getPlatform().isUnix());
    }
    @JRubyMethod(name = "bsd?", module=true)
    public static IRubyObject bsd_p(ThreadContext contextIRubyObject recv) {
        return context.runtime.newBoolean(Platform.getPlatform().isBSD());
    }
    @JRubyMethod(name = "linux?", module=true)
    public static IRubyObject linux_p(ThreadContext contextIRubyObject recv) {
        return context.runtime.newBoolean( == .);
    }
    @JRubyMethod(name = "solaris?", module=true)
    public static IRubyObject solaris_p(ThreadContext contextIRubyObject recv) {
        return context.runtime.newBoolean( == .);
    }
    
An extension over System.getProperty method. Handles security restrictions, and returns the default value if the access to the property is restricted.

Parameters:
property The system property name.
defValue The default value.
Returns:
The value of the system property, or the default value.
    public static String getProperty(String propertyString defValue) {
        try {
            return System.getProperty(propertydefValue);
        } catch (SecurityException se) {
            return defValue;
        }
    }
    
Gets the size of a C 'long' on the native platform.

Returns:
the size of a long in bits
    public final int longSize() {
        return ;
    }

    
Gets the size of a C address/pointer on the native platform.

Returns:
the size of a pointer in bits
    public final int addressSize() {
        return ;
    }
    
    
Gets the 32/64bit mask of a C address/pointer on the native platform.

Returns:
the size of a pointer in bits
    public final long addressMask() {
        return ;
    }

    
Gets the name of this Platform.

Returns:
The name of this platform.
    public String getName() {
        return  + "-" + ;
    }
    public String mapLibraryName(String libName) {
        //
        // A specific version was requested - use as is for search
        //
        if (.matcher(libName).find()) {
            return libName;
        }
        return System.mapLibraryName(libName);
    }
    private static class Supported extends Platform {
        public Supported(OS_TYPE os) {
            super(os);
        }
    }
    private static class Unsupported extends Platform {
        public Unsupported(OS_TYPE os) {
            super(os);
        }
    }
    private static final class Default extends Platform {
        public Default(OS_TYPE os) {
            super(os);
        }
    }
    
A Platform subclass representing the MacOS system.
    private static final class Darwin extends Supported {
        public Darwin() {
            super(.);
        }
        @Override
        public String mapLibraryName(String libName) {
            //
            // A specific version was requested - use as is for search
            //
            if (.matcher(libName).find()) {
                return libName;
            }
            return "lib" + libName + ".dylib";
        }
    }

    
A Platform subclass representing the Linux operating system.
    private static final class Linux extends Supported {
        public Linux() {
            super(.);
        }
        
        @Override
        public String mapLibraryName(String libName) {
            // Older JDK on linux map 'c' to 'libc.so' which doesn't work
            return "c".equals(libName) || "libc.so".equals(libName)
                    ? "libc.so.6" : super.mapLibraryName(libName);
        }
    }

    
A Platform subclass representing the Linux operating system.
    private static final class AIX extends Supported {
        public AIX() {
            super(.);
        }
        @Override
        public String mapLibraryName(String libName) {
            return "c".equals(libName) || "libc.so".equals(libName)
                    ?  : super.mapLibraryName(libName);
        }
    }

    
A Platform subclass representing the Windows system.
    private static class Windows extends Supported {
        public Windows() {
            super(.);
        }
    }
    private static boolean startsWithIgnoreCase(String s1String s2) {
        return s1.startsWith(s2)
            || s1.toUpperCase().startsWith(s2.toUpperCase())
            || s1.toLowerCase().startsWith(s2.toLowerCase());
    }
New to GrepCode? Check out our FAQ X