PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/emuckenhuber/jboss-modules
Java | 156 lines | 100 code | 9 blank | 47 comment | 67 complexity | e1aec29f02fa2eb17dbbb19014df48b0 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("PPC64")) {
  91. realArch = "ppc64";
  92. } else if (sysArch.startsWith("PPC") || sysArch.startsWith("POWER")) {
  93. realArch = "ppc";
  94. } else if (sysArch.startsWith("ARM")) {
  95. realArch = "arm";
  96. } else if (sysArch.startsWith("PA_RISC2.0W")) {
  97. realArch = "parisc64";
  98. } else if (sysArch.startsWith("PA_RISC") || sysArch.startsWith("PA-RISC")) {
  99. realArch = "parisc";
  100. } else if (sysArch.startsWith("IA64")) {
  101. // HP-UX reports IA64W for 64-bit Itanium and IA64N when running
  102. // in 32-bit mode.
  103. realArch = sysArch.toLowerCase(Locale.US);
  104. } else if (sysArch.startsWith("ALPHA")) {
  105. realArch = "alpha";
  106. } else if (sysArch.startsWith("MIPS")) {
  107. realArch = "mips";
  108. } else {
  109. realArch = "unknown";
  110. }
  111. ARCH_NAME = realName + "-" + realArch;
  112. }
  113. private static final String ARCH_NAME;
  114. /**
  115. * The filesystem root of the resource loader.
  116. */
  117. private final File root;
  118. /**
  119. * Construct a new instance.
  120. *
  121. * @param root the filesystem root of the resource loader
  122. */
  123. public NativeLibraryResourceLoader(final File root) {
  124. this.root = root;
  125. }
  126. /** {@inheritDoc} */
  127. public String getLibrary(final String name) {
  128. final File file = new File(root, ARCH_NAME + File.separatorChar + System.mapLibraryName(name));
  129. return file.exists() ? file.getAbsolutePath() : null;
  130. }
  131. /**
  132. * Get the filesystem root of the resource loader.
  133. *
  134. * @return the filesystem root of the resource loader
  135. */
  136. public File getRoot() {
  137. return root;
  138. }
  139. /**
  140. * Get the detected architecture name for this platform.
  141. *
  142. * @return the architecture name
  143. */
  144. public static String getArchName() {
  145. return ARCH_NAME;
  146. }
  147. }