1 19 20 package org.netbeans.core.startup; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.Enumeration ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Set ; 37 import java.util.jar.Attributes ; 38 import java.util.jar.Manifest ; 39 import java.util.logging.Level ; 40 import org.netbeans.DuplicateException; 41 import org.netbeans.Events; 42 import org.netbeans.Module; 43 import org.netbeans.ModuleManager; 44 import org.netbeans.Util; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileSystem; 47 import org.openide.util.Exceptions; 48 49 57 public final class ModuleSystem { 58 private final ModuleManager mgr; 59 private final NbInstaller installer; 60 private final ModuleList list; 61 private final Events ev; 62 63 68 public ModuleSystem(FileSystem systemFileSystem) throws IOException { 69 if (Boolean.getBoolean("org.netbeans.core.startup.ModuleSystem.CULPRIT")) Thread.dumpStack(); ev = Boolean.getBoolean("netbeans.modules.quiet") ? (Events)new QuietEvents() : new NbEvents(); 71 installer = new NbInstaller(ev); 72 mgr = new ModuleManager(installer, ev); 73 PropertyChangeListener l = new PropertyChangeListener () { 74 public void propertyChange(PropertyChangeEvent ev) { 75 if (ModuleManager.PROP_CLASS_LOADER.equals(ev.getPropertyName())) { 76 org.netbeans.core.startup.MainLookup.systemClassLoaderChanged(mgr.getClassLoader()); 77 } 78 } 79 }; 80 mgr.addPropertyChangeListener(l); 81 82 org.netbeans.core.startup.MainLookup.systemClassLoaderChanged(installer.getClass ().getClassLoader ()); 84 org.netbeans.core.startup.MainLookup.moduleLookupReady(mgr.getModuleLookup()); 86 if (systemFileSystem.isReadOnly()) { 87 list = null; 88 } else { 89 FileObject root = systemFileSystem.getRoot(); 90 FileObject modulesFolder = root.getFileObject("Modules"); if (modulesFolder == null) { 92 modulesFolder = root.createFolder("Modules"); } 94 list = new ModuleList(mgr, modulesFolder, ev); 95 installer.registerList(list); 96 installer.registerManager(mgr); 97 } 98 ev.log(Events.CREATED_MODULE_SYSTEM); 99 } 100 101 105 public ModuleManager getManager() { 106 return mgr; 107 } 108 109 111 public Events getEvents() { 112 return ev; 113 } 114 115 121 public List <File > getModuleJars () { 122 mgr.mutexPrivileged().enterReadAccess(); 123 try { 124 List <File > l = new ArrayList <File >(); 125 for (Module m: mgr.getEnabledModules()) { 126 l.addAll(m.getAllJars()); 127 } 128 return l; 129 } finally { 130 mgr.mutexPrivileged().exitReadAccess(); 131 } 132 } 133 134 137 private Set <Module> bootModules = null; 138 139 143 public void loadBootModules() { 144 Collection <String > ignoredPrefixes = new ArrayList <String >(3); try { 149 String jdk = System.getProperty("java.home"); 151 if (jdk.endsWith(File.separator + "jre")) { jdk = jdk.substring(0, jdk.length() - 4); 153 } 154 File f = new File (jdk); 155 if (new File (new File (f, "lib"), "tools.jar").isFile()) { ignoredPrefixes.add("jar:" + f.toURI().toURL()); } 159 String nbhomeS = System.getProperty("netbeans.home"); 162 if (nbhomeS != null) { 163 File nbhome = new File (nbhomeS); 164 f = new File (new File (nbhome, "lib"), "ext"); ignoredPrefixes.add("jar:" + f.toURI().toURL()); } 167 } catch (MalformedURLException e) { 168 Util.err.log(Level.WARNING, null, e); 169 } 170 Util.err.log(Level.FINE, "ignoredPrefixes={0}", ignoredPrefixes); 171 172 mgr.mutexPrivileged().enterWriteAccess(); 173 ev.log(Events.START_LOAD_BOOT_MODULES); 174 try { 175 bootModules = new HashSet <Module>(10); 176 ClassLoader loader = ModuleSystem.class.getClassLoader(); 177 Enumeration <URL > e = loader.getResources("META-INF/MANIFEST.MF"); ev.log(Events.PERF_TICK, "got all manifests"); 180 Set <URL > checkedManifests = new HashSet <URL >(); 182 MANIFESTS: 183 while (e.hasMoreElements()) { 184 URL manifestUrl = e.nextElement(); 185 if (!checkedManifests.add(manifestUrl)) { 186 continue; 188 } 189 String manifestUrlS = manifestUrl.toExternalForm(); 190 for (String pref: ignoredPrefixes) { 191 if (manifestUrlS.startsWith(pref)) { 192 Util.err.log(Level.FINE, "ignoring JDK/JRE manifest: {0}", manifestUrlS); 193 continue MANIFESTS; 194 } 195 } 196 Util.err.log(Level.FINE, "Checking boot manifest: {0}", manifestUrlS); 197 198 InputStream is; 199 try { 200 is = manifestUrl.openStream(); 201 } catch (IOException ioe) { 202 Exceptions.attachMessage(ioe, "URL: " + manifestUrl); throw ioe; 205 } 206 try { 207 Manifest mani = new Manifest (is); 208 Attributes attr = mani.getMainAttributes(); 209 if (attr.getValue("OpenIDE-Module") == null) { continue; 212 } 213 bootModules.add(mgr.createFixed(mani, manifestUrl, loader)); 214 } finally { 215 is.close(); 216 } 217 } 218 if (list == null) { 219 mgr.enable(bootModules); 222 } 223 ev.log(Events.PERF_TICK, "added all classpath modules"); 225 } catch (IOException ioe) { 226 Util.err.log(Level.WARNING, null, ioe); 231 } catch (DuplicateException de) { 232 Util.err.log(Level.WARNING, null, de); 233 } finally { 234 ev.log(Events.FINISH_LOAD_BOOT_MODULES); 236 mgr.mutexPrivileged().exitWriteAccess(); 237 } 238 } 239 240 242 public void readList() { 243 ev.log(Events.PERF_START, "ModuleSystem.readList"); mgr.mutexPrivileged().enterWriteAccess(); 245 try { 246 list.readInitial(); 247 } finally { 248 mgr.mutexPrivileged().exitWriteAccess(); 249 } 250 ev.log(Events.PERF_END, "ModuleSystem.readList"); } 252 253 255 public void restore() { 256 ev.log(Events.PERF_START, "ModuleSystem.restore"); mgr.mutexPrivileged().enterWriteAccess(); 258 try { 259 Set <Module> toTrigger = new HashSet <Module>(bootModules); 260 list.trigger(toTrigger); 261 } finally { 262 mgr.mutexPrivileged().exitWriteAccess(); 263 } 264 ev.log(Events.PERF_END, "ModuleSystem.restore"); } 266 267 270 public boolean shutDown(Runnable midHook) { 271 mgr.mutexPrivileged().enterWriteAccess(); 272 try { 273 return mgr.shutDown(midHook); 274 } finally { 275 mgr.mutexPrivileged().exitWriteAccess(); 276 } 277 } 278 279 287 final void deployTestModule(File jar) throws IOException { 288 if (! jar.isAbsolute()) throw new IOException ("Absolute paths only please"); mgr.mutexPrivileged().enterWriteAccess(); 290 ev.log(Events.START_DEPLOY_TEST_MODULE, jar); 291 System.err.println("Deploying test module " + jar + "..."); try { 296 Module tm = null; 298 Set <Module> toReenable = new HashSet <Module>(); 300 Iterator it = mgr.getModules().iterator(); 303 while (it.hasNext()) { 304 Module m = (Module)it.next(); 305 if (m.getJarFile() != null) { 306 if (jar.equals(m.getJarFile())) { 307 if (! m.isReloadable()) { 309 m.setReloadable(true); 310 } 311 turnOffModule(m, toReenable); 312 mgr.reload(m); 313 tm = m; 314 break; 315 } 316 } 317 } 318 if (tm == null) { 319 try { 323 tm = mgr.create(jar, new ModuleHistory(jar.getAbsolutePath()), true, false, false); 324 } catch (DuplicateException dupe) { 325 Module old = dupe.getOldModule(); 326 System.err.println("Replacing old module in " + old); turnOffModule(old, toReenable); 328 mgr.delete(old); 329 try { 330 tm = mgr.create(jar, new ModuleHistory(jar.getAbsolutePath()), true, false, false); 331 } catch (DuplicateException dupe2) { 332 throw (IOException ) new IOException (dupe2.toString()).initCause(dupe2); 334 } 335 } 336 } 337 System.err.println("Enabling " + tm + "..."); if (!mgr.simulateEnable(Collections.singleton(tm)).contains(tm)) { 340 throw new IOException ("Cannot enable " + tm + "; problems: " + tm.getProblems()); 341 } 342 mgr.enable(tm); 343 if (! toReenable.isEmpty()) { 349 System.err.println("Also re-enabling:"); it = toReenable.iterator(); 351 while (it.hasNext()) { 352 Module m = (Module)it.next(); 353 System.err.println("\t" + m.getDisplayName()); if (m.isReloadable()) { 355 m.reload(); 356 } 357 } 358 try { 359 mgr.enable(toReenable); 360 } catch (IllegalArgumentException iae) { 361 throw new IOException (iae.toString()); 363 } 364 } 365 System.err.println("Done."); } finally { 367 ev.log(Events.FINISH_DEPLOY_TEST_MODULE, jar); 368 mgr.mutexPrivileged().exitWriteAccess(); 369 } 370 } 371 375 private void turnOffModule(Module m, Set <Module> toReenable) { 376 if (! m.isEnabled()) { 377 return; 379 } 380 for (Module m2: mgr.simulateDisable(Collections.<Module>singleton(m))) { 381 if (!m2.isAutoload() && !m2.isEager()) { 382 toReenable.add(m2); 383 } 384 } 385 try { 386 System.err.println("Disabling " + m + "..."); mgr.disable(toReenable); 389 } finally { 390 toReenable.remove(m); 391 } 392 } 393 394 408 public String getEffectiveClasspath(Module m) { 409 return installer.getEffectiveClasspath(m); 410 } 411 412 416 private static final class QuietEvents extends Events { 417 QuietEvents() {} 418 protected void logged(String message, Object [] args) {} 419 } 420 421 } 422 | Popular Tags |