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