001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber;
031:
032: import com.caucho.amber.entity.AmberCompletion;
033: import com.caucho.amber.entity.AmberEntityHome;
034: import com.caucho.amber.entity.EntityItem;
035: import com.caucho.amber.entity.EntityKey;
036: import com.caucho.amber.gen.AmberEnhancer;
037: import com.caucho.amber.gen.AmberGenerator;
038: import com.caucho.amber.manager.AmberConnection;
039: import com.caucho.amber.manager.AmberPersistenceUnit;
040: import com.caucho.amber.query.QueryCacheKey;
041: import com.caucho.amber.query.ResultSetCacheChunk;
042: import com.caucho.amber.type.EntityType;
043: import com.caucho.bytecode.JClassLoader;
044: import com.caucho.config.ConfigException;
045: import com.caucho.loader.DynamicClassLoader;
046: import com.caucho.loader.EnvironmentLocal;
047: import com.caucho.loader.enhancer.EnhancerManager;
048: import com.caucho.log.Log;
049: import com.caucho.util.L10N;
050: import com.caucho.util.LruCache;
051:
052: import java.io.IOException;
053: import java.lang.ref.SoftReference;
054: import java.util.ArrayList;
055: import java.util.HashMap;
056: import java.util.Iterator;
057: import java.util.logging.Level;
058: import java.util.logging.Logger;
059:
060: /**
061: * Main interface between Resin and the connector. It's the
062: * top-level SPI class for creating the SPI ManagedConnections.
063: *
064: * The resource configuration in Resin's web.xml will use bean-style
065: * configuration to configure the ManagecConnectionFactory.
066: */
067: public class EnvAmberManager {
068: private static final Logger log = Logger
069: .getLogger(AmberPersistenceUnit.class.getName());
070: private static final L10N L = new L10N(AmberPersistenceUnit.class);
071:
072: private static EnvironmentLocal<EnvAmberManager> _localManager = new EnvironmentLocal<EnvAmberManager>();
073:
074: private ClassLoader _parentLoader;
075:
076: private ArrayList<AmberPersistenceUnit> _managerList = new ArrayList<AmberPersistenceUnit>();
077:
078: private AmberEnhancer _enhancer;
079:
080: private long _tableCacheTimeout = 250;
081:
082: private JClassLoader _jClassLoader;
083:
084: private HashMap<String, AmberEntityHome> _entityHomeMap = new HashMap<String, AmberEntityHome>();
085:
086: private LruCache<QueryCacheKey, SoftReference<ResultSetCacheChunk>> _queryCache = new LruCache<QueryCacheKey, SoftReference<ResultSetCacheChunk>>(
087: 1024);
088:
089: private LruCache<EntityKey, SoftReference<EntityItem>> _entityCache = new LruCache<EntityKey, SoftReference<EntityItem>>(
090: 32 * 1024);
091:
092: private EntityKey _entityKey = new EntityKey();
093:
094: private long _xid;
095:
096: private AmberGenerator _generator;
097:
098: private volatile boolean _isInit;
099:
100: private EnvAmberManager() {
101: _parentLoader = Thread.currentThread().getContextClassLoader();
102: _jClassLoader = EnhancerManager.create(_parentLoader)
103: .getJavaClassLoader();
104:
105: try {
106: if (_parentLoader instanceof DynamicClassLoader)
107: ((DynamicClassLoader) _parentLoader).make();
108: } catch (RuntimeException e) {
109: throw e;
110: } catch (Exception e) {
111: throw new RuntimeException(e);
112: }
113:
114: // _enhancer = new AmberEnhancer(this);
115: EnhancerManager.create().addClassEnhancer(_enhancer);
116: }
117:
118: public static EnvAmberManager createLocal() {
119: EnvAmberManager manager = _localManager.get();
120:
121: if (manager == null) {
122: manager = new EnvAmberManager();
123: _localManager.set(manager);
124: }
125:
126: return manager;
127: }
128:
129: /**
130: * Adds an amber manager.
131: */
132: public void addAmberManager(AmberPersistenceUnit manager) {
133: _managerList.add(manager);
134: }
135:
136: /**
137: * Set the default table cache time.
138: */
139: public void setTableCacheTimeout(long timeout) {
140: _tableCacheTimeout = timeout;
141: }
142:
143: /**
144: * Get the default table cache time.
145: */
146: public long getTableCacheTimeout() {
147: return _tableCacheTimeout;
148: }
149:
150: /**
151: * Returns a new xid.
152: */
153: public long getXid() {
154: synchronized (this ) {
155: return _xid++;
156: }
157: }
158:
159: /**
160: * Returns the enhanced loader.
161: */
162: public ClassLoader getEnhancedLoader() {
163: return _parentLoader;
164: }
165:
166: /**
167: * Returns the enhanced loader.
168: */
169: public JClassLoader getJClassLoader() {
170: return _jClassLoader;
171: }
172:
173: /**
174: * Adds the entity home.
175: */
176: public void addEntityHome(String name, AmberEntityHome home) {
177: _entityHomeMap.put(name, home);
178: }
179:
180: /**
181: * Returns the entity home.
182: */
183: public AmberEntityHome getEntityHome(String name) {
184: if (!_isInit) {
185: /* XXX:
186: try {
187: initEntityHomes();
188: } catch (RuntimeException e) {
189: throw e;
190: } catch (Exception e) {
191: throw new AmberRuntimeException(e);
192: }
193: */
194: }
195:
196: return _entityHomeMap.get(name);
197: }
198:
199: /**
200: * Returns a matching entity.
201: */
202: public EntityType getEntity(String className) {
203: AmberEntityHome home = _entityHomeMap.get(className);
204:
205: if (home != null)
206: return home.getEntityType();
207: else
208: return null;
209: }
210:
211: /**
212: * Returns a matching entity.
213: */
214: public EntityType getEntityByInstanceClass(String className) {
215: throw new UnsupportedOperationException();
216: }
217:
218: /**
219: * Sets the generator.
220: */
221: public void setGenerator(AmberGenerator generator) {
222: _generator = generator;
223: }
224:
225: /**
226: * Sets the generator.
227: */
228: public AmberGenerator getGenerator() {
229: if (_generator != null)
230: return _generator;
231: else if (_enhancer != null)
232: return _enhancer;
233: else {
234: // _generator = new AmberGeneratorImpl(this);
235:
236: return _generator;
237: }
238: }
239:
240: /**
241: * Initialize the resource.
242: */
243: public void initLoaders() throws ConfigException, IOException {
244: }
245:
246: /**
247: * Returns the cache connection.
248: */
249: public AmberConnection createAmberConnection(boolean isExtended) {
250: // XXX: needs to be an EnvAmberConnection
251: return _managerList.get(0).createAmberConnection(isExtended);
252: }
253:
254: /**
255: * Initialize the home interfaces.
256: */
257: public void initEntityHomes() throws Exception {
258: for (AmberPersistenceUnit manager : _managerList)
259: manager.initEntityHomes();
260: }
261:
262: /**
263: * Initialize the resource.
264: */
265: public void init() throws ConfigException, IOException {
266: initLoaders();
267: }
268:
269: /**
270: * Returns an EntityHome.
271: */
272: public AmberEntityHome getHome(Class cl) {
273: return getEntityHome(cl.getName());
274: }
275:
276: /**
277: * Returns the query result.
278: */
279: public ResultSetCacheChunk getQueryChunk(QueryCacheKey key) {
280: SoftReference<ResultSetCacheChunk> ref = _queryCache.get(key);
281:
282: if (ref == null)
283: return null;
284: else {
285: ResultSetCacheChunk chunk = ref.get();
286:
287: if (chunk != null && chunk.isValid())
288: return chunk;
289: else
290: return null;
291: }
292: }
293:
294: /**
295: * Sets the query result.
296: */
297: public void putQueryChunk(QueryCacheKey key,
298: ResultSetCacheChunk chunk) {
299: _queryCache.put(key, new SoftReference<ResultSetCacheChunk>(
300: chunk));
301: }
302:
303: /**
304: * Returns the entity item.
305: */
306: public EntityItem getEntityItem(String homeName, Object key)
307: throws AmberException {
308: AmberEntityHome home = getEntityHome(homeName);
309:
310: // return home.findEntityItem(getCacheConnection(), key, false);
311:
312: return null; // XXX:
313: }
314:
315: /**
316: * Returns the query result.
317: */
318: public EntityItem getEntity(EntityType rootType, Object key) {
319: SoftReference<EntityItem> ref;
320:
321: synchronized (_entityKey) {
322: _entityKey.init(rootType.getInstanceClass(), key);
323: ref = _entityCache.get(_entityKey);
324: }
325:
326: if (ref != null)
327: return ref.get();
328: else
329: return null;
330: }
331:
332: /**
333: * Sets the entity result.
334: */
335: public EntityItem putEntity(EntityType rootType, Object key,
336: EntityItem entity) {
337: SoftReference<EntityItem> ref = new SoftReference<EntityItem>(
338: entity);
339: EntityKey entityKey = new EntityKey(
340: rootType.getInstanceClass(), key);
341:
342: ref = _entityCache.putIfNew(entityKey, ref);
343:
344: return ref.get();
345: }
346:
347: /**
348: * Remove the entity result.
349: */
350: public EntityItem removeEntity(EntityType rootType, Object key) {
351: SoftReference<EntityItem> ref;
352:
353: synchronized (_entityKey) {
354: _entityKey.init(rootType.getInstanceClass(), key);
355: ref = _entityCache.remove(_entityKey);
356: }
357:
358: if (ref != null)
359: return ref.get();
360: else
361: return null;
362: }
363:
364: /**
365: * Completions affecting the cache.
366: */
367: public void complete(ArrayList<AmberCompletion> completions) {
368: int size = completions.size();
369: if (size == 0)
370: return;
371:
372: synchronized (_entityCache) {
373: Iterator<LruCache.Entry<EntityKey, SoftReference<EntityItem>>> iter;
374:
375: iter = _entityCache.iterator();
376: while (iter.hasNext()) {
377: LruCache.Entry<EntityKey, SoftReference<EntityItem>> entry;
378: entry = iter.next();
379:
380: EntityKey key = entry.getKey();
381: SoftReference<EntityItem> valueRef = entry.getValue();
382: EntityItem value = valueRef.get();
383:
384: if (value == null)
385: continue;
386:
387: EntityType entityRoot = value.getEntityHome()
388: .getEntityType();
389: Object entityKey = key.getKey();
390:
391: for (int i = 0; i < size; i++) {
392: if (completions.get(i).complete(entityRoot,
393: entityKey, value)) {
394: // XXX: delete
395: }
396: }
397: }
398: }
399:
400: synchronized (_queryCache) {
401: Iterator<SoftReference<ResultSetCacheChunk>> iter;
402:
403: iter = _queryCache.values();
404: while (iter.hasNext()) {
405: SoftReference<ResultSetCacheChunk> ref = iter.next();
406:
407: ResultSetCacheChunk chunk = ref.get();
408:
409: if (chunk != null) {
410: for (int i = 0; i < size; i++) {
411: if (completions.get(i).complete(chunk)) {
412: // XXX: delete
413: }
414: }
415: }
416: }
417: }
418: }
419:
420: /**
421: * destroys the manager.
422: */
423: public void destroy() {
424: _queryCache = null;
425: _entityCache = null;
426: _parentLoader = null;
427: }
428:
429: public String toString() {
430: return "EnvAmberManager[]";
431: }
432: }
|