📄 eventengine.java
字号:
/* * eventEngine * * $LastChangedRevision: 3757 $ * * $LastChangedDate: 2006-11-24 13:31:59 +0100 (fre, 24 nov 2006) $ * * Copyright 2002-2004 Norwegian University of Science and Technology * * This file is part of Network Administration Visualized (NAV) * * NAV 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. * * NAV 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 NAV; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */import java.io.*;import java.util.*;import java.util.jar.*;import java.net.*;import java.text.*;import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetException;import java.sql.*;import no.ntnu.nav.ConfigParser.*;import no.ntnu.nav.Database.*;import no.ntnu.nav.logger.*;import no.ntnu.nav.eventengine.*;import no.ntnu.nav.Path;/** * eventEngine processes events posted on the eventq via plugin modules. * * @version $LastChangedRevision: 3757 $ $LastChangedDate: 2006-11-24 13:31:59 +0100 (fre, 24 nov 2006) $ * @author Kristian Eide <kreide@online.no> */class eventEngine{ //public static final String navRoot = "c:/jprog/itea/".replace('/', File.separatorChar); //public static final String navRoot = "/home/kristian/devel/".replace('/', File.separatorChar); public static final String navConfigFile = (Path.sysconfdir + "/nav.conf").replace('/', File.separatorChar); public static final String dbConfigFile = (Path.sysconfdir + "/db.conf").replace('/', File.separatorChar); public static final String configFile = (Path.sysconfdir + "/eventEngine.conf").replace('/', File.separatorChar); public static final String alertmsgFile = (Path.sysconfdir + "/alertmsg.conf").replace('/', File.separatorChar); public static final String scriptName = "eventEngine"; public static final String logFile = (Path.localstatedir + "/log/eventEngine.log").replace('/', File.separatorChar); public static final boolean ERROR_OUT = true; public static final boolean VERBOSE_OUT = true; public static final boolean DEBUG_OUT = true; public static final boolean DB_UPDATE = true; public static final boolean DB_COMMIT = true; // END USER CONFIG // // A timer static Thread[] threads; static Timer timer; static Stack idleThreads; //static HashSet safeCloseBoksid = new HashSet(); static String qBoks; static ConfigParser navCp; public static void main(String[] args) throws Exception { String cf = null; // Check arguments if (args.length > 0) { // Assume this argument is the config file File f = new File(args[0]); if (f.exists() && !f.isDirectory()) cf = f.getAbsolutePath(); } try { navCp = new ConfigParser(navConfigFile); } catch (IOException e) { Log.e("INIT", "Could not read config file: " + navConfigFile); return; } ConfigParser dbCp; try { dbCp = new ConfigParser(dbConfigFile); } catch (IOException e) { System.err.println("Error, could not read config file: " + dbConfigFile); return; } if (!Database.openConnection(dbCp.get("dbhost"), dbCp.get("dbport"), dbCp.get("db_nav"), dbCp.get("script_"+scriptName), dbCp.get("userpw_"+dbCp.get("script_"+scriptName)))) { System.err.println("Error, could not connect to database!"); return; } // Init logger Log.init(logFile, "eventEngine"); // Deamon timer timer = new Timer(false); // Set up the config file monitor if (cf == null) cf = configFile; ConfigFileMonitorTask cfmt = new ConfigFileMonitorTask(cf, navCp); if (cfmt.cfNotFound()) { System.err.println("Error, could not read config file: " + cf); return; } cfmt.run(); // Load config first time timer.schedule(cfmt, 5 * 1000, 5 * 1000); // 5 second delay MessagePropagatorImpl mp = new MessagePropagatorImpl(); HashMap handlerClassMap = new HashMap(); HashMap deviceMap = new HashMap(); DeviceDBImpl devDB; try { devDB = new DeviceDBImpl(mp, deviceMap, timer, alertmsgFile); } catch (ParseException e) { System.err.println("While reading " + alertmsgFile + ":"); System.err.println(" " + e.getMessage()); return; } // The eventq monitor EventqMonitorTask emt = new EventqMonitorTask(mp, handlerClassMap, devDB, cfmt); // Set up the plugin monitor PluginMonitorTask pmt = new PluginMonitorTask("device-plugins", new HashMap(), "handler-plugins", handlerClassMap, devDB, deviceMap, emt ); mp.setPluginMonitorTask(pmt); pmt.run(); // Load all plugins timer.schedule(pmt, 5 * 1000, 5 * 1000); // Check for new plugin every 5 seconds timer.schedule(emt, 5 * 1000, 5 * 1000); // Check for new events every 5 seconds } static class MessagePropagatorImpl implements MessagePropagator { private PluginMonitorTask pmt; void setPluginMonitorTask(PluginMonitorTask pmt) { this.pmt = pmt; } public void updateFromDB() { pmt.updateFromDB(); } } private static String format(long i, int n) { DecimalFormat nf = new DecimalFormat("#"); nf.setMinimumIntegerDigits(n); return nf.format(i); } public static String formatTime(long t) { long h = t / (60 * 60 * 1000); t %= 60 * 60 * 1000; long m = t / (60 * 1000); t %= 60 * 1000; long s = t / (1000); t %= 1000; long ms = t; return format(h,2)+":"+format(m,2)+":"+format(s,2)+"."+format(ms,3); } private static HashMap getHashFromResultSet(ResultSet rs, ResultSetMetaData md, boolean convertNull) throws SQLException { HashMap hm = new HashMap(); for (int i=md.getColumnCount(); i > 0; i--) { String val = rs.getString(i); hm.put(md.getColumnName(i), (convertNull&&val==null)?"":val); } return hm; }}class ConfigFileMonitorTask extends TimerTask{ private File cf; private ConfigParser cp; private boolean cfNotFound; private long lastMod; private ConfigParser navCp; public ConfigFileMonitorTask(String cfPath, ConfigParser navCp) { cf = new File(cfPath); if (!cf.isFile()) cfNotFound = true; this.navCp = navCp; } public void run() { if (cfNotFound) return; if (lastMod == cf.lastModified()) return; lastMod = cf.lastModified(); try { cp = new ConfigParser(cf.getAbsolutePath()); cp.setObject("navCp", navCp); } catch (IOException e) { Log.w("CONFIG_FILE_MONITOR_TASK", "RUN", "Could not read config file: " + cf); return; } } public ConfigParser getConfigParser() { return cp; } public boolean cfNotFound() { return cfNotFound; }}class PluginMonitorTask extends TimerTask{ private static final int UPDATE_INTERVAL = 60 * 60 * 1000; DynamicURLClassLoader cl = new DynamicURLClassLoader(); File deviceDir, handlerDir; Map deviceClassMap, handlerClassMap; Map deviceFileMap = new HashMap(); Map handlerFileMap = new HashMap(); DeviceDBImpl devDB; Map deviceMap; EventqMonitorTask emt; private long lastDBUpdate = System.currentTimeMillis(); public PluginMonitorTask(String devicePath, Map deviceClassMap, String handlerPath, Map handlerClassMap, DeviceDBImpl devDB, Map deviceMap, EventqMonitorTask emt) { deviceDir = new File(devicePath); this.deviceClassMap = deviceClassMap; handlerDir = new File(handlerPath); this.handlerClassMap = handlerClassMap; this.devDB = devDB; this.deviceMap = deviceMap; this.emt = emt; if (!deviceDir.isDirectory() || !handlerDir.isDirectory()) { Log.w("PLUGIN_MONITOR_TASK", "CONSTRUCTOR", "Plugins directory not found, exiting..."); System.exit(0); } } public synchronized void run() { // Update Devices if (update(deviceDir, deviceFileMap, deviceClassMap, deviceDir.listFiles() ) || (System.currentTimeMillis()-lastDBUpdate > UPDATE_INTERVAL)) { updateFromDB(); lastDBUpdate = System.currentTimeMillis(); } // Update EventHandlers if (update(handlerDir, handlerFileMap, handlerClassMap, deviceDir.listFiles() )) { emt.updateCache(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -