1 28 29 package com.caucho.amber.connection; 30 31 import java.util.Iterator; 32 import java.util.List; 33 34 import java.util.logging.Logger; 35 import java.util.logging.Level; 36 37 import java.sql.Connection; 38 import java.sql.ResultSet; 39 import java.sql.SQLException; 40 41 import javax.sql.DataSource; 42 43 import com.caucho.log.Log; 44 45 import com.caucho.util.L10N; 46 47 import com.caucho.config.ConfigException; 48 49 import com.caucho.amber.AmberManager; 50 import com.caucho.amber.AmberQuery; 51 import com.caucho.amber.AmberException; 52 import com.caucho.amber.AmberRuntimeException; 53 54 import com.caucho.amber.entity.Entity; 55 import com.caucho.amber.entity.AmberEntityHome; 56 import com.caucho.amber.entity.EntityItem; 57 import com.caucho.amber.entity.EntityFactory; 58 59 import com.caucho.amber.query.UserQuery; 60 import com.caucho.amber.query.AbstractQuery; 61 import com.caucho.amber.query.QueryParser; 62 import com.caucho.amber.query.CachedQueryKey; 63 64 import com.caucho.amber.collection.AmberCollection; 65 66 69 public class CacheConnectionImpl extends AmberConnectionImpl { 70 private static final L10N L = new L10N(CacheConnectionImpl.class); 71 private static final Logger log = Log.open(CacheConnectionImpl.class); 72 73 private Connection _conn; 74 75 private int _depth; 76 77 private CachedQueryKey _queryKey = new CachedQueryKey(); 78 79 public CacheConnectionImpl(AmberManager amberManager) 80 { 81 super(amberManager); 82 } 83 84 87 public void register(AmberCollection query) 88 { 89 } 90 91 94 public boolean isInTransaction() 95 { 96 return false; 97 } 98 99 102 public Object load(Class cl, Object key) 103 throws AmberException 104 { 105 AmberEntityHome entityHome = _amberManager.getEntityHome(cl.getName()); 106 107 if (entityHome == null) 108 return null; 109 else { 110 try { 111 entityHome.init(); 112 } catch (ConfigException e) { 113 throw new AmberException(e); 114 } 115 116 Entity entity = entityHome.load(this, key); 117 118 return entity; 119 } 120 } 121 122 123 126 public Entity getEntity(EntityItem item) 127 { 128 return item.copy(this); 129 } 130 131 134 public Object makePersistent(Object obj) 135 throws SQLException 136 { 137 Entity entity = (Entity) obj; 138 139 141 if (entity == null) 142 throw new NullPointerException(); 143 144 Class cl = entity.getClass(); 145 146 AmberEntityHome entityHome; 147 entityHome = _amberManager.getEntityHome(entity.getClass().getName()); 148 149 if (entityHome == null) 150 throw new AmberException(L.l("entity has no matching home")); 151 152 entityHome.makePersistent(entity, this, false); 153 154 return entity; 155 } 156 157 160 public Entity loadLazy(Class cl, String name, Object key) 161 { 162 if (key == null) 163 return null; 164 165 try { 166 AmberEntityHome home = _amberManager.getEntityHome(name); 167 168 if (home == null) 169 throw new RuntimeException(L.l("no matching home for {0}", cl.getName())); 170 171 home.init(); 172 173 Object obj = home.loadLazy(this, key); 174 175 Entity entity = (Entity) obj; 176 177 return entity; 178 } catch (SQLException e) { 179 log.log(Level.WARNING, e.toString(), e); 180 181 return null; 182 } catch (ConfigException e) { 183 throw new AmberRuntimeException(e); 184 } 185 } 186 187 190 public Object loadProxy(String name, Object key) 191 { 192 if (key == null) 193 return null; 194 195 try { 196 AmberEntityHome home = _amberManager.getEntityHome(name); 197 198 if (home == null) 199 throw new RuntimeException(L.l("no matching home for {0}", name)); 200 201 home.init(); 202 203 EntityItem item = home.findEntityItem(this, key, false); 204 205 if (item == null) 206 return null; 207 208 EntityFactory factory = home.getEntityFactory(); 209 210 Object entity = factory.getEntity(this, item); 211 212 return entity; 213 } catch (SQLException e) { 214 log.log(Level.WARNING, e.toString(), e); 215 216 return null; 217 } catch (ConfigException e) { 218 throw new AmberRuntimeException(e); 219 } 220 } 221 222 225 public Object load(Class cl, long intKey) 226 throws AmberException 227 { 228 AmberEntityHome entityHome = _amberManager.getEntityHome(cl.getName()); 229 230 if (entityHome == null) 231 return null; 232 233 Object key = entityHome.toObjectKey(intKey); 234 235 return load(cl, key); 236 } 237 238 241 public Object loadLazy(Class cl, long intKey) 242 throws AmberException 243 { 244 AmberEntityHome entityHome = _amberManager.getEntityHome(cl.getName()); 245 246 if (entityHome == null) 247 return null; 248 249 Object key = entityHome.toObjectKey(intKey); 250 251 return loadLazy(cl, cl.getName(), key); 252 } 253 254 257 public Entity getEntity(Class cl, Object key) 258 { 259 return null; 260 } 261 262 265 public void beginTransaction() 266 throws SQLException 267 { 268 } 269 270 273 public void commit() 274 throws SQLException 275 { 276 } 277 278 281 public void rollback() 282 throws SQLException 283 { 284 } 285 286 289 public void flush() 290 throws SQLException 291 { 292 } 293 294 297 public Connection getConnection() 298 throws SQLException 299 { 300 DataSource dataSource = _amberManager.getReadDataSource(); 301 302 if (dataSource == null) 303 dataSource = _amberManager.getDataSource(); 304 305 if (_conn == null) { 306 _conn = dataSource.getConnection(); 307 } 308 else if (_conn.isClosed()) { 309 closeConnection(); 310 _conn = dataSource.getConnection(); 311 } 312 313 return _conn; 314 } 315 316 323 public void makeTransactional(Entity entity) 324 { 325 throw new UnsupportedOperationException(); 326 } 327 328 334 public void update(Object obj) 335 { 336 } 337 338 343 public void create(Object obj) 344 throws SQLException 345 { 346 throw new UnsupportedOperationException(); 347 } 348 349 352 public void update(Entity entity) 353 { 354 } 355 356 361 public void delete(Object obj) 362 throws SQLException 363 { 364 throw new UnsupportedOperationException(); 365 } 366 367 370 public CachedQueryKey getQueryKey() 371 { 372 if (_queryKey == null) 373 _queryKey = new CachedQueryKey(); 374 375 return _queryKey; 376 } 377 378 383 public AmberQuery prepareQuery(String queryString) 384 throws AmberException 385 { 386 return prepareQuery(queryString, false); 387 } 388 389 394 public AmberQuery prepareLazyQuery(String queryString) 395 throws AmberException 396 { 397 return prepareQuery(queryString, true); 398 } 399 400 405 public AmberQuery prepareUpdate(String queryString) 406 throws AmberException 407 { 408 return prepareQuery(queryString, true); 409 } 410 411 416 private AmberQuery prepareQuery(String queryString, boolean isLazy) 417 throws AmberException 418 { 419 try { 420 _amberManager.initEntityHomes(); 421 } catch (Exception e) { 422 throw AmberRuntimeException.create(e); 423 } 424 425 QueryParser parser = new QueryParser(queryString); 426 427 parser.setAmberManager(_amberManager); 428 parser.setLazyResult(isLazy); 429 430 AbstractQuery queryProgram = parser.parse(); 431 UserQuery query = new UserQuery(queryProgram); 432 433 query.setSession(this); 434 435 return query; 436 } 437 438 445 public ResultSet query(String hsql) 446 throws SQLException 447 { 448 AmberQuery query = prepareQuery(hsql); 449 450 return query.executeQuery(); 451 } 452 453 460 public int update(String hsql) 461 throws SQLException 462 { 463 throw new UnsupportedOperationException(); 464 } 465 466 473 public List find(String hsql) 474 throws SQLException 475 { 476 AmberQuery query = prepareQuery(hsql); 477 478 return query.list(); 479 } 480 481 490 public List find(String query, Object value) 491 throws SQLException 492 { 493 return null; 494 } 495 496 505 public List find(String query, Object[] values) 506 throws SQLException 507 { 508 return null; 509 } 510 511 516 public Iterator iterate(String query) 517 throws SQLException 518 { 519 return null; 520 } 521 522 531 public Iterator iterate(String query, Object value) 532 throws SQLException 533 { 534 return null; 535 } 536 537 547 public Iterator iterate(String query, Object[] values) 548 throws SQLException 549 { 550 return null; 551 } 552 555 public void cleanup() 556 { 557 freeConnection(); 558 } 559 560 563 public void pushDepth() 564 { 565 _depth++; 566 } 567 568 571 public void popDepth() 572 { 573 _depth--; 574 575 if (_depth == 0) 576 closeConnection(); 577 } 578 579 582 public void freeConnection() 583 { 584 if (_depth != 0) 585 return; 586 587 closeConnection(); 588 589 _amberManager.freeCacheConnection(this); 590 } 591 592 595 protected void closeConnection() 596 { 597 super.freeConnection(); 598 599 Connection conn = _conn; 600 _conn = null; 601 602 try { 603 if (conn != null) 604 conn.close(); 605 } catch (Exception e) { 606 log.log(Level.WARNING, e.toString(), e); 607 } 608 } 609 610 613 public String toString() 614 { 615 return "CacheConnectionImpl[]"; 616 } 617 618 621 public void finalize() 622 { 623 } 624 } 625 | Popular Tags |