1 45 package org.exolab.jms.client.net; 46 47 import java.rmi.AccessException ; 48 import java.rmi.NotBoundException ; 49 import java.rmi.RemoteException ; 50 import java.util.HashMap ; 51 import java.util.Map ; 52 import javax.jms.ExceptionListener ; 53 import javax.jms.InvalidClientIDException ; 54 import javax.jms.JMSException ; 55 import javax.jms.JMSSecurityException ; 56 57 import org.exolab.jms.client.JmsServerStubIfc; 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.registry.Registry; 62 import org.exolab.jms.server.ServerConnection; 63 import org.exolab.jms.server.ServerConnectionFactory; 64 65 66 74 public class JmsServerStubImpl implements JmsServerStubIfc, CallerListener { 75 76 79 private ORB _orb; 80 81 84 private final Map _properties; 85 86 89 private final String _serverURI; 90 91 94 private final String _defaultUser; 95 96 99 private final String _defaultPassword; 100 101 105 private ExceptionListener _listener = null; 106 107 108 114 public JmsServerStubImpl(Map properties, Map environment) { 115 if (properties == null) { 116 throw new IllegalArgumentException ("Argument 'properties' is null"); 117 } 118 _properties = properties; 119 120 _serverURI = (String ) properties.get(ORB.PROVIDER_URI); 121 if (_serverURI == null) { 122 throw new IllegalArgumentException ( 123 "Argument 'properties' does not contain property " 124 + ORB.PROVIDER_URI); 125 } 126 if (environment != null) { 127 _defaultUser = (String ) environment.get(ORB.SECURITY_PRINCIPAL); 128 _defaultPassword = (String ) environment.get( 129 ORB.SECURITY_CREDENTIALS); 130 } else { 131 _defaultUser = null; 132 _defaultPassword = null; 133 } 134 } 135 136 158 public ServerConnection createConnection(String clientID, String user, 159 String password) 160 throws JMSException { 161 ServerConnection stub; 162 163 if (user == null) { 164 user = _defaultUser; 165 password = _defaultPassword; 166 } 167 168 ServerConnectionFactory factory 169 = getServerConnectionFactory(user, password); 170 171 ServerConnection connection 172 = factory.createConnection(clientID, user, password); 173 stub = new JmsConnectionStubImpl(this, connection, _orb, 174 _serverURI, user, password); 175 return stub; 176 } 177 178 184 public void setExceptionListener(ExceptionListener listener) { 185 _listener = listener; 186 } 187 188 193 public void disconnected(Caller caller) { 194 if (_listener != null) { 195 _listener.onException(new JMSException ("Lost connection")); 196 } 197 } 198 199 208 private synchronized ServerConnectionFactory getServerConnectionFactory( 209 String user, String password) 210 throws JMSException { 211 ServerConnectionFactory factory = null; 212 Map properties = _properties; 213 214 if (user != null) { 215 properties = new HashMap (_properties); 216 properties.put(ORB.SECURITY_PRINCIPAL, user); 217 properties.put(ORB.SECURITY_CREDENTIALS, password); 218 } 219 Registry registry = null; 220 try { 221 if (_orb == null) { 222 _orb = SharedORB.getInstance(); 223 } 224 registry = _orb.getRegistry(properties); 225 } catch (AccessException exception) { 226 JMSSecurityException error = new JMSSecurityException ( 227 exception.getMessage()); 228 error.setLinkedException(exception); 229 throw error; 230 } catch (RemoteException exception) { 231 JMSException error = new JMSException ( 232 "Failed to get registry service for URL: " + _serverURI); 233 error.setLinkedException(exception); 234 throw error; 235 } 236 237 try { 238 factory = (ServerConnectionFactory) registry.lookup("server"); 239 } catch (NotBoundException exception) { 240 throw new JMSException ("Server is not bound in the registry for URL: " 241 + _serverURI); 242 } catch (RemoteException exception) { 243 JMSException error = new JMSException ( 244 "Failed to lookup OpenJMS server for URL: " + _serverURI); 245 error.setLinkedException(exception); 246 throw error; 247 } 248 try { 249 _orb.addCallerListener(_serverURI, this); 250 } catch (RemoteException exception) { 251 JMSException error = new JMSException ( 252 "Failed to register for disconnection notification for " 253 + "URL: " + _serverURI); 254 error.setLinkedException(exception); 255 throw error; 256 } 257 return factory; 258 } 259 260 } 261 | Popular Tags |