1 23 24 25 26 31 32 package com.sun.jdo.spi.persistence.support.ejb.cmp; 33 34 import java.util.*; 35 36 import javax.ejb.EJBLocalObject ; 37 38 import com.sun.jdo.api.persistence.support.PersistenceManager; 39 import com.sun.jdo.api.persistence.support.Transaction; 40 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.JDOEJB20Helper; 41 import com.sun.jdo.spi.persistence.utility.logging.Logger; 42 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperSQLStore; 43 49 public class EJBHashSet extends HashSet { 50 private PersistenceManager pm = null; 53 private Transaction tx = null; 54 55 private HashSet pcSet = null; 57 58 private JDOEJB20Helper helper = null; 61 62 private boolean valid = false; 64 65 private static Logger logger = LogHelperSQLStore.getLogger(); 67 68 74 public EJBHashSet(PersistenceManager pm, JDOEJB20Helper helper, Collection pcs) { 75 this.pm = pm; 76 tx = pm.currentTransaction(); 77 this.helper = helper; 78 if (logger.isLoggable(Logger.FINEST)) { 79 logger.finest("---EJBHashSet.new--- " + ((pcs == null)? -1: pcs.size())); 81 } 82 83 setSCOHashSet(pcs); 85 86 valid = true; 87 } 88 89 91 100 public boolean add(Object o) { 101 logger.finest("---EJBHashSet.add---"); assertIsValid(); 103 assertInTransaction(); 104 helper.assertInstanceOfLocalInterfaceImpl(o); 105 Object pc = helper.convertEJBLocalObjectToPC((EJBLocalObject ) o, pm, true); 106 return pcSet.add(pc); 107 } 108 109 110 122 public boolean addAll(Collection c) { 123 logger.finest("---EJBHashSet.addAll---"); assertIsValid(); 125 assertInTransaction(); 126 assertInstancesOfLocalInterfaceImpl(c); 127 return pcSet.addAll(helper.convertCollectionEJBLocalObjectToPC(c, pm, true)); 128 } 129 130 137 public boolean remove(Object o) { 138 logger.finest("---EJBHashSet.remove---"); assertIsValid(); 140 assertInTransaction(); 141 helper.assertInstanceOfLocalInterfaceImpl(o); 142 EJBLocalObject lo = (EJBLocalObject ) o; 143 return pcSet.remove(helper.convertEJBLocalObjectToPC(lo, pm, true)); 144 } 145 146 162 public boolean removeAll(Collection c) { 163 logger.finest("---EJBHashSet.removeAll---"); assertIsValid(); 165 assertInTransaction(); 166 assertInstancesOfLocalInterfaceImpl(c); 167 return pcSet.removeAll(helper.convertCollectionEJBLocalObjectToPC(c, pm, true)); 168 } 169 170 183 public boolean retainAll(Collection c) { 184 logger.finest("---EJBHashSet.retainAll---"); assertIsValid(); 186 assertInTransaction(); 187 assertInstancesOfLocalInterfaceImpl(c); 188 return pcSet.retainAll(helper.convertCollectionEJBLocalObjectToPC(c, pm, true)); 189 } 190 191 192 196 public void clear() { 197 logger.finest("---EJBHashSet.clear---"); assertIsValid(); 199 assertInTransaction(); 200 pcSet.clear(); 201 } 202 203 208 public int size() { 209 logger.finest("---EJBHashSet.size---"); assertIsValid(); 211 assertInTransaction(); 212 return pcSet.size(); 213 } 214 215 220 public boolean isEmpty() { 221 logger.finest("---EJBHashSet.isEmpty---"); assertIsValid(); 223 assertInTransaction(); 224 return pcSet.isEmpty(); 225 } 226 227 233 public boolean contains(Object o) { 234 logger.finest("---EJBHashSet.contains---"); assertIsValid(); 236 assertInTransaction(); 237 helper.assertInstanceOfLocalInterfaceImpl(o); 238 EJBLocalObject lo = (EJBLocalObject ) o; 239 return pcSet.contains(helper.convertEJBLocalObjectToPC(lo, pm, true)); 240 } 241 242 257 public boolean containsAll(Collection c) { 258 logger.finest("---EJBHashSet.containsAll---"); assertIsValid(); 260 assertInTransaction(); 261 assertInstancesOfLocalInterfaceImpl(c); 262 return pcSet.containsAll(helper.convertCollectionEJBLocalObjectToPC(c, pm, true)); 263 } 264 265 271 public Object clone() { 272 logger.finest("---EJBHashSet.clone---"); EJBHashSet newSet = (EJBHashSet)super.clone(); 274 newSet.pcSet = (HashSet)pcSet.clone(); 275 return newSet; 276 } 277 278 283 public HashSet getSCOHashSet() { 284 assertIsValid(); 285 assertInTransaction(); 286 return (pcSet != null) ? (HashSet)pcSet.clone() : null; 287 } 288 289 295 public void setSCOHashSet(Collection coll) { 296 if (coll instanceof java.util.HashSet ) 297 pcSet = (java.util.HashSet )coll; 298 else 299 pcSet = new java.util.HashSet (coll); 300 } 301 302 309 public Iterator iterator() { 310 assertIsValid(); 311 assertInTransaction(); 312 return new EJBHashIterator(); 313 } 314 315 private class EJBHashIterator implements Iterator { 316 Iterator _iterator = null; 317 Object lastReturned = null; 318 319 EJBHashIterator() { 320 _iterator = pcSet.iterator(); 321 } 322 323 public boolean hasNext() { 324 assertIsValid(); 325 assertInTransaction(); 326 return _iterator.hasNext(); 327 } 328 329 public Object next() { 330 assertIsValid(); 331 assertInTransaction(); 332 try { 333 lastReturned = _iterator.next(); 334 } catch(ConcurrentModificationException e) { 335 IllegalStateException ise = new IllegalStateException ( 336 e.toString()); 337 ise.initCause(e); 338 throw ise; 339 } 340 return helper.convertPCToEJBLocalObject(lastReturned, pm); 341 } 342 343 public void remove() { 344 assertIsValid(); 345 assertInTransaction(); 346 try { 347 _iterator.remove(); 348 } catch(ConcurrentModificationException e) { 349 IllegalStateException ise = new IllegalStateException ( 350 e.toString()); 351 ise.initCause(e); 352 throw ise; 353 } 354 } 355 } 356 357 361 private void assertIsValid() { 362 if (!valid) 363 throw new IllegalStateException (); } 365 366 371 private void assertInTransaction() { 372 if (pm.isClosed() || !tx.isActive()) { 373 invalidate(); 374 throw new IllegalStateException (); } 376 } 377 378 383 private void assertInstancesOfLocalInterfaceImpl(Collection c) { 384 for (Iterator it = c.iterator(); it.hasNext();) 385 helper.assertInstanceOfLocalInterfaceImpl(it.next()); 386 } 387 388 391 public void invalidate() { 392 valid = false; 393 pm = null; 394 tx = null; 395 helper = null; 396 pcSet = null; 397 } 398 } 399 | Popular Tags |