1 29 30 package com.caucho.amber.manager; 31 32 import com.caucho.amber.AmberException; 33 import com.caucho.amber.AmberRuntimeException; 34 import com.caucho.amber.cfg.BaseConfigIntrospector; 35 import com.caucho.amber.cfg.EmbeddableIntrospector; 36 import com.caucho.amber.cfg.EntityIntrospector; 37 import com.caucho.amber.cfg.EntityMappingsConfig; 38 import com.caucho.amber.cfg.MappedSuperIntrospector; 39 import com.caucho.amber.cfg.NamedNativeQueryConfig; 40 import com.caucho.amber.cfg.SqlResultSetMappingConfig; 41 import com.caucho.amber.entity.AmberCompletion; 42 import com.caucho.amber.entity.AmberEntityHome; 43 import com.caucho.amber.entity.Entity; 44 import com.caucho.amber.entity.EntityItem; 45 import com.caucho.amber.entity.EntityKey; 46 import com.caucho.amber.entity.Listener; 47 import com.caucho.amber.gen.AmberGenerator; 48 import com.caucho.amber.gen.AmberGeneratorImpl; 49 import com.caucho.amber.idgen.IdGenerator; 50 import com.caucho.amber.idgen.SequenceIdGenerator; 51 import com.caucho.amber.query.QueryCacheKey; 52 import com.caucho.amber.query.ResultSetCacheChunk; 53 import com.caucho.amber.table.Table; 54 import com.caucho.amber.type.*; 55 import com.caucho.bytecode.JClass; 56 import com.caucho.bytecode.JClassLoader; 57 import com.caucho.bytecode.JMethod; 58 import com.caucho.config.ConfigException; 59 import com.caucho.java.gen.JavaClassGenerator; 60 import com.caucho.jdbc.JdbcMetaData; 61 import com.caucho.naming.Jndi; 62 import com.caucho.util.L10N; 63 import com.caucho.util.LruCache; 64 65 import javax.persistence.EntityManager; 66 import javax.sql.DataSource ; 67 import java.io.IOException ; 68 import java.lang.ref.SoftReference ; 69 import java.sql.ResultSetMetaData ; 70 import java.util.ArrayList ; 71 import java.util.HashMap ; 72 import java.util.Iterator ; 73 import java.util.logging.Level ; 74 import java.util.logging.Logger ; 75 76 83 public class AmberPersistenceUnit { 84 private static final L10N L = new L10N(AmberPersistenceUnit.class); 85 private static final Logger log 86 = Logger.getLogger(AmberPersistenceUnit.class.getName()); 87 88 private String _name; 89 90 private boolean _isJPA; 91 92 private AmberContainer _amberContainer; 93 94 private Object _entityManagerProxy; 96 97 private DataSource _dataSource; 99 100 private DataSource _readDataSource; 102 103 private DataSource _xaDataSource; 105 106 private JdbcMetaData _jdbcMetaData; 107 108 private boolean _createDatabaseTables; 109 private boolean _validateDatabaseTables = true; 110 111 private long _tableCacheTimeout = 2000; 113 114 private TypeManager _typeManager = new TypeManager(); 115 116 private HashMap <String ,Table> _tableMap = 117 new HashMap <String ,Table>(); 118 119 private HashMap <String ,AmberEntityHome> _entityHomeMap = 120 new HashMap <String ,AmberEntityHome>(); 121 122 private HashMap <String ,IdGenerator> _tableGenMap = 123 new HashMap <String ,IdGenerator>(); 124 125 private HashMap <String ,SequenceIdGenerator> _sequenceGenMap = 126 new HashMap <String ,SequenceIdGenerator>(); 127 128 private LruCache<QueryCacheKey,SoftReference <ResultSetCacheChunk>> _queryCache = 129 new LruCache<QueryCacheKey,SoftReference <ResultSetCacheChunk>>(1024); 130 131 private LruCache<QueryCacheKey,SoftReference <ResultSetMetaData >> _queryCacheMetaData = 132 new LruCache<QueryCacheKey,SoftReference <ResultSetMetaData >>(16); 133 134 private LruCache<EntityKey,SoftReference <EntityItem>> _entityCache = 135 new LruCache<EntityKey,SoftReference <EntityItem>>(32 * 1024); 136 137 private EntityKey _entityKey = new EntityKey(); 138 139 private ArrayList <EntityType> _lazyConfigure = new ArrayList <EntityType>(); 140 141 private ArrayList <EntityType> _lazyGenerate = new ArrayList <EntityType>(); 142 private ArrayList <AmberEntityHome> _lazyHomeInit = 143 new ArrayList <AmberEntityHome>(); 144 private ArrayList <Table> _lazyTable = new ArrayList <Table>(); 145 146 private HashMap <String ,String > _namedQueryMap = 147 new HashMap <String ,String >(); 148 149 private HashMap <String , SqlResultSetMappingConfig> _sqlResultSetMap = 150 new HashMap <String , SqlResultSetMappingConfig>(); 151 152 private HashMap <String , NamedNativeQueryConfig> _namedNativeQueryMap = 153 new HashMap <String , NamedNativeQueryConfig>(); 154 155 private EntityMappingsConfig _entityMappings; 156 157 private ArrayList <EmbeddableType> _embeddableTypes 158 = new ArrayList <EmbeddableType>(); 159 160 private ArrayList <ListenerType> _defaultListeners = 161 new ArrayList <ListenerType>(); 162 163 private EntityIntrospector _entityIntrospector; 164 private EmbeddableIntrospector _embeddableIntrospector; 165 private MappedSuperIntrospector _mappedSuperIntrospector; 166 167 private AmberGenerator _generator; 168 169 171 private ThreadLocal <AmberConnection> _threadConnection 172 = new ThreadLocal <AmberConnection>(); 173 174 private volatile boolean _isInit; 175 176 private long _xid = 1; 177 178 public AmberPersistenceUnit(AmberContainer container, 179 String name) 180 { 181 _amberContainer = container; 182 _name = name; 183 184 _dataSource = container.getDataSource(); 185 _xaDataSource = container.getXADataSource(); 186 _readDataSource = container.getReadDataSource(); 187 188 _createDatabaseTables = container.getCreateDatabaseTables(); 189 190 _embeddableIntrospector = new EmbeddableIntrospector(this); 191 _entityIntrospector = new EntityIntrospector(this); 192 _mappedSuperIntrospector = new MappedSuperIntrospector(this); 193 194 try { 196 bindProxy(); 197 } catch (Throwable e) { 198 log.log(Level.FINE, e.toString(), e); 199 } 200 } 201 202 public void setName(String name) 203 { 204 _name = name; 205 } 206 207 public String getName() 208 { 209 return _name; 210 } 211 212 private void bindProxy() 213 throws Exception 214 { 215 Jndi.bindDeep(_amberContainer.getPersistenceUnitJndiPrefix() + getName(), 216 new FactoryProxy(this)); 217 218 Jndi.bindDeep(_amberContainer.getPersistenceContextJndiPrefix() + getName(), 219 new EntityManagerNamingProxy(this)); 220 } 221 222 public EntityManager getEntityManager() 223 { 224 226 return null; 227 } 228 229 public AmberContainer getAmberContainer() 230 { 231 return _amberContainer; 232 } 233 234 public ClassLoader getEnhancedLoader() 235 { 236 return _amberContainer.getEnhancedLoader(); 237 } 238 239 242 public void setDataSource(DataSource dataSource) 243 { 244 _dataSource = dataSource; 245 } 246 247 250 public DataSource getDataSource() 251 { 252 return _dataSource; 253 } 254 255 258 public void setReadDataSource(DataSource dataSource) 259 { 260 _readDataSource = dataSource; 261 } 262 263 266 public DataSource getReadDataSource() 267 { 268 return _readDataSource; 269 } 270 271 274 public void setXADataSource(DataSource dataSource) 275 { 276 _xaDataSource = dataSource; 277 } 278 279 282 public DataSource getXADataSource() 283 { 284 return _xaDataSource; 285 } 286 287 290 public JdbcMetaData getMetaData() 291 { 292 if (_jdbcMetaData == null) { 293 if (getDataSource() == null) 294 throw new NullPointerException ("No data-source specified for PersistenceUnit"); 295 296 _jdbcMetaData = JdbcMetaData.create(getDataSource()); 297 } 298 299 return _jdbcMetaData; 300 } 301 302 305 public void setCreateDatabaseTables(boolean create) 306 { 307 _createDatabaseTables = create; 308 } 309 310 313 public boolean getCreateDatabaseTables() 314 { 315 return _createDatabaseTables; 316 } 317 318 321 public void setValidateDatabaseTables(boolean validate) 322 { 323 _validateDatabaseTables = validate; 324 } 325 326 329 public boolean getValidateDatabaseTables() 330 { 331 return _validateDatabaseTables; 332 } 333 334 337 public void setTableCacheTimeout(long timeout) 338 { 339 _tableCacheTimeout = timeout; 340 } 341 342 345 public long getTableCacheTimeout() 346 { 347 return _tableCacheTimeout; 348 } 349 350 353 public void setBytecodeGenerator(boolean isBytecodeGenerator) 354 { 355 if (isBytecodeGenerator) 356 _generator = _amberContainer.getGenerator(); 357 else 358 _generator = new AmberGeneratorImpl(this); 359 } 360 361 364 public long getXid() 365 { 366 synchronized (this) { 367 return _xid++; 368 } 369 } 370 371 374 public JClassLoader getJClassLoader() 375 { 376 return _amberContainer.getJClassLoader(); 377 } 378 379 382 public Table createTable(String tableName) 383 { 384 Table table = _tableMap.get(tableName); 385 386 if (table == null) { 387 table = new Table(this, tableName); 388 table.setCacheTimeout(getTableCacheTimeout()); 389 390 _tableMap.put(tableName, table); 391 392 _lazyTable.add(table); 393 } 394 395 return table; 396 } 397 398 public Throwable getConfigException() 399 { 400 return _amberContainer.getConfigException(); 401 } 402 403 410 public void addEntityClass(String className, 411 JClass type) 412 throws ConfigException 413 { 414 if (type == null) { 415 type = getJClassLoader().forName(className); 416 417 if (type == null) { 418 throw new ConfigException(L.l("'{0}' is an unknown type", 419 className)); 420 } 421 } 422 423 boolean isEntity = _entityIntrospector.isEntity(type); 424 boolean isEmbeddable = _embeddableIntrospector.isEmbeddable(type); 425 boolean isMappedSuper = _mappedSuperIntrospector.isMappedSuper(type); 426 427 if (! (isEntity || isEmbeddable || isMappedSuper)) { 428 throw new ConfigException(L.l("'{0}' must implement javax.persistence.Entity, javax.persistence.Embeddable or javax.persistence.MappedSuperclass", 429 className)); 430 } 431 432 try { 433 if (isEntity) { 434 EntityType entityType = _entityIntrospector.introspect(type); 435 436 438 _amberContainer.addEntity(className, entityType); 439 } 440 else if (isEmbeddable) { 441 EmbeddableType embeddableType = _embeddableIntrospector.introspect(type); 442 443 _amberContainer.addEmbeddable(className, embeddableType); 444 } 445 else if (isMappedSuper) { 446 } 450 } catch (Throwable e) { 451 _amberContainer.addEntityException(className, e); 452 453 throw new ConfigException(e); 454 } 455 } 456 457 460 public void addSqlResultSetMapping(String resultSetName, 461 SqlResultSetMappingConfig resultSet) 462 throws ConfigException 463 { 464 if (_sqlResultSetMap.containsKey(resultSetName)) { 465 throw new ConfigException(L.l("SqlResultSetMapping '{0}' is already defined.", 466 resultSetName)); 467 } 468 469 _sqlResultSetMap.put(resultSetName, resultSet); 470 } 471 472 475 public SqlResultSetMappingConfig getSqlResultSetMapping(String resultSetName) 476 { 477 return _sqlResultSetMap.get(resultSetName); 478 } 479 480 483 public void addNamedQuery(String name, 484 String query) 485 throws ConfigException 486 { 487 if (_namedQueryMap.containsKey(name)) { 488 throw new ConfigException(L.l("Named query '{0}': '{1}' is already defined.", 489 name, query)); 490 } 491 492 _namedQueryMap.put(name, query); 493 } 494 495 498 public String getNamedQuery(String name) 499 { 500 return (String ) _namedQueryMap.get(name); 501 } 502 503 506 public void addNamedNativeQuery(String name, 507 NamedNativeQueryConfig queryConfig) 508 throws ConfigException 509 { 510 if (_namedNativeQueryMap.containsKey(name)) { 511 throw new ConfigException(L.l("NamedNativeQuery '{0}' is already defined.", 512 name)); 513 } 514 515 _namedNativeQueryMap.put(name, queryConfig); 516 } 517 518 521 public NamedNativeQueryConfig getNamedNativeQuery(String name) 522 { 523 return _namedNativeQueryMap.get(name); 524 } 525 526 529 public EntityType createEntity(JClass beanClass) 530 { 531 return createEntity(beanClass.getName(), beanClass); 532 } 533 534 537 public EntityType createEntity(String name, 538 JClass beanClass) 539 { 540 EntityType entityType = (EntityType) _typeManager.get(name); 541 542 if (entityType != null) 543 return entityType; 544 545 548 if (entityType == null) { 549 EntityType parentType = null; 550 551 for (JClass parentClass = beanClass.getSuperClass(); 552 parentType == null && parentClass != null; 553 parentClass = parentClass.getSuperClass()) { 554 parentType = (EntityType) _typeManager.get(parentClass.getName()); 555 } 556 557 if (parentType != null) 558 entityType = new SubEntityType(this, parentType); 559 else 560 entityType = new EntityType(this); 561 } 562 563 _typeManager.put(name, entityType); 565 if (_typeManager.get(beanClass.getName()) == null) 567 _typeManager.put(beanClass.getName(), entityType); 568 569 entityType.setName(name); 570 entityType.setBeanClass(beanClass); 571 572 _lazyConfigure.add(entityType); 573 575 AmberEntityHome entityHome = _entityHomeMap.get(beanClass.getName()); 576 577 if (entityHome == null) { 578 entityHome = new AmberEntityHome(this, entityType); 579 _lazyHomeInit.add(entityHome); 580 _isInit = false; 581 } 582 583 addEntityHome(name, entityHome); 584 addEntityHome(beanClass.getName(), entityHome); 587 588 return entityType; 589 } 590 591 594 public EmbeddableType createEmbeddable(String name, 595 JClass beanClass) 596 { 597 EmbeddableType embeddableType; 598 599 embeddableType = (EmbeddableType) _typeManager.get(name); 600 601 if (embeddableType != null) 602 return embeddableType; 603 604 embeddableType = new EmbeddableType(this); 605 606 _typeManager.put(name, embeddableType); 607 608 if (_typeManager.get(beanClass.getName()) == null) 610 _typeManager.put(beanClass.getName(), embeddableType); 611 612 embeddableType.setName(name); 613 embeddableType.setBeanClass(beanClass); 614 615 _embeddableTypes.add(embeddableType); 616 617 return embeddableType; 618 } 619 620 623 public EnumType createEnum(String name, 624 JClass beanClass) 625 { 626 EnumType enumType = (EnumType) _typeManager.get(name); 627 628 if (enumType != null) 629 return enumType; 630 631 enumType = new EnumType(); 632 633 _typeManager.put(name, enumType); 634 635 if (_typeManager.get(beanClass.getName()) == null) 637 _typeManager.put(beanClass.getName(), enumType); 638 639 enumType.setName(name); 640 enumType.setBeanClass(beanClass); 641 642 return enumType; 643 } 644 645 648 public ListenerType getDefaultListener(String className) 649 { 650 return _amberContainer.getDefaultListener(className); 651 } 652 653 656 public ListenerType addDefaultListener(JClass beanClass) 657 { 658 ListenerType listenerType = getListener(beanClass); 659 660 if (! _defaultListeners.contains(listenerType)) { 661 _defaultListeners.add(listenerType); 662 663 _amberContainer.addDefaultListener(beanClass.getName(), 664 listenerType); 665 } 666 667 return listenerType; 668 } 669 670 673 public ListenerType getEntityListener(String className) 674 { 675 return _amberContainer.getEntityListener(className); 676 } 677 678 681 public ListenerType addEntityListener(String entityName, 682 JClass listenerClass) 683 { 684 ListenerType listenerType = getListener(listenerClass); 685 686 _amberContainer.addEntityListener(entityName, 687 listenerType); 688 689 return listenerType; 690 } 691 692 private ListenerType getListener(JClass beanClass) 693 { 694 String name = beanClass.getName(); 695 696 ListenerType listenerType = (ListenerType) _typeManager.get(name); 697 698 if (listenerType != null) 699 return listenerType; 700 701 listenerType = new ListenerType(this); 702 703 ListenerType parentType = null; 704 705 for (JClass parentClass = beanClass.getSuperClass(); 706 parentType == null && parentClass != null; 707 parentClass = parentClass.getSuperClass()) { 708 parentType = (ListenerType) _typeManager.get(parentClass.getName()); 709 } 710 711 if (parentType != null) 712 listenerType = new SubListenerType(this, parentType); 713 else 714 listenerType = new ListenerType(this); 715 716 _typeManager.put(name, listenerType); 717 718 listenerType.setName(name); 719 listenerType.setBeanClass(beanClass); 720 721 return listenerType; 722 } 723 724 727 private void addEntityHome(String name, AmberEntityHome home) 728 { 729 _entityHomeMap.put(name, home); 730 731 } 733 734 737 public IdGenerator getTableGenerator(String name) 738 { 739 return _tableGenMap.get(name); 740 } 741 742 745 public IdGenerator putTableGenerator(String name, IdGenerator gen) 746 { 747 synchronized (_tableGenMap) { 748 IdGenerator oldGen = _tableGenMap.get(name); 749 750 if (oldGen != null) 751 return oldGen; 752 else { 753 _tableGenMap.put(name, gen); 754 return gen; 755 } 756 } 757 } 758 759 762 public GeneratorTableType createGeneratorTable(String name) 763 { 764 Type type = _typeManager.get(name); 765 766 if (type instanceof GeneratorTableType) 767 return (GeneratorTableType) type; 768 769 if (type != null) 770 throw new RuntimeException (L.l("'{0}' is a duplicate generator table.", 771 type)); 772 773 GeneratorTableType genType = new GeneratorTableType(this, name); 774 775 _typeManager.put(name, genType); 776 777 779 return genType; 780 } 781 782 785 public SequenceIdGenerator createSequenceGenerator(String name, int size) 786 throws ConfigException 787 { 788 synchronized (_sequenceGenMap) { 789 SequenceIdGenerator gen = _sequenceGenMap.get(name); 790 791 if (gen == null) { 792 gen = new SequenceIdGenerator(this, name, size); 793 794 _sequenceGenMap.put(name, gen); 795 } 796 797 return gen; 798 } 799 } 800 801 804 public void initType(AbstractEnhancedType type) 805 throws Exception 806 { 807 type.init(); 808 809 getGenerator().generate(type); 810 } 811 812 815 public void generate() 816 throws Exception 817 { 818 configure(); 819 820 AbstractEnhancedType type = null; 821 822 try { 823 while (_lazyGenerate.size() > 0) { 824 EntityType entityType = _lazyGenerate.remove(0); 825 826 initType(type = entityType); 828 829 ArrayList <ListenerType> listeners; 830 831 String className = entityType.getBeanClass().getName(); 832 833 listeners = _amberContainer.getEntityListeners(className); 834 835 if (listeners == null) 836 continue; 837 838 for (ListenerType listenerType : listeners) 840 initType(type = listenerType); 841 } 842 843 for (EmbeddableType embeddableType : _embeddableTypes) 845 initType(type = embeddableType); 846 847 for (ListenerType listenerType : _defaultListeners) 849 initType(type = listenerType); 850 851 } catch (Exception e) { 852 if (type != null) { 853 type.setConfigException(e); 854 855 _amberContainer.addEntityException(type.getBeanClass().getName(), e); 856 } 857 858 throw e; 859 } 860 861 try { 862 initTables(); 863 864 getGenerator().compile(); 865 } catch (Exception e) { 866 _amberContainer.addException(e); 867 868 throw e; 869 } 870 } 871 872 875 public boolean isJPA() 876 { 877 return _isJPA; 878 } 879 880 883 public void setJPA(boolean isJPA) 884 { 885 _isJPA = isJPA; 886 } 887 888 891 public void generate(JavaClassGenerator javaGen) 892 throws Exception 893 { 894 configure(); 895 896 while (_lazyGenerate.size() > 0) { 897 EntityType type = _lazyGenerate.remove(0); 898 899 type.init(); 900 901 if (type instanceof EntityType) { 902 EntityType entityType = (EntityType) type; 903 904 if (! entityType.isGenerated()) { 905 if (entityType.getInstanceClassName() == null) 906 throw new ConfigException(L.l("'{0}' does not have a configured instance class.", 907 entityType)); 908 909 entityType.setGenerated(true); 910 911 try { 912 getGenerator().generateJava(javaGen, entityType); 913 } catch (Throwable e) { 914 log.log(Level.FINER, e.toString(), e); 915 } 916 } 917 } 918 919 configure(); 920 } 921 922 for (EmbeddableType embeddableType : _embeddableTypes) { 923 924 embeddableType.init(); 925 926 if (! embeddableType.isGenerated()) { 927 if (embeddableType.getInstanceClassName() == null) 928 throw new ConfigException(L.l("'{0}' does not have a configured instance class.", 929 embeddableType)); 930 931 embeddableType.setGenerated(true); 932 933 try { 934 getGenerator().generateJava(javaGen, embeddableType); 935 } catch (Throwable e) { 936 log.log(Level.FINER, e.toString(), e); 937 } 938 } 939 } 940 941 for (SequenceIdGenerator gen : _sequenceGenMap.values()) 942 gen.init(this); 943 944 while (_defaultListeners.size() > 0) { 945 ListenerType type = _defaultListeners.remove(0); 946 947 type.init(); 948 949 if (! type.isGenerated()) { 950 if (type.getInstanceClassName() == null) 951 throw new ConfigException(L.l("'{0}' does not have a configured instance class.", 952 type)); 953 954 type.setGenerated(true); 955 956 try { 957 getGenerator().generateJava(javaGen, type); 958 } catch (Throwable e) { 959 log.log(Level.FINER, e.toString(), e); 960 } 961 } 962 } 963 } 964 965 968 public EmbeddableIntrospector getEmbeddableIntrospector() 969 { 970 return _embeddableIntrospector; 971 } 972 973 976 public void configure() 977 throws Exception 978 { 979 _embeddableIntrospector.configure(); 980 _mappedSuperIntrospector.configure(); 981 982 _entityIntrospector.configure(); 983 984 while (_lazyConfigure.size() > 0) { 985 EntityType type = _lazyConfigure.remove(0); 986 987 if (type.startConfigure()) { 988 } 990 991 _entityIntrospector.configure(); 992 993 if (! _lazyGenerate.contains(type)) 994 _lazyGenerate.add(type); 995 } 996 } 997 998 1001 public AmberEntityHome getEntityHome(String name) 1002 { 1003 if (! _isInit) { 1004 try { 1005 initEntityHomes(); 1006 } catch (RuntimeException e) { 1007 throw e; 1008 } catch (Exception e) { 1009 throw new AmberRuntimeException(e); 1010 } 1011 } 1012 1013 return _entityHomeMap.get(name); 1014 } 1015 1016 1019 public AmberEntityHome getHomeBySchema(String name) 1020 { 1021 for (AmberEntityHome home : _entityHomeMap.values()) { 1022 if (name.equals(home.getEntityType().getName())) 1023 return home; 1024 } 1025 1026 try { 1027 createType(name); 1028 } catch (Throwable e) { 1029 } 1030 1031 return _entityHomeMap.get(name); 1032 } 1033 1034 1037 public EmbeddableType getEmbeddable(String className) 1038 { 1039 Type type = _typeManager.get(className); 1040 1041 if (type instanceof EmbeddableType) 1042 return (EmbeddableType) type; 1043 else 1044 return null; 1045 } 1046 1047 1050 public EntityType getEntity(String className) 1051 { 1052 Type type = _typeManager.get(className); 1053 1054 if (type instanceof EntityType) 1055 return (EntityType) type; 1056 else 1057 return null; 1058 } 1059 1060 1063 public EntityType getEntityByInstanceClass(String className) 1064 { 1065 return _typeManager.getEntityByInstanceClass(className); 1066 } 1067 1068 1071 public Type createType(String typeName) 1072 throws ConfigException 1073 { 1074 Type type = _typeManager.get(typeName); 1075 1076 if (type != null) 1077 return type; 1078 1079 JClass cl = _amberContainer.getJClassLoader().forName(typeName); 1080 1081 if (cl == null) 1082 throw new ConfigException(L.l("'{0}' is an unknown type", typeName)); 1083 1084 return createType(cl); 1085 } 1086 1087 1090 public Type createType(JClass javaType) 1091 throws ConfigException 1092 { 1093 Type type = _typeManager.create(javaType); 1094 1095 if (type != null) 1096 return type; 1097 1098 return createEntity(javaType); 1099 } 1100 1101 1104 public AmberGenerator getGenerator() 1105 { 1106 if (_generator != null) 1107 return _generator; 1108 1112 else { 1113 _generator = _amberContainer.getGenerator(); 1114 1115 return _generator; 1116 } 1117 } 1118 1119 1122 public boolean hasReturnGeneratedKeys() 1123 { 1124 return getMetaData().supportsGetGeneratedKeys(); 1125 } 1126 1127 1130 public void setEntityMappingsConfig(EntityMappingsConfig entityMappings) 1131 { 1132 _entityMappings = entityMappings; 1133 1134 if (_entityMappings != null) { 1135 _entityIntrospector.setEntityConfigMap(_entityMappings.getEntityMap()); 1136 _entityIntrospector.setMappedSuperclassConfigMap(_entityMappings.getMappedSuperclassMap()); 1137 } 1138 } 1139 1140 1143 public void init() 1144 throws ConfigException, IOException 1145 { 1146 initLoaders(); 1147 1148 if (_entityMappings != null) { 1149 BaseConfigIntrospector introspector = new BaseConfigIntrospector(); 1150 1151 introspector.initMetaData(_entityMappings, this); 1152 } 1153 1154 if (_dataSource == null) 1155 return; 1156 1157 1175 } 1176 1177 1180 public void initLoaders() 1181 throws ConfigException, IOException 1182 { 1183 } 1185 1186 public void initEntityHomes() 1187 throws AmberException, ConfigException 1188 { 1189 synchronized (this) { 1190 if (_isInit) 1191 return; 1192 _isInit = true; 1193 } 1194 1195 initTables(); 1196 1197 while (_lazyHomeInit.size() > 0) { 1198 AmberEntityHome home = _lazyHomeInit.remove(0); 1199 1200 home.init(); 1201 } 1202 } 1203 1204 1207 public void initTables() 1208 throws ConfigException 1209 { 1210 while (_lazyTable.size() > 0) { 1211 Table table = _lazyTable.remove(0); 1212 1213 if (getDataSource() == null) 1214 throw new ConfigException(L.l("No configured data-source found for <ejb-server>.")); 1215 1216 if (getCreateDatabaseTables()) 1217 table.createDatabaseTable(this); 1218 1219 if (getValidateDatabaseTables()) 1220 table.validateDatabaseTable(this); 1221 } 1222 } 1223 1224 1227 public CacheConnection getCacheConnection() 1228 { 1229 1231 CacheConnection cacheConnection = new CacheConnection(this); 1232 1233 cacheConnection.register(); 1235 1236 return cacheConnection; 1237 } 1238 1239 1242 public AmberConnection createAmberConnection() 1243 { 1244 return new AmberConnection(this); 1245 } 1246 1247 1250 public AmberConnection getThreadConnection() 1251 { 1252 AmberConnection aConn = _threadConnection.get(); 1253 1254 if (aConn == null) { 1255 aConn = new AmberConnection(this); 1256 aConn.initThreadConnection(); 1257 1258 _threadConnection.set(aConn); 1259 } 1260 1261 return aConn; 1262 } 1263 1264 1267 public void removeThreadConnection() 1268 { 1269 _threadConnection.set(null); 1270 } 1271 1272 1275 public AmberEntityHome getHome(Class cl) 1276 { 1277 return getEntityHome(cl.getName()); 1278 } 1279 1280 1283 public ResultSetCacheChunk getQueryChunk(QueryCacheKey key) 1284 { 1285 SoftReference <ResultSetCacheChunk> ref = _queryCache.get(key); 1286 1287 if (ref == null) 1288 return null; 1289 else { 1290 ResultSetCacheChunk chunk = ref.get(); 1291 1292 if (chunk != null && chunk.isValid()) 1293 return chunk; 1294 else 1295 return null; 1296 } 1297 } 1298 1299 1302 public ResultSetMetaData getQueryMetaData(QueryCacheKey key) 1303 { 1304 SoftReference <ResultSetMetaData > ref = _queryCacheMetaData.get(key); 1305 1306 if (ref == null) 1307 return null; 1308 else 1309 return ref.get(); 1310 } 1311 1312 1316 protected void callListeners(int callbackIndex, 1317 Entity entity) 1318 { 1319 if (! isJPA()) 1321 return; 1322 1323 String className = entity.getClass().getName(); 1324 1325 EntityType entityType = (EntityType) _typeManager.get(className); 1326 1327 if (! entityType.getExcludeDefaultListeners()) { 1328 for (ListenerType listenerType : _defaultListeners) { 1329 for (JMethod m : listenerType.getCallbacks(callbackIndex)) { 1330 Listener listener = (Listener) listenerType.getInstance(); 1331 listener.__caucho_callback(callbackIndex, entity); 1332 } 1333 } 1334 } 1335 1336 ArrayList <ListenerType> listeners; 1337 1338 listeners = _amberContainer.getEntityListeners(className); 1339 1340 if (listeners == null) 1341 return; 1342 1343 for (ListenerType listenerType : listeners) { 1344 1345 if ((! entityType.getExcludeDefaultListeners()) && 1346 _defaultListeners.contains(listenerType)) 1347 continue; 1348 1349 for (JMethod m : listenerType.getCallbacks(callbackIndex)) { 1350 Listener listener = (Listener) listenerType.getInstance(); 1351 listener.__caucho_callback(callbackIndex, entity); 1352 } 1353 } 1354 } 1355 1356 1359 public void putQueryChunk(QueryCacheKey key, ResultSetCacheChunk chunk) 1360 { 1361 _queryCache.put(key, new SoftReference <ResultSetCacheChunk>(chunk)); 1362 } 1363 1364 1367 public void putQueryMetaData(QueryCacheKey key, ResultSetMetaData metaData) 1368 { 1369 _queryCacheMetaData.put(key, new SoftReference <ResultSetMetaData >(metaData)); 1370 } 1371 1372 1375 public EntityItem getEntityItem(String homeName, Object key) 1376 throws AmberException 1377 { 1378 AmberEntityHome home = getEntityHome(homeName); 1379 1380 return home.findEntityItem(getCacheConnection(), key, false); 1381 } 1382 1383 1386 public EntityItem getEntity(EntityType rootType, Object key) 1387 { 1388 SoftReference <EntityItem> ref; 1389 1390 synchronized (_entityKey) { 1391 _entityKey.init(rootType, key); 1392 ref = _entityCache.get(_entityKey); 1393 } 1394 1395 if (ref != null) 1396 return ref.get(); 1397 else 1398 return null; 1399 } 1400 1401 1404 public EntityItem putEntity(EntityType rootType, 1405 Object key, 1406 EntityItem entity) 1407 { 1408 SoftReference <EntityItem> ref = new SoftReference <EntityItem>(entity); 1409 EntityKey entityKey = new EntityKey(rootType, key); 1410 1411 ref = _entityCache.putIfNew(entityKey, ref); 1412 1413 return ref.get(); 1414 } 1415 1416 1419 public EntityItem removeEntity(EntityType rootType, Object key) 1420 { 1421 SoftReference <EntityItem> ref; 1422 1423 synchronized (_entityKey) { 1424 _entityKey.init(rootType, key); 1425 ref = _entityCache.remove(_entityKey); 1426 } 1427 1428 if (ref != null) 1429 return ref.get(); 1430 else 1431 return null; 1432 } 1433 1434 1437 public void complete(ArrayList <AmberCompletion> completions) 1438 { 1439 int size = completions.size(); 1440 if (size == 0) 1441 return; 1442 1443 synchronized (_entityCache) { 1444 Iterator <LruCache.Entry<EntityKey,SoftReference <EntityItem>>> iter; 1445 1446 iter = _entityCache.iterator(); 1447 while (iter.hasNext()) { 1448 LruCache.Entry<EntityKey,SoftReference <EntityItem>> entry; 1449 entry = iter.next(); 1450 1451 EntityKey key = entry.getKey(); 1452 SoftReference <EntityItem> valueRef = entry.getValue(); 1453 EntityItem value = valueRef.get(); 1454 1455 if (value == null) 1456 continue; 1457 1458 EntityType entityRoot = key.getEntityType(); 1459 Object entityKey = key.getKey(); 1460 1461 for (int i = 0; i < size; i++) { 1462 if (completions.get(i).complete(entityRoot, entityKey, value)) { 1463 } 1465 } 1466 } 1467 } 1468 1469 synchronized (_queryCache) { 1470 Iterator <SoftReference <ResultSetCacheChunk>> iter; 1471 1472 iter = _queryCache.values(); 1473 while (iter.hasNext()) { 1474 SoftReference <ResultSetCacheChunk> ref = iter.next(); 1475 1476 ResultSetCacheChunk chunk = ref.get(); 1477 1478 if (chunk != null) { 1479 for (int i = 0; i < size; i++) { 1480 if (completions.get(i).complete(chunk)) { 1481 } 1483 } 1484 } 1485 } 1486 } 1487 } 1488 1489 1492 public void destroy() 1493 { 1494 _typeManager = null; 1495 _queryCache = null; 1496 _entityCache = null; 1497 } 1498 1499 1504 public String getCreateColumnSQL(int sqlType, int length, int precision, int scale) { 1505 return getMetaData().getCreateColumnSQL(sqlType, length, precision, scale); 1506 } 1507 1508 1514} 1515 | Popular Tags |