KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > settings > convertors > ModuleInfoManager


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.modules.settings.convertors;
21
22 import java.beans.*;
23 import java.util.*;
24 import org.openide.modules.ModuleInfo;
25 import org.openide.util.Lookup;
26 import org.openide.util.LookupListener;
27 import org.openide.util.LookupEvent;
28
29 /** Manager providing ModuleInfo of all modules presented in the system.
30  * @author Jan Pokorsky
31  */

32 final class ModuleInfoManager {
33     private final static ModuleInfoManager mim = new ModuleInfoManager();;
34
35     /** all modules <code bas name, ModuleInfo> */
36     private HashMap<String JavaDoc, ModuleInfo> modules = null;
37     /** lookup query to find out all modules */
38     private Lookup.Result<ModuleInfo> modulesResult = null;
39     /** <ModuleInfo, PCL> */
40     private HashMap<ModuleInfo, PCL> mapOfListeners;
41     /** Creates a new instance of ModuleInfoManager */
42     private ModuleInfoManager() {
43     }
44     
45     public final static ModuleInfoManager getDefault() {
46         return mim;
47     }
48     
49     /** find module info.
50      * @param codeBaseName module code base name (without revision)
51      * @return module info or null
52      */

53     public ModuleInfo getModule(String JavaDoc codeBaseName) {
54         Collection<? extends ModuleInfo> l = null;
55         if (modules == null) {
56             l = getModulesResult().allInstances();
57         }
58         synchronized (this) {
59             if (modules == null) fillModules(l);
60             return (ModuleInfo) modules.get(codeBaseName);
61         }
62     }
63
64     private Lookup.Result<ModuleInfo> getModulesResult() {
65         synchronized (this) {
66             if (modulesResult == null) {
67                 Lookup lookup = org.netbeans.core.NbTopManager.getModuleLookup();
68                 modulesResult = lookup.
69                     lookup(new Lookup.Template<ModuleInfo>(ModuleInfo.class));
70                 modulesResult.addLookupListener(new LookupListener() {
71                     public void resultChanged(LookupEvent ev) {
72                         Collection<? extends ModuleInfo> l = getModulesResult().allInstances();
73                         XMLSettingsSupport.err.fine("Modules changed: " + l); // NOI18N
74
List reloaded;
75                         synchronized (this) {
76                             fillModules(l);
77                             reloaded = replaceReloadedModules();
78                             XMLSettingsSupport.err.fine("Reloaded modules: " + reloaded); // NOI18N
79
}
80                         notifyReloads(reloaded);
81                     }
82                 });
83             }
84             return modulesResult;
85         }
86     }
87
88     /** notify registered listeners about reloaded modules
89      * @param l a list of PCLs of reloaded modules
90      */

91     private void notifyReloads(List l) {
92         Iterator it = l.iterator();
93         while (it.hasNext()) {
94             PCL lsnr = (PCL) it.next();
95             lsnr.notifyReload();
96         }
97     }
98
99     /** recompute accessible modules.
100      * @param l a collection of module infos
101      */

102     private void fillModules(Collection<? extends ModuleInfo> l) {
103         HashMap<String JavaDoc, ModuleInfo> m = new HashMap<String JavaDoc, ModuleInfo>((l.size() << 2) / 3 + 1);
104         for (ModuleInfo mi: l) {
105             m.put(mi.getCodeNameBase(), mi);
106         }
107         modules = m;
108     }
109     
110     /** replace old MIs of reloaded modules with new ones
111      * @return the list of PCLs of reloaded modules
112      */

113     private List<PCL> replaceReloadedModules() {
114         if (mapOfListeners == null) return Collections.emptyList();
115         
116         Iterator<ModuleInfo> it = new ArrayList<ModuleInfo>(mapOfListeners.keySet()).iterator();
117         List<PCL> reloaded = new ArrayList<PCL>();
118         
119         while (it.hasNext()) {
120             ModuleInfo mi = it.next();
121             ModuleInfo miNew = modules.get(mi.getCodeNameBase());
122             if (mi != miNew && miNew != null) {
123                 PCL lsnr = mapOfListeners.remove(mi);
124                 lsnr.setModuleInfo(miNew);
125                 reloaded.add(lsnr);
126                 mapOfListeners.put(miNew, lsnr);
127             }
128         }
129         
130         return reloaded;
131     }
132
133     /** look up ModuleInfo according to clazz
134      * @param clazz class used in the look up query
135      * @return module info of the module which clazz was loaded from
136      */

137     public ModuleInfo getModuleInfo(Class JavaDoc clazz) {
138         Iterator it = getModulesResult().allInstances().iterator();
139         while (it.hasNext()) {
140             ModuleInfo mi = (ModuleInfo) it.next();
141             if (mi.owns(clazz)) return mi;
142         }
143         return null;
144     }
145     
146     /** register listener to be notified about changes of mi
147      * @param sdc convertor
148      * @param mi ModuleInfo for which the listener will be registered
149      */

150     public synchronized void registerPropertyChangeListener(SerialDataConvertor sdc, ModuleInfo mi) {
151         if (mapOfListeners == null) {
152             mapOfListeners = new HashMap<ModuleInfo,PCL>(modules.size());
153         }
154         
155         PCL lsnr = mapOfListeners.get(mi);
156         if (lsnr == null) {
157             lsnr = new PCL(mi);
158             mapOfListeners.put(mi, lsnr);
159         }
160         PropertyChangeListener pcl = org.openide.util.WeakListeners.propertyChange(sdc, lsnr);
161         lsnr.addPropertyChangeListener(sdc, pcl);
162     }
163     
164     /** unregister listener
165      * @param sdc convertor
166      * @param mi ModuleInfo
167      * @see #registerPropertyChangeListener
168      */

169     public synchronized void unregisterPropertyChangeListener(SerialDataConvertor sdc, ModuleInfo mi) {
170         if (mapOfListeners == null) return;
171         PCL lsnr = (PCL) mapOfListeners.get(mi);
172         if (lsnr != null) {
173             lsnr.removePropertyChangeListener(sdc);
174             // do not try to discard lsnr to allow to track reloading of a module
175
}
176     }
177     
178     /** find out if a module was reloaded (disable+enabled)
179      * @param mi ModuleInfo of the queried module
180      * @return reload status
181      */

182     public synchronized boolean isReloaded(ModuleInfo mi) {
183         if (mapOfListeners == null) return false;
184         PCL lsnr = (PCL) mapOfListeners.get(mi);
185         return lsnr != null && lsnr.isReloaded();
186     }
187     
188     /** find out if a module was reloaded (disable+enabled)
189      * @param codeBaseName ModuleInfo's code base name of the queried module
190      * @return reload status
191      */

192     public synchronized boolean isReloaded(String JavaDoc codeBaseName) {
193         if (mapOfListeners == null) return false;
194         return isReloaded(getModule(codeBaseName));
195     }
196     
197     /** ModuleInfo status provider shared by registered listeners
198      * @see #registerPropertyChangeListener
199      */

200     private static final class PCL implements PropertyChangeListener {
201         /** a flag to be set to true when a module has been disabled */
202         private boolean aModuleHasBeenChanged = false;
203         private boolean wasModuleEnabled;
204         private ModuleInfo mi;
205         private PropertyChangeSupport changeSupport;
206         /** map of registered listeners <SerialDataConvertor, PropertyChangeListener> */
207         private Map<SerialDataConvertor, PropertyChangeListener> origs;
208         
209         public PCL(ModuleInfo mi) {
210             this.mi = mi;
211             wasModuleEnabled = mi.isEnabled();
212             mi.addPropertyChangeListener(this);
213         }
214         
215         /** replace an old module info with a new one */
216         void setModuleInfo(ModuleInfo mi) {
217             this.mi.removePropertyChangeListener(this);
218             aModuleHasBeenChanged = true;
219             this.mi = mi;
220             mi.addPropertyChangeListener(this);
221         }
222         
223         /** notify listeners about a module reload */
224         void notifyReload() {
225             firePropertyChange();
226         }
227         
228         boolean isReloaded() {
229             return aModuleHasBeenChanged;
230         }
231         
232         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
233             if(ModuleInfo.PROP_ENABLED.equals(evt.getPropertyName())) {
234
235                 boolean change;
236
237                 if (!Boolean.TRUE.equals (evt.getNewValue ())) {
238                     // a module has been disabled, use full checks
239
aModuleHasBeenChanged = true;
240
241                     // if wasModuleEnabled was true, we changed state
242
change = wasModuleEnabled;
243                 } else {
244                     // a module was enabled, if wasModuleEnabled was false
245
// we changed state
246
change = !wasModuleEnabled;
247                 }
248
249                 // update wasModuleEnabled to current state of the module
250
wasModuleEnabled = mi.isEnabled();
251
252                 if (change) {
253                     //instanceCookieChanged(null);
254
firePropertyChange();
255                 }
256             }
257         }
258
259         /** adds listener per convertor */
260         public void addPropertyChangeListener(SerialDataConvertor sdc, PropertyChangeListener listener) {
261             synchronized (this) {
262                 if (changeSupport == null) {
263                     changeSupport = new PropertyChangeSupport(this);
264                     origs = new WeakHashMap<SerialDataConvertor, PropertyChangeListener>();
265                 }
266                 
267                 PropertyChangeListener old = (PropertyChangeListener) origs.get(sdc);
268                 if (old != null) return;
269                 origs.put(sdc, listener);
270             }
271             changeSupport.addPropertyChangeListener(listener);
272         }
273         
274         public void removePropertyChangeListener(PropertyChangeListener listener) {
275             if (changeSupport != null) {
276                 changeSupport.removePropertyChangeListener(listener);
277             }
278         }
279         
280         /** unregister listener registered per convertor */
281         public void removePropertyChangeListener(SerialDataConvertor sdc) {
282             synchronized (this) {
283                 if (origs == null) return;
284                 
285                 PropertyChangeListener pcl = (PropertyChangeListener) origs.remove(sdc);
286                 if (pcl != null) {
287                     removePropertyChangeListener(pcl);
288                 }
289             }
290         }
291         
292         private void firePropertyChange() {
293             if (changeSupport != null) {
294                 changeSupport.firePropertyChange(ModuleInfo.PROP_ENABLED, null, null);
295             }
296         }
297     }
298     
299 }
300
Popular Tags