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

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

https://github.com/n1hility/jboss-modules
Java | 160 lines | 104 code | 9 blank | 47 comment | 72 complexity | 099eb550b8f20ac0aabf6c500579c4c1 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.modules;
  23. import java.io.File;
  24. import java.security.AccessController;
  25. import java.util.Locale;
  26. /**
  27. * A base class for resource loaders which can load native libraries.
  28. *
  29. * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
  30. */
  31. public class NativeLibraryResourceLoader extends AbstractResourceLoader {
  32. static {
  33. final PropertyReadAction osNameReadAction = new PropertyReadAction("os.name");
  34. final PropertyReadAction osArchReadAction = new PropertyReadAction("os.arch");
  35. final SecurityManager sm = System.getSecurityManager();
  36. final String sysName;
  37. final String sysArch;
  38. if (sm != null) {
  39. sysName = AccessController.doPrivileged(osNameReadAction).toUpperCase(Locale.US);
  40. sysArch = AccessController.doPrivileged(osArchReadAction).toUpperCase(Locale.US);
  41. } else {
  42. sysName = osNameReadAction.run().toUpperCase(Locale.US);
  43. sysArch = osArchReadAction.run().toUpperCase(Locale.US);
  44. }
  45. final String realName;
  46. final String realArch;
  47. if (sysName.startsWith("LINUX")) {
  48. realName = "linux";
  49. } else if (sysName.startsWith("MAC OS")) {
  50. realName = "macosx";
  51. } else if (sysName.startsWith("WINDOWS")) {
  52. realName = "win";
  53. } else if (sysName.startsWith("OS/2")) {
  54. realName = "os2";
  55. } else if (sysName.startsWith("SOLARIS") || sysName.startsWith("SUNOS")) {
  56. realName = "solaris";
  57. } else if (sysName.startsWith("MPE/IX")) {
  58. realName = "mpeix";
  59. } else if (sysName.startsWith("HP-UX")) {
  60. realName = "hpux";
  61. } else if (sysName.startsWith("AIX")) {
  62. realName = "aix";
  63. } else if (sysName.startsWith("OS/390")) {
  64. realName = "os390";
  65. } else if (sysName.startsWith("FREEBSD")) {
  66. realName = "freebsd";
  67. } else if (sysName.startsWith("OPENBSD")) {
  68. realName = "openbsd";
  69. } else if (sysName.startsWith("NETBSD")) {
  70. realName = "netbsd";
  71. } else if (sysName.startsWith("IRIX")) {
  72. realName = "irix";
  73. } else if (sysName.startsWith("DIGITAL UNIX")) {
  74. realName = "digitalunix";
  75. } else if (sysName.startsWith("OSF1")) {
  76. realName = "osf1";
  77. } else if (sysName.startsWith("OPENVMS")) {
  78. realName = "openvms";
  79. } else {
  80. realName = "unknown";
  81. }
  82. if (sysArch.startsWith("SPARCV9") || sysArch.startsWith("SPARC64")) {
  83. realArch = "sparcv9";
  84. } else if (sysArch.startsWith("SPARC")) {
  85. realArch = "sparc";
  86. } else if (sysArch.startsWith("X86_64") || sysArch.startsWith("AMD64")) {
  87. realArch = "x86_64";
  88. } else if (sysArch.startsWith("I386") || sysArch.startsWith("I586") || sysArch.startsWith("I686") || sysArch.startsWith("X86")) {
  89. realArch = "i686";
  90. } else if (sysArch.startsWith("X32")) {
  91. realArch = "x32";
  92. } else if (sysArch.startsWith("PPC64")) {
  93. realArch = "ppc64";
  94. } else if (sysArch.startsWith("PPC") || sysArch.startsWith("POWER")) {
  95. realArch = "ppc";
  96. } else if (sysArch.startsWith("ARM")) {
  97. realArch = "arm";
  98. } else if (sysArch.startsWith("AARCH64") || sysArch.startsWith("ARM64")) {
  99. realArch = "aarch64";
  100. } else if (sysArch.startsWith("PA_RISC2.0W")) {
  101. realArch = "parisc64";
  102. } else if (sysArch.startsWith("PA_RISC") || sysArch.startsWith("PA-RISC")) {
  103. realArch = "parisc";
  104. } else if (sysArch.startsWith("IA64")) {
  105. // HP-UX reports IA64W for 64-bit Itanium and IA64N when running
  106. // in 32-bit mode.
  107. realArch = sysArch.toLowerCase(Locale.US);
  108. } else if (sysArch.startsWith("ALPHA")) {
  109. realArch = "alpha";
  110. } else if (sysArch.startsWith("MIPS")) {
  111. realArch = "mips";
  112. } else {
  113. realArch = "unknown";
  114. }
  115. ARCH_NAME = realName + "-" + realArch;
  116. }
  117. private static final String ARCH_NAME;
  118. /**
  119. * The filesystem root of the resource loader.
  120. */
  121. private final File root;
  122. /**
  123. * Construct a new instance.
  124. *
  125. * @param root the filesystem root of the resource loader
  126. */
  127. public NativeLibraryResourceLoader(final File root) {
  128. this.root = root;
  129. }
  130. /** {@inheritDoc} */
  131. public String getLibrary(final String name) {
  132. final File file = new File(root, ARCH_NAME + File.separatorChar + System.mapLibraryName(name));
  133. return file.exists() ? file.getAbsolutePath() : null;
  134. }
  135. /**
  136. * Get the filesystem root of the resource loader.
  137. *
  138. * @return the filesystem root of the resource loader
  139. */
  140. public File getRoot() {
  141. return root;
  142. }
  143. /**
  144. * Get the detected architecture name for this platform.
  145. *
  146. * @return the architecture name
  147. */
  148. public static String getArchName() {
  149. return ARCH_NAME;
  150. }
  151. }