PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/baileyje/jboss-modules
Java | 301 lines | 249 code | 17 blank | 35 comment | 83 complexity | 134c040922f2e31f5e4553e3f40853c3 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 ModuleIdentifier moduleIdentifier;
  113. private final String rootName;
  114. private final File root;
  115. private final Manifest manifest;
  116. private final PathFilter exportFilter;
  117. FileResourceLoader(final ModuleIdentifier moduleIdentifier, final File root, final String rootName, final PathFilter exportFilter) {
  118. if (moduleIdentifier == null) {
  119. throw new IllegalArgumentException("moduleIdentifier is null");
  120. }
  121. if (root == null) {
  122. throw new IllegalArgumentException("root is null");
  123. }
  124. if (rootName == null) {
  125. throw new IllegalArgumentException("rootName is null");
  126. }
  127. if (exportFilter == null) {
  128. throw new IllegalArgumentException("exportFilter is null");
  129. }
  130. this.moduleIdentifier = moduleIdentifier;
  131. this.rootName = rootName;
  132. this.root = root;
  133. this.exportFilter = exportFilter;
  134. final File manifestFile = new File(root, "META-INF" + File.separator + "MANIFEST.MF");
  135. manifest = readManifestFile(manifestFile);
  136. }
  137. private static Manifest readManifestFile(final File manifestFile) {
  138. try {
  139. return new Manifest(new FileInputStream(manifestFile));
  140. } catch (IOException e) {
  141. return null;
  142. }
  143. }
  144. public PathFilter getExportFilter() {
  145. return exportFilter;
  146. }
  147. public String getRootName() {
  148. return rootName;
  149. }
  150. public ClassSpec getClassSpec(final String name) throws IOException {
  151. final String fileName = Module.fileNameOfClass(name);
  152. final File file = new File(root, fileName);
  153. if (! file.exists()) {
  154. return null;
  155. }
  156. final long size = file.length();
  157. final ClassSpec spec = new ClassSpec();
  158. final InputStream is = new FileInputStream(file);
  159. try {
  160. if (size <= (long) Integer.MAX_VALUE) {
  161. final int castSize = (int) size;
  162. byte[] bytes = new byte[castSize];
  163. int a = 0, res;
  164. while ((res = is.read(bytes, a, castSize - a)) > 0) {
  165. a += res;
  166. }
  167. // done
  168. is.close();
  169. spec.setBytes(bytes);
  170. return spec;
  171. } else {
  172. throw new IOException("Resource is too large to be a valid class file");
  173. }
  174. } finally {
  175. safeClose(is);
  176. }
  177. }
  178. private static void safeClose(final Closeable closeable) {
  179. if (closeable != null) try {
  180. closeable.close();
  181. } catch (IOException e) {
  182. // ignore
  183. }
  184. }
  185. public PackageSpec getPackageSpec(final String name) throws IOException {
  186. final PackageSpec spec = new PackageSpec();
  187. final Manifest manifest = this.manifest;
  188. if (manifest == null) {
  189. return spec;
  190. }
  191. final Attributes mainAttribute = manifest.getAttributes(name);
  192. final Attributes entryAttribute = manifest.getAttributes(name);
  193. spec.setSpecTitle(getDefinedAttribute(Attributes.Name.SPECIFICATION_TITLE, entryAttribute, mainAttribute));
  194. spec.setSpecVersion(getDefinedAttribute(Attributes.Name.SPECIFICATION_VERSION, entryAttribute, mainAttribute));
  195. spec.setSpecVendor(getDefinedAttribute(Attributes.Name.SPECIFICATION_VENDOR, entryAttribute, mainAttribute));
  196. spec.setImplTitle(getDefinedAttribute(Attributes.Name.IMPLEMENTATION_TITLE, entryAttribute, mainAttribute));
  197. spec.setImplVersion(getDefinedAttribute(Attributes.Name.IMPLEMENTATION_VERSION, entryAttribute, mainAttribute));
  198. spec.setImplVendor(getDefinedAttribute(Attributes.Name.IMPLEMENTATION_VENDOR, entryAttribute, mainAttribute));
  199. if (Boolean.parseBoolean(getDefinedAttribute(Attributes.Name.SEALED, entryAttribute, mainAttribute))) {
  200. spec.setSealBase(moduleIdentifier.toURL(rootName));
  201. }
  202. return spec;
  203. }
  204. private static String getDefinedAttribute(Attributes.Name name, Attributes entryAttribute, Attributes mainAttribute) {
  205. final String value = entryAttribute == null ? null : entryAttribute.getValue(name);
  206. return value == null ? mainAttribute == null ? null : mainAttribute.getValue(name) : value;
  207. }
  208. public String getLibrary(final String name) {
  209. final File file = new File(root, ARCH_NAME + "/" + name);
  210. return file.exists() ? file.getAbsolutePath() : null;
  211. }
  212. public Resource getResource(final String name) {
  213. try {
  214. final File file = new File(root, name);
  215. if (! file.exists()) {
  216. return null;
  217. }
  218. return new FileEntryResource(name, file, moduleIdentifier.toURL(rootName, name));
  219. } catch (MalformedURLException e) {
  220. // must be invalid...? (todo: check this out)
  221. return null;
  222. }
  223. }
  224. public Collection<String> getPaths() {
  225. final List<String> index = new ArrayList<String>();
  226. // First check for an index file
  227. final File indexFile = new File(root.getPath() + ".index");
  228. if (indexFile.exists()) {
  229. try {
  230. final BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(indexFile)));
  231. try {
  232. String s;
  233. while ((s = r.readLine()) != null) {
  234. index.add(s.trim());
  235. }
  236. return index;
  237. } finally {
  238. // if exception is thrown, undo index creation
  239. r.close();
  240. }
  241. } catch (IOException e) {
  242. index.clear();
  243. }
  244. }
  245. // Manually build index, starting with the root path
  246. index.add("");
  247. buildIndex(index, root, "");
  248. // Now try to write it
  249. boolean ok = false;
  250. try {
  251. final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(indexFile)));
  252. try {
  253. for (String name : index) {
  254. writer.write(name);
  255. writer.write('\n');
  256. }
  257. writer.close();
  258. ok = true;
  259. } finally {
  260. try {
  261. writer.close();
  262. } catch (IOException e) {
  263. // ignored
  264. }
  265. }
  266. } catch (IOException e) {
  267. // failed, ignore
  268. } finally {
  269. if (! ok) {
  270. // well, we tried...
  271. indexFile.delete();
  272. }
  273. }
  274. return index;
  275. }
  276. private void buildIndex(final List<String> index, final File root, final String pathBase) {
  277. for (File file : root.listFiles()) {
  278. if (file.isDirectory()) {
  279. index.add(pathBase + file.getName());
  280. buildIndex(index, file, pathBase + file.getName() + "/");
  281. }
  282. }
  283. }
  284. }