1 16 package com.google.gwt.dev.cfg; 17 18 import com.google.gwt.core.ext.TreeLogger; 19 import com.google.gwt.core.ext.UnableToCompleteException; 20 import com.google.gwt.dev.util.Util; 21 import com.google.gwt.dev.util.xml.ReflectiveParser; 22 import com.google.gwt.util.tools.Utility; 23 24 import java.io.File ; 25 import java.io.Reader ; 26 import java.net.URI ; 27 import java.net.URISyntaxException ; 28 import java.net.URL ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 38 public class ModuleDefLoader { 39 40 private static boolean enableCachingModules = true; 43 private static final Set forceInherits = new HashSet (); 44 private static final Map loadedModules = new HashMap (); 45 46 55 public static void forceInherit(String moduleName) { 56 forceInherits.add(moduleName); 57 } 58 59 65 public static boolean getEnableCachingModules() { 66 return enableCachingModules; 67 } 68 69 77 public static ModuleDef loadFromClassPath(TreeLogger logger, String moduleName) 78 throws UnableToCompleteException { 79 ModuleDef moduleDef = (ModuleDef) loadedModules.get(moduleName); 80 if (moduleDef == null || moduleDef.isGwtXmlFileStale()) { 81 moduleDef = new ModuleDefLoader().load(logger, moduleName); 82 if (enableCachingModules) { 83 loadedModules.put(moduleName, moduleDef); 84 } 85 } else { 86 moduleDef.refresh(logger); 87 } 88 return moduleDef; 89 } 90 91 99 public static void setEnableCachingModules(boolean enableCachingModules) { 100 ModuleDefLoader.enableCachingModules = enableCachingModules; 101 } 102 103 private final Set alreadyLoadedModules = new HashSet (); 104 105 private final ClassLoader classLoader; 106 107 private ModuleDefLoader() { 108 this.classLoader = Thread.currentThread().getContextClassLoader(); 109 } 110 111 119 void nestedLoad(TreeLogger logger, String moduleName, ModuleDef moduleDef) 120 throws UnableToCompleteException { 121 122 if (alreadyLoadedModules.contains(moduleName)) { 123 logger.log(TreeLogger.TRACE, "Module '" + moduleName 124 + "' has already been loaded and will be skipped", null); 125 return; 126 } else { 127 alreadyLoadedModules.add(moduleName); 128 } 129 130 String slashedModuleName = moduleName.replace('.', '/'); 133 String resName = slashedModuleName + ".gwt.xml"; 134 URL moduleURL = classLoader.getResource(resName); 135 136 if (moduleURL != null) { 137 String externalForm = moduleURL.toExternalForm(); 138 logger.log(TreeLogger.TRACE, "Module location: " + externalForm, null); 139 try { 140 if ((!(externalForm.startsWith("jar:file"))) 141 && (!(externalForm.startsWith("zip:file"))) 142 && (!(externalForm.startsWith("http://"))) 143 && (!(externalForm.startsWith("ftp://")))) { 144 File gwtXmlFile = new File (new URI (externalForm)); 145 moduleDef.addGwtXmlFile(gwtXmlFile); 146 } 147 } catch (URISyntaxException e) { 148 logger.log(TreeLogger.ERROR, "Error parsing URI", e); 149 throw new UnableToCompleteException(); 150 } 151 } 152 if (moduleURL == null) { 153 String msg = "Unable to find '" 154 + resName 155 + "' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?"; 156 logger.log(TreeLogger.ERROR, msg, null); 157 throw new UnableToCompleteException(); 158 } 159 160 String moduleDir = ""; 163 int i = slashedModuleName.lastIndexOf('/'); 164 if (i != -1) { 165 moduleDir = slashedModuleName.substring(0, i) + "/"; 166 } 167 168 Reader r = null; 171 try { 172 r = Util.createReader(logger, moduleURL); 173 ModuleDefSchema schema = new ModuleDefSchema(logger, this, moduleURL, 174 moduleDir, moduleDef); 175 ReflectiveParser.parse(logger, schema, r); 176 } finally { 177 Utility.close(r); 178 } 179 } 180 181 190 private ModuleDef load(TreeLogger logger, String moduleName) 191 throws UnableToCompleteException { 192 logger = logger.branch(TreeLogger.TRACE, "Loading module '" + moduleName 193 + "'", null); 194 195 if (!ModuleDef.isValidModuleName(moduleName)) { 196 logger.log(TreeLogger.ERROR, "Invalid module name: '" + moduleName + "'", 197 null); 198 throw new UnableToCompleteException(); 199 } 200 201 ModuleDef moduleDef = new ModuleDef(moduleName); 202 for (Iterator it = forceInherits.iterator(); it.hasNext();) { 203 String forceInherit = (String ) it.next(); 204 TreeLogger branch = logger.branch(TreeLogger.TRACE, 205 "Loading forceably inherited module '" + forceInherit + "'", null); 206 nestedLoad(branch, forceInherit, moduleDef); 207 } 208 nestedLoad(logger, moduleName, moduleDef); 209 210 moduleDef.normalize(logger); 213 214 return moduleDef; 215 } 216 } 217 | Popular Tags |