PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

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

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