PageRenderTime 303ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/alesj/jboss-modules
Java | 290 lines | 239 code | 16 blank | 35 comment | 83 complexity | 7ce0e46dee6e413aafee328862bfe904 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, 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.BufferedReader;
  24. import java.io.BufferedWriter;
  25. import java.io.Closeable;
  26. import java.io.File;
  27. import java.io.FileInputStream;
  28. import java.io.FileOutputStream;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.io.InputStreamReader;
  32. import java.io.OutputStreamWriter;
  33. import java.net.MalformedURLException;
  34. import java.net.URL;
  35. import java.security.AccessController;
  36. import java.security.CodeSigner;
  37. import java.security.CodeSource;
  38. import java.util.ArrayList;
  39. import java.util.Collection;
  40. import java.util.List;
  41. import java.util.jar.Attributes;
  42. import java.util.jar.Manifest;
  43. /**
  44. *
  45. * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
  46. */
  47. final class FileResourceLoader extends AbstractResourceLoader {
  48. private static final String ARCH_NAME;
  49. static {
  50. final PropertyReadAction osNameReadAction = new PropertyReadAction("os.name");
  51. final PropertyReadAction osArchReadAction = new PropertyReadAction("os.arch");
  52. final SecurityManager sm = System.getSecurityManager();
  53. final String sysName;
  54. final String sysArch;
  55. if (sm != null) {
  56. sysName = AccessController.doPrivileged(osNameReadAction).toUpperCase();
  57. sysArch = AccessController.doPrivileged(osArchReadAction).toUpperCase();
  58. } else {
  59. sysName = osNameReadAction.run().toUpperCase();
  60. sysArch = osArchReadAction.run().toUpperCase();
  61. }
  62. final String realName;
  63. final String realArch;
  64. if (sysName.startsWith("LINUX")) {
  65. realName = "linux";
  66. } else if (sysName.startsWith("MAC OS")) {
  67. realName = "macosx";
  68. } else if (sysName.startsWith("WINDOWS")) {
  69. realName = "win";
  70. } else if (sysName.startsWith("OS/2")) {
  71. realName = "os2";
  72. } else if (sysName.startsWith("SOLARIS") || sysName.startsWith("SUNOS")) {
  73. realName = "solaris";
  74. } else if (sysName.startsWith("MPE/IX")) {
  75. realName = "mpeix";
  76. } else if (sysName.startsWith("HP-UX")) {
  77. realName = "hpux";
  78. } else if (sysName.startsWith("AIX")) {
  79. realName = "aix";
  80. } else if (sysName.startsWith("OS/390")) {
  81. realName = "os390";
  82. } else if (sysName.startsWith("FREEBSD")) {
  83. realName = "freebsd";
  84. } else if (sysName.startsWith("IRIX")) {
  85. realName = "irix";
  86. } else if (sysName.startsWith("DIGITAL UNIX")) {
  87. realName = "digitalunix";
  88. } else if (sysName.startsWith("OSF1")) {
  89. realName = "osf1";
  90. } else if (sysName.startsWith("OPENVMS")) {
  91. realName = "openvms";
  92. } else {
  93. realName = "unknown";
  94. }
  95. if (sysArch.startsWith("SPARCV9") || sysArch.startsWith("SPARC64")) {
  96. realArch = "sparcv9";
  97. } else if (sysArch.startsWith("SPARC")) {
  98. realArch = "sparc";
  99. } else if (sysArch.startsWith("X86_64") || sysArch.startsWith("AMD64")) {
  100. realArch = "x86_64";
  101. } else if (sysArch.startsWith("I386") || sysArch.startsWith("I586") || sysArch.startsWith("I686") || sysArch.startsWith("X86")) {
  102. realArch = "i686";
  103. } else if (sysArch.startsWith("PPC64")) {
  104. realArch = "ppc64";
  105. } else if (sysArch.startsWith("PPC") || sysArch.startsWith("POWER")) {
  106. realArch = "ppc";
  107. } else if (sysArch.startsWith("ARM")) {
  108. realArch = "arm";
  109. } else if (sysArch.startsWith("PA_RISC") || sysArch.startsWith("PA-RISC")) {
  110. realArch = "parisc";
  111. } else if (sysArch.startsWith("ALPHA")) {
  112. realArch = "alpha";
  113. } else if (sysArch.startsWith("MIPS")) {
  114. realArch = "mips";
  115. } else {
  116. realArch = "unknown";
  117. }
  118. ARCH_NAME = realName + "-" + realArch;
  119. }
  120. private final String rootName;
  121. private final File root;
  122. private final Manifest manifest;
  123. private final CodeSource codeSource;
  124. FileResourceLoader(final String rootName, final File root) {
  125. if (root == null) {
  126. throw new IllegalArgumentException("root is null");
  127. }
  128. if (rootName == null) {
  129. throw new IllegalArgumentException("rootName is null");
  130. }
  131. this.rootName = rootName;
  132. this.root = root;
  133. final File manifestFile = new File(root, "META-INF" + File.separatorChar + "MANIFEST.MF");
  134. manifest = readManifestFile(manifestFile);
  135. final URL rootUrl;
  136. try {
  137. rootUrl = root.getAbsoluteFile().toURI().toURL();
  138. } catch (MalformedURLException e) {
  139. throw new IllegalArgumentException("Invalid root file specified", e);
  140. }
  141. codeSource = new CodeSource(rootUrl, (CodeSigner[])null);
  142. }
  143. private static Manifest readManifestFile(final File manifestFile) {
  144. try {
  145. return new Manifest(new FileInputStream(manifestFile));
  146. } catch (IOException e) {
  147. return null;
  148. }
  149. }
  150. public String getRootName() {
  151. return rootName;
  152. }
  153. public ClassSpec getClassSpec(final String fileName) throws IOException {
  154. final File file = new File(root, fileName);
  155. if (! file.exists()) {
  156. return null;
  157. }
  158. final long size = file.length();
  159. final ClassSpec spec = new ClassSpec();
  160. spec.setCodeSource(codeSource);
  161. final InputStream is = new FileInputStream(file);
  162. try {
  163. if (size <= (long) Integer.MAX_VALUE) {
  164. final int castSize = (int) size;
  165. byte[] bytes = new byte[castSize];
  166. int a = 0, res;
  167. while ((res = is.read(bytes, a, castSize - a)) > 0) {
  168. a += res;
  169. }
  170. // done
  171. is.close();
  172. spec.setBytes(bytes);
  173. return spec;
  174. } else {
  175. throw new IOException("Resource is too large to be a valid class file");
  176. }
  177. } finally {
  178. safeClose(is);
  179. }
  180. }
  181. private static void safeClose(final Closeable closeable) {
  182. if (closeable != null) try {
  183. closeable.close();
  184. } catch (IOException e) {
  185. // ignore
  186. }
  187. }
  188. public PackageSpec getPackageSpec(final String name) throws IOException {
  189. return getPackageSpec(name, this.manifest, root.toURI().toURL());
  190. }
  191. private static String getDefinedAttribute(Attributes.Name name, Attributes entryAttribute, Attributes mainAttribute) {
  192. final String value = entryAttribute == null ? null : entryAttribute.getValue(name);
  193. return value == null ? mainAttribute == null ? null : mainAttribute.getValue(name) : value;
  194. }
  195. public String getLibrary(final String name) {
  196. final File file = new File(root, ARCH_NAME + File.separatorChar + name);
  197. return file.exists() ? file.getAbsolutePath() : null;
  198. }
  199. public Resource getResource(final String name) {
  200. try {
  201. final File file = new File(root, PathUtils.canonicalize(name));
  202. if (! file.exists()) {
  203. return null;
  204. }
  205. return new FileEntryResource(name, file, file.toURI().toURL());
  206. } catch (MalformedURLException e) {
  207. // must be invalid...? (todo: check this out)
  208. return null;
  209. }
  210. }
  211. public Collection<String> getPaths() {
  212. final List<String> index = new ArrayList<String>();
  213. // First check for an index file
  214. final File indexFile = new File(root.getPath() + ".index");
  215. if (indexFile.exists()) {
  216. try {
  217. final BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(indexFile)));
  218. try {
  219. String s;
  220. while ((s = r.readLine()) != null) {
  221. index.add(s.trim());
  222. }
  223. return index;
  224. } finally {
  225. // if exception is thrown, undo index creation
  226. r.close();
  227. }
  228. } catch (IOException e) {
  229. index.clear();
  230. }
  231. }
  232. // Manually build index, starting with the root path
  233. index.add("");
  234. buildIndex(index, root, "");
  235. if (ResourceLoaders.WRITE_INDEXES) {
  236. // Now try to write it
  237. boolean ok = false;
  238. try {
  239. final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(indexFile)));
  240. try {
  241. for (String name : index) {
  242. writer.write(name);
  243. writer.write('\n');
  244. }
  245. writer.close();
  246. ok = true;
  247. } finally {
  248. try {
  249. writer.close();
  250. } catch (IOException e) {
  251. // ignored
  252. }
  253. }
  254. } catch (IOException e) {
  255. // failed, ignore
  256. } finally {
  257. if (! ok) {
  258. // well, we tried...
  259. indexFile.delete();
  260. }
  261. }
  262. }
  263. return index;
  264. }
  265. private void buildIndex(final List<String> index, final File root, final String pathBase) {
  266. File[] files = root.listFiles();
  267. if (files != null) for (File file : files) {
  268. if (file.isDirectory()) {
  269. index.add(pathBase + file.getName());
  270. buildIndex(index, file, pathBase + file.getName() + "/");
  271. }
  272. }
  273. }
  274. }