001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: DbQueryManagerCache.java 3737 2007-05-08 01:48:00Z gbevin $
007: */
008: package com.uwyn.rife.database;
009:
010: import com.uwyn.rife.database.Datasource;
011: import com.uwyn.rife.database.DbQueryManager;
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: /**
016: * This class is a simple cache for {@link DbQueryManager} objects. {@link
017: * DbQueryManager} objects are cached by their related {@link Datasource} and
018: * an identifier.
019: *
020: * @author JR Boyens (jboyens[remove] at uwyn dot com)
021: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
022: * @version $Revision: 3737 $
023: * @since 1.0
024: */
025: public class DbQueryManagerCache {
026: private final Map<Datasource, HashMap<String, DbQueryManager>> mCache = new HashMap<Datasource, HashMap<String, DbQueryManager>>();
027:
028: /**
029: * Default constructor
030: *
031: * @since 1.0
032: */
033: public DbQueryManagerCache() {
034: }
035:
036: /**
037: * Retrieve a cached {@link DbQueryManager}
038: *
039: * @param datasource the {@link Datasource} associated with the
040: * desired {@link DbQueryManager}
041: * @param identifier the identifier associate with the desired {@link
042: * DbQueryManager}
043: * @return the cached {@link DbQueryManager}
044: * @since 1.0
045: */
046: public DbQueryManager get(Datasource datasource, String identifier) {
047: if (null == datasource)
048: throw new IllegalArgumentException(
049: "datasource can't be null.");
050: if (null == identifier)
051: throw new IllegalArgumentException(
052: "identifier can't be null.");
053:
054: HashMap<String, DbQueryManager> dbquery_managers = null;
055: synchronized (mCache) {
056: dbquery_managers = mCache.get(datasource);
057: if (null == dbquery_managers) {
058: return null;
059: }
060: }
061:
062: synchronized (dbquery_managers) {
063: return dbquery_managers.get(identifier);
064: }
065: }
066:
067: /**
068: * Place a {@link DbQueryManager} in the cache
069: *
070: * @param datasource the {@link Datasource} associated with the {@link
071: * DbQueryManager} to put in the cache
072: * @param identifier the identifier associated with the {@link
073: * DbQueryManager} to put in the cache
074: * @param dbQueryManager the {@link DbQueryManager} to put in the
075: * cache
076: * @since 1.0
077: */
078: public void put(Datasource datasource, String identifier,
079: DbQueryManager dbQueryManager) {
080: if (null == datasource)
081: throw new IllegalArgumentException(
082: "datasource can't be null.");
083: if (null == identifier)
084: throw new IllegalArgumentException(
085: "identifier can't be null.");
086: if (null == dbQueryManager)
087: throw new IllegalArgumentException(
088: "dbQueryManager can't be null.");
089:
090: HashMap<String, DbQueryManager> dbquery_managers = null;
091:
092: synchronized (mCache) {
093: dbquery_managers = mCache.get(datasource);
094:
095: if (null == dbquery_managers) {
096: dbquery_managers = new HashMap<String, DbQueryManager>();
097: mCache.put(datasource, dbquery_managers);
098: }
099: }
100:
101: synchronized (dbquery_managers) {
102: dbquery_managers.put(identifier, dbQueryManager);
103: }
104:
105: assert mCache.containsKey(datasource);
106: assert mCache.get(datasource).containsKey(identifier);
107: assert mCache.get(datasource).get(identifier) == dbQueryManager;
108: }
109: }
|