001    // Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002    // The Molly framework is freely distributable under the terms of an
003    // MIT-style license. For details, see the molly pages web site at:
004    // http://www.mollypages.org/. Use, modify, have fun !
005    
006    package fc.util;
007    
008    
009    /**
010    Identifies the current platform/OS that the JVM is running
011    under. There is only 1 instance of this class corresponding 
012    to each type of platform. Therefore reference equality
013    can be used to compare whether the current platform is
014    the same as some particular platform type.
015    
016    @author hursh jain
017    **/
018    
019    public class Platform 
020    {
021    public static final Platform LINUX = new Platform("linux");
022    public static final Platform WINDOWS = new Platform("windows");
023    public static final Platform SOLARIS = new Platform("solaris");
024    public static final Platform OSX = new Platform("osx");
025    public static final Platform FREEBSD = new Platform("freebsd");
026    public static final Platform OPENBSD = new Platform("openbsd");
027    /** other OS's including HPUX, IRIX, AIX, and many others **/
028    public static final Platform OTHER = new Platform("other");
029    
030    private static Platform currentPlatform;
031    private String name;
032    private static byte count = 1;
033    
034    //inintialize the current platform
035    static {
036      Platform.init();  
037      }
038    
039    private Platform(String name) {
040      this.name = name;
041      }
042    
043    public static Platform getPlatform() {
044      return currentPlatform;
045        }
046    
047    //No need to define/use equals(), since there is one
048    //reference to each type only, == suffices
049    
050    public static boolean isWindows() {
051        return (currentPlatform == WINDOWS);
052        }
053    
054    public static boolean isOSX() {
055        return (currentPlatform == OSX);
056        }
057    
058    public static boolean isLinux() {
059        return (currentPlatform == LINUX);
060        }
061    
062    public static boolean isSolaris() {
063        return (currentPlatform == SOLARIS);
064        }
065    
066    public static boolean isFreeBSD() {
067        return (currentPlatform == FREEBSD);
068        }
069    
070    public static boolean isOpenBSD() {
071        return (currentPlatform == OPENBSD);
072        }
073    
074    public static boolean isOther() {
075        return (currentPlatform == OTHER);
076        }
077    
078    public static boolean isAIX() {
079        return (currentPlatform == WINDOWS);
080        }
081    
082    private static void init() 
083      {
084      String osname = System.getProperty("os.name").toLowerCase();
085      //System.out.println(osname);
086      if ( osname.indexOf("windows") != -1 ) {
087        currentPlatform = WINDOWS;
088        }
089      else if ( osname.indexOf("linux") != -1 ) {
090        currentPlatform = LINUX;
091        }
092      else if ( osname.indexOf("sun") != -1 ) {
093        currentPlatform = SOLARIS;
094        }
095      else if ( osname.toLowerCase().indexOf("mac") != -1 
096          && osname.toLowerCase().indexOf("x") != -1) {
097        currentPlatform = OSX;
098        }
099      else if ( osname.indexOf("freebsd") != -1 ) {
100        currentPlatform = FREEBSD;
101        }
102      else if ( osname.indexOf("openbsd") != -1 ) {
103        currentPlatform = OPENBSD;
104        }
105      else {
106        currentPlatform = OTHER;
107        }
108      }   //~init
109    
110    public String toString() {
111      return name;
112      }
113    
114    public static void main(String[] args)
115      {
116      System.out.println("Current platform: " + getPlatform()); 
117      }
118    
119    }