KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > EnvAmberManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
53 import java.lang.ref.SoftReference JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.logging.Level JavaDoc;
58 import java.util.logging.Logger JavaDoc;
59
60 /**
61  * Main interface between Resin and the connector. It's the
62  * top-level SPI class for creating the SPI ManagedConnections.
63  *
64  * The resource configuration in Resin's web.xml will use bean-style
65  * configuration to configure the ManagecConnectionFactory.
66  */

67 public class EnvAmberManager {
68   private static final Logger JavaDoc 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 JavaDoc _parentLoader;
75
76   private ArrayList JavaDoc<AmberPersistenceUnit> _managerList
77     = new ArrayList JavaDoc<AmberPersistenceUnit>();
78
79   private AmberEnhancer _enhancer;
80
81   private long _tableCacheTimeout = 250;
82
83   private JClassLoader _jClassLoader;
84
85   private HashMap JavaDoc<String JavaDoc,AmberEntityHome> _entityHomeMap
86     = new HashMap JavaDoc<String JavaDoc,AmberEntityHome>();
87
88   private LruCache<QueryCacheKey,SoftReference JavaDoc<ResultSetCacheChunk>> _queryCache
89     = new LruCache<QueryCacheKey,SoftReference JavaDoc<ResultSetCacheChunk>>(1024);
90
91   private LruCache<EntityKey,SoftReference JavaDoc<EntityItem>> _entityCache
92     = new LruCache<EntityKey,SoftReference JavaDoc<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 JavaDoc e) {
111       throw e;
112     } catch (Exception JavaDoc e) {
113       throw new RuntimeException JavaDoc(e);
114     }
115
116     // _enhancer = new AmberEnhancer(this);
117

118     EnhancerManager.create().addClassEnhancer(_enhancer);
119
120     try {
121       bindProxy();
122     } catch (Throwable JavaDoc e) {
123       log.log(Level.FINE, e.toString(), e);
124     }
125   }
126
127   private void bindProxy()
128     throws Throwable JavaDoc
129   {
130     /*
131     EntityManagerProxy userManager = null;//new EntityManagerProxy(this);
132
133     new InitialContext().rebind("java:comp/EntityManager", userManager);
134     */

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   /**
150    * Adds an amber manager.
151    */

152   public void addAmberManager(AmberPersistenceUnit manager)
153   {
154     _managerList.add(manager);
155   }
156
157   /**
158    * Set the default table cache time.
159    */

160   public void setTableCacheTimeout(long timeout)
161   {
162     _tableCacheTimeout = timeout;
163   }
164
165   /**
166    * Get the default table cache time.
167    */

168   public long getTableCacheTimeout()
169   {
170     return _tableCacheTimeout;
171   }
172
173   /**
174    * Returns a new xid.
175    */

176   public long getXid()
177   {
178     synchronized (this) {
179       return _xid++;
180     }
181   }
182
183   /**
184    * Returns the enhanced loader.
185    */

186   public ClassLoader JavaDoc getEnhancedLoader()
187   {
188     return _parentLoader;
189   }
190
191   /**
192    * Returns the enhanced loader.
193    */

194   public JClassLoader getJClassLoader()
195   {
196     return _jClassLoader;
197   }
198
199   /**
200    * Adds the entity home.
201    */

202   public void addEntityHome(String JavaDoc name, AmberEntityHome home)
203   {
204     _entityHomeMap.put(name, home);
205   }
206
207   /**
208    * Returns the entity home.
209    */

210   public AmberEntityHome getEntityHome(String JavaDoc name)
211   {
212     if (! _isInit) {
213       /* XXX:
214       try {
215     initEntityHomes();
216       } catch (RuntimeException e) {
217     throw e;
218       } catch (Exception e) {
219     throw new AmberRuntimeException(e);
220       }
221       */

222     }
223     
224     return _entityHomeMap.get(name);
225   }
226
227   /**
228    * Returns a matching entity.
229    */

230   public EntityType getEntity(String JavaDoc 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   /**
241    * Returns a matching entity.
242    */

243   public EntityType getEntityByInstanceClass(String JavaDoc className)
244   {
245     throw new UnsupportedOperationException JavaDoc();
246   }
247
248   /**
249    * Sets the generator.
250    */

251   public void setGenerator(AmberGenerator generator)
252   {
253     _generator = generator;
254   }
255
256   /**
257    * Sets the generator.
258    */

259   public AmberGenerator getGenerator()
260   {
261     if (_generator != null)
262       return _generator;
263     else if (_enhancer != null)
264       return _enhancer;
265     else {
266       // _generator = new AmberGeneratorImpl(this);
267

268       return _generator;
269     }
270   }
271
272   /**
273    * Initialize the resource.
274    */

275   public void initLoaders()
276     throws ConfigException, IOException JavaDoc
277   {
278   }
279
280   /**
281    * Returns the cache connection.
282    */

283   public AmberConnection createAmberConnection()
284   {
285     // XXX: needs to be an EnvAmberConnection
286
return _managerList.get(0).createAmberConnection();
287   }
288
289   /**
290    * Initialize the home interfaces.
291    */

292   public void initEntityHomes()
293     throws Exception JavaDoc
294   {
295     for (AmberPersistenceUnit manager : _managerList)
296       manager.initEntityHomes();
297   }
298
299   /**
300    * Initialize the resource.
301    */

302   public void init()
303     throws ConfigException, IOException JavaDoc
304   {
305     initLoaders();
306   }
307
308   /**
309    * Returns an EntityHome.
310    */

311   public AmberEntityHome getHome(Class JavaDoc cl)
312   {
313     return getEntityHome(cl.getName());
314   }
315
316   /**
317    * Returns the query result.
318    */

319   public ResultSetCacheChunk getQueryChunk(QueryCacheKey key)
320   {
321     SoftReference JavaDoc<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   /**
336    * Sets the query result.
337    */

338   public void putQueryChunk(QueryCacheKey key, ResultSetCacheChunk chunk)
339   {
340     _queryCache.put(key, new SoftReference JavaDoc<ResultSetCacheChunk>(chunk));
341   }
342
343   /**
344    * Returns the entity item.
345    */

346   public EntityItem getEntityItem(String JavaDoc homeName, Object JavaDoc key)
347     throws AmberException
348   {
349     AmberEntityHome home = getEntityHome(homeName);
350
351     // return home.findEntityItem(getCacheConnection(), key, false);
352

353     return null; // XXX:
354
}
355
356   /**
357    * Returns the query result.
358    */

359   public EntityItem getEntity(EntityType rootType, Object JavaDoc key)
360   {
361     SoftReference JavaDoc<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   /**
375    * Sets the entity result.
376    */

377   public EntityItem putEntity(EntityType rootType,
378                   Object JavaDoc key,
379                   EntityItem entity)
380   {
381     SoftReference JavaDoc<EntityItem> ref = new SoftReference JavaDoc<EntityItem>(entity);
382     EntityKey entityKey = new EntityKey(rootType, key);
383     
384     ref = _entityCache.putIfNew(entityKey, ref);
385
386     return ref.get();
387   }
388
389   /**
390    * Remove the entity result.
391    */

392   public EntityItem removeEntity(EntityType rootType, Object JavaDoc key)
393   {
394     SoftReference JavaDoc<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   /**
408    * Completions affecting the cache.
409    */

410   public void complete(ArrayList JavaDoc<AmberCompletion> completions)
411   {
412     int size = completions.size();
413     if (size == 0)
414       return;
415
416     synchronized (_entityCache) {
417       Iterator JavaDoc<LruCache.Entry<EntityKey,SoftReference JavaDoc<EntityItem>>> iter;
418
419       iter = _entityCache.iterator();
420       while (iter.hasNext()) {
421     LruCache.Entry<EntityKey,SoftReference JavaDoc<EntityItem>> entry;
422     entry = iter.next();
423
424     EntityKey key = entry.getKey();
425     SoftReference JavaDoc<EntityItem> valueRef = entry.getValue();
426     EntityItem value = valueRef.get();
427
428     if (value == null)
429       continue;
430
431     EntityType entityRoot = key.getEntityType();
432     Object JavaDoc entityKey = key.getKey();
433
434     for (int i = 0; i < size; i++) {
435       if (completions.get(i).complete(entityRoot, entityKey, value)) {
436         // XXX: delete
437
}
438     }
439       }
440     }
441
442     synchronized (_queryCache) {
443       Iterator JavaDoc<SoftReference JavaDoc<ResultSetCacheChunk>> iter;
444
445       iter = _queryCache.values();
446       while (iter.hasNext()) {
447     SoftReference JavaDoc<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           // XXX: delete
455
}
456       }
457     }
458       }
459     }
460   }
461
462   /**
463    * destroys the manager.
464    */

465   public void destroy()
466   {
467     _queryCache = null;
468     _entityCache = null;
469     _parentLoader = null;
470   }
471
472   public String JavaDoc toString()
473   {
474     return "EnvAmberManager[]";
475   }
476 }
477
Popular Tags