KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > ConnectionSupport


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.openide.loaders;
21
22
23 import java.io.*;
24 import java.util.*;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import org.openide.cookies.ConnectionCookie;
28 import org.openide.nodes.Node;
29
30 /** Support implementing ConnectionCookie, that stores
31 * listeners in extended attributes of associated entry.
32 *
33 * @author Jaroslav Tulach, Petr Hamernik
34  * @deprecated Should no longer be used.
35 */

36 @Deprecated JavaDoc
37 public class ConnectionSupport extends Object JavaDoc implements ConnectionCookie {
38     /** extended attribute to store (ArrayList of Type and Node.Handle) */
39     private static final String JavaDoc EA_LISTENERS = "EA-OpenIDE-Connection"; // NOI18N
40

41     /** entry to work on */
42     private MultiDataObject.Entry entry;
43     /** array of types */
44     private ConnectionCookie.Type[] types;
45     /** cached the return value for getTypes method */
46     private Set<ConnectionCookie.Type> typesSet;
47
48     /** table of listeners for non-persistent types. Array of Pairs.
49     */

50     private LinkedList<Pair> listeners;
51
52     /** Creates new connection support for given file entry.
53     * @param entry entry to store listener to its extended attributes
54     * @param types a list of event types to support
55     */

56     public ConnectionSupport (MultiDataObject.Entry entry, ConnectionCookie.Type[] types) {
57         this.entry = entry;
58         this.types = types;
59         // #45750 - init list for CC.T which are not persistent
60
listeners = new LinkedList<Pair>();
61     }
62
63     /** Attaches new node to listen to events produced by this
64     * event. The type must be one of event types supported by this
65     * cookie and the listener should have ConnectionCookie.Listener cookie
66     * attached so it can be notified when event of requested type occurs.
67     *
68     * @param type the type of event, must be supported by the cookie
69     * @param listener the node that should be notified
70     *
71     * @exception InvalidObjectException if the type is not supported by the cookie (subclass of IOException)
72     * @exception IOException if the type is persistent and the listener does not
73     * have serializable handle (listener.getHandle () is null or its serialization
74     * throws an exception)
75     */

76     public synchronized void register (ConnectionCookie.Type type, Node listener) throws IOException {
77         // test if the file is supported, if not throws exception
78
testSupported (type);
79
80         boolean persistent = type.isPersistent ();
81         LinkedList<Pair> list;
82
83         if (persistent) {
84             list = (LinkedList<Pair>)entry.getFile ().getAttribute (EA_LISTENERS);
85         } else {
86             list = listeners;
87         }
88
89         if (list == null) {
90             // empty list => create new
91
list = new LinkedList<Pair> ();
92         }
93
94         // System.out.println("======================================== ADD:"+entry.getFile().getName()); // NOI18N
95
// System.out.println(this);
96
// System.out.println("size:"+list.size()); // NOI18N
97

98         Iterator<Pair> it = list.iterator ();
99         while (it.hasNext ()) {
100             Pair pair = it.next ();
101             // System.out.println("test:"+pair.getType()); // NOI18N
102
if (type.equals (pair.getType ())) {
103                 Node n;
104                 try {
105                     n = pair.getNode ();
106                     // System.out.println(" node:"+n); // NOI18N
107
} catch (IOException e) {
108                     // node that cannot produce handle => remove it
109
Logger.getLogger(ConnectionSupport.class.getName()).log(Level.WARNING, null, e);
110                     it.remove ();
111                     // go on
112
continue;
113                 }
114                 // System.out.println(" compare with:"+listener); // NOI18N
115
if (n.equals (listener)) {
116                     // we found our node - it is already in the list.
117
// System.out.println("the listener found - remove it."); // NOI18N
118
it.remove();
119                     continue;
120                 }
121                 else {
122                     // System.out.println(" nene"); // NOI18N
123
}
124             }
125         }
126         list.add (persistent ? new Pair (type, listener.getHandle ()) : new Pair (type, listener));
127
128         // System.out.println("after add:"+list.size()); // NOI18N
129

130         if (persistent) {
131             // save can throw IOException
132
entry.getFile ().setAttribute (EA_LISTENERS, list);
133         }
134
135     }
136
137     /** Unregisters an listener.
138     * @param type type of event to unregister the listener from listening to
139     * @param listener to unregister
140     * @exception IOException if there is I/O operation error when the removing
141     * the listener from persistent storage
142     */

143     public synchronized void unregister (ConnectionCookie.Type type, Node listener) throws IOException {
144         // test if the file is supported, if not throws exception
145
testSupported (type);
146
147         boolean persistent = type.isPersistent ();
148         LinkedList list;
149
150         if (persistent) {
151             list = (LinkedList)entry.getFile ().getAttribute (EA_LISTENERS);
152         } else {
153             list = listeners;
154         }
155
156         if (list == null) {
157             // empty list => no work
158
return;
159         }
160
161         // System.out.println("======================================== REMOVE:"+entry.getFile().getName()); // NOI18N
162
// System.out.println(this);
163
// System.out.println("size:"+list.size()); // NOI18N
164

165         Iterator it = list.iterator ();
166         while (it.hasNext ()) {
167             Pair pair = (Pair)it.next ();
168
169             if (type.equals (pair.getType ())) {
170                 Node n;
171                 try {
172                     n = pair.getNode ();
173                 } catch (IOException e) {
174                     // node that cannot produce handle => remove it
175
it.remove ();
176                     // go on
177
continue;
178                 }
179                 if (n.equals (listener)) {
180                     // we found our node
181
it.remove ();
182                     // break the cycle but save if necessary
183

184                     continue;
185                 }
186             }
187         }
188
189         //System.out.println("after remove:"+list.size()); // NOI18N
190

191         if (persistent) {
192             // save can throw IOException
193
entry.getFile ().setAttribute (EA_LISTENERS, list);
194         }
195     }
196
197     /** Unmutable set of types supported by this connection source.
198     * @return a set of Type objects
199     */

200     public Set<ConnectionCookie.Type> getTypes () {
201         if (typesSet == null)
202             typesSet = Collections.unmodifiableSet (new HashSet<ConnectionCookie.Type> (Arrays.asList (types)));
203         return typesSet;
204     }
205
206     /** Get the list of all registered types in every (persistent
207     * or not persistent) connections.
208     *
209     * @return the list of ConnectionCookie.Type objects
210     */

211     public List<ConnectionCookie.Type> getRegisteredTypes() {
212         LinkedList<ConnectionCookie.Type> typesList = new LinkedList<ConnectionCookie.Type>();
213
214         LinkedList<Pair> list = listeners;
215         for (int i = 0; i <= 1; i++) {
216             if (i == 1)
217                 list = (LinkedList<Pair>)entry.getFile ().getAttribute (EA_LISTENERS);
218
219             if (list == null)
220                 continue;
221
222             for (Pair p: list) {
223                 typesList.add(p.getType());
224             }
225         }
226
227         return typesList;
228     }
229
230     /** Fires info for all listeners of given type.
231     * @param ev the event
232     */

233     public void fireEvent (ConnectionCookie.Event ev) {
234         LinkedList<Pair> list;
235         ConnectionCookie.Type type;
236         boolean persistent;
237         
238         synchronized (this) {
239             type = ev.getType ();
240
241             persistent = type.isPersistent ();
242             if (persistent) {
243                 list = (LinkedList<Pair>)entry.getFile ().getAttribute (EA_LISTENERS);
244             } else {
245                 list = listeners;
246             }
247
248             if (list == null) return;
249          
250             list = (LinkedList<Pair>)list.clone ();
251         }
252
253         int size = list.size ();
254
255         Iterator<Pair> it = list.iterator ();
256         while (it.hasNext ()) {
257             Pair pair = it.next ();
258
259             if (pair.getType ().overlaps(ev.getType())) {
260                 try {
261                     ConnectionCookie.Listener l = (ConnectionCookie.Listener)pair.getNode ().getCookie (ConnectionCookie.Listener.class);
262                     if (l != null) {
263                         try {
264                             l.notify (ev);
265                         } catch (IllegalArgumentException JavaDoc e) {
266                             it.remove ();
267                         } catch (ClassCastException JavaDoc e) {
268                             it.remove ();
269                         }
270             }
271                 } catch (IOException e) {
272                     it.remove ();
273                 }
274             }
275         }
276     
277     // if something in the list has changed, save it.
278
if (persistent && list.size() != size) {
279             // save can throw IOException
280
try {
281                 entry.getFile ().setAttribute (EA_LISTENERS, list);
282             } catch (IOException e) {
283                 // ignore never mind
284
}
285         }
286     }
287
288     /** Obtains a set of all listeners for given type.
289     * @param type type of events to test
290     * @return unmutable set of all listeners (Node) for a type
291     */

292     public synchronized java.util.Set JavaDoc/*<Node>*/ listenersFor (ConnectionCookie.Type type) {
293         LinkedList<Pair> list;
294
295         if (type.isPersistent ()) {
296             list = (LinkedList<Pair>)entry.getFile ().getAttribute (EA_LISTENERS);
297         } else {
298             list = listeners;
299         }
300
301         if (list == null) return Collections.emptySet();
302
303         Iterator<Pair> it = list.iterator ();
304         HashSet<Node> set = new HashSet<Node> (7);
305
306         while (it.hasNext ()) {
307             Pair pair = (Pair)it.next ();
308             if (type.overlaps(pair.getType ())) {
309                 try {
310                     set.add (pair.getNode ());
311                 } catch (IOException e) {
312                     // ignore the exception
313
}
314             }
315         }
316
317         return set;
318     }
319
320
321     /** Test if the type is supported.
322     * @param t type
323     * @exception InvalidObjectException if type is not valid
324     */

325     private void testSupported (ConnectionCookie.Type t) throws InvalidObjectException {
326         for (int i = 0; i < types.length; i++) {
327             if (t.overlaps(types[i])) {
328                 return;
329             }
330         }
331         throw new InvalidObjectException (t.toString ());
332     }
333
334     /** A pair of type of event and a handle to it.
335     */

336     private static final class Pair extends Object JavaDoc implements java.io.Serializable JavaDoc {
337         /** type of the listener */
338         private ConnectionCookie.Type type;
339         /** the node or the handle to a node */
340         private Object JavaDoc value;
341
342         static final long serialVersionUID =387180886175136728L;
343         /** @param t the type of the event
344         * @param n the listener
345         */

346         public Pair (ConnectionCookie.Type t, Node n) {
347             type = t;
348             value = n;
349         }
350
351         /** @param t the type of the event
352         * @param h the listener's handle
353         * @exception IOException if handle is null
354         */

355         public Pair (ConnectionCookie.Type t, Node.Handle h) throws IOException {
356
357             if (h == null) throw new IOException ();
358
359             type = t;
360             value = h;
361         }
362
363         /** Getter of the type.
364         */

365         public ConnectionCookie.Type getType () {
366             return type;
367         }
368
369         /** Getter of the listener.
370         * @return listener's node
371         * @exception IOException if the handle is not able to create a node
372         */

373         public Node getNode () throws IOException {
374             return value instanceof Node ? (Node)value : ((Node.Handle)value).getNode ();
375         }
376     }
377 }
378
Popular Tags