PageRenderTime 69ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

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

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