1 45 package org.exolab.jms.server.net; 46 47 import java.rmi.RemoteException ; 48 import java.rmi.server.ExportException ; 49 import java.util.ArrayList ; 50 import java.util.Collections ; 51 import java.util.List ; 52 import javax.jms.InvalidClientIDException ; 53 import javax.jms.JMSException ; 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 74 public class RemoteServerConnection 75 extends UnicastObject 76 implements ServerConnection, CallerListener { 77 78 81 private ServerConnection _connection; 82 83 86 private final String _uri; 87 88 91 private List _sessions = Collections.synchronizedList(new ArrayList ()); 92 93 96 private static final Log _log 97 = LogFactory.getLog(RemoteServerConnection.class); 98 99 100 107 public RemoteServerConnection(ServerConnection connection, ORB orb) 108 throws RemoteException { 109 super(orb, null, true); 110 if (connection == null) { 111 throw new IllegalArgumentException ("Argument 'connection' is null"); 112 } 113 Caller caller = orb.getCaller(); 114 if (caller == null) { 115 throw new ExportException ("Can't determine remote caller"); 116 } 117 _uri = caller.getRemoteURI().toString(); 118 orb.addCallerListener(_uri, this); 119 _connection = connection; 120 } 121 122 128 public long getConnectionId() throws JMSException { 129 return _connection.getConnectionId(); 130 } 131 132 138 public String getClientID() throws JMSException { 139 return _connection.getClientID(); 140 } 141 142 156 public void setClientID(String clientID) throws JMSException { 157 _connection.setClientID(clientID); 158 } 159 160 173 public ServerSession createSession(int acknowledgeMode, boolean transacted) 174 throws JMSException { 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 exception) { 182 throw new JMSException (exception.getMessage()); 183 } 184 return (ServerSession) remote.getProxy(); 185 } 186 187 192 public void close() throws JMSException { 193 JMSException rethrow = null; 194 197 RemoteServerSession[] sessions = (RemoteServerSession[]) 199 _sessions.toArray(new RemoteServerSession[0]); 200 if (sessions.length != 0) { 201 _log.debug("Cleaning up active sessions"); 204 for (int i = 0; i < sessions.length; ++i) { 205 try { 206 sessions[i].close(); 207 } catch (JMSException 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 exception) { 223 throw new JMSException (exception.getMessage()); 224 } 225 } 226 if (rethrow != null) { 227 throw rethrow; 228 } 229 } 230 231 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 exception) { 245 _log.debug("Failed to clean up resources of caller=" 246 + caller.getRemoteURI(), exception); 247 } 248 249 } 250 251 256 public void closed(RemoteServerSession session) { 257 _sessions.remove(session); 258 } 259 } 260 | Popular Tags |