KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > connector > MulticastCallerListener


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: MulticastCallerListener.java,v 1.2 2005/05/27 13:58:00 tanderson Exp $
44  */

45 package org.exolab.jms.net.connector;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.Collections JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52
53 import org.exolab.jms.net.uri.InvalidURIException;
54 import org.exolab.jms.net.uri.URI;
55 import org.exolab.jms.net.uri.URIHelper;
56
57
58 /**
59  * A listener for {@link Caller} events, which asynchronously propagates events
60  * to one or more registered listeners.
61  *
62  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
63  * @version $Revision: 1.2 $ $Date: 2005/05/27 13:58:00 $
64  */

65 public class MulticastCallerListener implements CallerListener {
66
67     /**
68      * The listeners. A set of <code>List</code> instances, keyed on
69      * URI.
70      */

71     private Map JavaDoc _listeners = Collections.synchronizedMap(new HashMap JavaDoc());
72
73     /**
74      * Helper for casts.
75      */

76     private static final CallerListener[] EMPTY = new CallerListener[0];
77
78
79     /**
80      * Registers an event listener for caller events.
81      *
82      * @param uri the remote URI to listen for events on
83      * @param listener the event listener
84      * @throws InvalidURIException if <code>uri</code> is invalid
85      */

86     public void addCallerListener(String JavaDoc uri, CallerListener listener)
87         throws InvalidURIException{
88         URI parsed = URIHelper.parse(uri);
89         parsed = URIHelper.convertHostToAddress(parsed);
90         List JavaDoc list = null;
91         synchronized (_listeners) {
92             list = (List JavaDoc) _listeners.get(parsed);
93             if (list == null) {
94                 list = Collections.synchronizedList(new ArrayList JavaDoc());
95                 _listeners.put(parsed, list);
96             }
97         }
98         list.add(listener);
99     }
100
101     /**
102      * Removes an event listener for caller events.
103      *
104      * @param uri the remote URI to remove the listener for
105      * @param listener the event listener to remove
106      * @throws InvalidURIException if <code>uri</code> is invalid
107      */

108     public void removeCallerListener(String JavaDoc uri, CallerListener listener)
109         throws InvalidURIException {
110         URI parsed = URIHelper.parse(uri);
111         parsed = URIHelper.convertHostToAddress(parsed);
112         List JavaDoc list = null;
113         synchronized (_listeners) {
114             list = (List JavaDoc) _listeners.get(parsed);
115             if (list != null) {
116                 list.remove(listener);
117             }
118         }
119     }
120
121     /**
122      * Notifies that a connection has been disconnected.
123      *
124      * @param caller the caller that was disconnected
125      */

126     public void disconnected(Caller caller) {
127         CallerListener[] listeners = getListeners(caller.getRemoteURI());
128         for (int i = 0; i < listeners.length; ++i) {
129             listeners[i].disconnected(caller);
130         }
131     }
132
133     /**
134      * Returns the registered listeners for a given URI.
135      *
136      * @param uri the URI to return the registered listsners for
137      * @return the registered listeners
138      */

139     private CallerListener[] getListeners(URI uri) {
140         CallerListener[] result = EMPTY;
141         List JavaDoc list = null;
142         synchronized (_listeners) {
143             list = (List JavaDoc) _listeners.get(uri);
144             if (list == null) {
145                 list = (List JavaDoc) _listeners.get(uri);
146             }
147         }
148         if (list != null) {
149             result = (CallerListener[]) list.toArray(EMPTY);
150         }
151         return result;
152     }
153 }
154
Popular Tags