PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

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