]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/OS.java
Deal with corner case of empty localised strings
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / OS.java
1 package org.argeo.cms.util;
2
3 import java.io.File;
4 import java.nio.file.Path;
5 import java.nio.file.Paths;
6
7 /**
8 * Wrapper around system properties and portable Java APIS, for when OS specific
9 * informations are needed.
10 */
11 public class OS {
12 public final static OS LOCAL = new OS();
13
14 private final String arch, name, version;
15
16 /** The OS of the running JVM */
17 protected OS() {
18 arch = System.getProperty("os.arch");
19 name = System.getProperty("os.name");
20 version = System.getProperty("os.version");
21 }
22
23 public String getArch() {
24 return arch;
25 }
26
27 public String getName() {
28 return name;
29 }
30
31 public String getVersion() {
32 return version;
33 }
34
35 public boolean isMSWindows() {
36 // only MS Windows would use such an horrendous separator...
37 return File.separatorChar == '\\';
38 }
39
40 public String[] getDefaultShellCommand() {
41 if (!isMSWindows())
42 return new String[] { "/bin/bash", "-l", "-i" };
43 else
44 return new String[] { "cmd.exe", "/C" };
45 }
46
47 // public static long getJvmPid() {
48 // return ProcessHandle.current().pid();
49 //// String pidAndHost = ManagementFactory.getRuntimeMXBean().getName();
50 //// return Integer.parseInt(pidAndHost.substring(0, pidAndHost.indexOf('@')));
51 // }
52
53 /**
54 * Get the runtime directory. It will be the environment variable
55 * XDG_RUNTIME_DIR if it is set, or /run if the user is 'root', or
56 * ~/.cache/argeo if not.
57 */
58 public static Path getRunDir() {
59 Path runDir;
60 String xdgRunDir = System.getenv("XDG_RUNTIME_DIR");
61 if (xdgRunDir != null) {
62 // TODO support multiple names
63 runDir = Paths.get(xdgRunDir);
64 } else {
65 if (System.getProperty("user.name").equals("root")) {
66 runDir = Paths.get("/run");
67 } else {
68 runDir = Paths.get(System.getProperty("user.home"), ".cache/argeo");
69 }
70 }
71 return runDir;
72 }
73 }