summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/util/PlatformUtil.java
blob: 52a25fda5c186fa405d1fdfb7ff5f8223ff831c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
	}
	
}