1 29 30 package com.caucho.amber.manager; 31 32 import com.caucho.amber.AmberRuntimeException; 33 import com.caucho.amber.cfg.EntityConfig; 34 import com.caucho.amber.cfg.EntityMappingsConfig; 35 import com.caucho.amber.cfg.PersistenceConfig; 36 import com.caucho.amber.cfg.PersistenceUnitConfig; 37 import com.caucho.amber.gen.AmberEnhancer; 38 import com.caucho.amber.gen.AmberGenerator; 39 import com.caucho.amber.type.EmbeddableType; 40 import com.caucho.amber.type.EntityType; 41 import com.caucho.amber.type.ListenerType; 42 import com.caucho.bytecode.JClass; 43 import com.caucho.bytecode.JClassLoader; 44 import com.caucho.config.Config; 45 import com.caucho.config.ConfigException; 46 import com.caucho.loader.DynamicClassLoader; 47 import com.caucho.loader.Environment; 48 import com.caucho.loader.EnvironmentLocal; 49 import com.caucho.loader.enhancer.EnhancerManager; 50 import com.caucho.vfs.Path; 51 52 import javax.sql.DataSource ; 53 import java.io.InputStream ; 54 import java.util.ArrayList ; 55 import java.util.HashMap ; 56 import java.util.Iterator ; 57 import java.util.Map ; 58 import java.util.logging.Level ; 59 import java.util.logging.Logger ; 60 61 64 public class AmberContainer { 65 private static final Logger log 66 = Logger.getLogger(AmberContainer.class.getName()); 67 68 private static final EnvironmentLocal<AmberContainer> _localContainer 69 = new EnvironmentLocal<AmberContainer>(); 70 71 private ClassLoader _parentLoader; 72 74 private JClassLoader _jClassLoader; 75 76 private AmberEnhancer _enhancer; 77 78 private DataSource _dataSource; 79 private DataSource _readDataSource; 80 private DataSource _xaDataSource; 81 82 private boolean _createDatabaseTables; 83 84 private HashMap <String ,AmberPersistenceUnit> _unitMap 85 = new HashMap <String ,AmberPersistenceUnit>(); 86 87 private HashMap <String ,EmbeddableType> _embeddableMap 88 = new HashMap <String ,EmbeddableType>(); 89 90 private HashMap <String ,EntityType> _entityMap 91 = new HashMap <String ,EntityType>(); 92 93 private HashMap <String ,ListenerType> _defaultListenerMap 94 = new HashMap <String ,ListenerType>(); 95 96 private HashMap <String , ArrayList <ListenerType>> 97 _entityListenerMap = new HashMap <String , ArrayList <ListenerType>>(); 98 99 private Throwable _exception; 100 101 private HashMap <String ,Throwable > _embeddableExceptionMap 102 = new HashMap <String ,Throwable >(); 103 104 private HashMap <String ,Throwable > _entityExceptionMap 105 = new HashMap <String ,Throwable >(); 106 107 private HashMap <String ,Throwable > _listenerExceptionMap 108 = new HashMap <String ,Throwable >(); 109 110 private AmberContainer() 111 { 112 _parentLoader = Thread.currentThread().getContextClassLoader(); 113 _jClassLoader = EnhancerManager.create(_parentLoader).getJavaClassLoader(); 114 115 _enhancer = new AmberEnhancer(this); 116 117 EnhancerManager.create().addClassEnhancer(_enhancer); 118 119 try { 120 if (_parentLoader instanceof DynamicClassLoader) 121 ((DynamicClassLoader) _parentLoader).make(); 122 } catch (RuntimeException e) { 123 throw e; 124 } catch (Exception e) { 125 throw new RuntimeException (e); 126 } 127 } 128 129 132 public static AmberContainer getLocalContainer() 133 { 134 synchronized (_localContainer) { 135 AmberContainer container = _localContainer.getLevel(); 136 137 if (container == null) { 138 container = new AmberContainer(); 139 140 _localContainer.set(container); 141 } 142 143 return container; 144 } 145 } 146 147 150 public void setDataSource(DataSource dataSource) 151 { 152 _dataSource = dataSource; 153 } 154 155 158 public DataSource getDataSource() 159 { 160 return _dataSource; 161 } 162 163 166 public void setReadDataSource(DataSource dataSource) 167 { 168 _readDataSource = dataSource; 169 } 170 171 174 public DataSource getReadDataSource() 175 { 176 return _readDataSource; 177 } 178 179 182 public void setXADataSource(DataSource dataSource) 183 { 184 _xaDataSource = dataSource; 185 } 186 187 190 public DataSource getXADataSource() 191 { 192 return _xaDataSource; 193 } 194 195 198 public boolean getCreateDatabaseTables() 199 { 200 return _createDatabaseTables; 201 } 202 203 206 public void setCreateDatabaseTables(boolean isCreate) 207 { 208 _createDatabaseTables = isCreate; 209 } 210 211 214 public ClassLoader getParentClassLoader() 215 { 216 return _parentLoader; 217 } 218 219 222 public ClassLoader getEnhancedLoader() 223 { 224 return _parentLoader; 225 } 226 227 230 public AmberGenerator getGenerator() 231 { 232 return _enhancer; 233 } 234 235 238 public String getPersistenceUnitJndiPrefix() 239 { 240 return "java:comp/env/persistence/_amber_PersistenceUnit/"; 241 } 242 243 246 public String getPersistenceContextJndiPrefix() 247 { 248 return "java:comp/env/persistence/"; 250 } 251 252 255 public JClassLoader getJClassLoader() 256 { 257 return _jClassLoader; 258 } 259 260 263 public EmbeddableType getEmbeddable(String className) 264 { 265 Throwable e = _embeddableExceptionMap.get(className); 266 267 if (e != null) 268 throw new AmberRuntimeException(e); 269 else if (_exception != null) { 270 throw new AmberRuntimeException(_exception); 271 } 272 273 return _embeddableMap.get(className); 274 } 275 276 279 public EntityType getEntity(String className) 280 { 281 Throwable e = _entityExceptionMap.get(className); 282 283 if (e != null) 284 throw new AmberRuntimeException(e); 285 else if (_exception != null) { 286 throw new AmberRuntimeException(_exception); 287 } 288 289 return _entityMap.get(className); 290 } 291 292 295 public ListenerType getDefaultListener(String className) 296 { 297 Throwable e = _listenerExceptionMap.get(className); 298 299 if (e != null) 300 throw new AmberRuntimeException(e); 301 else if (_exception != null) { 302 throw new AmberRuntimeException(_exception); 303 } 304 305 return _defaultListenerMap.get(className); 306 } 307 308 311 public ListenerType getEntityListener(String className) 312 { 313 Throwable e = _listenerExceptionMap.get(className); 314 315 if (e != null) 316 throw new AmberRuntimeException(e); 317 else if (_exception != null) { 318 throw new AmberRuntimeException(_exception); 319 } 320 321 ArrayList <ListenerType> listenerList; 322 323 for (Map.Entry <String , ArrayList <ListenerType>> 324 entry : _entityListenerMap.entrySet()) { 325 326 listenerList = entry.getValue(); 327 328 if (listenerList == null) 329 continue; 330 331 for (ListenerType listener : listenerList) { 332 if (className.equals(listener.getBeanClass().getName())) 333 return listener; 334 } 335 } 336 337 return null; 338 } 339 340 343 public ListenerType getListener(String className) 344 { 345 ListenerType listener = getDefaultListener(className); 346 347 if (listener == null) 348 listener = getEntityListener(className); 349 350 return listener; 351 } 352 353 356 public ArrayList <ListenerType> 357 getEntityListeners(String entityClassName) 358 { 359 return _entityListenerMap.get(entityClassName); 360 } 361 362 365 public void addEntityException(String className, Throwable e) 366 { 367 _entityExceptionMap.put(className, e); 368 } 369 370 373 public void addException(Throwable e) 374 { 375 if (_exception == null) { 376 _exception = e; 377 378 Environment.setConfigException(e); 379 } 380 } 381 382 public Throwable getConfigException() 383 { 384 return _exception; 385 } 386 387 390 public void addEmbeddable(String className, EmbeddableType type) 391 { 392 _embeddableMap.put(className, type); 393 } 394 395 398 public void addEntity(String className, EntityType type) 399 { 400 _entityMap.put(className, type); 401 } 402 403 406 public void addDefaultListener(String className, 407 ListenerType type) 408 { 409 _defaultListenerMap.put(className, type); 410 } 411 412 415 public void addEntityListener(String entityClassName, 416 ListenerType listenerType) 417 { 418 ArrayList <ListenerType> listenerList 419 = _entityListenerMap.get(entityClassName); 420 421 if (listenerList == null) { 422 listenerList = new ArrayList <ListenerType>(); 423 _entityListenerMap.put(entityClassName, listenerList); 424 } 425 426 listenerList.add(listenerType); 427 } 428 429 432 public void initEntityHomes() 433 { 434 throw new UnsupportedOperationException (); 435 } 436 437 public AmberPersistenceUnit createPersistenceUnit(String name) 438 { 439 AmberPersistenceUnit unit = new AmberPersistenceUnit(this, name); 440 441 _unitMap.put(unit.getName(), unit); 442 443 return unit; 444 } 445 446 public AmberPersistenceUnit getPersistenceUnit(String name) 447 { 448 if (_exception != null) 449 throw new AmberRuntimeException(_exception); 450 451 return _unitMap.get(name); 452 } 453 454 457 public void addPersistenceRoot(Path root) 458 { 459 Path persistenceXml = root.lookup("META-INF/persistence.xml"); 460 InputStream is = null; 461 462 try { 463 464 Path ormXml = root.lookup("META-INF/orm.xml"); 465 466 EntityMappingsConfig entityMappings = null; 467 468 if (ormXml.exists()) { 469 is = ormXml.openRead(); 470 471 entityMappings = new EntityMappingsConfig(); 472 entityMappings.setRoot(root); 473 474 new Config().configure(entityMappings, is, 475 "com/caucho/amber/cfg/mapping-30.rnc"); 476 } 477 478 HashMap <String , JClass> classMap 479 = new HashMap <String , JClass>(); 480 481 lookupClasses(root.getPath().length(), root, classMap, entityMappings); 483 484 is = persistenceXml.openRead(); 485 486 PersistenceConfig persistence = new PersistenceConfig(); 487 persistence.setRoot(root); 488 489 new Config().configure(persistence, is, 490 "com/caucho/amber/cfg/persistence-30.rnc"); 491 492 for (PersistenceUnitConfig unitConfig : persistence.getUnitList()) { 493 try { 494 unitConfig.addAllClasses(classMap); 495 496 AmberPersistenceUnit unit = unitConfig.init(this, entityMappings); 497 498 _unitMap.put(unit.getName(), unit); 499 } catch (Throwable e) { 500 addException(e); 501 502 log.log(Level.WARNING, e.toString(), e); 503 } 504 } 505 506 } catch (ConfigException e) { 507 addException(e); 508 509 log.warning(e.getMessage()); 510 } catch (Throwable e) { 511 addException(e); 512 513 log.log(Level.WARNING, e.toString(), e); 514 } finally { 515 try { 516 if (is != null) 517 is.close(); 518 } catch (Throwable e) { 519 } 520 } 521 } 522 523 527 public void lookupClasses(int rootNameLength, 528 Path curr, 529 HashMap <String , JClass> classMap, 530 EntityMappingsConfig entityMappings) 531 throws Exception 532 { 533 Iterator <String > it = curr.iterator(); 534 535 while (it.hasNext()) { 536 537 String s = it.next(); 538 539 Path path = curr.lookup(s); 540 541 if (path.isDirectory()) { 542 lookupClasses(rootNameLength, path, classMap, entityMappings); 543 } 544 else if (s.endsWith(".class")) { 545 String packageName = curr.getPath().substring(rootNameLength); 546 547 packageName = packageName.replace('/', '.'); 548 549 if (packageName.length() > 0) 550 packageName = packageName + '.'; 551 552 String className = packageName + s.substring(0, s.length() - 6); 553 554 JClass type = _jClassLoader.forName(className); 555 556 if (type != null) { 557 558 boolean isEntity 559 = type.getAnnotation(javax.persistence.Entity.class) != null; 560 boolean isEmbeddable 561 = type.getAnnotation(javax.persistence.Embeddable.class) != null; 562 boolean isMappedSuperclass 563 = type.getAnnotation(javax.persistence.MappedSuperclass.class) != null; 564 565 EntityConfig entityConfig = null; 566 567 if (entityMappings != null) 568 entityConfig = entityMappings.getEntityConfig(className); 569 570 if (isEntity || isEmbeddable || isMappedSuperclass || 571 (entityConfig != null)) { 572 classMap.put(className, type); 573 } 574 } 575 } 576 } 577 } 578 } 579 | Popular Tags |