1 43 package org.exolab.jms.persistence; 44 45 import java.sql.Connection ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.Vector ; 51 52 import org.exolab.jms.client.JmsDestination; 53 import org.exolab.jms.client.JmsQueue; 54 import org.exolab.jms.client.JmsTopic; 55 56 57 66 class Destinations { 67 68 71 private HashMap _destinations; 72 73 76 private HashMap _ids; 77 78 81 private static Destinations _instance; 82 83 86 private static final Object _block = new Object (); 87 88 92 private static final String DESTINATION_ID_SEED = "destinationId"; 93 94 99 private Destinations() 100 throws PersistenceException { 101 102 _destinations = new HashMap (); 103 _ids = new HashMap (); 104 } 105 106 114 public static Destinations instance() { 115 return _instance; 116 } 117 118 125 public static Destinations initialise(Connection connection) 126 throws PersistenceException { 127 128 if (_instance == null) { 129 synchronized (_block) { 130 if (_instance == null) { 131 _instance = new Destinations(); 132 _instance.load(connection); 133 } 134 } 135 } 136 return _instance; 137 } 138 139 147 public synchronized void add(Connection connection, 148 JmsDestination destination) 149 throws PersistenceException { 150 151 PreparedStatement insert = null; 152 try { 153 long Id = SeedGenerator.instance().next(connection, 154 DESTINATION_ID_SEED); 155 boolean isQueue = (destination instanceof JmsQueue); 156 157 insert = connection.prepareStatement( 158 "insert into destinations (name, isqueue, destinationid) " 159 + "values (?, ?, ?)"); 160 insert.setString(1, destination.getName()); 161 insert.setBoolean(2, isQueue); 162 insert.setLong(3, Id); 163 insert.executeUpdate(); 164 cache(destination, Id); 165 } catch (Exception error) { 166 throw new PersistenceException("Destinations.add failed with " + 167 error.toString()); 168 } finally { 169 SQLHelper.close(insert); 170 } 171 } 172 173 182 public synchronized boolean remove(Connection connection, 183 JmsDestination destination) 184 throws PersistenceException { 185 186 boolean success = false; 187 PreparedStatement deleteDestinations = null; 188 PreparedStatement deleteMessages = null; 189 PreparedStatement deleteConsumers = null; 190 PreparedStatement deleteMessageHandles = null; 191 192 Pair pair = (Pair) _destinations.get(destination.getName()); 193 if (pair != null) { 194 try { 195 deleteDestinations = connection.prepareStatement( 196 "delete from destinations where name=?"); 197 deleteDestinations.setString(1, destination.getName()); 198 199 deleteMessages = connection.prepareStatement( 200 "delete from messages where destinationId=?"); 201 deleteMessages.setLong(1, pair.Id); 202 203 deleteMessageHandles = connection.prepareStatement( 204 "delete from message_handles where destinationId=?"); 205 deleteMessageHandles.setLong(1, pair.Id); 206 207 deleteConsumers = connection.prepareStatement( 208 "delete from consumers where destinationId=?"); 209 deleteConsumers.setLong(1, pair.Id); 210 211 212 deleteDestinations.executeUpdate(); 213 deleteMessages.executeUpdate(); 214 deleteMessageHandles.executeUpdate(); 215 deleteConsumers.executeUpdate(); 216 217 Consumers.instance().removeCached(pair.Id); 218 _destinations.remove(destination.getName()); 219 _ids.remove(new Long (pair.Id)); 220 success = true; 221 } catch (Exception error) { 222 throw new PersistenceException("Destinations.remove failed " + 223 error.toString()); 224 } finally { 225 SQLHelper.close(deleteDestinations); 226 SQLHelper.close(deleteMessages); 227 SQLHelper.close(deleteConsumers); 228 SQLHelper.close(deleteMessageHandles); 229 } 230 } 231 232 return success; 233 } 234 235 241 public synchronized JmsDestination get(String name) { 242 Pair pair = (Pair) _destinations.get(name); 243 return (pair != null) ? pair.destination : null; 244 } 245 246 252 public synchronized JmsDestination get(long id) { 253 Pair pair = (Pair) _ids.get(new Long (id)); 254 return (pair != null) ? pair.destination : null; 255 } 256 257 263 public synchronized long getId(String name) { 264 Pair pair = (Pair) _destinations.get(name); 265 return (pair != null) ? pair.Id : 0; 266 } 267 268 273 public synchronized Vector getNames() { 274 Vector result = new Vector (_destinations.size()); 276 Iterator iter = _destinations.keySet().iterator(); 277 while (iter.hasNext()) { 278 result.add((String ) iter.next()); 279 } 280 281 return result; 282 } 283 284 289 public synchronized Vector getDestinations() { 290 Vector result = new Vector (_destinations.size()); 292 Iterator iter = _destinations.values().iterator(); 293 while (iter.hasNext()) { 294 result.add(((Pair) iter.next()).destination); 295 } 296 297 return result; 298 } 299 300 303 public synchronized void close() { 304 _destinations.clear(); 305 _destinations = null; 306 307 _ids.clear(); 308 _ids = null; 309 310 _instance = null; 311 } 312 313 320 private void load(Connection connection) 321 throws PersistenceException { 322 323 PreparedStatement select = null; 324 ResultSet set = null; 325 try { 326 select = connection.prepareStatement( 327 "select name, isqueue, destinationid from destinations"); 328 329 set = select.executeQuery(); 330 String name = null; 331 boolean isQueue = false; 332 JmsDestination destination = null; 333 long Id = 0; 334 while (set.next()) { 335 name = set.getString(1); 336 isQueue = set.getBoolean(2); 337 destination = (isQueue) 338 ? (JmsDestination) new JmsQueue(name) 339 : (JmsDestination) new JmsTopic(name); 340 Id = set.getLong(3); 341 destination.setPersistent(true); 342 cache(destination, Id); 343 } 344 } catch (Exception exception) { 345 throw new PersistenceException("Failed to load destinations", 346 exception); 347 } finally { 348 SQLHelper.close(set); 349 SQLHelper.close(select); 350 } 351 } 352 353 359 private void cache(JmsDestination destination, long Id) { 360 Pair pair = new Pair(destination, Id); 361 362 _destinations.put(destination.getName(), pair); 363 _ids.put(new Long (Id), pair); 364 } 365 366 367 371 private static class Pair { 372 373 public Pair(JmsDestination destination, long Id) { 374 this.destination = destination; 375 this.Id = Id; 376 } 377 378 public JmsDestination destination; 379 public long Id; 380 } 381 } 382 | Popular Tags |