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
006package fc.util;
007
008
009/**
010Identifies the current platform/OS that the JVM is running
011under. There is only 1 instance of this class corresponding 
012to each type of platform. Therefore reference equality
013can be used to compare whether the current platform is
014the same as some particular platform type.
015
016@author hursh jain
017**/
018
019public class Platform 
020{
021public static final Platform LINUX = new Platform("linux");
022public static final Platform WINDOWS = new Platform("windows");
023public static final Platform SOLARIS = new Platform("solaris");
024public static final Platform OSX = new Platform("osx");
025public static final Platform FREEBSD = new Platform("freebsd");
026public static final Platform OPENBSD = new Platform("openbsd");
027/** other OS's including HPUX, IRIX, AIX, and many others **/
028public static final Platform OTHER = new Platform("other");
029
030private static Platform currentPlatform;
031private String name;
032private static byte count = 1;
033
034//inintialize the current platform
035static {
036  Platform.init();  
037  }
038
039private Platform(String name) {
040  this.name = name;
041  }
042
043public 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
050public static boolean isWindows() {
051    return (currentPlatform == WINDOWS);
052    }
053
054public static boolean isOSX() {
055    return (currentPlatform == OSX);
056    }
057
058public static boolean isLinux() {
059    return (currentPlatform == LINUX);
060    }
061
062public static boolean isSolaris() {
063    return (currentPlatform == SOLARIS);
064    }
065
066public static boolean isFreeBSD() {
067    return (currentPlatform == FREEBSD);
068    }
069
070public static boolean isOpenBSD() {
071    return (currentPlatform == OPENBSD);
072    }
073
074public static boolean isOther() {
075    return (currentPlatform == OTHER);
076    }
077
078public static boolean isAIX() {
079    return (currentPlatform == WINDOWS);
080    }
081
082private 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
110public String toString() {
111  return name;
112  }
113
114public static void main(String[] args)
115  {
116  System.out.println("Current platform: " + getPlatform()); 
117  }
118
119}