1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.crypto;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Locale;
24
25
26
27
28
29 final class OsInfo {
30
31 private final static HashMap<String, String> archMapping = new HashMap<>();
32
33
34
35
36
37 static final String X86 = "x86";
38
39
40
41
42
43 static final String X86_64 = "x86_64";
44
45
46
47
48
49 static final String IA64_32 = "ia64_32";
50
51
52
53
54
55 static final String IA64 = "ia64";
56
57
58
59
60
61 static final String PPC = "ppc";
62
63
64
65
66
67 static final String PPC64 = "ppc64";
68
69 static {
70
71 archMapping.put(X86, X86);
72 archMapping.put("i386", X86);
73 archMapping.put("i486", X86);
74 archMapping.put("i586", X86);
75 archMapping.put("i686", X86);
76 archMapping.put("pentium", X86);
77
78
79 archMapping.put(X86_64, X86_64);
80 archMapping.put("amd64", X86_64);
81 archMapping.put("em64t", X86_64);
82 archMapping.put("universal", X86_64);
83
84
85 archMapping.put(IA64, IA64);
86 archMapping.put("ia64w", IA64);
87
88
89 archMapping.put(IA64_32, IA64_32);
90 archMapping.put("ia64n", IA64_32);
91
92
93 archMapping.put(PPC, PPC);
94 archMapping.put("power", PPC);
95 archMapping.put("powerpc", PPC);
96 archMapping.put("power_pc", PPC);
97 archMapping.put("power_rs", PPC);
98
99
100 archMapping.put(PPC64, PPC64);
101 archMapping.put("power64", PPC64);
102 archMapping.put("powerpc64", PPC64);
103 archMapping.put("power_pc64", PPC64);
104 archMapping.put("power_rs64", PPC64);
105 }
106
107
108
109
110
111
112 static String getArchName() {
113
114 final String osArch = System.getProperty("os.arch");
115 if (osArch.startsWith("arm") && getOsNameProperty().contains("Linux")) {
116 final String javaHome = System.getProperty("java.home");
117 try {
118
119 final String[] cmdarray = { "/bin/sh", "-c",
120 "find '" + javaHome + "' -name 'libjvm.so' | head -1 | xargs readelf -A | grep 'Tag_ABI_VFP_args: VFP registers'" };
121 final int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
122 if (exitCode == 0) {
123 return "armhf";
124 }
125 } catch (final IOException | InterruptedException e) {
126
127 }
128 } else {
129 final String string = archMapping.get(osArch.toLowerCase(Locale.US));
130 if (string != null) {
131 return string;
132 }
133 }
134 return translateArchNameToFolderName(osArch);
135 }
136
137
138
139
140
141
142 static String getNativeLibFolderPathForCurrentOS() {
143 return getOSName() + File.separator + getArchName();
144 }
145
146
147
148
149
150
151 static String getOSName() {
152 return translateOSNameToFolderName(getOsNameProperty());
153 }
154
155 static String getOsNameProperty() {
156 return System.getProperty("os.name");
157 }
158
159
160
161
162
163
164 public static void main(final String[] args) {
165 if (args.length >= 1) {
166 if ("--os".equals(args[0])) {
167 System.out.println(getOSName());
168 return;
169 }
170 if ("--arch".equals(args[0])) {
171 System.out.println(getArchName());
172 return;
173 }
174 }
175
176 System.out.println(getNativeLibFolderPathForCurrentOS());
177 }
178
179
180
181
182
183
184
185 private static String translateArchNameToFolderName(final String archName) {
186 return archName.replaceAll("\\W", "");
187 }
188
189
190
191
192
193
194
195 private static String translateOSNameToFolderName(final String osName) {
196 if (osName.contains("Windows")) {
197 return "Windows";
198 }
199 if (osName.contains("Mac")) {
200 return "Mac";
201 }
202 if (osName.contains("Linux")) {
203 return "Linux";
204 }
205 if (osName.contains("AIX")) {
206 return "AIX";
207 }
208 return osName.replaceAll("\\W", "");
209 }
210
211
212
213
214 private OsInfo() {
215 }
216 }