PageRenderTime 73ms CodeModel.GetById 52ms RepoModel.GetById 0ms app.codeStats 0ms

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

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