📄 cachetodohandler.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: CacheToDoHandler.java,v 1.27 2003/04/14 20:41:09 joerg Exp $ ******************************************************************************/package de.iiit.access.server.plugins.cachemanager;import de.iiit.access.common.api.*;import de.iiit.access.server.*;import de.iiit.access.server.api.*;import de.iiit.access.server.plugins.parser.*;import de.iiit.access.server.util.*;import de.iiit.access.server.util.db.*;import de.iiit.access.server.util.db.cachedb.*;import de.iiit.access.server.util.db.admindb.*;import de.iiit.ldap.*;import de.iiit.jdbc.*;import de.iiit.util.*;import java.util.*;import java.security.*;import javax.naming.*;import org.apache.log4j.Logger;/** This Class reads records of the todolist table one by one, evaluates the * expressions and refreshes the persistent 2<sup>nd</SUP>-level cache. */public class CacheToDoHandler extends Thread{ /** CVS Version Tag */ private static final String vcid = "$Id: CacheToDoHandler.java,v 1.27 2003/04/14 20:41:09 joerg Exp $"; private static final int sleepTime = 10000; private Logger logger = Logger.getLogger(this.getClass()); private JdbcConnectionPool adminPool = null; private int cachePoolCount; private CacheManager manager; private CacheFileHandler fileHandler = null; private LdapUtil ldapUtil = null; private String ldapFormulaClass = null; private String ldapFormulaBase = null; private String ldapFormulaExpr = null; private String ldapCommonName = null; private boolean running = true; private boolean ignoreCase; /** Creates a new instance of CacheToDoHandler * @param manager The parent CacheManager holding the configuration data etc. */ public CacheToDoHandler(CacheManager manager) { setName("CacheToDoHandler"); this.manager = manager; ignoreCase = AccessServer.getIgnoreCase(); ldapUtil = manager.getLdapUtil(); ldapFormulaClass = ldapUtil.getLdapFormulaClass(); ldapFormulaBase = ldapUtil.getLdapFormulaBase(); ldapFormulaExpr = ldapUtil.getLdapExpression(); ldapCommonName = ldapUtil.getLdapCommonName(); fileHandler = new CacheFileHandler(manager); } /** This method is called by the AccessServer when the background threads should * stop because of a shutdown of the AccessServer itself. */ public void shutdown() { running = false; interrupt(); if (ldapUtil != null) ldapUtil.shutdown(); if (fileHandler != null) fileHandler.shutdown(); } private void initializeAdminDb() { createPropertyTable(adminPool); createToDoList(adminPool); } private void initializeCacheDb() { cachePoolCount = manager.getCachePoolCount(); for (int i = 0; i < cachePoolCount; i++) { JdbcConnectionPool pool = manager.getCacheConnectionPool(i); createPropertyTable(pool); createIndexTable(pool); } } private void createPropertyTable(JdbcConnectionPool pool) { boolean loop = false; do { try { if (PropertyTable.createPropertyTable(pool)) logger.info("Table <properties> successfully created."); loop = false; } catch (JdbcException e) { logger.error("SQL error while creating table <properties>", e); loop = true; } if (running && loop) { try { logger.info("Sleeping for 60 seconds"); sleep(60000); } catch(InterruptedException ie) { // do nothing } } } while (running && loop); } private void createIndexTable(JdbcConnectionPool pool) { boolean loop = false; do { try { if (IndexTable.createIndexTable(pool)) logger.info("Table <indextable> successfully created."); loop = false; } catch (JdbcException e) { logger.error("SQL error while creating table <todolist>", e); loop = true; } if (running && loop) { try { logger.info("Sleeping for 60 seconds"); sleep(60000); } catch(InterruptedException ie) { // do nothing } } } while (running && loop); } private void createToDoList(JdbcConnectionPool pool) { boolean loop = false; do { try { if (ToDoList.createToDoList(pool)) logger.info("Table <todolist> successfully created."); loop = false; } catch (JdbcException e) { logger.error("SQL error while creating table <todolist>", e); loop = true; } if (running && loop) { try { logger.info("Sleeping for 60 seconds"); sleep(60000); } catch(InterruptedException ie) { // do nothing } } } while (running && loop); } private Set buildToDoList(String searchBase, String filter, String attr) throws NamingException { HashSet result = new HashSet(); long t = System.currentTimeMillis(); String[] attrs = new String[] { attr }; Vector v = ldapUtil.search(searchBase, filter, attrs); int iMax = v.size(); for (int i = 0; i < iMax; i++) { LdapGenericObject o = (LdapGenericObject) v.get(i); Vector cn = o.getAttribute(attr); int jMax = cn.size(); for (int j = 0; j < jMax; j++) { result.add((String) cn.get(j)); } } t = System.currentTimeMillis() - t; logger.info(result.size() + " entries found, " + t + " ms"); return result; } private void writeToDoList(JdbcConnectionPool pool, Set set) throws JdbcException { long t = System.currentTimeMillis(); ToDoList.fillToDoList(pool, set); t = System.currentTimeMillis() - t; logger.info(set.size() + " results written, " + t + " ms"); } private void fillToDoList() { Set result; boolean loop = true; do { try { if (PropertyTable.selectProperty(adminPool, "InitialReadCompleted", "false").equals("false")) { logger.info("Reading LDAP group list"); String filter = "(& (cn=*)(objectClass=" + ldapUtil.getLdapGroupClass() + "))"; result = buildToDoList(ldapUtil.getLdapGroupBase(), filter, ldapUtil.getLdapCommonName()); writeToDoList(adminPool, result); logger.info("Reading LDAP formula list"); filter = "(& (cn=*)(objectClass=" + ldapUtil.getLdapFormulaClass() + "))"; result = buildToDoList(ldapUtil.getLdapFormulaBase(), filter, ldapUtil.getLdapCommonName()); writeToDoList(adminPool, result); PropertyTable.writeProperty(adminPool, "InitialReadCompleted", "true"); } loop = false; } catch (JdbcException e) { logger.fatal("SQL error while filling todolist", e); } catch (NamingException e) { logger.fatal("LDAP error while filling todolist", e); } if (running && loop) { try { logger.info("Sleeping for 60 seconds"); sleep(60000); } catch(InterruptedException ie) { // do nothing } } } while (loop && running); } private String nextTableName(JdbcConnectionPool pool) throws JdbcException { String s = PropertyTable.selectProperty(pool, "tablenumber"); long tn = 1; if (s != null) { tn = Long.parseLong(s) + 1; s = String.valueOf(tn); PropertyTable.updateProperty(pool, "tablenumber", s); } else PropertyTable.insertProperty(pool, "tablenumber", "1"); s = "0000000000" + tn; return "T" + s.substring(s.length() - 10); } private Set searchReferences(String expression) throws NamingException { HashSet result = new HashSet();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -