|
From: Marcus <the...@us...> - 2004-03-30 05:43:06
|
Update of /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12039/junk/WEB-INF/classes/junk/plugin/scanner Added Files: ScannerConfig.java Log Message: moved back to extra class --- NEW FILE: ScannerConfig.java --- /* * juNK - a file search system for smb shares * * Copyright 2004 by * Marcus Proest (theevilflow at users dot sf dot net) * Uwe van Heesch (tyron_e at users dot sf dot net) * * This file is part of junk (java useful net kollektor). * * junk 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. * * junk 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 junk; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ package junk.plugin.scanner; import junk.util.IpSubnet; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionServlet; import org.apache.struts.config.ModuleConfig; /** handles the config of the scanner plugin. * @author Marcus Proest (theevilflow at users dot sf dot net) */ public class ScannerConfig { /** plugin configuration key defining the sleep time of the scanner thread */ protected static final String KEY_THREAD_SLEEP = "thread-sleep"; /** * number of concurrent threads */ protected static final String KEY_NUM_OF_THREADS = "thread-num"; /** plugin configuration key defining the scanning interval */ protected static final String KEY_SCAN_INTERVAL = "scan-interval"; /** plugin configuration key defining the ip range */ protected static final String KEY_IP_RANGE = "ip-range"; /** plugin configuration key defining the tcp timeout */ protected static final String KEY_TCP_TO = "tcp-timeout"; /** plugin configuration key defining the save map path */ protected static final String KEY_SAVE_MAP = "map-save-file"; /** the ip subnets to scan over */ private ArrayList ipSubnets = null; /** log writer */ private Log log = LogFactory.getLog(ScannerConfig.class); /** initial ipsubnetsString */ private String ipSubnetsString = null; /** the path to load and save the hostmap to */ private String savemappath = "data/hostmap"; /** * number of concurrent threads */ private int numOfThreads = 3; /** the tcp timeout */ private int tcpTimeout = 100; /** the scanning interval */ private long interval = 300000; /** the time the scannerthread sleeps */ private long sleep = 10000; /** Creates a new instance of ScannerConfig. * protected standard constructor to prevent instantiation outside * the package. */ protected ScannerConfig() { } /** Creates a new instance of ScannerConfig. * @param m ModuleConfig * @param a ActionServlet * @throws IllegalArgumentException * thrown if reading the configuration failed */ protected ScannerConfig(ActionServlet a, ModuleConfig m) throws IllegalArgumentException { HashMap conf = junk.plugin.PluginTools.getPluginConfig(ScannerPlugin.class, m); String path = a.getServletContext().getRealPath("/WEB-INF"); this.applyConfig(conf, path); } /** Returns the interval value. * @return returns the interval value */ public long getInterval() { return this.interval; } /** * returns the number of concurrent threads * @return the number of concurrent threads */ public int getNumOfThreads() { return this.numOfThreads; } /** Returns the path to the saved hostmap. * @return the path to the saved hostmap */ public String getSaveMapPath() { return this.savemappath; } /** Returns the sleep value. * @return returns the sleep value */ public long getSleep() { return this.sleep; } /** return the initial ipSubnets String * @return initial ipsubnets String */ public String getSubnets() { return this.ipSubnetsString; } /** Returns the tcp timeout value. * @return tcp timeout */ public int getTcpTimeout() { return this.tcpTimeout; } /** Returns an iterator over the configured ip subnets. * @return an iterator over the configured ip subnets */ protected Iterator getIpSubnetIterator() { return this.ipSubnets.iterator(); } /** Apply the config values. * @param conf configuration values * @throws IllegalArgumentException * thrown if reading the configuration failed * @param basepath global path to the web-inf directory */ private void applyConfig(HashMap conf, String basepath) throws IllegalArgumentException { try { this.sleep = Long.parseLong((String) conf.get(KEY_THREAD_SLEEP)); } catch (Exception e) { log.warn("Loading parameter " + KEY_THREAD_SLEEP + " FAILED, defaulting to " + this.sleep, e); } try { this.interval = Long.parseLong((String) conf.get(KEY_SCAN_INTERVAL)); } catch (Exception e) { log.warn("Loading parameter " + KEY_SCAN_INTERVAL + "FAILED, defaulting to " + this.interval, e); } try { String subnetConfigs = (String) conf.get(KEY_IP_RANGE); this.ipSubnetsString = subnetConfigs; String[] scArray = subnetConfigs.split(","); this.ipSubnets = new ArrayList(scArray.length); for (int x = 0; x < scArray.length; x++) { this.ipSubnets.add(x, new IpSubnet(scArray[x])); } } catch (Exception e) { //this value cannot be defaulted, so failing ist fatal. throw new IllegalArgumentException("Loading parameter " + KEY_IP_RANGE + " FAILED. (" + e.getMessage() + ")"); } try { this.tcpTimeout = Integer.parseInt((String) conf.get(KEY_TCP_TO)); } catch (Exception e) { log.warn("Loading parameter " + KEY_TCP_TO + " FAILED, defaulting to " + this.tcpTimeout, e); } try { this.numOfThreads = Integer.parseInt((String) conf.get( KEY_NUM_OF_THREADS)); } catch (Exception e) { log.warn("Loading parameter " + KEY_NUM_OF_THREADS + " FAILED, defaulting to " + this.numOfThreads, e); } try { String filename = (String) conf.get(KEY_SAVE_MAP); this.savemappath = basepath + File.separatorChar + ".." + File.separatorChar + filename; } catch (Exception e) { log.warn("Loading parameter " + KEY_SAVE_MAP + " FAILED, defaulting to " + this.savemappath, e); } } } |