/wonderland/web/modules/src/java/org/jdesktop/wonderland/modules/archive/ArchiveModule.java

https://github.com/jehc/MondocosmOS · Java · 205 lines · 109 code · 20 blank · 76 comment · 18 complexity · b1aaee01d7ca1a598cd28dcfe66ccd6b MD5 · raw file

  1. /**
  2. * Project Wonderland
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
  5. *
  6. * Redistributions in source code form must reproduce the above
  7. * copyright and this condition.
  8. *
  9. * The contents of this file are subject to the GNU General Public
  10. * License, Version 2 (the "License"); you may not use this file
  11. * except in compliance with the License. A copy of the License is
  12. * available at http://www.opensource.org/licenses/gpl-license.php.
  13. *
  14. * Sun designates this particular file as subject to the "Classpath"
  15. * exception as provided by Sun in the License file that accompanied
  16. * this code.
  17. */
  18. package org.jdesktop.wonderland.modules.archive;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import java.util.Map;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import org.jdesktop.wonderland.modules.Module;
  29. import org.jdesktop.wonderland.common.modules.ModuleInfo;
  30. import org.jdesktop.wonderland.modules.ModulePart;
  31. import org.jdesktop.wonderland.common.modules.ModuleRepository;
  32. import org.jdesktop.wonderland.common.modules.ModuleRequires;
  33. import org.jdesktop.wonderland.utils.ArchiveManifest;
  34. import org.jdesktop.wonderland.utils.RunUtil;
  35. /**
  36. * The ArchiveModule class extends the Module abstract base class and represents
  37. * all modules that are contained within either a JAR or ZIP archive.
  38. * <p>
  39. * @author Jordan Slott <jslott@dev.java.net>
  40. */
  41. public class ArchiveModule extends Module {
  42. private static Logger logger = Logger.getLogger(ArchiveModule.class.getName());
  43. /* The manifest of the archive */
  44. private ArchiveManifest manifest = null;
  45. /**
  46. * Constructor, takes File of the module jar. Throws IOException upon
  47. * general I/O error reading the archive module.
  48. *
  49. * @param file The archive module File object
  50. * @throw IOException Upon general I/O exception readin the module
  51. */
  52. public ArchiveModule(File file) throws IOException {
  53. super();
  54. this.setFile(file);
  55. this.manifest = new ArchiveManifest(file);
  56. /*
  57. * Fetch the module info, this is pretty bad if module.xml doesn't exist
  58. */
  59. ModuleInfo info = this.fetchModuleInfo();
  60. if (info == null) {
  61. info = new ModuleInfo();
  62. }
  63. this.setInfo(info);
  64. /*
  65. * Fetch the module dependencies, this isn't terrible if it doesn't exist
  66. */
  67. ModuleRequires requires = this.fetchModuleRequires();
  68. if (requires == null) {
  69. requires = new ModuleRequires();
  70. }
  71. this.setRequires(requires);
  72. /*
  73. * Fetch the module asset servers, this isn't terrible if it doesn't exist
  74. */
  75. ModuleRepository repository = this.fetchModuleRepository();
  76. if (repository == null) {
  77. repository = new ModuleRepository();
  78. }
  79. this.setRepository(repository);
  80. /*
  81. * Fetch the module parts, at least this should return an empty map
  82. */
  83. this.setParts(this.fetchModuleParts());
  84. }
  85. /**
  86. * Reads the module info from the module.
  87. */
  88. private ModuleInfo fetchModuleInfo() {
  89. InputStreamReader reader = null;
  90. try {
  91. /* Fetch the input stream, parse and return */
  92. InputStream is = manifest.getEntryInputStream(Module.MODULE_INFO);
  93. if (is == null) {
  94. /* This is pretty bad -- if this doesn't exist, then the module is invalid */
  95. logger.log(Level.WARNING, "[MODULE] Invalid Module " + this.getFile());
  96. return null;
  97. }
  98. /* Read in the file and parse */
  99. reader = new InputStreamReader(is);
  100. return ModuleInfo.decode(reader);
  101. } catch (java.lang.Exception excp) {
  102. /* This is pretty bad -- if this doesn't exist, then the module is invalid */
  103. logger.log(Level.WARNING, "[MODULE] Invalid Module " + this.getFile(), excp);
  104. return null;
  105. } finally {
  106. RunUtil.close(reader);
  107. }
  108. }
  109. /**
  110. * Reads the dependency info from the module.
  111. */
  112. private ModuleRequires fetchModuleRequires() {
  113. InputStreamReader reader = null;
  114. try {
  115. /* Fetch the input stream, parse and return */
  116. InputStream is = manifest.getEntryInputStream(Module.MODULE_REQUIRES);
  117. if (is == null) {
  118. /* This is not too bad if it does not exist */
  119. logger.log(Level.INFO, "[MODULE] No requires.xml for Module " + this.getFile());
  120. return null;
  121. }
  122. /* Read in the file and parse */
  123. reader = new InputStreamReader(is);
  124. return ModuleRequires.decode(reader);
  125. } catch (java.lang.Exception excp) {
  126. /* This is not too bad if it does not exist */
  127. logger.log(Level.INFO, "[MODULE] No requires.xml for Module " + this.getFile(), excp);
  128. return null;
  129. } finally {
  130. RunUtil.close(reader);
  131. }
  132. }
  133. /**
  134. * Reads the asset server info from the module.
  135. */
  136. public ModuleRepository fetchModuleRepository() {
  137. InputStreamReader reader = null;
  138. try {
  139. /* Fetch the input stream, parse and return */
  140. InputStream is = manifest.getEntryInputStream(Module.MODULE_REPOSITORY);
  141. if (is == null) {
  142. /* This is not too bad if it does not exist */
  143. logger.log(Level.INFO, "[MODULE] No repository.xml for Module " + this.getFile());
  144. return null;
  145. }
  146. /* Read in the file and parse */
  147. reader = new InputStreamReader(is);
  148. return ModuleRepository.decode(reader);
  149. } catch (java.lang.Exception excp) {
  150. /* This is not too bad if it does not exist */
  151. logger.log(Level.INFO, "[MODULE] No repository.xml for Module " + this.getFile(), excp);
  152. return null;
  153. } finally {
  154. RunUtil.close(reader);
  155. }
  156. }
  157. /**
  158. * Reads the module parts.
  159. */
  160. private Map<String, ModulePart> fetchModuleParts() {
  161. /* Create a map to store the entries, get the entries */
  162. Map<String, ModulePart> map = new HashMap<String, ModulePart>();
  163. Iterator<String> it = manifest.getEntries().listIterator();
  164. /*
  165. * Loop through each entry and see if its name begins with "art/"
  166. * does not end with "/". If so, take the name, minus the beginning
  167. * "art/" part.
  168. */
  169. while (it.hasNext() == true) {
  170. String name = it.next();
  171. /* See if the name ends with a "/", then it is a directory */
  172. if (name.endsWith("/") == true) {
  173. /* Look at the part type, ignore META-INF */
  174. String partType = name.substring(0, name.length() - 1);
  175. if (partType.equals("META-INF") == true) {
  176. continue;
  177. }
  178. /* Just use the jar file */
  179. ModulePart part = new ModulePart(partType, this.getFile());
  180. map.put(partType, part);
  181. }
  182. }
  183. return map;
  184. }
  185. }