?? cachefilehandler.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: CacheFileHandler.java,v 1.20 2003/04/14 20:40:48 joerg Exp $ ******************************************************************************/package de.iiit.access.server.plugins.cachemanager;import de.iiit.access.server.*;import de.iiit.access.server.api.*;import de.iiit.access.server.util.*;import de.iiit.access.server.util.db.*;import de.iiit.access.server.util.db.admindb.*;import de.iiit.jdbc.*;import org.apache.log4j.Logger;import java.io.*;import java.util.*;import java.util.regex.*;import java.nio.channels.*;import javax.naming.*;/** This class checks whether there are new LDAP replication records to transfer to * the cache. All records found are written into the todolist in the admin * database. */public class CacheFileHandler extends Thread{ /** CVS Version Tag */ private static final String vcid = "$Id: CacheFileHandler.java,v 1.20 2003/04/14 20:40:48 joerg Exp $"; private static final int sleepTime1 = 10000; private static final int sleepTime2 = 5000; private Logger logger = Logger.getLogger(this.getClass()); private JdbcConnectionPool adminPool = null; private String ldapPersonClass = null; private String ldapGroupClass = null; private String ldapFormulaClass = null; private String ldapPersonBase = null; private String ldapGroupBase = null; private String ldapFormulaBase = null; private String replicationFile = null; private LdapUtil ldapUtil = null; private int refreshIntervall; private CacheManager manager; private boolean running = true; /** Creates a new instance of CacheFileHandler * @param manager The parent CacheManager holding the configuration data etc. */ public CacheFileHandler(CacheManager manager) { setName("CacheFileHandler"); this.manager = manager; ldapUtil = manager.getLdapUtil(); ldapPersonClass = ldapUtil.getLdapPersonClass(); ldapGroupClass = ldapUtil.getLdapGroupClass(); ldapFormulaClass = ldapUtil.getLdapFormulaClass(); ldapPersonBase = ldapUtil.getLdapPersonBase(); ldapGroupBase = ldapUtil.getLdapGroupBase(); ldapFormulaBase = ldapUtil.getLdapFormulaBase(); replicationFile = manager.replicationFile(); refreshIntervall = manager.refreshIntervall(); } /** 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(); } private void readReplicationFile(File rft) throws Exception { BufferedReader br = new BufferedReader(new FileReader(rft)); if (adminPool == null) { adminPool = manager.getAdminPool(); } Pattern regex = Pattern.compile(": "); Hashtable ldapObject = new Hashtable(); Vector v = null; String line = br.readLine(); while (line != null) { // Split on ': ' String parts[] = regex.split(line, 2); if (parts.length > 1) { v = (Vector) ldapObject.get(parts[0]); if (v == null) { v = new Vector(); ldapObject.put(parts[0], v); } v.add(parts[1]); } else if (line.equals("")) // Every block is finished with an empty line { String changetype = (String) ((Vector) ldapObject.get("changetype")).get(0); String dn = (String) ((Vector) ldapObject.get("dn")).get(0); if (changetype.equals("add")) { Vector objectClass = (Vector) ldapObject.get("objectClass"); if (objectClass.contains(ldapGroupClass) || objectClass.contains(ldapFormulaClass)) { // There must be exactly one cn attribute, otherwise the result is undefined! String cn = (String) ((Vector) ldapObject.get("cn")).get(0); ToDoList todo = new ToDoList(cn, System.currentTimeMillis(), false); todo.insertToDo(adminPool); } } else if (changetype.equals("modify")) { if (dn.endsWith(ldapGroupBase) || dn.endsWith(ldapFormulaBase)) { String s1[] = dn.split("cn=", 2); String s2[] = s1[1].split(",", 2); ToDoList todo = new ToDoList(s2[0], System.currentTimeMillis(), false); todo.insertToDo(adminPool); } } else if (changetype.equals("delete")) { if (dn.endsWith(ldapGroupBase) || dn.endsWith(ldapFormulaBase)) { String s1[] = dn.split("cn=", 2); String s2[] = s1[1].split(",", 2); ToDoList todo = new ToDoList(s2[0], System.currentTimeMillis(), false); todo.insertToDo(adminPool); } } else { logger.warn("Unknown changetype <" + changetype + ">"); } ldapObject = new Hashtable(); } line = br.readLine(); } br.close(); rft.delete(); } /** The main method of the CacheFileHandler thread. */ public void run() { String replicationLockFile = replicationFile + ".lock"; String replicationTmpFile = replicationFile + ".tmp"; File rf = new File(replicationFile); File rfl = new File(replicationLockFile); File rft = new File(replicationTmpFile); File rfp = rf.getParentFile(); while(running) { try { if (rft.exists()) { if (! (rft.canRead() && rft.canWrite())) logger.error("Can't read and write temporary LDAP replication file <" + replicationTmpFile + ">"); else { readReplicationFile(rft); } } try { if (running) sleep(refreshIntervall); } catch(InterruptedException ie) { // do nothing } int sleepTime = sleepTime1; if (!rfp.exists()) logger.error("LDAP replication file directory <" + rfp.getAbsolutePath() + "> does not exist"); else if (! (rfp.canRead() && rfp.canWrite())) logger.error("Can't read and write LDAP replication file directory <" + rfp.getAbsolutePath() + ">"); else if (!rf.exists()) { // Do nothing // logger.warn("LDAP replication file <" + replicationFile + "> does not exist"); } else if (! (rf.canRead() && rf.canWrite())) logger.error("Can't read and write LDAP replication file <" + replicationFile + ">"); else if (!rfl.exists()) logger.warn("LDAP replication lock file <" + replicationLockFile + "> does not exist"); else if (! rf.canWrite()) logger.error("Can't write LDAP replication lock file <" + replicationLockFile + ">"); else { sleepTime = 0; if (rf.length() > 0) { FileOutputStream rfls = new FileOutputStream(rfl); FileChannel rflc = rfls.getChannel(); FileLock lock = rflc.lock(); if (lock != null) { rf.renameTo(rft); lock.release(); } else logger.warn("Could not get lock on LDAP replication lock file"); rflc.close(); rfls.close(); } } try { if (running && sleepTime > 0) sleep(sleepTime); } catch(InterruptedException ie) { // do nothing } } catch(JdbcException se) { logger.error("Cought SQL error while trying to process replication file <" + replicationFile + ">", se); try { if (running) sleep(sleepTime1); } catch(InterruptedException ie) { // do nothing } } catch(Exception e) { logger.error("Cought error while trying to process replication file <" + replicationFile + ">", e); try { if (running) sleep(sleepTime1); } catch(InterruptedException ie) { // do nothing } } } running = false; }}/** * $Log: CacheFileHandler.java,v $ * Revision 1.20 2003/04/14 20:40:48 joerg * Setting name for thread. * * Revision 1.19 2003/04/13 20:28:01 joerg * Package structure modified * * Revision 1.18 2003/04/13 20:16:41 joerg * Package structure modified * * Revision 1.17 2003/02/01 19:29:58 joerg * *** empty log message *** * * Revision 1.16 2003/01/01 21:04:17 joerg * Copyright-Statement aktualisiert * * Revision 1.15 2002/12/27 21:26:20 joerg * Fehlermeldungen geaendert * * Revision 1.14 2002/12/24 21:04:33 joerg * Umbau der Paketstruktur * iiitLdapPlugin integriert * JavaDoc-Kommentare weiter vervollstaendigt. * * Revision 1.13 2002/12/23 11:24:58 joerg * shutdown()-Methode hinzugefuegt. * * Revision 1.12 2002/12/21 19:55:03 joerg * Nicht mehr benoetigte Methoden entfernt, interne Methoden auf * private oder protected geaendert. * JavaDoc Kommentare ergaenzt. * * Revision 1.11 2002/12/19 15:54:33 joerg * Paket umbenannt in iiitLdapPlugin * * Revision 1.10 2002/12/08 19:34:11 joerg * Auskommentierten Code entfernt * * Revision 1.9 2002/12/08 16:37:32 joerg * Aufraeumungsarbeiten nach dem grossen Umbau * * Revision 1.8 2002/12/08 16:09:46 joerg * Paket-Struktur ueberarbeitet * * Revision 1.7 2002/12/08 13:55:31 joerg * Der Zugriff auf das LdapPlugin erfolgt jetzt ueber den CacheManager * und nicht mehr ueber den AccessServer * * Revision 1.6 2002/11/21 21:49:45 joerg * Umstellung auf JdbcConnectionPool * * Revision 1.5 2002/11/20 20:41:29 joerg * Klassen fuer das HAndling der DB-Tabellen in Packages * de.iiit.AccessServer.db.* verschoben * * Revision 1.4 2002/11/20 20:25:49 joerg * Bezuege auf UserList und Kontrollausgaben entfernt * * Revision 1.3 2002/11/18 22:09:00 joerg * CacheManager ausgelagert als PlugIn * * Revision 1.2 2002/11/18 21:21:52 joerg * LDAP-Anbindung auf LdapPlugin umgestellt. * * Revision 1.1 2002/11/18 10:17:49 joerg * Klassen des CacheManagers in eigenes Package verschoben * * Revision 1.2 2002/11/18 10:13:18 joerg * CacheManager ist jetzt vollstaendig implementiert * * Revision 1.1 2002/11/17 22:03:26 joerg * Neue Klassen fuer 2nd-level Cache * */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -