1 package org.apache.ojb.jdo; 2 3 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 21 import javax.jdo.Extent; 22 import javax.jdo.InstanceCallbacks; 23 import javax.jdo.JDOFatalUserException; 24 import javax.jdo.PersistenceManager; 25 import javax.jdo.PersistenceManagerFactory; 26 import javax.jdo.Query; 27 import javax.jdo.Transaction; 28 29 import org.apache.ojb.broker.Identity; 30 import org.apache.ojb.broker.PBKey; 31 import org.apache.ojb.broker.PersistenceBrokerFactory; 32 import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl; 33 import org.apache.ojb.broker.core.proxy.ProxyHelper; 34 import org.apache.ojb.otm.EditingContext; 35 import org.apache.ojb.otm.OTMConnection; 36 import org.apache.ojb.otm.OTMKit; 37 import org.apache.ojb.otm.kit.SimpleKit; 38 import org.apache.ojb.otm.lock.LockType; 39 import org.apache.ojb.otm.lock.LockingException; 40 import org.apache.ojb.otm.states.State; 41 42 46 public class PersistenceManagerImpl implements PersistenceManager 47 { 48 private OTMConnection m_conn; 49 private OTMKit m_kit; 50 private boolean m_multiThreaded = false; 51 private boolean m_ignoreCache = false; 52 private PersistenceManagerFactory m_factory; 53 private String m_userID; 54 private String m_password; 55 private String m_alias; 56 private Object m_usersObject; 57 private TransactionImpl m_tx; 58 59 public PersistenceManagerImpl(PersistenceManagerFactory factory, String alias, String userid, String password) 60 { 61 m_factory = factory; 62 m_userID = userid; 63 m_password = password; 64 m_alias = alias; 65 m_kit = SimpleKit.getInstance(); 66 69 if (null == m_alias) 70 { 71 m_conn = m_kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey()); 72 } 73 else 74 { 75 PBKey key = new PBKey(m_alias, m_userID, m_password); 76 m_conn = m_kit.acquireConnection(key); 77 } 78 m_tx = new TransactionImpl(this, m_kit, m_conn); 79 } 80 81 OTMConnection getConnection() 82 { 83 return this.m_conn; 84 } 85 86 OTMKit getKit() 87 { 88 return this.m_kit; 89 } 90 91 public boolean isClosed() 92 { 93 return m_conn.isClosed(); 94 } 95 96 100 public void close() 101 { 102 if (isClosed()) 103 { 104 throw new JDOFatalUserException(generateIsClosedErrorMessage("close()")); 105 } 106 if (m_tx.isActive()) m_tx.rollback(); 107 m_conn.close(); 108 } 109 110 public Transaction currentTransaction() 111 { 112 return m_tx; 113 } 114 115 120 public void evict(Object o) 121 { 122 if (isClosed()) 123 { 124 throw new JDOFatalUserException(generateIsClosedErrorMessage("evict(Object)")); 125 } 126 if (null != o) 127 { 128 try 129 { 130 Identity oid = m_conn.getIdentity(o); 131 State state = m_conn.getEditingContext().lookupState(oid); 132 135 if (State.PERSISTENT_CLEAN == state) 136 { 137 140 if (o instanceof InstanceCallbacks) 141 { 142 ((InstanceCallbacks) o).jdoPreClear(); 143 } 144 m_conn.invalidate(m_conn.getIdentity(o)); 145 148 149 152 } 153 if (null == state) 154 { 155 158 m_conn.serviceObjectCache().remove(m_conn.getIdentity(o)); 159 } 160 } 161 catch (LockingException e) 162 { 163 167 } 168 } 169 } 170 171 public void evictAll(Object [] objects) 172 { 173 if (isClosed()) 174 { 175 throw new JDOFatalUserException(generateIsClosedErrorMessage("evictAll(Object[])")); 176 } 177 if (null == objects) 178 { 179 throw new NullPointerException ("evictAll(Object[]) was passed a null Array."); 180 } 181 int length = objects.length; 182 for (int i = 0; i < length; i++) 183 { 184 evict(objects[i]); 185 } 186 } 187 188 public void evictAll(Collection collection) 189 { 190 if (isClosed()) 191 { 192 throw new JDOFatalUserException(generateIsClosedErrorMessage("evictAll(Collection)")); 193 } 194 if (null == collection) 195 { 196 throw new NullPointerException ("evictAll(Collection) was passed a null Collection."); 197 } 198 Iterator it = collection.iterator(); 199 while (it.hasNext()) 200 { 201 evict(it.next()); 202 } 203 } 204 205 public void evictAll() 206 { 207 if (isClosed()) 208 { 209 throw new JDOFatalUserException(generateIsClosedErrorMessage("evictAll()")); 210 } 211 EditingContext ctx = m_conn.getEditingContext(); 212 if (ctx != null) 213 { 214 for (Iterator i = ctx.getAllObjectsInContext().iterator(); i.hasNext();) 215 { 216 evict(i.next()); 217 } 218 } 219 222 m_conn.serviceObjectCache().clear(); 223 } 224 225 public void refresh(Object o) 226 { 227 if (isClosed()) 228 { 229 throw new JDOFatalUserException(generateIsClosedErrorMessage("refresh(Object)")); 230 } 231 m_conn.refresh(o); 232 if (o instanceof InstanceCallbacks) 233 { 234 ((InstanceCallbacks) o).jdoPostLoad(); 235 } 236 } 237 238 public void refreshAll(Object [] objects) 239 { 240 if (isClosed()) 241 { 242 throw new JDOFatalUserException(generateIsClosedErrorMessage("refreshAll(Object[])")); 243 } 244 if (null == objects) 245 { 246 throw new NullPointerException ("refreshAll(Object[]) was passed a null Array."); 247 } 248 int length = objects.length; 249 for (int i = 0; i < length; i++) 250 { 251 refresh(objects[i]); 252 } 253 } 254 255 public void refreshAll(Collection collection) 256 { 257 if (isClosed()) 258 { 259 throw new JDOFatalUserException(generateIsClosedErrorMessage("refreshAll(Collection)")); 260 } 261 if (null == collection) 262 { 263 throw new NullPointerException ("refreshAll(Collection) was passed a null Collection."); 264 } 265 Iterator it = collection.iterator(); 266 while (it.hasNext()) 267 { 268 refresh(it.next()); 269 } 270 } 271 272 public void refreshAll() 273 { 274 if (isClosed()) 275 { 276 throw new JDOFatalUserException(generateIsClosedErrorMessage("refreshAll()")); 277 } 278 if (currentTransaction().isActive()) 279 { 280 Collection collection = m_conn.getEditingContext().getAllObjectsInContext(); 281 Iterator it = collection.iterator(); 282 while (it.hasNext()) 283 { 284 refresh(it.next()); 285 } 286 } 287 } 288 289 public Query newQuery() 290 { 291 if (isClosed()) 292 { 293 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery()")); 294 } 295 296 return new QueryImpl(this); 297 } 298 299 public Query newQuery(Object o) 300 { 301 if (isClosed()) 302 { 303 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Object)")); 304 } 305 try 306 { 307 return ((QueryImpl) o).ojbClone(); 308 } 309 catch (ClassCastException e) 310 { 311 throw new IllegalArgumentException ("newQuery(Object) must be passed a Query instance"); 312 } 313 } 314 315 public Query newQuery(String s, Object o) 316 { 317 if (isClosed()) 318 { 319 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(String, Object)")); 320 } 321 322 throw new UnsupportedOperationException ("Not yet implemented!"); 323 } 324 325 public Query newQuery(Class aClass) 326 { 327 if (isClosed()) 328 { 329 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Class)")); 330 } 331 Query query = new QueryImpl(this); 332 query.setClass(aClass); 333 return query; 334 } 335 336 public Query newQuery(Extent extent) 337 { 338 if (isClosed()) 339 { 340 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Extent)")); 341 } 342 343 Query query = new QueryImpl(this); 344 query.setCandidates(extent); 345 return query; 346 } 347 348 public Query newQuery(Class aClass, Collection collection) 349 { 350 if (isClosed()) 351 { 352 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Class, Collection)")); 353 } 354 Query query = new QueryImpl(this); 355 query.setCandidates(collection); 356 query.setClass(aClass); 357 return query; 358 } 359 360 public Query newQuery(Class aClass, String s) 361 { 362 if (isClosed()) 363 { 364 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Class, String)")); 365 } 366 Query query = new QueryImpl(this); 367 query.setClass(aClass); 368 query.setFilter(s); 369 return query; 370 } 371 372 public Query newQuery(Class aClass, Collection collection, String s) 373 { 374 if (isClosed()) 375 { 376 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Class, Collection, String)")); 377 } 378 Query query = new QueryImpl(this); 379 query.setCandidates(collection); 380 query.setClass(aClass); 381 query.setFilter(s); 382 return query; 383 } 384 385 public Query newQuery(Extent extent, String s) 386 { 387 if (isClosed()) 388 { 389 throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Extent, String)")); 390 } 391 Query query = new QueryImpl(this); 392 query.setCandidates(extent); 393 query.setFilter(s); 394 return query; 395 } 396 397 403 public Extent getExtent(Class aClass, boolean include_extent) 404 { 405 if (!include_extent) throw new UnsupportedOperationException ("Not yet implemented!"); 406 if (isClosed()) 407 { 408 throw new JDOFatalUserException(generateIsClosedErrorMessage("getExtent(Class, boolean)")); 409 } 410 return new ExtentImpl(aClass, m_conn, this, include_extent); 411 } 412 413 public Object getObjectById(Object o, boolean validate) 414 { 415 if (isClosed()) 416 { 417 throw new JDOFatalUserException(generateIsClosedErrorMessage("getObjectById(Object, boolean)")); 418 } 419 Object retval = null; 420 try 421 { 422 retval = m_conn.getObjectByIdentity((Identity) o); 423 } 424 catch (LockingException e) 425 { 426 e.printStackTrace(); } 428 catch (ClassCastException e) 429 { 430 throw new IllegalArgumentException ("Object passed as id is not an id: " + o); 431 } 432 return retval; 433 } 434 435 438 public Object getObjectId(Object o) 439 { 440 if (isClosed()) 441 { 442 throw new JDOFatalUserException(generateIsClosedErrorMessage("getObjectId(Object)")); 443 } 444 return m_conn.getIdentity(o); 445 } 446 447 public Object getTransactionalObjectId(Object o) 448 { 449 if (isClosed()) 450 { 451 throw new JDOFatalUserException(generateIsClosedErrorMessage("getTransactionalObjectId(Object)")); 452 } 453 return m_conn.getIdentity(o); 454 } 455 456 public Object newObjectIdInstance(Class aClass, String s) 457 { 458 if (isClosed()) 459 { 460 throw new JDOFatalUserException(generateIsClosedErrorMessage("newObjectIdInstance(Class, String)")); 461 } 462 return null; 463 } 464 465 public void makePersistent(Object o) 466 { 467 if (isClosed()) 468 { 469 throw new JDOFatalUserException(generateIsClosedErrorMessage("makePersistent(Object)")); 470 } 471 try 472 { 473 m_conn.makePersistent(o); 474 } 475 catch (LockingException e) 476 { 477 } 479 } 480 481 public void makePersistentAll(Object [] objects) 482 { 483 if (isClosed()) 484 { 485 throw new JDOFatalUserException(generateIsClosedErrorMessage("makePersistentAll(Object[])")); 486 } 487 if (null == objects) 488 { 489 throw new NullPointerException ("makePersistentAll(Object[]) was passed a null Array."); 490 } 491 int length = objects.length; 492 for (int i = 0; i < length; i++) 493 { 494 makePersistent(objects[i]); 495 } 496 } 497 498 public void makePersistentAll(Collection collection) 499 { 500 if (isClosed()) 501 { 502 throw new JDOFatalUserException(generateIsClosedErrorMessage("makePersistentAll(Collection)")); 503 } 504 if (null == collection) 505 { 506 throw new NullPointerException ("makePersistentAll(Collection) was passed a null Collection."); 507 } 508 Iterator it = collection.iterator(); 509 while (it.hasNext()) 510 { 511 makePersistent(it.next()); 512 } 513 } 514 515 public void deletePersistent(Object o) 516 { 517 if (isClosed()) 518 { 519 throw new JDOFatalUserException(generateIsClosedErrorMessage("deletePersistent(Object)")); 520 } 521 try 522 { 523 m_conn.deletePersistent(o); 524 } 525 catch (LockingException e) 526 { 527 handleLockingException(e); 528 } 529 } 530 531 public void deletePersistentAll(Object [] objects) 532 { 533 if (isClosed()) 534 { 535 throw new JDOFatalUserException(generateIsClosedErrorMessage("deletePersistentAll(Object[])")); 536 } 537 if (null == objects) 538 { 539 throw new NullPointerException ("deletePersistentAll(Object[]) was passed a null Array."); 540 } 541 int length = objects.length; 542 for (int i = 0; i < length; i++) 543 { 544 deletePersistent(objects[i]); 545 } 546 } 547 548 public void deletePersistentAll(Collection collection) 549 { 550 if (isClosed()) 551 { 552 throw new JDOFatalUserException(generateIsClosedErrorMessage("deletePersistentAll(Collection)")); 553 } 554 if (null == collection) 555 { 556 throw new NullPointerException ("deletePersistentAll(Collection) was passed a null Collection."); 557 } 558 Iterator it = collection.iterator(); 559 while (it.hasNext()) 560 { 561 deletePersistent(it.next()); 562 } 563 } 564 565 570 public void makeTransient(Object o) 571 { 572 if (isClosed()) 573 { 574 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeTransient(Object)")); 575 } 576 m_conn.getEditingContext().remove(m_conn.getIdentity(o)); 577 m_conn.serviceObjectCache().remove(m_conn.getIdentity(o)); 578 } 579 580 public void makeTransientAll(Object [] objects) 581 { 582 if (isClosed()) 583 { 584 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeTransientAll(Object[])")); 585 } 586 if (null == objects) 587 { 588 throw new NullPointerException ("makeTransientAll(Object[]) was passed a null Array."); 589 } 590 for (int i = 0; i < objects.length; i++) 591 { 592 this.makeTransient(objects[i]); 593 } 594 } 595 596 public void makeTransientAll(Collection collection) 597 { 598 if (isClosed()) 599 { 600 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeTransientAll(Collection)")); 601 } 602 if (null == collection) 603 { 604 throw new NullPointerException ("makeTransientAll(Collection) was passed a null Collection."); 605 } 606 for (Iterator iterator = collection.iterator(); iterator.hasNext();) 607 { 608 this.makeTransient(iterator.next()); 609 } 610 } 611 612 public void makeTransactional(Object o) 613 { 614 if (isClosed()) 615 { 616 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeTransactional(Object)")); 617 } 618 try 619 { 620 m_conn.getEditingContext().insert(m_conn.getIdentity(o), o, LockType.READ_LOCK); 621 } 622 catch (LockingException e) 623 { 624 handleLockingException(e); 625 } 626 } 627 628 public void makeTransactionalAll(Object [] objects) 629 { 630 if (isClosed()) 631 { 632 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeTransactionalAll(Object[])")); 633 } 634 if (null == objects) 635 { 636 throw new NullPointerException ("makeTransactionalAll(Object[]) was passed a null Array."); 637 } 638 for (int i = 0; i < objects.length; i++) 639 { 640 this.makeTransactional(objects[i]); 641 } 642 } 643 644 public void makeTransactionalAll(Collection collection) 645 { 646 if (isClosed()) 647 { 648 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeTransactionalAll(Collection)")); 649 } 650 if (null == collection) 651 { 652 throw new NullPointerException ("makeTransactionalAll(Collection) was passed a null Collection."); 653 } 654 for (Iterator iterator = collection.iterator(); iterator.hasNext();) 655 { 656 this.makeTransactional(iterator.next()); 657 } 658 } 659 660 663 public void makeNontransactional(Object o) 664 { 665 if (isClosed()) 666 { 667 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeNontransactional(Object)")); 668 } 669 this.makeTransient(o); 670 } 671 672 public void makeNontransactionalAll(Object [] objects) 673 { 674 if (isClosed()) 675 { 676 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeNontransactionalAll(Object[])")); 677 } 678 if (null == objects) 679 { 680 throw new NullPointerException ("makeNontransactionalAll(Object[]) was passed a null Array."); 681 } 682 for (int i = 0; i < objects.length; i++) 683 { 684 this.makeNontransactional(objects[i]); 685 } 686 } 687 688 public void makeNontransactionalAll(Collection collection) 689 { 690 if (isClosed()) 691 { 692 throw new JDOFatalUserException(generateIsClosedErrorMessage("makeNontransactionalAll(Collection)")); 693 } 694 if (null == collection) 695 { 696 throw new NullPointerException ("makeNontransactionalAll(Collection) was passed a null Collection."); 697 } 698 for (Iterator iterator = collection.iterator(); iterator.hasNext();) 699 { 700 this.makeNontransactional(iterator.next()); 701 } 702 } 703 704 708 public void retrieve(Object o) 709 { 710 if (isClosed()) 711 { 712 throw new JDOFatalUserException(generateIsClosedErrorMessage("retrieve(Object)")); 713 } 714 ProxyHelper.getRealObject(o); 715 } 716 717 721 public void retrieveAll(Collection collection) 722 { 723 if (isClosed()) 724 { 725 throw new JDOFatalUserException(generateIsClosedErrorMessage("retrieveAll(Collection)")); 726 } 727 if (null == collection) 728 { 729 throw new NullPointerException ("retrieveAll(Collection) was passed a null Collection."); 730 } 731 732 733 if (collection instanceof CollectionProxyDefaultImpl) 734 { 735 CollectionProxyDefaultImpl cp = (CollectionProxyDefaultImpl) collection; 736 cp.getData(); 737 } 738 } 739 740 public void retrieveAll(Object [] objects) 741 { 742 if (isClosed()) 743 { 744 throw new JDOFatalUserException(generateIsClosedErrorMessage("retrieveAll(Object[])")); 745 } 746 if (null == objects) 747 { 748 throw new NullPointerException ("retrieveAll(Object[]) was passed a null Array."); 749 } 750 } 752 753 public void setUserObject(Object o) 754 { 755 if (isClosed()) 756 { 757 throw new JDOFatalUserException(generateIsClosedErrorMessage("setUserObject(Object)")); 758 } 759 m_usersObject = o; 760 } 761 762 public Object getUserObject() 763 { 764 if (isClosed()) 765 { 766 throw new JDOFatalUserException(generateIsClosedErrorMessage("getUserObject()")); 767 } 768 return m_usersObject; 769 } 770 771 public PersistenceManagerFactory getPersistenceManagerFactory() 772 { 773 if (isClosed()) 774 { 775 throw new JDOFatalUserException(generateIsClosedErrorMessage("getPersistenceManagerFactory()")); 776 } 777 return this.m_factory; 778 } 779 780 783 public Class getObjectIdClass(Class aClass) 784 { 785 if (isClosed()) 786 { 787 throw new JDOFatalUserException(generateIsClosedErrorMessage("getObjectIdClass(Class)")); 788 } 789 return Identity.class; 790 } 791 792 public void setMultithreaded(boolean b) 793 { 794 if (isClosed()) 795 { 796 throw new JDOFatalUserException(generateIsClosedErrorMessage("setMultithreaded(boolean)")); 797 } 798 m_multiThreaded = b; 799 } 800 801 public boolean getMultithreaded() 802 { 803 if (isClosed()) 804 { 805 throw new JDOFatalUserException(generateIsClosedErrorMessage("getMultithreaded()")); 806 } 807 return m_multiThreaded; 808 } 809 810 public void setIgnoreCache(boolean b) 811 { 812 if (isClosed()) 813 { 814 throw new JDOFatalUserException(generateIsClosedErrorMessage("setIgnoreCache(boolean)")); 815 } 816 m_ignoreCache = b; 817 } 818 819 public boolean getIgnoreCache() 820 { 821 if (isClosed()) 822 { 823 throw new JDOFatalUserException(generateIsClosedErrorMessage("getIgnoreCache()")); 824 } 825 return m_ignoreCache; 826 } 827 828 834 private static final String generateIsClosedErrorMessage(String methodSignature) 835 { 836 return "PersistenceManager already closed, cannot call '" + methodSignature + "'. Obtain a new PersistenceBroker and retry."; 837 } 838 839 private void handleLockingException(LockingException e) 840 { 841 throw new UnsupportedOperationException ("Not yet implemented!"); 842 } 843 844 851 private static final String generateNullParameterErrorMessage(String methodSignature, String type) 852 { 853 return methodSignature + " was passed a null " + type + "."; 854 } 855 856 860 public void retrieveAll(Collection arg0, boolean arg1) 861 { 862 864 } 865 866 870 public void retrieveAll(Object [] arg0, boolean arg1) 871 { 872 } 874 } 875 | Popular Tags |