1 19 20 package org.netbeans.core.registry.enabledisabletest; 21 22 import java.net.URI ; 23 import org.netbeans.Module; 24 import org.netbeans.core.startup.ModuleHistory; 25 import org.netbeans.ModuleManager; 26 import org.openide.util.Mutex; 27 import org.openide.util.MutexException; 28 29 import java.io.File ; 30 31 32 36 public class ModuleUtils { 37 38 public static ModuleUtils DEFAULT = new ModuleUtils(); 39 40 private ModuleManager mgr; 41 private Module bookModule; 42 private Module cdModule; 43 44 private ModuleUtils() { 45 mgr = org.netbeans.core.startup.Main.getModuleSystem().getManager(); 46 } 47 48 public void install() throws Exception { 49 try { 50 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 51 public Object run() throws Exception { 52 File jar1 = new File (URI.create(ModuleUtils.class.getResource("data/bookmodule.jar").toExternalForm())); 53 bookModule = mgr.create(jar1, new ModuleHistory(jar1.getAbsolutePath()), false, false, false); 54 if (!bookModule.getProblems().isEmpty()) throw new IllegalStateException ("bookModule is uninstallable: " + bookModule.getProblems()); 55 File jar2 = new File (URI.create(ModuleUtils.class.getResource("data/cdmodule.jar").toExternalForm())); 56 cdModule = mgr.create(jar2, new ModuleHistory(jar2.getAbsolutePath()), false, false, false); 57 return null; 58 } 59 }); 60 } catch (MutexException me) { 61 throw me.getException(); 62 } 63 } 64 65 protected void uninstall() throws Exception { 66 try { 67 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 68 public Object run() throws Exception { 69 if (bookModule.isEnabled()) mgr.disable(bookModule); 70 mgr.delete(bookModule); 71 if (cdModule.isEnabled()) mgr.disable(cdModule); 72 mgr.delete(cdModule); 73 return null; 74 } 75 }); 76 } catch (MutexException me) { 77 throw me.getException(); 78 } 79 bookModule = null; 80 cdModule = null; 81 mgr = null; 82 } 83 84 protected static final int TWIDDLE_ENABLE = 0; 85 protected static final int TWIDDLE_DISABLE = 1; 86 87 private void twiddle(final Module m, final int action) throws Exception { 88 try { 89 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 90 public Object run() throws Exception { 91 switch (action) { 92 case TWIDDLE_ENABLE: 93 mgr.enable(m); 94 break; 95 case TWIDDLE_DISABLE: 96 mgr.disable(m); 97 break; 98 default: 99 throw new IllegalArgumentException ("bad action: " + action); 100 } 101 return null; 102 } 103 }); 104 } catch (MutexException me) { 105 throw me.getException(); 106 } 107 } 108 109 public Module getBookModule() { 110 return bookModule; 111 } 112 113 public Module getCDModule() { 114 return cdModule; 115 } 116 117 public void enableBookModule(boolean enable) throws Exception { 118 twiddle(bookModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE); 119 } 120 121 public void enableCDModule(boolean enable) throws Exception { 122 twiddle(cdModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE); 123 } 124 125 } 126 | Popular Tags |