?? dbcache.java
字號:
/******************************************************************************* * Copyright (C) 2002, 2003 * ingenieurbuero fuer innovative informationstechnik (iiit) * Dipl.-Ing. Joerg Beckmann, Dortmund, Germany * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * version $Id: DbCache.java,v 1.18 2003/04/14 20:39:18 joerg Exp $ ******************************************************************************/package de.iiit.access.server.plugins.cache;import de.iiit.access.common.cache.*;import de.iiit.access.server.*;import de.iiit.access.server.api.*;import de.iiit.access.server.util.*;import de.iiit.access.server.util.db.cachedb.*;import de.iiit.xmlconfig.*;import de.iiit.util.*;import de.iiit.jdbc.*;import de.iiit.cache.*;import org.apache.log4j.Logger;import java.util.*;import java.security.*;/** This cache can be loaded into the AccessServer as a plug-in to accelerate the * evaluation of expressions. It uses one or more MySQL databases as 2<sup>nd</sup>-level * caches. */public class DbCache implements CachePluginIf{ /** CVS Version Tag */ private static final String vcid = "$Id: DbCache.java,v 1.18 2003/04/14 20:39:18 joerg Exp $"; private static final String INVALIDATION_TIMEOUT = "InvalidationTimeout"; private static final String LRU_TIMEOUT = "LRUTimeout"; private static final String SLEEP_TIME = "SleepTime"; private static final String MD5PATTERN_LENGTH = "Md5PatternLength"; private static final String JDBC_DRIVER = "ClassName"; private static final String JDBC_URL = "Url"; private static final String JDBC_USERNAME = "UserName"; private static final String JDBC_PASSWORD = "Password"; private static final String JDBC_CONNECTIONS = "Connections"; private static final String JDBC_DRIVER_CONFIG = "JdbcDriver"; private static final String CACHE_DATABASE_CONFIG = "CacheDatabase"; private static final char[] hexadecimal = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private Configuration config = null; private Vector cacheDbConfigVector = null; private Vector jdbcConfigVector = null; private Vector cachePools = new Vector(); private Hashtable md5PatternIndex = new Hashtable(); private int md5PatternLength = 0; private int invalidationTimeout; private int lruTimeout; private int sleepTime; private boolean ignoreCase; private Logger logger = Logger.getLogger(this.getClass()); UserRightsCache cache = null; /** Creates a new instance of DbCachePlugin */ public DbCache() { } /** Initializes the cache * @param config the configuration of the cache. */ public void initialize(Configuration config) { boolean cfgError = false; this.config = config; ignoreCase = AccessServer.getIgnoreCase(); invalidationTimeout = Integer.parseInt(config.getAttribute(INVALIDATION_TIMEOUT, "1800")) * 1000; // 30 Min lruTimeout = Integer.parseInt(config.getAttribute(LRU_TIMEOUT , "1800")) * 1000; // 30 Min sleepTime = Integer.parseInt(config.getAttribute(LRU_TIMEOUT , "10")) * 1000; // 10 Sec md5PatternLength = Integer.parseInt(config.getAttribute(MD5PATTERN_LENGTH, "0")); cacheDbConfigVector = config.getSubConfigurations(CACHE_DATABASE_CONFIG); if (cacheDbConfigVector == null) { logger.fatal("No configuration for cache db found"); cfgError = true; } jdbcConfigVector = config.getSubConfigurations(JDBC_DRIVER_CONFIG); if (jdbcConfigVector == null) { logger.fatal("No JDBC configuration found"); cfgError = true; } if (cfgError) System.exit(1); try { JdbcUtil.registerJdbcDriver(jdbcConfigVector); } catch(Exception e) { System.exit(1); } initializeCacheDb(); } /** Starts the cleaner thread. This method is called by the AccessServer after the * initialization of all sub-moduls */ public void start() { cache = new UserRightsCache(lruTimeout, invalidationTimeout, sleepTime); } /** This method is called by the AccessServer when the background threads should * stop because of a shutdown of the AccessServer itself. */ public void shutdown() { if (cache != null) cache.shutdown(); } /** Adds the result of one expression to the cache of the user. * @param user The name of the user * @param expression The expression * @param isAllowed The result of the expression */ public void addUserRight(String user, String expression, boolean isAllowed) { if (ignoreCase) { user = user.toLowerCase(); expression = expression.toLowerCase(); } cache.addUserRight(user, expression, isAllowed); } /** Retrieves the result of an expression from the cache. * @param user The name of the user * @param expression The expression * @throws CacheFaultException if the result is not found in the cache * @return the result of the expression */ public boolean getUserRight(String user, String expression) throws CacheFaultException { boolean answer = false; if (ignoreCase) { user = user.toLowerCase(); expression = expression.toLowerCase(); } try { answer = cache.getUserRight(user, expression); } catch(CacheFaultException e) { JdbcConnectionPool pool = null; try { if (expression.matches("^\\s*([a-zA-Z0-9_]+)\\s*$")) // Is it a single argument ? { boolean debug = logger.isDebugEnabled(); if (debug) logger.debug("Start reading cache"); String md5 = Md5Cache.get(expression); if (md5 == null) { md5 = StringUtil.md5Sum(expression); Md5Cache.put(expression, md5); } pool = getCacheConnectionPool(md5); if (debug) logger.debug("Got pool"); IndexTable index = IndexTable.selectIndex(pool, md5); if (debug) logger.debug("Got index"); if (index == null) throw new CacheFaultException(); if (debug) logger.debug("Requesting user"); answer = CacheTable.searchCacheTable(pool, index.getTableName(), user); if (debug) logger.debug("got answer"); cache.addUserRight(user, expression, answer); if (debug) logger.debug("written into 1st-level cache"); } else // Real expressions can't be found in the databases throw new CacheFaultException(); } catch(JdbcException se) { logger.error("SQL error while reading from cache db", se); throw new CacheFaultException(); } catch(NoSuchAlgorithmException nsae) { logger.fatal("Initialisation of MD5 digest failed.", nsae); System.exit(1); } } return answer; } /** * @param md5sum * @return */ private JdbcConnectionPool getCacheConnectionPool(String md5sum) // throws JdbcException { String key = md5sum.substring(md5sum.length() - 2); int dbnum = ((Integer) md5PatternIndex.get(key)).intValue(); logger.debug("DB = <" + dbnum + ">"); return (JdbcConnectionPool) cachePools.get(dbnum); } /** * @param md5sum * @return */ private int getDbNum(String md5sum) { String key = md5sum.substring(md5sum.length() - 2); int value = ((Integer) md5PatternIndex.get(key)).intValue(); return value; } private void initializeCacheDb() { JdbcConnectionPool pool = null; int pattern = 1 << md5PatternLength; int iMax = cacheDbConfigVector.size(); if (iMax < pattern) { logger.fatal("Not enough cache databases configured for md5PatternLength = <" + md5PatternLength + ">."); System.exit(1); } for (int i = 0; i < iMax; i++) { Configuration cfg = (Configuration) cacheDbConfigVector.get(i); String jdbcUrl = cfg.getAttribute(JDBC_URL); String jdbcUsername = cfg.getAttribute(JDBC_USERNAME); String jdbcPassword = cfg.getAttribute(JDBC_PASSWORD); int jdbcConnections = cfg.getIntAttribute(JDBC_CONNECTIONS); pool = new JdbcConnectionPool(jdbcUrl, jdbcUsername, jdbcPassword, jdbcConnections); cachePools.add(i, pool); } for (int i = 0; i < 256; i++) { int low = i & 0x0f; int high = (i & 0xf0) >> 4; String key = "" + hexadecimal[high] + hexadecimal[low]; Integer value = new Integer(i % pattern); md5PatternIndex.put(key, value); } }}/** * $Log: DbCache.java,v $ * Revision 1.18 2003/04/14 20:39:18 joerg * Initializing JdbcConnectionPools with pre-opened connections. * * Revision 1.17 2003/04/13 20:28:01 joerg * Package structure modified * * Revision 1.16 2003/04/13 20:16:41 joerg * Package structure modified * * Revision 1.15 2003/01/29 20:31:53 joerg * MD5-Summen werden zuerst im Cache gesucht. * * Revision 1.14 2003/01/17 19:58:36 joerg * Zusaetzliche Debug-Ausgaben * * Revision 1.13 2003/01/04 17:15:43 joerg * Zus鋞zliche Config-Option IgnoreCase * * Revision 1.12 2003/01/01 21:04:17 joerg * Copyright-Statement aktualisiert * * Revision 1.11 2002/12/24 21:04:33 joerg * Umbau der Paketstruktur * iiitLdapPlugin integriert * JavaDoc-Kommentare weiter vervollstaendigt. * * Revision 1.10 2002/12/23 11:24:09 joerg * Diverse kleine Korrekturen * * Revision 1.9 2002/12/23 10:02:39 joerg * Klasse umbenannt in DbCachePlugin, * shutdown()-Methode hinzugefuegt. * * Revision 1.8 2002/12/21 19:55:03 joerg * Nicht mehr benoetigte Methoden entfernt, interne Methoden auf * private oder protected geaendert. * JavaDoc Kommentare ergaenzt. * * Revision 1.7 2002/12/19 15:54:33 joerg * Paket umbenannt in iiitLdapPlugin * * Revision 1.6 2002/12/08 19:33:58 joerg * Auskommentierten Code entfernt * * Revision 1.5 2002/12/08 16:37:32 joerg * Aufraeumungsarbeiten nach dem grossen Umbau * * Revision 1.4 2002/12/08 16:09:46 joerg * Paket-Struktur ueberarbeitet * * Revision 1.3 2002/11/27 22:31:51 joerg * Aufteilung der Cache-Daten auf verschiedene Datenbanken * * Revision 1.2 2002/11/21 21:49:45 joerg * Umstellung auf JdbcConnectionPool * * Revision 1.1 2002/11/21 08:38:56 joerg * Neues CachePlugin mit 2nd-level-Cache. In den 1st-level * Cache wird noch nicht geschrieben, um Tests zu vereinfachen * */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -