1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.common.utility;
21
22 import io.netty.util.internal.SystemPropertyUtil;
23
24
25
26
27
28 public final class DetectionUtils {
29 private static final int JAVA_VERSION = javaVersion0();
30 private static final boolean IS_WINDOWS;
31 private static final boolean IS_UNIX_IBM;
32 private static final int NUMBERTHREAD;
33
34 static {
35 final String os = SystemPropertyUtil.get("os.name").toLowerCase();
36
37 IS_WINDOWS = os.contains("win");
38 if (!IS_WINDOWS) {
39 String vendor = SystemPropertyUtil.get("java.vm.vendor");
40 vendor = vendor.toLowerCase();
41 IS_UNIX_IBM = vendor.contains("ibm");
42 } else {
43 IS_UNIX_IBM = false;
44 }
45 NUMBERTHREAD = Math.max(1, SystemPropertyUtil.getInt("org.waarp.numThreads",
46 Runtime.getRuntime()
47 .availableProcessors() *
48 2));
49 }
50
51 private DetectionUtils() {
52 }
53
54
55
56
57 public static int numberThreads() {
58 return NUMBERTHREAD;
59 }
60
61
62
63
64 public static boolean isWindows() {
65 return IS_WINDOWS;
66 }
67
68
69
70
71 public static boolean isUnixIBM() {
72 return IS_UNIX_IBM;
73 }
74
75 public static int javaVersion() {
76 return JAVA_VERSION;
77 }
78
79 private static boolean isAndroid0() {
80
81
82
83
84
85
86
87 final String vmName = SystemPropertyUtil.get("java.vm.name");
88 return "Dalvik".equals(vmName);
89 }
90
91 private static int javaVersion0() {
92 final int majorVersion;
93
94 if (isAndroid0()) {
95 majorVersion = 6;
96 } else {
97 majorVersion = majorVersionFromJavaSpecificationVersion();
98 }
99
100 return majorVersion;
101 }
102
103
104 static int majorVersionFromJavaSpecificationVersion() {
105 return majorVersion(
106 SystemPropertyUtil.get("java.specification.version", "1.6"));
107 }
108
109
110 static int majorVersion(final String javaSpecVersion) {
111 final String[] components = javaSpecVersion.split("\\.");
112 final int[] version = new int[components.length];
113 for (int i = 0; i < components.length; i++) {
114 version[i] = Integer.parseInt(components[i]);
115 }
116
117 if (version[0] == 1) {
118 assert version[1] >= 6;
119 return version[1];
120 } else {
121 return version[0];
122 }
123 }
124 }