1 19 20 package org.netbeans.api.convertor; 21 22 import org.netbeans.Module; 23 import org.netbeans.core.startup.ModuleHistory; 24 import org.netbeans.ModuleManager; 25 import org.openide.util.Mutex; 26 import org.openide.util.MutexException; 27 28 import java.io.File ; 29 30 34 public class ModuleUtils { 35 36 public static ModuleUtils DEFAULT = new ModuleUtils(); 37 38 private ModuleManager mgr; 39 private Module bookConvertorModule; 40 private Module dvdConvertorModule; 41 private Module shoppingCartConvertorModule; 42 private Module storeConvertorModule; 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 (ModuleUtils.class.getResource("data/bookconvertor.jar").getPath()); 53 bookConvertorModule = mgr.create(jar1, new ModuleHistory(jar1.getAbsolutePath()), false, false, false); 54 File jar2 = new File (ModuleUtils.class.getResource("data/dvdconvertor.jar").getPath()); 55 dvdConvertorModule = mgr.create(jar2, new ModuleHistory(jar2.getAbsolutePath()), false, false, false); 56 File jar3 = new File (ModuleUtils.class.getResource("data/shoppingcartconvertor.jar").getPath()); 57 shoppingCartConvertorModule = mgr.create(jar3, new ModuleHistory(jar3.getAbsolutePath()), false, false, false); 58 File jar4 = new File (ModuleUtils.class.getResource("data/storeconvertor.jar").getPath()); 59 storeConvertorModule = mgr.create(jar4, new ModuleHistory(jar4.getAbsolutePath()), false, false, false); 60 return null; 61 } 62 }); 63 } catch (MutexException me) { 64 throw me.getException(); 65 } 66 } 67 68 protected void uninstall() throws Exception { 69 try { 70 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 71 public Object run() throws Exception { 72 if (bookConvertorModule.isEnabled()) mgr.disable(bookConvertorModule); 73 mgr.delete(bookConvertorModule); 74 if (dvdConvertorModule.isEnabled()) mgr.disable(dvdConvertorModule); 75 mgr.delete(dvdConvertorModule); 76 if (shoppingCartConvertorModule.isEnabled()) mgr.disable(shoppingCartConvertorModule); 77 mgr.delete(shoppingCartConvertorModule); 78 if (storeConvertorModule.isEnabled()) mgr.disable(storeConvertorModule); 79 mgr.delete(storeConvertorModule); 80 return null; 81 } 82 }); 83 } catch (MutexException me) { 84 throw me.getException(); 85 } 86 bookConvertorModule = null; 87 dvdConvertorModule = null; 88 shoppingCartConvertorModule = null; 89 storeConvertorModule = null; 90 mgr = null; 91 } 92 93 protected static final int TWIDDLE_ENABLE = 0; 94 protected static final int TWIDDLE_DISABLE = 1; 95 96 private void twiddle(final Module m, final int action) throws Exception { 97 try { 98 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 99 public Object run() throws Exception { 100 switch (action) { 101 case TWIDDLE_ENABLE: 102 mgr.enable(m); 103 break; 104 case TWIDDLE_DISABLE: 105 mgr.disable(m); 106 break; 107 default: 108 throw new IllegalArgumentException ("bad action: " + action); 109 } 110 return null; 111 } 112 }); 113 } catch (MutexException me) { 114 throw me.getException(); 115 } 116 } 117 118 public void enableBookModule(boolean enable) throws Exception { 119 twiddle(bookConvertorModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE); 120 } 121 122 public void enableDVDConvertorModule(boolean enable) throws Exception { 123 twiddle(dvdConvertorModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE); 124 } 125 126 public void enableShoppingCartConvertorModule(boolean enable) throws Exception { 127 twiddle(shoppingCartConvertorModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE); 128 } 129 130 public void enableStoreConvertorModule(boolean enable) throws Exception { 131 twiddle(storeConvertorModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE); 132 } 133 134 } 135 | Popular Tags |