KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > enabledisabletest > 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.core.registry.enabledisabletest;
21
22 import java.net.URI JavaDoc;
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 JavaDoc;
30
31
32 /**
33  *
34  * @author Jesse Glick, David Konecny
35  */

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 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(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 JavaDoc("bookModule is uninstallable: " + bookModule.getProblems());
55                     File JavaDoc jar2 = new File JavaDoc(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 JavaDoc {
66         try {
67             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
68                 public Object JavaDoc run() throws Exception JavaDoc {
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 JavaDoc {
88         try {
89             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
90                 public Object JavaDoc run() throws Exception JavaDoc {
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 JavaDoc("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 JavaDoc {
118         twiddle(bookModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE);
119     }
120     
121     public void enableCDModule(boolean enable) throws Exception JavaDoc {
122         twiddle(cdModule, enable ? TWIDDLE_ENABLE : TWIDDLE_DISABLE);
123     }
124     
125 }
126
Popular Tags