View Javadoc
1   package org.argeo.util;
2   
3   import java.io.File;
4   import java.lang.management.ManagementFactory;
5   
6   /** When OS specific informations are needed. */
7   public class OS {
8   	public final static OS LOCAL = new OS();
9   
10  	private final String arch, name, version;
11  
12  	/** The OS of the running JVM */
13  	protected OS() {
14  		arch = System.getProperty("os.arch");
15  		name = System.getProperty("os.name");
16  		version = System.getProperty("os.version");
17  	}
18  
19  	public String getArch() {
20  		return arch;
21  	}
22  
23  	public String getName() {
24  		return name;
25  	}
26  
27  	public String getVersion() {
28  		return version;
29  	}
30  
31  	public boolean isMSWindows() {
32  		// only MS Windows would use such an horrendous separator...
33  		return File.separatorChar == '\\';
34  	}
35  
36  	public String[] getDefaultShellCommand() {
37  		if (!isMSWindows())
38  			return new String[] { "/bin/sh", "-l", "-i" };
39  		else
40  			return new String[] { "cmd.exe", "/C" };
41  	}
42  
43  	public static Integer getJvmPid() {
44  		/*
45  		 * This method works on most platforms (including Linux). Although when Java 9
46  		 * comes along, there is a better way: long pid =
47  		 * ProcessHandle.current().getPid();
48  		 *
49  		 * See:
50  		 * http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-
51  		 * process-id
52  		 */
53  		String pidAndHost = ManagementFactory.getRuntimeMXBean().getName();
54  		return Integer.parseInt(pidAndHost.substring(0, pidAndHost.indexOf('@')));
55  	}
56  }