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 jima@intalio.com.
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 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: TestAcceptorEventListener.java,v 1.4 2005/04/19 12:31:20 tanderson Exp $
44   */
45  package org.exolab.jms.net.connector;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.Iterator;
50  import java.util.List;
51  
52  
53  /***
54   * A test listener for {@link ManagedConnectionAcceptor} events.
55   *
56   * @version     $Revision: 1.4 $ $Date: 2005/04/19 12:31:20 $
57   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
58   */
59  public class TestAcceptorEventListener
60          implements ManagedConnectionAcceptorListener {
61  
62      /***
63       * The set of active connections.
64       */
65      private List _connections = Collections.synchronizedList(new ArrayList());
66  
67      /***
68       * The set of connection errors.
69       */
70      private List _errors = new ArrayList();
71  
72      /***
73       * The invocation handler to register on accepted connections.
74       * If <code>null</code>, no handler will be registered.
75       */
76      private InvocationHandler _handler = null;
77  
78  
79      /***
80       * Construct a new <code>TestAcceptorEventListener</code>.
81       */
82      public TestAcceptorEventListener() {
83          this(null);
84      }
85  
86      /***
87       * Construct a new <code>TestAcceptorEventListener</code>.
88       *
89       * @param handler the invocation handler to register on accepted
90       * connections. If <code>null</code>, no handler will be registered
91       */
92      public TestAcceptorEventListener(InvocationHandler handler) {
93          _handler = handler;
94      }
95  
96      /***
97       * Invoked when a new connection is accepted.
98       *
99       * @param acceptor the acceptor which received the connection
100      * @param connection the accepted connection
101      */
102     public void accepted(ManagedConnectionAcceptor acceptor,
103                          ManagedConnection connection) {
104         if (_handler != null) {
105             try {
106                 connection.setInvocationHandler(_handler);
107                 _connections.add(connection);
108             } catch (ResourceException exception) {
109                 _errors.add(exception);
110                 try {
111                     connection.destroy();
112                 } catch (ResourceException ignore) {
113                     // no-op
114                 }
115             }
116         } else {
117             _connections.add(connection);
118         }
119     }
120 
121     /***
122      * Invoked when the acceptor receives an error.
123      *
124      * @param acceptor the acceptor generating the event
125      * @param throwable the error
126      */
127     public void error(ManagedConnectionAcceptor acceptor,
128                       Throwable throwable) {
129         _errors.add(throwable);
130         try {
131             acceptor.close();
132         } catch (ResourceException ignore) {
133         }
134     }
135 
136     /***
137      * Returns the set of active connections.
138      *
139      * @return a list of <code>ManagedConnection</code> instances
140      */
141     public List getConnections() {
142         return _connections;
143     }
144 
145     /***
146      * Returns the first active connection.
147      *
148      * @return the first active connection, or <code>null</code>
149      * if no connection has been accepted
150      */
151     public ManagedConnection getConnection() {
152         ManagedConnection result = null;
153         synchronized (_connections) {
154             if (!_connections.isEmpty()) {
155                 result = (ManagedConnection) _connections.get(0);
156             }
157         }
158         return result;
159     }
160 
161     /***
162      * Returns any errors raised by the connection acceptor, or during
163      * the handling of accepted connections.
164      *
165      * @return a list of <code>Throwable</code> instances
166      */
167     public List getErrors() {
168         return _errors;
169     }
170 
171     /***
172      * Destroys all accepted connections.
173      *
174      * @throws ResourceException if a connecion can't be destroyed
175      */
176     public void destroy() throws ResourceException {
177         Iterator iterator = _connections.iterator();
178         while (iterator.hasNext()) {
179             ManagedConnection connection = (ManagedConnection) iterator.next();
180             connection.destroy();
181         }
182     }
183 
184 }