package com.btr.proxy.util; import com.btr.proxy.util.Logger.LogLevel; /***************************************************************************** * Defines some helper methods to find the correct platform. * * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009 ****************************************************************************/ public class PlatformUtil { public enum Platform {WIN, LINUX, MAC_OS, SOLARIS, OTHER} public enum Desktop {WIN, KDE, GNOME, MAC_OS, OTHER} public enum Browser {IE, FIREFOX} private static Platform platform = null; private static Desktop desktop = null; /************************************************************************* * Gets the platform we are currently running on. * @return a platform code. ************************************************************************/ public static synchronized Platform getCurrentPlattform() { if (platform != null) return platform; String osName = System.getProperty("os.name"); Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detecting platform. Name is: {0}", osName); if (osName.toLowerCase().contains("windows")) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Windows platform: {0}", osName); platform = Platform.WIN; } if (osName.toLowerCase().contains("linux")) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Linux platform: {0}", osName); platform = Platform.LINUX; } if (osName.startsWith("Mac OS")) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Mac OS platform: {0}", osName); platform = Platform.MAC_OS; } if (osName.startsWith("SunOS")) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Solaris platform: {0}", osName); platform = Platform.SOLARIS; } if (platform == null) { platform = Platform.OTHER; } return platform; } /************************************************************************* * Gets the ID for the platform default browser. * @return a browser ID, null if no supported browser was detected. ************************************************************************/ public static Browser getDefaultBrowser() { // Use better logic to detect default browser? if (getCurrentPlattform() == Platform.WIN) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Browser is InternetExplorer"); return Browser.IE; } else { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Browser Firefox. Fallback?"); return Browser.FIREFOX; } } /************************************************************************* * Gets the desktop that we are running on. * @return the desktop identifier. ************************************************************************/ public static synchronized Desktop getCurrentDesktop() { if (desktop != null) return desktop; Platform platf = getCurrentPlattform(); if (platf == Platform.WIN) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Windows desktop"); desktop = Desktop.WIN; } if (platf == Platform.MAC_OS) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Mac OS desktop"); desktop = Desktop.MAC_OS; } if (platf == Platform.LINUX || platf == Platform.SOLARIS || platf == Platform.OTHER) { if (isKDE()) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected KDE desktop"); desktop = Desktop.KDE; } if (isGnome()) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Gnome desktop"); desktop = Desktop.GNOME; } } if (desktop == null) { Logger.log(PlatformUtil.class, LogLevel.TRACE, "Detected Unknown desktop"); desktop = Desktop.OTHER; } return desktop; } /************************************************************************* * Checks if we are currently running under Gnome desktop. * @return true if it is a Gnome else false. ************************************************************************/ private static boolean isGnome() { return System.getenv("GNOME_DESKTOP_SESSION_ID") != null; } /************************************************************************* * Checks if we are currently running under KDE desktop. * @return true if it is a KDE else false. ************************************************************************/ private static boolean isKDE() { return System.getenv("KDE_SESSION_VERSION") != null; } }