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-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: IpcJmsSessionList.java,v 1.6 2003/08/07 13:33:09 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * $Date	    jimm    Created
47   */
48  
49  
50  package org.exolab.jms.server.mipc;
51  
52  import java.io.IOException;
53  import java.util.HashMap;
54  import java.util.Iterator;
55  
56  import org.exolab.core.mipc.MultiplexConnectionIfc;
57  import org.exolab.core.mipc.ObjectChannel;
58  import org.exolab.jms.server.JmsServerSession;
59  
60  
61  /***
62   * Keep a list of all session Client connections for a client. All sessions
63   * for a client are multiplexed through a single connection. Ensure the
64   * same connection is used for all the clients sessions, and that the
65   * connection is not destroyed until the last session for the client is
66   * shutdown.
67   *
68   * @version     $Revision: 1.6 $ $Date: 2003/08/07 13:33:09 $
69   * @author      <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
70   * @see         org.exolab.jms.server.mipc.IpcJmsServer
71   * @see		org.exolab.jms.server.mipc.IpcJmsSessionSender
72   */
73  public class IpcJmsSessionList {
74  
75      /***
76       * The client connection.
77       */
78      private ObjectChannel _client;
79  
80      /***
81       * The list of session senders
82       */
83      private HashMap _list;
84  
85  
86      /***
87       * Create a connection to the client listener for sending JmsMessages.
88       *
89       * @param connection The MultiplexConnection between client and server
90       * @exception IOException If a connection error results.
91       *
92       */
93      public IpcJmsSessionList(MultiplexConnectionIfc connection)
94          throws IOException {
95          _list = new HashMap(10);
96          _client = new ObjectChannel("message", connection);
97      }
98  
99      /***
100      * Add a new session sender for the client. If its not already active.
101      *
102      * @param session The session to add a notifier on
103      */
104     public synchronized void add(JmsServerSession session) {
105         if (!_list.containsKey(session.getSessionId())) {
106             _list.put(session.getSessionId(),
107                 new IpcJmsSessionSender(_client, session));
108         }
109     }
110 
111     /***
112      * Remove a sesson notifier. If this is the last session for this client
113      * close the client connection.
114      *
115      * @param session The session to remove the notifier from
116      * @return boolean True there are no more connections for this client
117      *
118      */
119     public synchronized boolean remove(JmsServerSession session) {
120         IpcJmsSessionSender sender =
121             (IpcJmsSessionSender) _list.remove(session.getSessionId());
122         boolean result = false;
123 
124         if (sender != null) {
125             sender.close();
126         }
127         if (_list.isEmpty()) {
128             closeConnection();
129             _list = null;
130             result = true;
131         }
132 
133         return result;
134     }
135 
136     /***
137      * Remove all session notfiers and close the client connection.
138      */
139     public synchronized void removeAll() {
140         Iterator iterator = _list.values().iterator();
141         while (iterator.hasNext()) {
142             IpcJmsSessionSender sender = (IpcJmsSessionSender) iterator.next();
143             sender.close();
144         }
145         _list.clear();
146         closeConnection();
147         _list = null;
148     }
149 
150     // Close the connection to the client
151     private void closeConnection() {
152         if (_client != null) {
153             try {
154                 _client.close();
155             } catch (IOException ignore) {
156             }
157             _client = null;
158         }
159     }
160 
161 } //-- IpcJmsSessionList