KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > convertor > ModuleUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
29
30 /**
31  *
32  * @author Jesse Glick, David Konecny
33  */

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 JavaDoc {
49         try {
50             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
51                 public Object JavaDoc run() throws Exception JavaDoc {
52                     File JavaDoc jar1 = new File JavaDoc(ModuleUtils.class.getResource("data/bookconvertor.jar").getPath());
53                     bookConvertorModule = mgr.create(jar1, new ModuleHistory(jar1.getAbsolutePath()), false, false, false);
54                     File JavaDoc jar2 = new File JavaDoc(ModuleUtils.class.getResource("data/dvdconvertor.jar").getPath());
55                     dvdConvertorModule = mgr.create(jar2, new ModuleHistory(jar2.getAbsolutePath()), false, false, false);
56                     File JavaDoc jar3 = new File JavaDoc(ModuleUtils.class.getResource("data/shoppingcartconvertor.jar").getPath());
57                     shoppingCartConvertorModule = mgr.create(jar3, new ModuleHistory(jar3.getAbsolutePath()), false, false, false);
58                     File JavaDoc jar4 = new File JavaDoc(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 JavaDoc {
69         try {
70             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
71                 public Object JavaDoc run() throws Exception JavaDoc {
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 JavaDoc {
97         try {
98             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
99                 public Object JavaDoc run() throws Exception JavaDoc {
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 JavaDoc("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 JavaDoc {
119         twiddle(bookConvertorModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE);
120     }
121     
122     public void enableDVDConvertorModule(boolean enable) throws Exception JavaDoc {
123         twiddle(dvdConvertorModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE);
124     }
125     
126     public void enableShoppingCartConvertorModule(boolean enable) throws Exception JavaDoc {
127         twiddle(shoppingCartConvertorModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE);
128     }
129     
130     public void enableStoreConvertorModule(boolean enable) throws Exception JavaDoc {
131         twiddle(storeConvertorModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE);
132     }
133     
134 }
135
Popular Tags