1 27 28 package org.objectweb.speedo.pm.lib; 29 30 import java.util.ArrayList ; 31 import java.util.Arrays ; 32 import java.util.Collection ; 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 40 import javax.jdo.datastore.DataStoreCache; 41 import javax.jdo.listener.InstanceLifecycleListener; 42 import javax.jdo.JDODataStoreException; 43 import javax.jdo.JDOException; 44 import javax.jdo.JDOFatalInternalException; 45 import javax.jdo.JDOUnsupportedOptionException; 46 import javax.jdo.JDOUserException; 47 import javax.jdo.PersistenceManager; 48 import javax.jdo.PersistenceManagerFactory; 49 50 import org.objectweb.fractal.api.Component; 51 import org.objectweb.fractal.api.Interface; 52 import org.objectweb.fractal.api.NoSuchInterfaceException; 53 import org.objectweb.fractal.api.control.BindingController; 54 import org.objectweb.jorm.api.PClassMapping; 55 import org.objectweb.jorm.api.PException; 56 import org.objectweb.jorm.api.PMapper; 57 import org.objectweb.perseus.cache.api.CacheEntry; 58 import org.objectweb.perseus.cache.api.CacheEntryFilter; 59 import org.objectweb.perseus.cache.api.CacheException; 60 import org.objectweb.perseus.cache.api.CacheManager; 61 import org.objectweb.perseus.cache.api.FixableCacheEntry; 62 import org.objectweb.perseus.cache.api.UnbindManager; 63 import org.objectweb.perseus.persistence.api.PersistenceException; 64 import org.objectweb.perseus.persistence.api.TransactionalPersistenceManager; 65 import org.objectweb.perseus.pool.api.Pool; 66 import org.objectweb.speedo.Speedo; 67 import org.objectweb.speedo.api.ExceptionHelper; 68 import org.objectweb.speedo.api.SpeedoProperties; 69 import org.objectweb.speedo.mapper.api.ConnectionSpecFactory; 70 import org.objectweb.speedo.mapper.api.JormFactory; 71 import org.objectweb.speedo.mim.api.SpeedoHome; 72 import org.objectweb.speedo.mim.api.SpeedoProxy; 73 import org.objectweb.speedo.pm.api.ProxyManager; 74 import org.objectweb.speedo.pm.api.ProxyManagerFactory; 75 import org.objectweb.speedo.pm.api.ProxyManagerSwitch; 76 import org.objectweb.speedo.sequence.api.SequenceManager; 77 import org.objectweb.util.monolog.api.BasicLevel; 78 import org.objectweb.util.monolog.api.Logger; 79 80 98 public class SpeedoProxyManagerFactory 99 implements ProxyManagerFactory, 100 PersistenceManagerFactory, 101 BindingController { 102 103 public final static String MAPPER_BINDING = "mapper"; 104 public final static String JORM_FACTORY_BINDING = "jorm-factory"; 105 public final static String PM_POOL_BINDING = "proxy-manager-pool"; 106 public final static String PROXY_MANAGER_SWITCH_BINDING = "proxy-manager-switch"; 107 public final static String TPM_BINDING = "transactional-persistence-manager"; 108 public final static String CACHE_M_BINDING = "cache-manager"; 109 public final static String UNBIND_M_BINDING = "unbind-manager"; 110 public final static String COMPONENT_BINDING = "component"; 111 public final static String SEQUENCE_M_BINDING = "sequence-manager"; 112 113 private final static String optionArray[] = { 114 SpeedoProperties.JDO_OPTION_TRANSIENT_TRANSACTIONAL, 115 SpeedoProperties.JDO_OPTION_RETAIN_VALUES, 118 SpeedoProperties.JDO_OPTION_OPTIMISTIC, 120 SpeedoProperties.JDO_OPTION_APPLICATION_IDENTITY, 121 SpeedoProperties.JDO_OPTION_DATASTORE_IDENTITY, 122 SpeedoProperties.JDO_OPTION_NON_DURABLE_IDENTITY, 123 SpeedoProperties.JDO_OPTION_ARRAY_LIST, 124 SpeedoProperties.JDO_OPTION_HASH_MAP, 125 SpeedoProperties.JDO_OPTION_HASH_TABLE, 126 SpeedoProperties.JDO_OPTION_LINKED_LIST, 127 SpeedoProperties.JDO_OPTION_TREE_MAP, 128 SpeedoProperties.JDO_OPTION_TREE_SET, 129 SpeedoProperties.JDO_OPTION_VECTOR, 130 SpeedoProperties.JDO_OPTION_ARRAY, 131 SpeedoProperties.JDO_OPTION_MAP, 132 SpeedoProperties.JDO_OPTION_LIST, 133 SpeedoProperties.JDO_OPTION_NULL_COLLECTION, 134 SpeedoProperties.JDO_QUERY_JDOQL 135 }; 136 137 private final static int GETMAXPOOL = 100; 138 private boolean imbricatedAuthorized = true; 139 140 143 private Pool managedPM = null; 144 145 148 private Logger logger = null; 149 150 153 private PMapper mapper = null; 154 155 private Properties connectionProperties = null; 156 157 private ProxyManagerSwitch pms = null; 158 159 private Object _this = null; 160 161 private ConnectionSpecFactory csf = null; 162 163 private boolean started = false; 164 165 private CacheManager cacheManager = null; 166 167 private UnbindManager unbindManager = null; 168 169 private JormFactory jormFactory = null; 170 171 private TransactionalPersistenceManager tpm = null; 172 173 private SequenceManager sequenceManager = null; 174 175 public SpeedoProxyManagerFactory() { 176 connectionProperties = new Properties (); 177 } 178 179 private ProxyManagerFactory getThis() { 180 if (_this == null) { 181 throw new JDODataStoreException( 182 "Cannot provides a PersistenceManager without a reference to the component"); 183 } else if (_this instanceof Component) { 184 try { 185 _this = ((Component)_this).getFcInterface("proxy-manager-factory"); 186 } catch (NoSuchInterfaceException e) { 187 throw new JDODataStoreException( 188 "Cannot provides a PersistenceManager ", new Exception []{e}); 189 } 190 } 191 return (ProxyManagerFactory) _this; 192 } 193 194 199 private void assertIsSpeedoProxy(Object pc, String cmd) { 200 if (!(pc instanceof SpeedoProxy)) { 201 if (pc == null) { 202 throw new JDOUserException("Null persistent object instance specified"); 203 } else if (pc.getClass().isArray() 204 || pc instanceof Collection ) { 205 throw new JDOUserException("You must use the " + cmd 206 + "All method with a multivalued parameter: " + pc); 207 } else { 208 throw new JDOUserException("The object is not a Speedoproxy, beware to : " + pc); 209 } 210 } 211 } 212 213 216 public String [] listFc() { 217 return new String [] { 218 PM_POOL_BINDING, 219 MAPPER_BINDING, 220 PROXY_MANAGER_SWITCH_BINDING, 221 CACHE_M_BINDING, 222 UNBIND_M_BINDING, 223 JORM_FACTORY_BINDING, 224 TPM_BINDING, 225 SEQUENCE_M_BINDING 226 }; 227 } 228 229 public Object lookupFc(String s) { 230 if (PM_POOL_BINDING.equals(s)) 231 return managedPM; 232 else if (MAPPER_BINDING.equals(s)) 233 return mapper; 234 else if (PROXY_MANAGER_SWITCH_BINDING.equals(s)) 235 return pms; 236 else if (CACHE_M_BINDING.equals(s)) 237 return cacheManager; 238 else if (UNBIND_M_BINDING.equals(s)) 239 return unbindManager; 240 else if (JORM_FACTORY_BINDING.equals(s)) 241 return jormFactory; 242 else if (TPM_BINDING.equals(s)) 243 return tpm; 244 else if (SEQUENCE_M_BINDING.equals(s)) 245 return sequenceManager; 246 else 247 return null; 248 } 249 250 public void bindFc(String s, Object o) { 251 if ("logger".equals(s)) { 252 logger = (Logger) o; 253 } else if (PM_POOL_BINDING.equals(s)) { 254 managedPM = (Pool) o; 255 } else if (MAPPER_BINDING.equals(s)) { 256 mapper = (PMapper) o; 257 } else if (PROXY_MANAGER_SWITCH_BINDING.equals(s)) { 258 pms = (ProxyManagerSwitch) o; 259 } else if (CACHE_M_BINDING.equals(s)) { 260 cacheManager = (CacheManager) o; 261 } else if (UNBIND_M_BINDING.equals(s)) { 262 unbindManager = (UnbindManager) o; 263 } else if (COMPONENT_BINDING.equals(s)) { 264 _this = o; 265 } else if (JORM_FACTORY_BINDING.equals(s)) { 266 jormFactory = (JormFactory) o; 267 } else if (TPM_BINDING.equals(s)) { 268 tpm = (TransactionalPersistenceManager) o; 269 } else if (SEQUENCE_M_BINDING.equals(s)) { 270 sequenceManager = (SequenceManager) o; 271 } 272 } 273 274 public void unbindFc(String s) { 275 if (PM_POOL_BINDING.equals(s)) { 276 managedPM = null; 277 } else if (MAPPER_BINDING.equals(s)) { 278 mapper = null; 279 } else if (PROXY_MANAGER_SWITCH_BINDING.equals(s)) { 280 pms = null; 281 } else if (CACHE_M_BINDING.equals(s)) { 282 cacheManager = null; 283 } else if (UNBIND_M_BINDING.equals(s)) { 284 unbindManager = null; 285 } else if (JORM_FACTORY_BINDING.equals(s)) { 286 jormFactory = null; 287 } else if (TPM_BINDING.equals(s)) { 288 tpm = null; 289 } else if (SEQUENCE_M_BINDING.equals(s)) { 290 sequenceManager = null; 291 } 292 } 293 294 public void evict(Object oid) { 297 try { 298 tpm.evict(null, oid, false); 299 } catch (PersistenceException e) { 300 throw new JDOException("Error during the eviction of an entry: ", e); 301 } 302 } 303 public void evictAll(Class clazz, boolean subClasses) { 304 final List classesToEvict = getClasses(clazz, subClasses); 305 try { 306 unbindManager.unbind(new CacheEntryFilter() { 307 public boolean accept(CacheEntry ce) { 308 Object o = ce.getCeObject(); 309 return o != null && classesToEvict.contains(o.getClass()); 310 } 311 }, false); 312 } catch (CacheException e) { 313 throw new JDOException("Error during the eviction of entries of the class '" 314 + clazz.getName() + "' with" 315 + (subClasses ? "" : "out") + " sub classes: ", e); 316 } 317 } 318 319 private List getClasses(Class clazz, boolean subClasses) { 320 List classes = new ArrayList (); 321 classes.add(clazz); 322 if (subClasses) { 323 PClassMapping[] subpcms; 324 try { 325 subpcms = jormFactory.getPClassMapping(clazz).getSubPCMs(); 326 } catch (PException e1) { 327 return classes; 328 } 329 for (int i = 0; i < subpcms.length; i++) { 330 ClassLoader cl = subpcms[i].getClass().getClassLoader(); 331 if (cl == null) { 332 cl = getClass().getClassLoader(); 333 if (cl == null) { 334 cl = ClassLoader.getSystemClassLoader(); 335 } 336 } 337 try { 338 Class subclass = cl.loadClass(subpcms[i].getClassName()); 339 classes.add(subclass); 340 } catch (ClassNotFoundException e) { 341 logger.log(BasicLevel.WARN, 342 "Impossible to load the class '" 343 + subpcms[i].getClassName() + "': ", e); 344 continue; 345 } 346 } 347 } 348 return classes; 349 } 350 351 public void evictAll(Collection oids) { 352 try { 353 unbindManager.unbind(oids, false); 354 } catch (CacheException e) { 355 throw new JDOException("Error during the eviction of an entry: ", e); 356 } 357 } 358 public void evictAll(Object [] oids) { 359 evictAll(Arrays.asList(oids)); 360 } 361 public void evictAll() { 362 try { 363 unbindManager.unbindUnfixed(false); 364 } catch (CacheException e) { 365 throw new JDOException("Error during the eviction of an entry: ", e); 366 } 367 } 368 369 public void pin(Object oid) { 370 FixableCacheEntry ce = (FixableCacheEntry) cacheManager.lookup(oid); 371 if (ce != null && ce.getCeFixCount() == 0) { 372 try { 373 cacheManager.fix(ce); 374 } catch (CacheException e) { 375 logger.log(BasicLevel.WARN, "Impossible to pin the persistent class: ", e); 376 } 377 } } 379 public void pinAll(Class clazz, boolean subClasses) { 380 final List classesToPin = getClasses(clazz, subClasses); 381 try { 382 cacheManager.unfix(new CacheEntryFilter() { 383 public boolean accept(CacheEntry ce) { 384 Object o = ce.getCeObject(); 385 return ((FixableCacheEntry) ce).getCeFixCount() == 0 386 && o != null && classesToPin.contains(o.getClass()); 387 } 388 }); 389 } catch (CacheException e) { 390 throw new JDOException("Error during the eviction of entries of the class '" 391 + clazz.getName() + "' with" 392 + (subClasses ? "" : "out") + " sub classes: ", e); 393 } 394 } 396 397 public void pinAll(Collection oids) { 398 for (Iterator iter = oids.iterator(); iter.hasNext();) { 399 pin(iter.next()); 400 } 401 } 402 403 public void pinAll(Object [] oids) { 404 pinAll(Arrays.asList(oids)); 405 } 406 407 public void unpin(Object oid) { 408 FixableCacheEntry ce = (FixableCacheEntry) cacheManager.lookup(oid); 409 if (ce != null && ce.getCeFixCount() == 0) { 410 try { 411 cacheManager.unfix(ce); 412 } catch (CacheException e) { 413 logger.log(BasicLevel.WARN, "Impossible to pin the persistent class: ", e); 414 } 415 } 416 } 417 public void unpinAll(Class clazz, boolean subClasses) { 418 final List classesToUnpin = getClasses(clazz, subClasses); 419 try { 420 cacheManager.unfix(new CacheEntryFilter() { 421 public boolean accept(CacheEntry ce) { 422 Object o = ce.getCeObject(); 423 return ((FixableCacheEntry) ce).getCeFixCount() == 1 424 && o != null && classesToUnpin.contains(o.getClass()); 425 } 426 }); 427 } catch (CacheException e) { 428 throw new JDOException("Error during the eviction of entries of the class '" 429 + clazz.getName() + "' with" 430 + (subClasses ? "" : "out") + " sub classes: ", e); 431 } 432 } 434 public void unpinAll(Collection oids) { 435 for (Iterator iter = oids.iterator(); iter.hasNext();) { 436 unpin(iter.next()); 437 } 438 } 439 public void unpinAll(Object [] oids) { 440 unpinAll(Arrays.asList(oids)); 441 } 442 443 446 public ProxyManager lookup() { 447 return pms.lookup(getThis()); 448 } 449 450 public void bindPM2Thread(ProxyManager pm) { 451 if (logger.isLoggable(BasicLevel.DEBUG)) { 452 logger.log(BasicLevel.DEBUG, "Bind a pm to the thread: " + pm); 453 } 454 pms.bind(pm); 455 } 456 457 public void unbindPM() { 458 if (logger.isLoggable(BasicLevel.DEBUG)) { 459 logger.log(BasicLevel.DEBUG, "Unbind the current PM from the thread"); 460 } 461 pms.unbind(getThis()); 462 } 463 464 public void proxyManagerClosed(ProxyManager pr) { 465 try { 466 if (logger.isLoggable(BasicLevel.DEBUG)) { 467 logger.log(BasicLevel.DEBUG, "Unbind a PM from the thread: " + pr); 468 } 469 pms.unbind(pr); 470 managedPM.releaseResource(pr); 471 } catch (Exception e) { 472 throw new JDOFatalInternalException( 473 "Impossible to release this PersistenceManager in the pool.", 474 new Exception []{ExceptionHelper.getNested(e)}); 475 } 476 } 477 478 public SequenceManager getSequenceManager() { 479 return sequenceManager; 480 } 481 482 public void setSequenceManager(SequenceManager sequenceManager) { 483 this.sequenceManager = sequenceManager; 484 } 485 486 489 495 public PersistenceManager getPersistenceManager() { 496 return getPersistenceManager(null); 497 } 498 499 public void close() { 500 } 502 503 512 public PersistenceManager getPersistenceManager(String userid, String password) { 513 Object cs = null; 514 if (csf == null) { 515 String cn = connectionProperties.getProperty(Speedo.CONNECTION_SPEC_FACTORY); 516 if (cn != null && cn.length() > 0) { 517 try { 518 csf = (ConnectionSpecFactory) Class.forName(cn).newInstance(); 519 cs = csf.getConnectionSpec(this, userid, password); 520 } catch (Exception e) { 521 logger.log(BasicLevel.ERROR, 522 "Impossible to instanciate the ConnectionSpecFactory: " 523 + cn, e); 524 } 525 } 526 } else { 527 cs = csf.getConnectionSpec(this, userid, password); 528 } 529 return getPersistenceManager(cs); 530 } 531 532 public synchronized PersistenceManager getPersistenceManager(Object cs) { 533 if (!started) { 534 imbricatedAuthorized = Boolean.valueOf( 535 connectionProperties.getProperty( 536 SpeedoProperties.IMRICATED_PM_ALLOWED, "false")) 537 .booleanValue(); 538 started = true; 539 } 540 if (imbricatedAuthorized) { 541 ProxyManager pm = pms.lookup(getThis()); 542 if (pm != null) { 543 pm.getSemaphore().P(); 544 try { 545 if ((cs == null && pm.getConnectionSpec() == null) 546 || (cs != null && cs.equals(pm.getConnectionSpec()))) { 547 if (!pm.isClosed()) { 548 logger.log(BasicLevel.INFO, "reuse the same PersistenceManager (Imbricated)"); 549 pm.addUse(); 550 return pm; 551 } 552 } 553 } finally { 554 pm.getSemaphore().V(); 555 } 556 } 557 } 558 559 try { 560 Object pr = managedPM.getResource(null); 562 logger.log(BasicLevel.DEBUG, "get a persistenceManager from the pool"); 563 Component ci = ((Interface) pr).getFcItfOwner(); 564 ProxyManager pm = (ProxyManager) ci.getFcInterface("proxy-manager"); 565 pm.open(cs); 566 bindPM2Thread(pm); 567 pm.addUse(); 568 return pm; 569 } catch (Exception e) { 570 throw new JDODataStoreException( 571 "Cannot provides a PersistenceManager ", new Exception []{e}); 572 } 573 } 574 575 public DataStoreCache getDataStoreCache() { 576 return this; 577 } 578 579 public boolean isClosed() { 580 return false; 582 } 583 public String getMapping() { 584 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_MAPPING, "no user name defined"); 585 } 586 public void setMapping(String arg0) { 587 connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_MAPPING, arg0); 588 } 589 590 593 public void setConnectionUserName(String userName) { 594 connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, userName); 595 } 596 597 600 public String getConnectionUserName() { 601 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, "no user name defined"); 602 } 603 604 607 public void setConnectionPassword(String password) { 608 connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, password); 609 } 610 611 614 protected String getConnectionPassword() { 615 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD); 616 } 617 618 621 public void setConnectionURL(String URL) { 622 connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, URL); 623 } 624 625 628 public String getConnectionURL() { 629 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL); 630 } 631 632 635 public void setConnectionDriverName(String driverName) { 636 if (started) { 637 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 638 } 639 connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME, driverName); 640 } 641 642 645 public String getConnectionDriverName() { 646 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME); 647 } 648 649 652 public void setConnectionFactoryName(String connectionFactoryName) { 653 if (started) { 654 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 655 } 656 connectionProperties.setProperty( 657 SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME, connectionFactoryName); 658 } 659 660 663 public String getConnectionFactoryName() { 664 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME); 665 } 666 667 672 public void setConnectionFactory(Object connectionFactory) { 673 if (started) { 674 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 675 } 676 throw new JDOUnsupportedOptionException("Cannot set a Connection Factory, use <> instead"); 677 } 678 679 682 public Object getConnectionFactory() { 683 return mapper.getConnectionFactory(); 684 } 685 686 691 public void setConnectionFactory2Name(String connectionFactoryName) { 692 if (started) { 693 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 694 } 695 connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY2_NAME, connectionFactoryName); 696 } 697 698 703 public String getConnectionFactory2Name() { 704 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY2_NAME); 705 } 706 707 714 public void setConnectionFactory2(Object connectionfactory) { 715 if (started) { 716 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 717 } 718 throw new JDOUnsupportedOptionException( 719 "Cannot set a second Connection Factory"); 720 } 721 722 727 public Object getConnectionFactory2() { 728 return mapper.getConnectionFactory(); 729 } 730 731 736 public void setMultithreaded(boolean flag) { 737 if (started) { 738 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 739 } 740 connectionProperties.setProperty( 741 SpeedoProperties.JDO_OPTION_MULTITREADED, new Boolean (flag).toString()); 742 } 743 744 749 public boolean getMultithreaded() { 750 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_MULTITREADED, "").equals("true"); 751 } 752 753 758 public void setOptimistic(boolean flag) { 759 if (started) { 760 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 761 } 762 connectionProperties.setProperty( 763 SpeedoProperties.JDO_OPTION_OPTIMISTIC, new Boolean (flag).toString()); 764 } 765 766 771 public boolean getOptimistic() { 772 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_OPTIMISTIC,"").equals("true"); 773 } 774 775 780 public void setRetainValues(boolean flag) { 781 if (started) { 782 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 783 } 784 connectionProperties.setProperty( 785 SpeedoProperties.JDO_OPTION_RETAIN_VALUES, new Boolean (flag).toString()); 786 } 787 788 793 public boolean getRetainValues() { 794 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_RETAIN_VALUES, "").equals("true"); 795 } 796 797 802 public void setNontransactionalRead(boolean flag) { 803 if (started) { 804 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 805 } 806 connectionProperties.setProperty( 807 SpeedoProperties.JDO_OPTION_NON_TRANSACTIONAL_READ, new Boolean (flag).toString()); 808 } 809 810 815 public boolean getNontransactionalRead() { 816 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_NON_TRANSACTIONAL_READ).equals("true"); 817 } 818 819 824 public void setNontransactionalWrite(boolean flag) { 825 if (started) { 826 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 827 } 828 connectionProperties.setProperty( 829 SpeedoProperties.JDO_OPTION_NON_TRANSACTIONAL_WRITE, new Boolean (flag).toString()); 830 } 831 832 837 public boolean getNontransactionalWrite() { 838 return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_NON_TRANSACTIONAL_WRITE).equals("true"); 839 } 840 841 846 public void setIgnoreCache(boolean flag) { 847 if (started) { 848 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 849 } 850 connectionProperties.setProperty( 851 SpeedoProperties.JDO_OPTION_IGNORE_CACHE, new Boolean (flag).toString()); 852 } 853 854 859 public boolean getIgnoreCache() { 860 return "true".equalsIgnoreCase(connectionProperties 861 .getProperty(SpeedoProperties.JDO_OPTION_IGNORE_CACHE)); 862 } 863 864 868 public int getMaxPool() { 869 String v = connectionProperties.getProperty(SpeedoProperties.PM_POOL_MAX); 870 if (v != null && v.length()>0) { 871 return Integer.parseInt(v); 872 } else { 873 return GETMAXPOOL; 874 } 875 } 876 877 881 public void setMaxPool(int maxPool) { 882 if (started) { 883 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 884 } 885 connectionProperties.setProperty(SpeedoProperties.PM_POOL_MAX, Integer.toString(maxPool)); 886 } 887 888 892 public int getMinPool() { 893 return Integer.parseInt(connectionProperties.getProperty(SpeedoProperties.PM_POOL_MIN)); 894 } 895 896 900 public void setMinPool(int minPool) { 901 if (started) { 902 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 903 } 904 connectionProperties.setProperty(SpeedoProperties.PM_POOL_MIN, Integer.toString(minPool)); 905 } 906 907 911 public int getMsWait() { 912 return Integer.parseInt(connectionProperties.getProperty(SpeedoProperties.PM_POOL_TIMEOUT)); 913 } 914 915 919 public void setMsWait(int msWait) { 920 if (started) { 921 throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call"); 922 } 923 connectionProperties.setProperty(SpeedoProperties.PM_POOL_TIMEOUT, Integer.toString(msWait)); 924 } 925 926 932 public Properties getProperties() { 933 return connectionProperties; 934 } 935 936 966 public Collection supportedOptions() { 967 return Collections.unmodifiableList(Arrays.asList(optionArray)); 968 } 969 970 public void setRestoreValues(boolean b) { 971 } 972 973 public boolean getRestoreValues() { 974 return false; 975 } 976 977 private Map class2listeners; 978 private Map listener2classes; 979 980 public synchronized void addInstanceLifecycleListener( 981 InstanceLifecycleListener l, 982 Class [] classes) { 983 if (class2listeners == null) { 985 class2listeners = new HashMap (); 986 listener2classes = new HashMap (); 987 } 988 List classesCol = (List ) listener2classes.get(l); 989 if (classesCol == null) { 990 classesCol = new ArrayList (); 991 listener2classes.put(l, classesCol); 992 } 993 classesCol.addAll(Arrays.asList(classes)); 994 995 for (int i = 0; i < classes.length; i++) { 996 List listenersCol = (List ) class2listeners.get(classes[i]); 997 if (listenersCol == null) { 998 listenersCol = new ArrayList (); 999 class2listeners.put(classes[i], listenersCol); 1000 } 1001 listenersCol.add(l); 1002 SpeedoHome sh = null; 1003 try { 1004 sh = (SpeedoHome) jormFactory.getPClassMapping(classes[i]); 1005 } catch (PException e) { 1006 } 1007 if (sh == null) { 1008 logger.log(BasicLevel.WARN, "Class '" + classes[i].getName() 1009 + "' is not recongnized as a persistent class. The listener will not receiver event, but it is registerd."); 1010 } else { 1011 sh.addInstanceLifeCycleListener(l); 1012 } 1013 } 1014 } 1015 1016 public synchronized void removeInstanceLifecycleListener(InstanceLifecycleListener l) { 1017 List classes = (List ) listener2classes.remove(l); 1018 if (classes != null) { 1019 for (Iterator iter = classes.iterator(); iter.hasNext();) { 1020 Class clazz = (Class ) iter.next(); 1021 List listenersCol = (List ) class2listeners.get(clazz); 1022 if (listenersCol != null) { 1023 if (listenersCol.remove(l)) { 1024 SpeedoHome sh = null; 1025 try { 1026 sh = (SpeedoHome) jormFactory.getPClassMapping(clazz); 1027 } catch (PException e) { 1028 } 1029 if (sh != null) { 1030 sh.removeInstanceLifeCycleListener(l); 1031 } 1032 1033 } 1034 } 1035 } 1036 } 1037 } 1038 1039 1042 1045 public Pool getPool() { 1046 return managedPM; 1047 } 1048 1049} 1050 | Popular Tags |