View Javadoc

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-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: IpcJmsReceiver.java,v 1.8 2003/08/17 01:32:26 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * $Date	    jimm    Created
47   */
48  
49  package org.exolab.jms.server.mipc;
50  
51  import java.io.Serializable;
52  import java.util.HashMap;
53  import java.util.Vector;
54  
55  import org.exolab.core.ipc.NotifierIfc;
56  
57  
58  /***
59   * The receiver class handles all requests, delegating them to registered
60   * {@link NotifierIfc} instances.
61   *
62   * @version     $Revision: 1.8 $ $Date: 2003/08/17 01:32:26 $
63   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
64   * @see         org.exolab.core.ipc.NotifierIfc
65   * @see         org.exolab.jms.server.mipc.IpcJmsServer
66   */
67  public class IpcJmsReceiver implements NotifierIfc {
68  
69      /***
70       * The set of registered dispatchers
71       */
72      private HashMap _dispatchers = new HashMap();
73  
74  
75      /***
76       * Construct a new <code>IpcJmsReceiver</code>
77       */
78      public IpcJmsReceiver() {
79      }
80  
81      /***
82       * A new request has been received.
83       * Carry out the request, and pass back any relevent data.
84       *
85       * @param object the request
86       * @param id the unique identifier of this connection.
87       * @return the response. This must never be null.
88       */
89      public Serializable notify(Object object, String id) {
90          Vector request = (Vector) object;
91          Serializable result;
92  
93          String name = (String) request.get(0);
94          NotifierIfc dispatcher = getDispatcher(name);
95          if (dispatcher == null) {
96              String error = "No dispatcher registered to handle messages " +
97                  "for: " + name;
98              result = pack(Boolean.FALSE, error);
99          } else {
100             result = dispatcher.notify(object, id);
101         }
102         return result;
103     }
104 
105     /***
106      * Register a dispatcher to handle requests
107      *
108      * @param name the name of the dispatcher
109      * @param dispatcher the dispatcher
110      */
111     public synchronized void addDispatcher(String name,
112                                            NotifierIfc dispatcher) {
113         _dispatchers.put(name, dispatcher);
114     }
115 
116     /***
117      * Returns a dispatcher for the specified name
118      *
119      * @param name the name of the dipatcher
120      * @return a dispatcher corresponding to <code>name</code>, or
121      * <code>null</code> if no dispatcher is registered
122      */
123     public synchronized NotifierIfc getDispatcher(String name) {
124         return (NotifierIfc) _dispatchers.get(name);
125     }
126 
127     /***
128      * Invoked if the connection has been broken.
129      * This notifies all registered dispatchers, before removing them.
130      *
131      * @param id the unique identifier of this connection.
132      */
133     public void disconnection(String id) {
134         NotifierIfc[] dispatchers;
135         synchronized (_dispatchers) {
136             dispatchers = (NotifierIfc[]) _dispatchers.values().toArray(
137                 new NotifierIfc[0]);
138         }
139         for (int i = 0; i < dispatchers.length; ++i) {
140             dispatchers[i].disconnection(id);
141         }
142 
143         // clean up the dispatchers
144         synchronized (_dispatchers) {
145             _dispatchers.clear();
146         }
147     }
148 
149 
150     /***
151      * Pack all the data that is required by the server in a vector.
152      * Set the size of the vector to be exactly the right size for efficiency.
153      *
154      * @param success Boolean indicating success or failure of request.
155      * @param ob The Object being returned.
156      * @return Vector The vector containing all the data.
157      */
158     private Vector pack(Boolean success, Object ob) {
159         Vector v = new Vector(2);
160         v.add(success);
161         v.add(ob);
162         return v;
163     }
164 
165 } //-- IpcJmsReceiver