KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > util > OscarDispatchQueue


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar.util;
37
38 import java.util.EventListener JavaDoc;
39 import java.util.EventObject JavaDoc;
40
41 import org.osgi.framework.BundleListener;
42 import org.osgi.framework.ServiceListener;
43 import org.osgi.framework.SynchronousBundleListener;
44 import org.ungoverned.oscar.Oscar;
45
46 /**
47  * This is a subclass of <tt>DispatchQueue</tt> that specifically adds support
48  * for <tt>SynchronousBundleListener</tt>s; the OSGi specification
49  * says that synchronous bundle listeners should receive their events
50  * immediately, i.e., they should not be delivered on a separate thread
51  * like is the case with the <tt>DispatchQueue</tt>. To achieve this
52  * functionality, this class overrides the dispatch method to automatically
53  * fire any bundle events to synchronous bundle listeners using the
54  * calling thread. In order to ensure that synchronous bundle listeners
55  * do not receive an event twice, it wraps the passed in <tt>Dipatcher</tt>
56  * instance so that it filters synchronous bundle listeners.
57 **/

58 public class OscarDispatchQueue extends DispatchQueue
59 {
60     /**
61      * Dispatches an event to a set of event listeners using a specified
62      * dispatcher object. This overrides the definition of the super class
63      * to deliver events to <tt>ServiceListener</tt>s and
64      * <tt>SynchronousBundleListener</tt>s using
65      * the calling thread instead of the event dispatching thread. All
66      * other events are still delivered asynchronously.
67      *
68      * @param dispatcher the dispatcher used to actually dispatch the event; this
69      * varies according to the type of event listener.
70      * @param clazz the class associated with the target event listener type;
71      * only event listeners of this type will receive the event.
72      * @param eventObj the actual event object to dispatch.
73     **/

74     public void dispatch(Dispatcher dispatcher, Class JavaDoc clazz, EventObject JavaDoc eventObj)
75     {
76         Object JavaDoc[] listeners = getListeners();
77
78         // If this is an event for service listeners, then dispatch it
79
// immediately since service events are never asynchronous.
80
if ((clazz == ServiceListener.class) && (listeners.length > 0))
81         {
82             // Notify appropriate listeners.
83
for (int i = listeners.length - 2; i >= 0; i -= 2)
84             {
85                 // If the original listener is a synchronous bundle listener
86
// or a service listener, then dispatch event immediately
87
// per the specification.
88
ListenerWrapper lw = (ListenerWrapper) listeners[i + 1];
89                 if (lw.getListenerClass() == ServiceListener.class)
90                 {
91                     try {
92                         dispatcher.dispatch(
93                             (EventListener JavaDoc) lw, eventObj);
94                     } catch (Throwable JavaDoc th) {
95                         Oscar.error("OscarDispatchQueue: Error during dispatch.", th);
96                     }
97                 }
98             }
99         }
100         // Dispatch bundle events to synchronous bundle listeners immediately,
101
// but deliver to standard bundle listeners asynchronously.
102
else if ((clazz == BundleListener.class) && (listeners.length > 0))
103         {
104             // Notify appropriate listeners.
105
for (int i = listeners.length - 2; i >= 0; i -= 2)
106             {
107                 // If the original listener is a synchronous bundle listener,
108
// then dispatch event immediately per the specification.
109
ListenerWrapper lw = (ListenerWrapper) listeners[i + 1];
110                 if (lw.getListenerClass() == SynchronousBundleListener.class)
111                 {
112                     try {
113                         dispatcher.dispatch(
114                             (EventListener JavaDoc) lw, eventObj);
115                     } catch (Throwable JavaDoc th) {
116                         Oscar.error("OscarDispatchQueue: Error during dispatch.", th);
117                     }
118                 }
119             }
120
121             // Wrap the dispatcher so that it ignores synchronous
122
// bundle listeners since they have already been dispatched.
123
IgnoreSynchronousDispatcher ignoreDispatcher = new IgnoreSynchronousDispatcher();
124             ignoreDispatcher.setDispatcher(dispatcher);
125
126             // Dispatch the bundle listener asynchronously.
127
dispatch(listeners, ignoreDispatcher, clazz, eventObj);
128         }
129         // All other events are dispatched asynchronously.
130
else
131         {
132             dispatch(listeners, dispatcher, clazz, eventObj);
133         }
134     }
135
136     private static class IgnoreSynchronousDispatcher implements Dispatcher
137     {
138         private Dispatcher m_dispatcher = null;
139
140         public void setDispatcher(Dispatcher dispatcher)
141         {
142             m_dispatcher = dispatcher;
143         }
144
145         public void dispatch(EventListener JavaDoc l, EventObject JavaDoc eventObj)
146         {
147             if (l instanceof ListenerWrapper)
148             {
149                 ListenerWrapper lw = (ListenerWrapper) l;
150                 // Do not dispatch events to synchronous listeners,
151
// since they are dispatched immediately above.
152
if (!(lw.getListenerClass() == SynchronousBundleListener.class))
153                 {
154                     m_dispatcher.dispatch(l, eventObj);
155                 }
156             }
157         }
158     }
159 }
Popular Tags