1 29 30 package com.caucho.amber; 31 32 import com.caucho.amber.entity.AmberCompletion; 33 import com.caucho.amber.entity.AmberEntityHome; 34 import com.caucho.amber.entity.EntityItem; 35 import com.caucho.amber.entity.EntityKey; 36 import com.caucho.amber.gen.AmberEnhancer; 37 import com.caucho.amber.gen.AmberGenerator; 38 import com.caucho.amber.manager.AmberConnection; 39 import com.caucho.amber.manager.AmberPersistenceUnit; 40 import com.caucho.amber.query.QueryCacheKey; 41 import com.caucho.amber.query.ResultSetCacheChunk; 42 import com.caucho.amber.type.EntityType; 43 import com.caucho.bytecode.JClassLoader; 44 import com.caucho.config.ConfigException; 45 import com.caucho.loader.DynamicClassLoader; 46 import com.caucho.loader.EnvironmentLocal; 47 import com.caucho.loader.enhancer.EnhancerManager; 48 import com.caucho.log.Log; 49 import com.caucho.util.L10N; 50 import com.caucho.util.LruCache; 51 52 import java.io.IOException ; 53 import java.lang.ref.SoftReference ; 54 import java.util.ArrayList ; 55 import java.util.HashMap ; 56 import java.util.Iterator ; 57 import java.util.logging.Level ; 58 import java.util.logging.Logger ; 59 60 67 public class EnvAmberManager { 68 private static final Logger log = Log.open(AmberPersistenceUnit.class); 69 private static final L10N L = new L10N(AmberPersistenceUnit.class); 70 71 private static EnvironmentLocal<EnvAmberManager> _localManager 72 = new EnvironmentLocal<EnvAmberManager>(); 73 74 private ClassLoader _parentLoader; 75 76 private ArrayList <AmberPersistenceUnit> _managerList 77 = new ArrayList <AmberPersistenceUnit>(); 78 79 private AmberEnhancer _enhancer; 80 81 private long _tableCacheTimeout = 250; 82 83 private JClassLoader _jClassLoader; 84 85 private HashMap <String ,AmberEntityHome> _entityHomeMap 86 = new HashMap <String ,AmberEntityHome>(); 87 88 private LruCache<QueryCacheKey,SoftReference <ResultSetCacheChunk>> _queryCache 89 = new LruCache<QueryCacheKey,SoftReference <ResultSetCacheChunk>>(1024); 90 91 private LruCache<EntityKey,SoftReference <EntityItem>> _entityCache 92 = new LruCache<EntityKey,SoftReference <EntityItem>>(32 * 1024); 93 94 private EntityKey _entityKey = new EntityKey(); 95 96 private long _xid; 97 98 private AmberGenerator _generator; 99 100 private volatile boolean _isInit; 101 102 private EnvAmberManager() 103 { 104 _parentLoader = Thread.currentThread().getContextClassLoader(); 105 _jClassLoader = EnhancerManager.create(_parentLoader).getJavaClassLoader(); 106 107 try { 108 if (_parentLoader instanceof DynamicClassLoader) 109 ((DynamicClassLoader) _parentLoader).make(); 110 } catch (RuntimeException e) { 111 throw e; 112 } catch (Exception e) { 113 throw new RuntimeException (e); 114 } 115 116 118 EnhancerManager.create().addClassEnhancer(_enhancer); 119 120 try { 121 bindProxy(); 122 } catch (Throwable e) { 123 log.log(Level.FINE, e.toString(), e); 124 } 125 } 126 127 private void bindProxy() 128 throws Throwable 129 { 130 135 } 136 137 public static EnvAmberManager createLocal() 138 { 139 EnvAmberManager manager = _localManager.get(); 140 141 if (manager == null) { 142 manager = new EnvAmberManager(); 143 _localManager.set(manager); 144 } 145 146 return manager; 147 } 148 149 152 public void addAmberManager(AmberPersistenceUnit manager) 153 { 154 _managerList.add(manager); 155 } 156 157 160 public void setTableCacheTimeout(long timeout) 161 { 162 _tableCacheTimeout = timeout; 163 } 164 165 168 public long getTableCacheTimeout() 169 { 170 return _tableCacheTimeout; 171 } 172 173 176 public long getXid() 177 { 178 synchronized (this) { 179 return _xid++; 180 } 181 } 182 183 186 public ClassLoader getEnhancedLoader() 187 { 188 return _parentLoader; 189 } 190 191 194 public JClassLoader getJClassLoader() 195 { 196 return _jClassLoader; 197 } 198 199 202 public void addEntityHome(String name, AmberEntityHome home) 203 { 204 _entityHomeMap.put(name, home); 205 } 206 207 210 public AmberEntityHome getEntityHome(String name) 211 { 212 if (! _isInit) { 213 222 } 223 224 return _entityHomeMap.get(name); 225 } 226 227 230 public EntityType getEntity(String className) 231 { 232 AmberEntityHome home = _entityHomeMap.get(className); 233 234 if (home != null) 235 return home.getEntityType(); 236 else 237 return null; 238 } 239 240 243 public EntityType getEntityByInstanceClass(String className) 244 { 245 throw new UnsupportedOperationException (); 246 } 247 248 251 public void setGenerator(AmberGenerator generator) 252 { 253 _generator = generator; 254 } 255 256 259 public AmberGenerator getGenerator() 260 { 261 if (_generator != null) 262 return _generator; 263 else if (_enhancer != null) 264 return _enhancer; 265 else { 266 268 return _generator; 269 } 270 } 271 272 275 public void initLoaders() 276 throws ConfigException, IOException 277 { 278 } 279 280 283 public AmberConnection createAmberConnection() 284 { 285 return _managerList.get(0).createAmberConnection(); 287 } 288 289 292 public void initEntityHomes() 293 throws Exception 294 { 295 for (AmberPersistenceUnit manager : _managerList) 296 manager.initEntityHomes(); 297 } 298 299 302 public void init() 303 throws ConfigException, IOException 304 { 305 initLoaders(); 306 } 307 308 311 public AmberEntityHome getHome(Class cl) 312 { 313 return getEntityHome(cl.getName()); 314 } 315 316 319 public ResultSetCacheChunk getQueryChunk(QueryCacheKey key) 320 { 321 SoftReference <ResultSetCacheChunk> ref = _queryCache.get(key); 322 323 if (ref == null) 324 return null; 325 else { 326 ResultSetCacheChunk chunk = ref.get(); 327 328 if (chunk != null && chunk.isValid()) 329 return chunk; 330 else 331 return null; 332 } 333 } 334 335 338 public void putQueryChunk(QueryCacheKey key, ResultSetCacheChunk chunk) 339 { 340 _queryCache.put(key, new SoftReference <ResultSetCacheChunk>(chunk)); 341 } 342 343 346 public EntityItem getEntityItem(String homeName, Object key) 347 throws AmberException 348 { 349 AmberEntityHome home = getEntityHome(homeName); 350 351 353 return null; } 355 356 359 public EntityItem getEntity(EntityType rootType, Object key) 360 { 361 SoftReference <EntityItem> ref; 362 363 synchronized (_entityKey) { 364 _entityKey.init(rootType, key); 365 ref = _entityCache.get(_entityKey); 366 } 367 368 if (ref != null) 369 return ref.get(); 370 else 371 return null; 372 } 373 374 377 public EntityItem putEntity(EntityType rootType, 378 Object key, 379 EntityItem entity) 380 { 381 SoftReference <EntityItem> ref = new SoftReference <EntityItem>(entity); 382 EntityKey entityKey = new EntityKey(rootType, key); 383 384 ref = _entityCache.putIfNew(entityKey, ref); 385 386 return ref.get(); 387 } 388 389 392 public EntityItem removeEntity(EntityType rootType, Object key) 393 { 394 SoftReference <EntityItem> ref; 395 396 synchronized (_entityKey) { 397 _entityKey.init(rootType, key); 398 ref = _entityCache.remove(_entityKey); 399 } 400 401 if (ref != null) 402 return ref.get(); 403 else 404 return null; 405 } 406 407 410 public void complete(ArrayList <AmberCompletion> completions) 411 { 412 int size = completions.size(); 413 if (size == 0) 414 return; 415 416 synchronized (_entityCache) { 417 Iterator <LruCache.Entry<EntityKey,SoftReference <EntityItem>>> iter; 418 419 iter = _entityCache.iterator(); 420 while (iter.hasNext()) { 421 LruCache.Entry<EntityKey,SoftReference <EntityItem>> entry; 422 entry = iter.next(); 423 424 EntityKey key = entry.getKey(); 425 SoftReference <EntityItem> valueRef = entry.getValue(); 426 EntityItem value = valueRef.get(); 427 428 if (value == null) 429 continue; 430 431 EntityType entityRoot = key.getEntityType(); 432 Object entityKey = key.getKey(); 433 434 for (int i = 0; i < size; i++) { 435 if (completions.get(i).complete(entityRoot, entityKey, value)) { 436 } 438 } 439 } 440 } 441 442 synchronized (_queryCache) { 443 Iterator <SoftReference <ResultSetCacheChunk>> iter; 444 445 iter = _queryCache.values(); 446 while (iter.hasNext()) { 447 SoftReference <ResultSetCacheChunk> ref = iter.next(); 448 449 ResultSetCacheChunk chunk = ref.get(); 450 451 if (chunk != null) { 452 for (int i = 0; i < size; i++) { 453 if (completions.get(i).complete(chunk)) { 454 } 456 } 457 } 458 } 459 } 460 } 461 462 465 public void destroy() 466 { 467 _queryCache = null; 468 _entityCache = null; 469 _parentLoader = null; 470 } 471 472 public String toString() 473 { 474 return "EnvAmberManager[]"; 475 } 476 } 477 | Popular Tags |