1 28 29 package com.caucho.jms; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.jms.jdbc.JdbcManager; 33 import com.caucho.jms.jdbc.JdbcQueue; 34 import com.caucho.jms.jdbc.JdbcTopic; 35 import com.caucho.jms.memory.MemoryQueue; 36 import com.caucho.jms.memory.MemoryTopic; 37 import com.caucho.jms.session.ConnectionImpl; 38 import com.caucho.log.Log; 39 import com.caucho.management.j2ee.J2EEManagedObject; 40 import com.caucho.management.j2ee.JMSResource; 41 import com.caucho.util.L10N; 42 43 import javax.jms.Connection ; 44 import javax.jms.ConnectionFactory ; 45 import javax.jms.JMSException ; 46 import javax.jms.JMSSecurityException ; 47 import javax.jms.Queue ; 48 import javax.jms.Topic ; 49 import javax.sql.DataSource ; 50 import java.sql.SQLException ; 51 import java.util.ArrayList ; 52 import java.util.Collections ; 53 import java.util.HashMap ; 54 import java.util.List ; 55 import java.util.logging.Logger ; 56 57 60 public class ConnectionFactoryImpl implements ConnectionFactory { 61 static final Logger log = Log.open(ConnectionFactoryImpl.class); 62 static final L10N L = new L10N(ConnectionFactoryImpl.class); 63 64 private String _name; 65 private String _clientID; 66 67 private String _user; 68 private String _password; 69 70 private JdbcManager _jdbcManager; 71 72 private List <ConnectionImpl> _connections = 73 Collections.synchronizedList(new ArrayList <ConnectionImpl>()); 74 75 private HashMap <String ,Queue> _queues = 76 new HashMap <String ,Queue>(); 77 78 private HashMap <String ,Topic> _topics = 79 new HashMap <String ,Topic>(); 80 81 public ConnectionFactoryImpl() 82 { 83 } 84 85 88 public void setUser(String user) 89 { 90 _user = user; 91 } 92 93 96 public void setPassword(String password) 97 { 98 _password = password; 99 } 100 101 104 public void setName(String name) 105 { 106 _name = name; 107 } 108 109 112 public String getName() 113 { 114 return _name; 117 } 118 119 122 public void setClientID(String id) 123 { 124 _clientID = id; 125 } 126 127 130 public void setDataSource(DataSource dataSource) 131 { 132 if (_jdbcManager == null) 133 _jdbcManager = new JdbcManager(); 134 135 _jdbcManager.setDataSource(dataSource); 136 } 137 138 141 public JdbcManager getJdbcManager() 142 { 143 return new JdbcManager(); 144 } 145 146 149 public void init() 150 throws ConfigException, SQLException 151 { 152 if (_jdbcManager != null) 153 _jdbcManager.init(); 154 155 J2EEManagedObject.register(new JMSResource(this)); 156 } 157 158 161 public Connection createConnection() 162 throws JMSException 163 { 164 return createConnection(_user, _password); 165 } 166 167 175 public Connection createConnection(String username, String password) 176 throws JMSException 177 { 178 authenticate(username, password); 179 180 ConnectionImpl conn = new ConnectionImpl(this); 181 182 if (_clientID != null) { 183 if (findByClientID(_clientID) != null) 184 throw new JMSException (L.l("ClientID[{0}] is only allowed for a single connection.", 185 _clientID)); 186 conn.setClientID(_clientID); 187 } 188 189 addConnection(conn); 190 191 return conn; 192 } 193 194 protected void addConnection(ConnectionImpl conn) 195 { 196 _connections.add(conn); 197 } 198 199 202 public ConnectionImpl findByClientID(String id) 203 { 204 for (int i = 0; i < _connections.size(); i++) { 205 ConnectionImpl conn = _connections.get(i); 206 207 try { 208 if (id.equals(conn.getClientID())) 209 return conn; 210 } catch (Throwable e) { 211 } 212 } 213 214 return null; 215 } 216 217 220 public void removeConnection(ConnectionImpl conn) 221 { 222 _connections.remove(conn); 223 } 224 225 228 public Queue createQueue(String name) 229 throws JMSException 230 { 231 try { 232 synchronized (_queues) { 233 Queue queue = _queues.get(name); 234 235 if (queue != null) 236 return queue; 237 238 if (_jdbcManager != null) { 239 JdbcQueue jdbcQueue = new JdbcQueue(); 240 jdbcQueue.setJdbcManager(_jdbcManager); 241 jdbcQueue.setQueueName(name); 242 jdbcQueue.init(); 243 244 _queues.put(name, jdbcQueue); 245 246 return jdbcQueue; 247 } 248 else { 249 MemoryQueue memoryQueue = new MemoryQueue(); 250 memoryQueue.setQueueName(name); 251 252 _queues.put(name, memoryQueue); 253 254 return memoryQueue; 255 } 256 } 257 } catch (RuntimeException e) { 258 throw e; 259 } catch (Exception e) { 260 throw new JMSExceptionWrapper(e); 261 } 262 } 263 264 267 public Topic createTopic(String name) 268 throws JMSException 269 { 270 try { 271 synchronized (_topics) { 272 Topic topic = _topics.get(name); 273 274 if (topic != null) 275 return topic; 276 277 if (_jdbcManager != null) { 278 JdbcTopic jdbcTopic = new JdbcTopic(); 279 jdbcTopic.setJdbcManager(_jdbcManager); 280 jdbcTopic.setTopicName(name); 281 jdbcTopic.init(); 282 283 _topics.put(name, jdbcTopic); 284 285 return jdbcTopic; 286 } 287 else { 288 MemoryTopic memoryTopic = new MemoryTopic(); 289 memoryTopic.setTopicName(name); 290 291 _topics.put(name, memoryTopic); 292 293 return memoryTopic; 294 } 295 } 296 } catch (RuntimeException e) { 297 throw e; 298 } catch (Exception e) { 299 throw new JMSExceptionWrapper(e); 300 } 301 } 302 303 protected void authenticate(String username, String password) 304 throws JMSException 305 { 306 if (_user != null && ! _user.equals(username) || 307 _password != null && ! _password.equals(password)) { 308 throw new JMSSecurityException (L.l("'{0}' is an unknown user", 309 username)); 310 } 311 } 312 } 313 | Popular Tags |