1 28 29 package com.caucho.amber.entity; 30 31 import com.caucho.amber.AmberException; 32 import com.caucho.amber.manager.AmberConnection; 33 import com.caucho.amber.manager.AmberPersistenceUnit; 34 import com.caucho.amber.query.CacheUpdate; 35 import com.caucho.amber.type.EntityType; 36 import com.caucho.config.ConfigException; 37 import com.caucho.log.Log; 38 import com.caucho.util.L10N; 39 40 import java.lang.ref.SoftReference ; 41 import java.lang.reflect.Method ; 42 import java.sql.ResultSet ; 43 import java.sql.SQLException ; 44 import java.util.ArrayList ; 45 import java.util.Map ; 46 import java.util.logging.Level ; 47 import java.util.logging.Logger ; 48 49 52 public class AmberEntityHome { 53 private static final L10N L = new L10N(AmberEntityHome.class); 54 private static final Logger log = Log.open(AmberEntityHome.class); 55 56 private AmberPersistenceUnit _manager; 57 private EntityType _entityType; 58 59 private EntityFactory _entityFactory = new EntityFactory(); 60 61 private Entity _homeBean; 62 63 private ArrayList <SoftReference <CacheUpdate>> _cacheUpdates = 64 new ArrayList <SoftReference <CacheUpdate>>(); 65 66 private EntityKey _cacheKey = new EntityKey(); 67 68 private volatile boolean _isInit; 69 70 private ConfigException _configException; 71 72 private Method _cauchoGetBeanMethod; 73 74 public AmberEntityHome(AmberPersistenceUnit manager, EntityType type) 75 { 76 _manager = manager; 77 _entityType = type; 78 79 try { 80 Class cl = Class.forName("com.caucho.ejb.entity.EntityObject"); 81 _cauchoGetBeanMethod = cl.getMethod("_caucho_getBean", new Class [0]); 82 } catch (Throwable e) { 83 log.log(Level.FINEST, e.toString(), e); 84 } 85 } 86 87 90 public AmberPersistenceUnit getManager() 91 { 92 return _manager; 93 } 94 95 98 public EntityType getEntityType() 99 { 100 return _entityType; 101 } 102 103 106 public EntityType getRootType() 107 { 108 return (EntityType) _entityType.getRootType(); 109 } 110 111 114 public EntityFactory getEntityFactory() 115 { 116 return _entityFactory; 117 } 118 119 122 public void setEntityFactory(EntityFactory factory) 123 { 124 _entityFactory = factory; 125 } 126 127 130 public long getCacheTimeout() 131 { 132 return _entityType.getCacheTimeout(); 133 } 134 135 138 public Class getInstanceClass() 139 { 140 return _entityType.getInstanceClass(); 141 } 142 143 146 void link() 147 throws ConfigException 148 { 149 } 151 152 155 public void init() 156 throws ConfigException 157 { 158 synchronized (this) { 159 if (_isInit) 160 return; 161 _isInit = true; 162 } 163 164 _entityType.init(); 165 166 try { 167 _homeBean = (Entity) _entityType.getInstanceClass().newInstance(); 168 } catch (Exception e) { 169 _entityType.setConfigException(e); 170 171 _configException = new ConfigException(e); 172 throw _configException; 173 } 174 175 _entityType.start(); 176 } 177 178 181 public Object getKeyFromEntity(Entity entity) 182 throws AmberException 183 { 184 return null; 186 } 187 188 191 public Object toObjectKey(long key) 192 { 193 return _entityType.getId().toObjectKey(key); 194 } 195 196 199 public Entity load(AmberConnection aConn, 200 Object key) 201 throws AmberException 202 { 203 return load(aConn, key, null); 204 } 205 206 209 public Entity load(AmberConnection aConn, 210 Object key, 211 Map preloadedProperties) 212 throws AmberException 213 { 214 return find(aConn, key, true, preloadedProperties); 215 } 216 217 220 public Entity loadLazy(AmberConnection aConn, Object key) 221 throws AmberException 222 { 223 return find(aConn, key, false); 224 } 225 226 229 public EntityItem findItem(AmberConnection aConn, 230 ResultSet rs, int index) 231 throws SQLException 232 { 233 return _homeBean.__caucho_home_find(aConn, this, rs, index); 234 } 235 236 239 public Object loadFull(AmberConnection aConn, ResultSet rs, int index) 240 throws SQLException 241 { 242 EntityItem item = findItem(aConn, rs, index); 243 244 if (item == null) 245 return null; 246 247 Entity entity = null; 248 249 Object value = _entityFactory.getEntity(aConn, item); 250 251 if (aConn.isInTransaction()) { 252 if (value instanceof Entity) 253 entity = (Entity) value; 254 else if (_cauchoGetBeanMethod != null) { 255 try { 256 entity = (Entity) _cauchoGetBeanMethod.invoke(value, new Object [0]); 257 entity.__caucho_makePersistent(aConn, item); 258 } catch (Throwable e) { 259 log.log(Level.FINEST, e.toString(), e); 260 } 261 } 262 263 if (entity == null) 264 entity = aConn.getEntity(item); 265 } 266 else 267 entity = item.getEntity(); 268 269 int keyLength = _entityType.getId().getKeyCount(); 270 271 entity.__caucho_load(aConn, rs, index + keyLength); 272 273 return value; 274 } 275 276 279 public Object loadLazy(AmberConnection aConn, ResultSet rs, int index) 280 throws SQLException 281 { 282 EntityItem item = findItem(aConn, rs, index); 283 284 if (item == null) 285 return null; 286 287 return _entityFactory.getEntity(aConn, item); 288 } 289 290 297 public Entity find(AmberConnection aConn, 298 Object key, 299 boolean isLoad) 300 throws AmberException 301 { 302 return find(aConn, key, isLoad, null); 303 } 304 305 312 public Entity find(AmberConnection aConn, 313 Object key, 314 boolean isLoad, 315 Map preloadedProperties) 316 throws AmberException 317 { 318 EntityItem item = findEntityItem(aConn, key, isLoad, preloadedProperties); 319 320 if (item == null) 321 return null; 322 323 325 return item.copy(aConn); 326 } 327 328 335 public EntityItem findEntityItem(AmberConnection aConn, 336 Object key, 337 boolean isLoad) 338 throws AmberException 339 { 340 return findEntityItem(aConn, key, isLoad, null); 341 } 342 343 350 public EntityItem findEntityItem(AmberConnection aConn, 351 Object key, 352 boolean isLoad, 353 Map preloadedProperties) 354 throws AmberException 355 { 356 if (key == null) 357 return null; 359 try { 360 EntityItem item = null; 361 362 item = _manager.getEntity(getRootType(), key); 365 366 if (item == null) { 367 if (_homeBean == null && _configException != null) 368 throw _configException; 369 370 boolean loadFromResultSet = ! getEntityType().hasDependent(); 374 375 Entity cacheEntity; 376 cacheEntity = (Entity) _homeBean.__caucho_home_new(aConn, this, key, loadFromResultSet); 377 378 if (cacheEntity == null) 380 return null; 381 382 if (isLoad) { 383 cacheEntity.__caucho_retrieve(aConn, preloadedProperties); 384 } 385 386 item = new CacheableEntityItem(this, cacheEntity); 387 item = _manager.putEntity(getRootType(), key, item); 388 } 389 else if (isLoad) { 390 item.loadEntity(0, preloadedProperties); 391 } 392 393 return item; 394 } catch (Exception e) { 395 throw AmberException.create(e); 396 } 397 } 398 399 406 public EntityItem setEntityItem(Object key, EntityItem item) 407 throws AmberException 408 { 409 if (key == null) 410 throw new NullPointerException ("primaryKey"); 411 412 try { 413 item.getEntity().__caucho_setConnection(_manager.getCacheConnection()); 414 415 return _manager.putEntity(getRootType(), key, item); 416 } catch (Exception e) { 417 throw AmberException.create(e); 418 } 419 } 420 421 428 public EntityItem findDiscriminatorEntityItem(AmberConnection aConn, 429 Object key, 430 String discriminator) 431 throws SQLException 432 { 433 EntityItem item = null; 434 435 if (aConn.shouldRetrieveFromCache()) 437 item = _manager.getEntity(getRootType(), key); 438 439 if (item == null) { 440 EntityType subEntity 441 = (EntityType) _entityType.getSubClass(discriminator); 442 443 Entity cacheEntity = subEntity.createBean(); 444 445 cacheEntity.__caucho_setPrimaryKey(key); 446 cacheEntity.__caucho_makePersistent(_manager.getCacheConnection(), 447 subEntity); 448 449 item = new CacheableEntityItem(this, cacheEntity); 450 item = _manager.putEntity(getRootType(), key, item); 451 } 452 453 return item; 454 } 455 456 459 public Entity makePersistent(Entity entity, 460 AmberConnection aConn, 461 boolean isLazy) 462 throws SQLException 463 { 464 entity.__caucho_makePersistent(aConn, _entityType); 465 466 return entity; 467 } 468 469 472 public void save(AmberConnection aConn, Entity entity) 473 throws SQLException 474 { 475 entity.__caucho_create(aConn, _entityType); 476 } 477 478 481 public void delete(AmberConnection aConn, Object key) 482 throws SQLException 483 { 484 _manager.removeEntity(getRootType(), key); 485 486 502 } 503 504 507 public void delete(AmberConnection aConn, long primaryKey) 508 throws SQLException 509 { 510 526 } 527 528 531 public void update(Entity entity) 532 throws SQLException 533 { 534 } 535 536 539 public void addUpdate(CacheUpdate update) 540 { 541 _cacheUpdates.add(new SoftReference <CacheUpdate>(update)); 542 } 543 } 544 | Popular Tags |