PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/java/org/jboss/modules/NativeLibraryResourceLoader.java

https://github.com/jboss-modules/jboss-modules
Java | 430 lines | 357 code | 21 blank | 52 comment | 180 complexity | 36b5955f38c519fb034d92f300cf438c MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2014 Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.jboss.modules;
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.net.URI;
  25. import java.nio.charset.StandardCharsets;
  26. import java.security.AccessController;
  27. import java.security.PrivilegedAction;
  28. import java.util.ArrayList;
  29. import java.util.Locale;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32. /**
  33. * A base class for resource loaders which can load native libraries.
  34. *
  35. * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
  36. */
  37. public class NativeLibraryResourceLoader extends AbstractResourceLoader {
  38. /**
  39. * Separate class for native platform ID which is only loaded when native libs are loaded.
  40. */
  41. static class Identification {
  42. static final String OS_ID;
  43. static final String CPU_ID;
  44. static final String ARCH_NAME;
  45. static final String[] NATIVE_SEARCH_PATHS;
  46. static final Pattern OS_RELEASE_VERSION_ID_PATTERN = Pattern.compile("([0-9]+).*");
  47. static final Pattern DISTRIBUTION_RELEASE_VERSION_PATTERN = Pattern.compile(".*\\s([0-9]+).*");
  48. static final Pattern MAC_VERSION_PATTERN = Pattern.compile("([0-9]+\\.[0-9]+)\\.[0-9]+");
  49. static final String OS_RELEASE_FILE = "/etc/os-release";
  50. static final String FEDORA_RELEASE_FILE = "/etc/fedora-release";
  51. static final String REDHAT_RELEASE_FILE = "/etc/redhat-release";
  52. static final String ID = "ID=";
  53. static final String VERSION_ID = "VERSION_ID=";
  54. static final String RHEL = "rhel";
  55. static final String FEDORA = "fedora";
  56. static {
  57. final Object[] strings = AccessController.doPrivileged(new PrivilegedAction<Object[]>() {
  58. public Object[] run() {
  59. // First, identify the operating system.
  60. boolean knownOs = true;
  61. String osName;
  62. String osVersion = null;
  63. // let the user override it.
  64. osName = System.getProperty("jboss.modules.os-name");
  65. if (osName == null) {
  66. String sysOs = System.getProperty("os.name");
  67. if (sysOs == null) {
  68. osName = "unknown";
  69. knownOs = false;
  70. } else {
  71. sysOs = sysOs.toUpperCase(Locale.US);
  72. if (sysOs.startsWith("LINUX")) {
  73. osName = "linux";
  74. osVersion = getLinuxOSVersion();
  75. } else if (sysOs.startsWith("MAC OS")) {
  76. osName = "macosx";
  77. String sysVersion = System.getProperty("os.version");
  78. Matcher m = MAC_VERSION_PATTERN.matcher(sysVersion);
  79. if (m.matches()) {
  80. osVersion = m.group(1);
  81. }
  82. } else if (sysOs.startsWith("WINDOWS")) {
  83. osName = "win";
  84. } else if (sysOs.startsWith("OS/2")) {
  85. osName = "os2";
  86. } else if (sysOs.startsWith("SOLARIS") || sysOs.startsWith("SUNOS")) {
  87. osName = "solaris";
  88. } else if (sysOs.startsWith("MPE/IX")) {
  89. osName = "mpeix";
  90. } else if (sysOs.startsWith("HP-UX")) {
  91. osName = "hpux";
  92. } else if (sysOs.startsWith("AIX")) {
  93. osName = "aix";
  94. } else if (sysOs.startsWith("OS/390")) {
  95. osName = "os390";
  96. } else if (sysOs.startsWith("OS/400")) {
  97. osName = "os400";
  98. } else if (sysOs.startsWith("FREEBSD")) {
  99. osName = "freebsd";
  100. } else if (sysOs.startsWith("OPENBSD")) {
  101. osName = "openbsd";
  102. } else if (sysOs.startsWith("NETBSD")) {
  103. osName = "netbsd";
  104. } else if (sysOs.startsWith("IRIX")) {
  105. osName = "irix";
  106. } else if (sysOs.startsWith("DIGITAL UNIX")) {
  107. osName = "digitalunix";
  108. } else if (sysOs.startsWith("OSF1")) {
  109. osName = "osf1";
  110. } else if (sysOs.startsWith("OPENVMS")) {
  111. osName = "openvms";
  112. } else if (sysOs.startsWith("IOS")) {
  113. osName = "iOS";
  114. } else {
  115. osName = "unknown";
  116. knownOs = false;
  117. }
  118. }
  119. }
  120. // Next, our CPU ID and its compatible variants.
  121. boolean knownCpu = true;
  122. ArrayList<String> cpuNames = new ArrayList<>();
  123. String cpuName = System.getProperty("jboss.modules.cpu-name");
  124. if (cpuName == null) {
  125. String sysArch = System.getProperty("os.arch");
  126. if (sysArch == null) {
  127. cpuName = "unknown";
  128. knownCpu = false;
  129. } else {
  130. boolean hasEndian = false;
  131. boolean hasHardFloatABI = false;
  132. sysArch = sysArch.toUpperCase(Locale.US);
  133. if (sysArch.startsWith("SPARCV9") || sysArch.startsWith("SPARC64")) {
  134. cpuName = "sparcv9";
  135. } else if (sysArch.startsWith("SPARC")) {
  136. cpuName = "sparc";
  137. } else if (sysArch.startsWith("X86_64") || sysArch.startsWith("AMD64")) {
  138. cpuName = "x86_64";
  139. } else if (sysArch.startsWith("I386") || sysArch.startsWith("I486") || sysArch.startsWith("I586") || sysArch.startsWith("I686") || sysArch.startsWith("X86") || sysArch.contains("IA32")) {
  140. cpuName = "i686";
  141. } else if (sysArch.startsWith("X32")) {
  142. cpuName = "x32";
  143. } else if (sysArch.startsWith("PPC64")) {
  144. cpuName = "ppc64";
  145. } else if (sysArch.startsWith("PPC") || sysArch.startsWith("POWER")) {
  146. cpuName = "ppc";
  147. } else if (sysArch.startsWith("ARMV7A") || sysArch.contains("AARCH32")) {
  148. hasEndian = true;
  149. hasHardFloatABI = true;
  150. cpuName = "armv7a";
  151. } else if (sysArch.startsWith("AARCH64") || sysArch.startsWith("ARM64") || sysArch.startsWith("ARMV8") || sysArch.startsWith("PXA9") || sysArch.startsWith("PXA10")) {
  152. hasEndian = true;
  153. cpuName = "aarch64";
  154. } else if (sysArch.startsWith("PXA27")) {
  155. hasEndian = true;
  156. cpuName = "armv5t-iwmmx";
  157. } else if (sysArch.startsWith("PXA3")) {
  158. hasEndian = true;
  159. cpuName = "armv5t-iwmmx2";
  160. } else if (sysArch.startsWith("ARMV4T") || sysArch.startsWith("EP93")) {
  161. hasEndian = true;
  162. cpuName = "armv4t";
  163. } else if (sysArch.startsWith("ARMV4") || sysArch.startsWith("EP73")) {
  164. hasEndian = true;
  165. cpuName = "armv4";
  166. } else if (sysArch.startsWith("ARMV5T") || sysArch.startsWith("PXA") || sysArch.startsWith("IXC") || sysArch.startsWith("IOP") || sysArch.startsWith("IXP") || sysArch.startsWith("CE")) {
  167. hasEndian = true;
  168. String isaList = System.getProperty("sun.arch.isalist");
  169. if (isaList != null) {
  170. if (isaList.toUpperCase(Locale.US).contains("MMX2")) {
  171. cpuName = "armv5t-iwmmx2";
  172. } else if (isaList.toUpperCase(Locale.US).contains("MMX")) {
  173. cpuName = "armv5t-iwmmx";
  174. } else {
  175. cpuName = "armv5t";
  176. }
  177. } else {
  178. cpuName = "armv5t";
  179. }
  180. } else if (sysArch.startsWith("ARMV5")) {
  181. hasEndian = true;
  182. cpuName = "armv5";
  183. } else if (sysArch.startsWith("ARMV6")) {
  184. hasEndian = true;
  185. hasHardFloatABI = true;
  186. cpuName = "armv6";
  187. } else if (sysArch.startsWith("PA_RISC2.0W")) {
  188. cpuName = "parisc64";
  189. } else if (sysArch.startsWith("PA_RISC") || sysArch.startsWith("PA-RISC")) {
  190. cpuName = "parisc";
  191. } else if (sysArch.startsWith("IA64")) {
  192. // HP-UX reports IA64W for 64-bit Itanium and IA64N when running
  193. // in 32-bit mode.
  194. cpuName = sysArch.toLowerCase(Locale.US);
  195. } else if (sysArch.startsWith("ALPHA")) {
  196. cpuName = "alpha";
  197. } else if (sysArch.startsWith("MIPS")) {
  198. cpuName = "mips";
  199. } else {
  200. knownCpu = false;
  201. cpuName = "unknown";
  202. }
  203. boolean be = false;
  204. boolean hf = false;
  205. if (knownCpu && hasEndian && "big".equals(System.getProperty("sun.cpu.endian", "little"))) {
  206. be = true;
  207. }
  208. if (knownCpu && hasHardFloatABI) {
  209. String archAbi = System.getProperty("sun.arch.abi");
  210. if (archAbi != null) {
  211. if (archAbi.toUpperCase(Locale.US).contains("HF")) {
  212. hf = true;
  213. }
  214. } else {
  215. String libPath = System.getProperty("java.library.path");
  216. if (libPath != null && libPath.toUpperCase(Locale.US).contains("GNUEABIHF")) {
  217. hf = true;
  218. }
  219. }
  220. if (hf) cpuName += "-hf";
  221. }
  222. if (knownCpu) {
  223. switch (cpuName) {
  224. case "i686": cpuNames.add("i686");
  225. case "i586": cpuNames.add("i586");
  226. case "i486": cpuNames.add("i486");
  227. case "i386": cpuNames.add("i386");
  228. break;
  229. case "armv7a": cpuNames.add("armv7a"); if (hf) break;
  230. case "armv6": cpuNames.add("armv6"); if (hf) break;
  231. case "armv5t": cpuNames.add("armv5t");
  232. case "armv5": cpuNames.add("armv5");
  233. case "armv4t": cpuNames.add("armv4t");
  234. case "armv4": cpuNames.add("armv4");
  235. break;
  236. case "armv5t-iwmmx2": cpuNames.add("armv5t-iwmmx2");
  237. case "armv5t-iwmmx": cpuNames.add("armv5t-iwmmx");
  238. cpuNames.add("armv5t");
  239. cpuNames.add("armv5");
  240. cpuNames.add("armv4t");
  241. cpuNames.add("armv4");
  242. break;
  243. default: cpuNames.add(cpuName);
  244. break;
  245. }
  246. if (hf || be) for (int i = 0; i < cpuNames.size(); i++) {
  247. String name = cpuNames.get(i);
  248. if (be) name += "-be";
  249. if (hf) name += "-hf";
  250. cpuNames.set(i, name);
  251. }
  252. cpuName = cpuNames.get(0);
  253. }
  254. }
  255. } else {
  256. cpuNames.add(cpuName);
  257. }
  258. // Finally, search paths.
  259. final int cpuCount = cpuNames.size();
  260. final int searchPathsSize = osVersion != null ? cpuCount * 2 : cpuCount;
  261. String[] searchPaths = new String[searchPathsSize];
  262. if (knownOs && knownCpu) {
  263. // attempt OS-version specific category first
  264. String osNameAndVersion = osVersion != null ? osName + "-" + osVersion : osName;
  265. for (int i = 0; i < cpuCount; i++) {
  266. final String name = cpuNames.get(i);
  267. searchPaths[i] = osNameAndVersion + "-" + name;
  268. }
  269. // fallback to general category
  270. if (osVersion != null) {
  271. int j = cpuCount;
  272. for (int i = 0; i < cpuCount; i++) {
  273. final String name = cpuNames.get(i);
  274. searchPaths[j++] = osName + "-" + name;
  275. }
  276. }
  277. } else {
  278. searchPaths = new String[0];
  279. }
  280. return new Object[] {
  281. osName,
  282. cpuName,
  283. osName + "-" + cpuName,
  284. searchPaths
  285. };
  286. }
  287. });
  288. OS_ID = strings[0].toString();
  289. CPU_ID = strings[1].toString();
  290. ARCH_NAME = strings[2].toString();
  291. NATIVE_SEARCH_PATHS = (String[]) strings[3];
  292. }
  293. private static String getLinuxOSVersionFromOSReleaseFile() {
  294. try (InputStream releaseFile = new FileInputStream(OS_RELEASE_FILE)) {
  295. try (InputStreamReader inputStreamReader = new InputStreamReader(releaseFile, StandardCharsets.UTF_8)) {
  296. try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
  297. String currentLine;
  298. String id = null;
  299. String versionId = null;
  300. while ((id == null || versionId == null) && (currentLine = reader.readLine()) != null) {
  301. final String trimmed = currentLine.trim();
  302. if (trimmed.startsWith(ID)) {
  303. int equalsIndex = trimmed.indexOf('=');
  304. id = trimmed.substring(equalsIndex + 1).replaceAll("\"", "");
  305. } else if (trimmed.startsWith(VERSION_ID)) {
  306. int equalsIndex = trimmed.indexOf('=');
  307. versionId = trimmed.substring(equalsIndex + 1).replaceAll("\"", "");
  308. }
  309. }
  310. if (id != null && versionId != null) {
  311. String abbreviatedVersionId = versionId;
  312. Matcher m = OS_RELEASE_VERSION_ID_PATTERN.matcher(versionId);
  313. if (m.matches()) {
  314. abbreviatedVersionId = m.group(1);
  315. }
  316. return id + abbreviatedVersionId;
  317. }
  318. return null;
  319. }
  320. }
  321. } catch (Exception e) {
  322. return null;
  323. }
  324. }
  325. private static String getLinuxOSVersionFromDistributionFile(String distributionFile) {
  326. try (InputStream releaseFile = new FileInputStream(distributionFile)) {
  327. try (InputStreamReader inputStreamReader = new InputStreamReader(releaseFile, StandardCharsets.UTF_8)) {
  328. try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
  329. String currentLine;
  330. String id = null;
  331. String abbreviatedVersionId = null;
  332. if ((currentLine = reader.readLine()) != null) {
  333. final String trimmed = currentLine.trim();
  334. if (trimmed.startsWith("Red Hat Enterprise Linux")) {
  335. id = RHEL;
  336. } else if (trimmed.startsWith("Fedora")) {
  337. id = FEDORA;
  338. }
  339. Matcher m = DISTRIBUTION_RELEASE_VERSION_PATTERN.matcher(trimmed);
  340. if (m.matches()) {
  341. abbreviatedVersionId = m.group(1);
  342. }
  343. }
  344. if (id != null && abbreviatedVersionId != null) {
  345. return id + abbreviatedVersionId;
  346. }
  347. return null;
  348. }
  349. }
  350. } catch (Exception e) {
  351. return null;
  352. }
  353. }
  354. private static String getLinuxOSVersion() {
  355. String osVersion = getLinuxOSVersionFromOSReleaseFile();
  356. if (osVersion == null) {
  357. osVersion = getLinuxOSVersionFromDistributionFile(FEDORA_RELEASE_FILE);
  358. if (osVersion == null) {
  359. osVersion = getLinuxOSVersionFromDistributionFile(REDHAT_RELEASE_FILE);
  360. }
  361. }
  362. return osVersion;
  363. }
  364. }
  365. /**
  366. * The filesystem root of the resource loader.
  367. */
  368. private final File root;
  369. /**
  370. * Construct a new instance.
  371. *
  372. * @param root the filesystem root of the resource loader
  373. */
  374. public NativeLibraryResourceLoader(final File root) {
  375. this.root = root;
  376. }
  377. /** {@inheritDoc} */
  378. public String getLibrary(final String name) {
  379. final String mappedName = System.mapLibraryName(name);
  380. final File root = this.root;
  381. File testFile;
  382. for (String path : Identification.NATIVE_SEARCH_PATHS) {
  383. testFile = new File(new File(root, path), mappedName);
  384. if (testFile.exists()) {
  385. return testFile.getAbsolutePath();
  386. }
  387. }
  388. return null;
  389. }
  390. public URI getLocation() {
  391. return root.toURI();
  392. }
  393. /**
  394. * Get the filesystem root of the resource loader.
  395. *
  396. * @return the filesystem root of the resource loader
  397. */
  398. public File getRoot() {
  399. return root;
  400. }
  401. /**
  402. * Get the detected architecture name for this platform.
  403. *
  404. * @return the architecture name
  405. */
  406. public static String getArchName() {
  407. return Identification.ARCH_NAME;
  408. }
  409. }