1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.impl; 31 32 import java.util.ResourceBundle ; 33 import java.util.Properties ; 34 import java.io.PrintWriter ; 35 import javax.sql.DataSource ; 36 37 import com.sun.jdo.api.persistence.support.*; 38 import com.sun.jdo.spi.persistence.support.sqlstore.RuntimeVersion; 39 import com.sun.jdo.spi.persistence.utility.I18NHelper; 40 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper; 41 42 47 48 public class PersistenceManagerFactoryImpl implements PersistenceManagerFactory 49 { 50 53 private String URL = null; 54 private String userName = null; 55 private String password = null; 56 private String driverName = null; 57 private ConnectionFactory connectionFactory = null; 58 private Object dataSource = null; 59 private String connectionFactoryName = null; 60 private String identifier = null; 61 private int connectionMaxPool = 0; 62 private int connectionMinPool = 0; 63 private int connectionMsInterval = 0; 64 private int connectionLoginTimeout = 0; 65 private int connectionMsWait = 0; 66 private int txIsolation = -1; 67 private PrintWriter connectionLogWriter; 68 private boolean optimistic = true; 69 private boolean retainValues = true; 70 private boolean nontransactionalRead = true; 71 private boolean ignoreCache = false; 72 private int queryTimeout = 0; 73 private int updateTimeout = 0; 74 private int maxPool = 0; 75 private int minPool = 0; 76 private boolean supersedeDeletedInstance = true; 77 private boolean requireCopyObjectId = true; 78 private boolean requireTrackedSCO = true; 79 80 private static final int NOT_SET = 0; 81 private static final int SET_AS_CONNECTIONFACTORY = 1; 82 private static final int SET_AS_DATASOURCE = 2; 83 84 86 private int providedConnectionFactory = 0; 87 88 89 93 private transient SQLPersistenceManagerFactory pmFactory = null; 94 95 98 private final static ResourceBundle messages = I18NHelper.loadBundle( 99 PersistenceManagerFactoryImpl.class); 100 101 104 public PersistenceManagerFactoryImpl() { 105 EJBHelper.setPersistenceManagerFactoryDefaults(this); 106 } 107 108 115 public PersistenceManagerFactoryImpl( 116 String URL, 117 String userName, 118 String password, 119 String driverName) 120 { 121 EJBHelper.setPersistenceManagerFactoryDefaults(this); 122 123 this.URL = URL; 124 this.userName = userName; 125 this.password = password; 126 this.driverName = driverName; 127 128 } 129 130 134 public void setConnectionUserName (String userName) { 135 assertNotConfigured(); 136 this.userName = userName; 137 } 138 139 143 public String getConnectionUserName() 144 { 145 if (connectionFactory != null) 146 return connectionFactory.getUserName(); 147 148 return userName; 149 150 } 151 152 156 public void setConnectionPassword (String password) { 157 assertNotConfigured(); 158 this.password = password; 159 } 160 161 162 166 public void setConnectionURL (String URL) { 167 assertNotConfigured(); 168 this.URL = URL; 169 } 170 171 175 public String getConnectionURL() 176 { 177 if (connectionFactory != null) 178 return connectionFactory.getURL(); 179 180 return URL; 181 182 } 183 184 188 public void setConnectionDriverName (String driverName) { 189 assertNotConfigured(); 190 this.driverName = driverName; 191 } 192 193 197 public String getConnectionDriverName() 198 { 199 if (connectionFactory != null) 200 return connectionFactory.getDriverName(); 201 return driverName; 202 203 } 204 205 210 public void setConnectionFactory (Object connectionFactory) { 211 assertNotConfigured(); 212 if (connectionFactory == null) { 213 this.connectionFactory = null; 214 this.dataSource = null; 215 providedConnectionFactory = NOT_SET; 216 217 } else { 218 if (EJBHelper.isManaged() || (connectionFactory instanceof DataSource )) { 219 this.dataSource = connectionFactory; 220 providedConnectionFactory = SET_AS_DATASOURCE; 221 } else if (connectionFactory instanceof ConnectionFactory) { 222 this.connectionFactory = (ConnectionFactory)connectionFactory; 223 providedConnectionFactory = SET_AS_CONNECTIONFACTORY; 224 } else { 225 throw new JDOUserException(I18NHelper.getMessage( messages, 226 "persistencemanagerfactoryimpl.wrongtype")); } 228 } 229 } 230 231 235 public Object getConnectionFactory() { 236 if (dataSource != null) 237 return dataSource; 238 239 return connectionFactory; 240 } 241 242 246 public void setConnectionFactoryName (String connectionFactoryName) 247 { 248 assertNotConfigured(); 249 this.connectionFactoryName = connectionFactoryName; 250 } 251 252 256 public String getConnectionFactoryName () 257 { 258 return connectionFactoryName; 259 } 260 261 268 public void setIdentifier(String identifier) { 269 assertNotConfigured(); 270 this.identifier = identifier; 271 } 272 273 280 public String getIdentifier() { 281 return identifier; 282 } 283 284 288 public void setConnectionMaxPool (int MaxPool) 289 { 290 assertNotConfigured(); 291 this.connectionMaxPool = MaxPool; 292 } 293 294 295 299 public int getConnectionMaxPool () 300 { 301 if (connectionFactory != null) 302 return connectionFactory.getMaxPool(); 303 304 return connectionMaxPool; 305 } 306 307 311 312 public void setConnectionMinPool (int MinPool) 313 { 314 assertNotConfigured(); 315 this.connectionMinPool = MinPool; 316 } 317 318 319 323 public int getConnectionMinPool () 324 { 325 if (connectionFactory != null) 326 return connectionFactory.getMinPool(); 327 return connectionMinPool; 328 } 329 330 331 336 337 public void setConnectionMsWait (int MsWait) 338 { 339 assertNotConfigured(); 340 this.connectionMsWait = MsWait; 341 } 342 343 344 349 public int getConnectionMsWait () 350 { 351 if (connectionFactory != null) 352 return connectionFactory.getMsWait(); 353 354 return connectionMsWait; 355 } 356 357 361 public int getMaxPool () { 362 return maxPool; 363 } 364 365 366 370 public void setMaxPool (int MaxPool) { 371 assertNotConfigured(); 372 this.maxPool = MaxPool; 373 } 374 375 379 public int getMinPool () { 380 return minPool; 381 } 382 383 384 388 public void setMinPool (int MinPool) { 389 assertNotConfigured(); 390 this.minPool = MinPool; 391 } 392 393 394 395 402 public void setConnectionMsInterval (int MsInterval) 403 { 404 assertNotConfigured(); 405 this.connectionMsInterval = MsInterval; 406 } 407 408 409 414 public int getConnectionMsInterval () 415 { 416 if (connectionFactory != null) 417 return connectionFactory.getMsInterval(); 418 419 return connectionMsInterval; 420 } 421 422 423 428 429 public void setConnectionLoginTimeout (int LoginTimeout) 430 { 431 assertNotConfigured(); 432 this.connectionLoginTimeout = LoginTimeout; 433 } 434 435 436 441 public int getConnectionLoginTimeout () 442 { 443 if (connectionFactory != null) { 444 return connectionFactory.getLoginTimeout(); 445 449 } else { 450 return connectionLoginTimeout; 451 } 452 } 453 454 455 459 public void setConnectionLogWriter(PrintWriter pw) 460 { 461 assertNotConfigured(); 462 this.connectionLogWriter = pw; 463 } 464 465 469 public PrintWriter getConnectionLogWriter () 470 { 471 return connectionLogWriter; 472 } 473 474 479 public void setQueryTimeout (int timeout) 480 { 481 assertNotConfigured(); 482 this.queryTimeout = timeout; 483 } 484 485 490 public int getQueryTimeout () 491 { 492 return queryTimeout; 493 } 494 495 500 public void setUpdateTimeout (int timeout) 501 { 502 assertNotConfigured(); 503 this.updateTimeout = timeout; 504 } 505 506 511 public int getUpdateTimeout() 512 { 513 return updateTimeout; 514 } 515 516 517 525 public void setConnectionTransactionIsolation (int level) 526 { 527 assertNotConfigured(); 528 txIsolation = level; 529 } 530 531 535 public int getConnectionTransactionIsolation () 536 { 537 if (connectionFactory != null) 538 return connectionFactory.getTransactionIsolation(); 539 540 return txIsolation; 541 } 542 543 547 public void setOptimistic (String flag) 548 { 549 setOptimistic(Boolean.getBoolean(flag)); 550 } 551 552 556 public void setOptimistic (boolean flag) 557 { 558 assertNotConfigured(); 559 optimistic = flag; 560 561 if (flag) 563 nontransactionalRead = flag; 564 } 565 566 570 public boolean getOptimistic () 571 { 572 return optimistic; 573 } 574 575 579 public void setRetainValues (String flag) 580 { 581 setRetainValues(Boolean.getBoolean(flag)); 582 } 583 584 588 public void setRetainValues (boolean flag) 589 { 590 assertNotConfigured(); 591 retainValues = flag; 592 593 if (flag) { 595 nontransactionalRead = flag; 596 } 597 } 598 599 604 public boolean getRetainValues () 605 { 606 return retainValues; 607 } 608 609 613 public void setNontransactionalRead (String flag) 614 { 615 setNontransactionalRead(Boolean.getBoolean(flag)); 616 } 617 618 622 public void setNontransactionalRead (boolean flag) 623 { 624 assertNotConfigured(); 625 nontransactionalRead = flag; 626 627 if (flag == false) 629 { 630 retainValues = flag; 631 optimistic = flag; 632 } 633 } 634 635 640 public boolean getNontransactionalRead () 641 { 642 return nontransactionalRead; 643 } 644 645 649 public void setIgnoreCache (String flag) 650 { 651 setIgnoreCache(Boolean.getBoolean(flag)); 652 } 653 654 659 public void setIgnoreCache (boolean flag) 660 { 661 assertNotConfigured(); 662 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 663 "persistencemanagerfactoryimpl.notsupported")); } 666 667 672 public boolean getIgnoreCache () 673 { 674 return ignoreCache; 675 } 676 677 678 682 public Properties getProperties () 683 { 684 if (pmFactory != null) 685 { 686 return pmFactory.getProperties(); 687 } 688 return RuntimeVersion.getVendorProperties( 689 "/com/sun/jdo/spi/persistence/support/sqlstore/sys.properties"); } 691 692 693 694 698 public PersistenceManager getPersistenceManager() { 699 return getPersistenceManager(null, null); 700 } 701 702 709 public boolean getSupersedeDeletedInstance () { 710 return supersedeDeletedInstance; 711 } 712 713 714 718 public void setSupersedeDeletedInstance (boolean flag) { 719 assertNotConfigured(); 720 supersedeDeletedInstance = flag; 721 } 722 723 733 public boolean getRequireCopyObjectId() { 734 return requireCopyObjectId; 735 } 736 737 738 748 public void setRequireCopyObjectId (boolean flag) { 749 assertNotConfigured(); 750 requireCopyObjectId = flag; 751 } 752 753 761 public boolean getRequireTrackedSCO() { 762 return requireTrackedSCO; 763 } 764 765 773 public void setRequireTrackedSCO (boolean flag) { 774 assertNotConfigured(); 775 requireTrackedSCO = flag; 776 } 777 778 779 786 public PersistenceManager getPersistenceManager (String username, String passwd){ 787 synchronized (this) { 788 789 if (pmFactory == null) { 790 if (providedConnectionFactory == NOT_SET) { 792 793 if (connectionFactoryName == null) { 794 795 assertConnectionWait(); 797 798 connectionFactory = new ConnectionFactoryImpl(); 804 805 connectionFactory.setURL(URL); 806 connectionFactory.setUserName(userName); 807 connectionFactory.setPassword(password); 808 connectionFactory.setDriverName(driverName); 809 connectionFactory.setMinPool(connectionMinPool); 810 connectionFactory.setMaxPool(connectionMaxPool); 811 812 connectionFactory.setMsWait(this.connectionMsWait); 814 connectionFactory.setMsInterval(this.connectionMsInterval); 815 connectionFactory.setLogWriter(this.connectionLogWriter); 816 connectionFactory.setLoginTimeout(this.connectionLoginTimeout); 817 if (txIsolation > 0) 818 connectionFactory.setTransactionIsolation(txIsolation); 819 } else { 820 try { 822 javax.naming.InitialContext ctx = 823 (javax.naming.InitialContext ) Class.forName("javax.naming.InitialContext").newInstance(); Object o = ctx.lookup(connectionFactoryName); 825 if (EJBHelper.isManaged() || (o instanceof DataSource )) 826 dataSource = o; 827 else if (o instanceof ConnectionFactory) 828 connectionFactory = (ConnectionFactory) o; 829 else 830 throw new JDOUserException(I18NHelper.getMessage( 831 messages, 832 "persistencemanagerfactoryimpl.wrongtype")); 834 } catch (JDOException e) { 835 throw e; 837 } catch (ClassNotFoundException e) { 838 throw new JDOUserException(I18NHelper.getMessage(messages, 839 "persistencemanagerfactoryimpl.initialcontext")); 841 } catch (Exception e) { 842 throw new JDOUserException(I18NHelper.getMessage(messages, 843 "persistencemanagerfactoryimpl.lookup"), e); } 845 } 846 } 847 848 if(getIdentifier() == null) { 850 setIdentifier(getConnectionFactoryName()); 851 } 852 pmFactory = new SQLPersistenceManagerFactory(this); 854 pmFactory = 856 (SQLPersistenceManagerFactory)EJBHelper.replaceInternalPersistenceManagerFactory(pmFactory); 857 858 } 859 } 861 if (username != null && connectionFactory != null) { 863 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 864 "persistencemanagerfactoryimpl.notsupported")); } 866 867 return pmFactory.getPersistenceManager(username, passwd); 868 869 } 870 871 877 public void setBooleanProperty(String name, boolean value) { 878 if (name.equals("optimistic")) { setOptimistic(value); 881 882 } else if (name.equals("retainValues")) { setRetainValues(value); 884 885 } else if (name.equals("nontransactionalRead")) { setNontransactionalRead(value); 887 888 } else if (name.equals("ignoreCache")) { setIgnoreCache(value); 890 891 } else if (name.equals("supersedeDeletedInstance")) { setSupersedeDeletedInstance(value); 893 894 } else if (name.equals("requireCopyObjectId")) { setRequireCopyObjectId(value); 896 897 } else if (name.equals("requireTrackedSCO")) { setRequireTrackedSCO(value); 899 900 } 902 } 903 904 910 public boolean equals(Object obj) { 911 if ((obj == null) || !(obj instanceof PersistenceManagerFactoryImpl)) { 912 return false; 913 } 914 PersistenceManagerFactoryImpl pmf = (PersistenceManagerFactoryImpl)obj; 915 916 if (pmf.providedConnectionFactory == this.providedConnectionFactory) { 917 if (pmf.providedConnectionFactory == SET_AS_CONNECTIONFACTORY) { 918 return (pmf.connectionFactory.equals(this.connectionFactory) && 919 equalBooleanProperties(pmf)); 920 921 } else if (pmf.providedConnectionFactory == SET_AS_DATASOURCE) { 922 return (pmf.dataSource.equals(this.dataSource) && 923 equalBooleanProperties(pmf)); 924 925 } else if (pmf.connectionFactoryName != null) { 926 return (pmf.connectionFactoryName.equals(this.connectionFactoryName) && 927 equalBooleanProperties(pmf)); 928 929 } 930 return (pmf.URL.equals(this.URL) && pmf.userName.equals(this.userName) && 931 pmf.password.equals(this.password) && 932 pmf.driverName.equals(this.driverName) && 933 equalBooleanProperties(pmf)); 934 } 935 return false; 936 } 937 938 943 public int hashCode() { 944 if (providedConnectionFactory == SET_AS_CONNECTIONFACTORY) { 945 return connectionFactory.hashCode(); 946 } else if (providedConnectionFactory == SET_AS_DATASOURCE) { 947 return dataSource.hashCode(); 948 } else if (connectionFactoryName != null) { 949 return connectionFactoryName.hashCode(); 950 } 951 return URL.hashCode() + userName.hashCode() + password.hashCode() + driverName.hashCode(); 952 } 953 954 958 private void assertNotConfigured() { 959 if ( pmFactory != null) { 960 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 961 "persistencemanagerfactoryimpl.configured")); } 963 } 964 965 966 970 private void assertConnectionWait() { 971 if ( connectionMsWait < 0 ) 972 { 973 throw new JDOUserException(I18NHelper.getMessage(messages, 974 "connection.connectionmanager.mswaitvalue")); } 976 else if ( connectionMsInterval < 0 || 977 connectionMsInterval > connectionMsWait || 978 (connectionMsWait > 0 && connectionMsInterval == 0) ) 979 { 980 throw new JDOUserException(I18NHelper.getMessage(messages, 981 "connection.connectionmanager.msintervalvalue")); } 983 } 984 985 990 private boolean equalBooleanProperties(PersistenceManagerFactory pmf) { 991 return (pmf.getOptimistic() == optimistic && 992 pmf.getRetainValues() == retainValues && 993 pmf.getNontransactionalRead() == nontransactionalRead && 994 pmf.getIgnoreCache() == ignoreCache && 995 pmf.getSupersedeDeletedInstance() == supersedeDeletedInstance && 996 pmf.getRequireCopyObjectId() == requireCopyObjectId && 997 pmf.getRequireTrackedSCO() == requireTrackedSCO 998 ); 999 1000 } 1001} 1002 | Popular Tags |