KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > server > net > RemoteServerConnection


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 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: RemoteServerConnection.java,v 1.2 2005/06/13 23:05:45 tanderson Exp $
44  */

45 package org.exolab.jms.server.net;
46
47 import java.rmi.RemoteException JavaDoc;
48 import java.rmi.server.ExportException JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Collections JavaDoc;
51 import java.util.List JavaDoc;
52 import javax.jms.InvalidClientIDException JavaDoc;
53 import javax.jms.JMSException JavaDoc;
54
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57
58 import org.exolab.jms.net.connector.Caller;
59 import org.exolab.jms.net.connector.CallerListener;
60 import org.exolab.jms.net.orb.ORB;
61 import org.exolab.jms.net.orb.UnicastObject;
62 import org.exolab.jms.server.ServerConnection;
63 import org.exolab.jms.server.ServerSession;
64
65
66 /**
67  * Implementation of the {@link ServerConnection{ interface which wraps an
68  * {@link ServerConnection} to make it remotable.
69  *
70  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
71  * @version $Revision: 1.2 $ $Date: 2005/06/13 23:05:45 $
72  * @see JmsServerConnection
73  */

74 public class RemoteServerConnection
75         extends UnicastObject
76         implements ServerConnection, CallerListener {
77
78     /**
79      * The connection to delegate calls to.
80      */

81     private ServerConnection _connection;
82
83     /**
84      * The URI of the remote caller.
85      */

86     private final String JavaDoc _uri;
87
88     /**
89      * The set of {@link RemoteServerSession} instances created by this.
90      */

91     private List JavaDoc _sessions = Collections.synchronizedList(new ArrayList JavaDoc());
92
93     /**
94      * The logger.
95      */

96     private static final Log _log
97             = LogFactory.getLog(RemoteServerConnection.class);
98
99
100     /**
101      * Construct a new <code>RemoteServerConnection</code>.
102      *
103      * @param connection the connection to delegate calls to
104      * @param orb the ORB to export this with
105      * @throws RemoteException if this can't be exported
106      */

107     public RemoteServerConnection(ServerConnection connection, ORB orb)
108             throws RemoteException JavaDoc {
109         super(orb, null, true);
110         if (connection == null) {
111             throw new IllegalArgumentException JavaDoc("Argument 'connection' is null");
112         }
113         Caller caller = orb.getCaller();
114         if (caller == null) {
115             throw new ExportException JavaDoc("Can't determine remote caller");
116         }
117         _uri = caller.getRemoteURI().toString();
118         orb.addCallerListener(_uri, this);
119         _connection = connection;
120     }
121
122     /**
123      * Returns the connection identifier.
124      *
125      * @return the connection identifier
126      * @throws JMSException for any JMS error
127      */

128     public long getConnectionId() throws JMSException JavaDoc {
129         return _connection.getConnectionId();
130     }
131
132     /**
133      * Returns the client identifier.
134      *
135      * @return the client identifier
136      * @throws JMSException for any JMS error
137      */

138     public String JavaDoc getClientID() throws JMSException JavaDoc {
139         return _connection.getClientID();
140     }
141
142     /**
143      * Sets the client identifier for this connection.
144      *
145      * @param clientID the unique client identifier
146      * @throws JMSException if the JMS provider fails to set the
147      * client ID for this connection due to
148      * some internal error.
149      * @throws InvalidClientIDException if the JMS client specifies an invalid
150      * or duplicate client ID.
151      * @throws IllegalStateException if the JMS client attempts to set a
152      * connection's client ID at the wrong time
153      * or when it has been administratively
154      * configured.
155      */

156     public void setClientID(String JavaDoc clientID) throws JMSException JavaDoc {
157         _connection.setClientID(clientID);
158     }
159
160     /**
161      * Create a new session.
162      *
163      * @param transacted indicates whether the session is transacted
164      * @param acknowledgeMode indicates whether the consumer or the client will
165      * acknowledge any messages it receives; ignored if
166      * the session is transacted. Legal values are
167      * <code>Session.AUTO_ACKNOWLEDGE</code>,
168      * <code>Session.CLIENT_ACKNOWLEDGE</code>, and
169      * <code>Session.DUPS_OK_ACKNOWLEDGE</code>.
170      * @return a newly created session
171      * @throws JMSException for any JMS error
172      */

173     public ServerSession createSession(int acknowledgeMode, boolean transacted)
174             throws JMSException JavaDoc {
175         ServerSession session = _connection.createSession(acknowledgeMode,
176                                                           transacted);
177         RemoteServerSession remote = null;
178         try {
179             remote = new RemoteServerSession(getORB(), this, session);
180             _sessions.add(remote);
181         } catch (RemoteException JavaDoc exception) {
182             throw new JMSException JavaDoc(exception.getMessage());
183         }
184         return (ServerSession) remote.getProxy();
185     }
186
187     /**
188      * Closes the connection.
189      *
190      * @throws JMSException for any JMS error
191      */

192     public void close() throws JMSException JavaDoc {
193         JMSException JavaDoc rethrow = null;
194         // Need to catch exceptions and propagate last, to ensure all objects
195
// are cleaned up and unexported.
196

197         // Clean up sessions.
198
RemoteServerSession[] sessions = (RemoteServerSession[])
199                 _sessions.toArray(new RemoteServerSession[0]);
200         if (sessions.length != 0) {
201             // There will only be sessions present if the client has disconnected
202
// without closing its connection.
203
_log.debug("Cleaning up active sessions");
204             for (int i = 0; i < sessions.length; ++i) {
205                 try {
206                     sessions[i].close();
207                 } catch (JMSException JavaDoc exception) {
208                     rethrow = exception;
209                     if (_log.isDebugEnabled()) {
210                         _log.debug("Exception while cleaning up session",
211                                    exception);
212                     }
213                 }
214             }
215         }
216         try {
217             _connection.close();
218         } finally {
219             try {
220                 getORB().removeCallerListener(_uri, this);
221                 unexportObject();
222             } catch (RemoteException JavaDoc exception) {
223                 throw new JMSException JavaDoc(exception.getMessage());
224             }
225         }
226         if (rethrow != null) {
227             throw rethrow;
228         }
229     }
230
231     /**
232      * Notifies that a caller has been disconnected. This implementation invokes
233      * {@link #close}.
234      *
235      * @param caller the caller that was disconnected
236      */

237     public void disconnected(Caller caller) {
238         if (_log.isDebugEnabled()) {
239             _log.debug("Detected disconnection of caller="
240                        + caller.getRemoteURI() + ". Cleaning up resources");
241         }
242         try {
243             close();
244         } catch (JMSException JavaDoc exception) {
245             _log.debug("Failed to clean up resources of caller="
246                        + caller.getRemoteURI(), exception);
247         }
248
249     }
250
251     /**
252      * Notify closure of a session.
253      *
254      * @param session the closed session
255      */

256     public void closed(RemoteServerSession session) {
257         _sessions.remove(session);
258     }
259 }
260
Popular Tags