From: Marcus <the...@us...> - 2004-03-30 14:44:40
|
Update of /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6571 Modified Files: ScanThread.java ScannerPlugin.java Added Files: ControlThread.java Scanner.java ScannerConfig.java ScannerPluginConfig.java StandaloneScanner.java StandaloneScannerConfig.java Log Message: lots of stuff for standalone --- NEW FILE: Scanner.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 * */ /* * Scanner.java * * Created on 30. März 2004, 14:32 */ package junk.plugin.scanner; import junk.util.HostMap; import java.util.HashMap; /** * * @author flow */ public interface Scanner { public static final int SMB_PORT = 139; /** * deprecated * @deprecated use KEY_HOSTS_MAP */ public static final String HOSTS_MAP_KEY = ScannerPlugin.KEY_HOSTS_MAP; /** the key under which the map is stored in the servlet context */ public static final String KEY_HOSTS_MAP = "scanner-host-map"; /** the key under which the scanning status * is saved in the servlet context */ public static final String KEY_SCANNER_RUNNING = "scanner-running"; public ScannerConfig getConf(); public HostMap getHosts(); public boolean isRunning(); public void setScanning(boolean isScanning); public boolean isScanning(); public void reportDone(long time); public void saveHosts(); } --- NEW FILE: ScannerPluginConfig.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 ScannerPluginConfig implements 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(this.getClass()); /** 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 ScannerPluginConfig() { } /** Creates a new instance of ScannerConfig. * @param m ModuleConfig * @param a ActionServlet * @throws IllegalArgumentException * thrown if reading the configuration failed */ protected ScannerPluginConfig(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 an iterator over the configured ip subnets. * @return an iterator over the configured ip subnets */ public Iterator getIpSubnetIterator() { return this.ipSubnets.iterator(); } /** * 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; } /** 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); } } } Index: ScannerPlugin.java =================================================================== RCS file: /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner/ScannerPlugin.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** ScannerPlugin.java 30 Mar 2004 05:32:49 -0000 1.11 --- ScannerPlugin.java 30 Mar 2004 14:33:01 -0000 1.12 *************** *** 40,43 **** --- 40,44 ---- import java.util.HashMap; import java.util.Iterator; + import java.util.NoSuchElementException; import javax.servlet.ServletException; *************** *** 57,90 **** * * @author Marcus Proest (theevilflow at users dot sf dot net) ! *@todo there are some issues regarding multiple ip subnets... */ ! public class ScannerPlugin implements PlugIn, Runnable { ! /** ! * deprecated ! * @deprecated use KEY_HOSTS_MAP ! */ ! public static final String HOSTS_MAP_KEY = ScannerPlugin.KEY_HOSTS_MAP; ! ! /** the key under which the map is stored in the servlet context */ ! public static final String KEY_HOSTS_MAP = "scanner-host-map"; ! ! /** the key under which the scann config is stored in the servlet context */ ! ! //i dont know why to save this.... ! //public static final String KEY_SCANNER_CONFIG = "scanner-config"; ! ! /** the key under which the scanning status ! * is saved in the servlet context ! */ ! public static final String KEY_SCANNER_RUNNING = "scanner-running"; ! /** version */ private static final String VERSION = "v0.1"; - /** smb scanport */ - protected static final int SMB_PORT = 139; - /** the actionservlet of the struts application */ private ActionServlet actionServlet = null; /** the hostmap */ --- 58,70 ---- * * @author Marcus Proest (theevilflow at users dot sf dot net) ! * */ ! public class ScannerPlugin implements PlugIn, Scanner { /** version */ private static final String VERSION = "v0.1"; /** the actionservlet of the struts application */ private ActionServlet actionServlet = null; + private ControlThread ct = null; /** the hostmap */ *************** *** 97,120 **** private ScannerConfig conf = null; - /** the scanner thread */ - private Thread scanThread = null; - - /** whether the thread is running. - * has to be maintained by the thread itself - */ - private boolean isRunning = false; - /** semaphore * also used to tell the spider whether the scanner scans right now */ private boolean isScanning = false; ! /** ! * yet finished threads ! */ private int doneThreads = 0; /** destroys the plugin. */ public void destroy() { ! this.scanThread = null; //the thread will now terminate /* the server will continue to shutdown as we leave the destroy --- 77,121 ---- private ScannerConfig conf = null; /** semaphore * also used to tell the spider whether the scanner scans right now */ private boolean isScanning = false; ! ! /** yet finished threads */ private int doneThreads = 0; + /** + * returns the scanner configuration + * @return the scanner configuration + */ + public ScannerConfig getConf() { + return this.conf; + } + + /** + * returns the hostmap + * @return the hostmap + */ + public HostMap getHosts() { + return this.hosts; + } + + public boolean isRunning() { + return this.ct.isRunning(); + } + + public void setScanning(boolean isScanning) { + this.isScanning = isScanning; + this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, + new Boolean(this.isScanning)); + } + + public boolean isScanning() { + return this.isScanning; + } + /** destroys the plugin. */ public void destroy() { ! //terminate the thread! /* the server will continue to shutdown as we leave the destroy *************** *** 124,131 **** * I know is is not very stylish, but for now i have no better idea */ - while (this.isRunning) { - ; - } - this.saveMap(this.hosts); --- 125,128 ---- *************** *** 144,148 **** try { ! this.conf = new ScannerConfig(a, m); } catch (IllegalArgumentException e) { log.fatal("Loading config FAILED. This is FATAL. " --- 141,145 ---- try { ! this.conf = new ScannerPluginConfig(a, m); } catch (IllegalArgumentException e) { log.fatal("Loading config FAILED. This is FATAL. " *************** *** 152,222 **** } - //forgot why to do this - // this.actionServlet.getServletContext().setAttribute(ScannerPlugin.KEY_SCANNER_CONFIG, - // this.conf); //load the persistent map and save it in the context this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, ! this.loadMap()); ! ! //start the real thread ! if (this.scanThread == null) { ! this.scanThread = new Thread(this); ! this.scanThread.start(); ! } else { ! log.fatal("Scanner thread could not be started. " ! + "It seems it is already running."); ! ! return; ! } log.info("juNK scanner plugin (" + VERSION + ") thread started"); } - /** the thread run method. */ - public void run() { - this.isRunning = true; - - this.scan(); //scan at startup to provide values as fast as possible - - Thread runningThread = Thread.currentThread(); - - while (scanThread == runningThread) { - try { - Thread.sleep(this.conf.getSleep()); - } catch (InterruptedException e) { - ; //wake up, neo - } - - //every 'interval' milliseconds - if ((System.currentTimeMillis() % (this.conf.getInterval())) <= this.conf - .getSleep()) { - this.scan(); - } - } - - this.isRunning = false; - } - - /** - * returns the scanner configuration - * @return the scanner configuration - */ - protected ScannerConfig getConf() { - return this.conf; - } - - /** - * returns the hostmap - * @return the hostmap - */ - protected HostMap getHosts() { - return this.hosts; - } - /** * the scanner threads call this to maintain the isScanning property * @param time the time the thread took to finish ! */ ! protected synchronized void reportDone(long time) { this.doneThreads++; --- 149,166 ---- } //load the persistent map and save it in the context + this.hosts = this.loadMap(); this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, ! this.hosts); ! this.ct = new ControlThread(this); log.info("juNK scanner plugin (" + VERSION + ") thread started"); } /** * the scanner threads call this to maintain the isScanning property * @param time the time the thread took to finish ! */ ! public synchronized void reportDone(long time) { this.doneThreads++; *************** *** 231,234 **** --- 175,183 ---- } + public void saveHosts() { + this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, + this.hosts); + } + /** load and deserialize the hostmap. * @return loaded map *************** *** 236,240 **** private HostMap loadMap() { String path = this.conf.getSaveMapPath(); ! HostMap h = null; FileInputStream f = null; ObjectInputStream o = null; --- 185,190 ---- private HostMap loadMap() { String path = this.conf.getSaveMapPath(); ! HostMap h = this.populateMap(); ! int hp = h.size(); FileInputStream f = null; ObjectInputStream o = null; *************** *** 243,247 **** f = new FileInputStream(path); o = new ObjectInputStream(f); ! h = (HostMap) o.readObject(); log.info("Persistent data map loaded (" + path + ")"); } catch (IOException e1) { --- 193,198 ---- f = new FileInputStream(path); o = new ObjectInputStream(f); ! h.putAll((HostMap) o.readObject()); ! log.info("Persistent data map loaded (" + path + ")"); } catch (IOException e1) { *************** *** 252,259 **** + "This is not critical, but should not happen.", ex); } finally { - if (h == null) { - h = populateMap(); //if there is no data, populate map - } - try { //cleanup. don't care if this fails. --- 203,206 ---- *************** *** 286,289 **** --- 233,237 ---- String ip = (String) i.next(); h.put(ip, new NameOnline(ip, Boolean.FALSE)); + log.info(ip); } } *************** *** 324,369 **** } } - - /** - * scans the net. - * @return <CODE>true</CODE> if the scan was performed. - */ - private synchronized boolean scan() { - if (this.isScanning) { - return false; //semaphore, to be sure - } - - this.isScanning = true; //set the semaphore - this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, - new Boolean(this.isScanning)); - - //get the old hostmap - this.hosts = (HostMap) this.actionServlet.getServletContext() - .getAttribute(KEY_HOSTS_MAP); - - //get subnet iterator - Iterator iterator = this.conf.getIpSubnetIterator(); - - //iterate over the subnet - //this iterates over the configured subnets, not over ip's! - while (iterator.hasNext()) { - //get an IpSubnet clone - IpSubnet i = (IpSubnet) ((IpSubnet) iterator.next()).clone(); - - for (int x = 0; x < this.conf.getNumOfThreads(); x++) { - new ScanThread(this, i, x); - } - } - - //store the results in the context - this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, hosts); - - //this is now maintained by the threads - - /* this.isScanning = false; - this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, - new Boolean(this.isScanning)); - */ - return true; - } } --- 272,274 ---- --- NEW FILE: StandaloneScanner.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 * */ /* * DUMMY, yet */ package junk.plugin.scanner; /** * * @author flow */ public class StandaloneScanner implements Scanner { /** Creates a new instance of StandaloneScanner */ public StandaloneScanner() { } public ScannerConfig getConf() { return null; } public junk.util.HostMap getHosts() { return null; } /** should return isRunning method of controlthread*/ public boolean isRunning() { return false; } public void setScanning(boolean isScanning) { } public boolean isScanning() { return false; } public void reportDone(long time) { } public void saveHosts() { } } Index: ScanThread.java =================================================================== RCS file: /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/scanner/ScanThread.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ScanThread.java 30 Mar 2004 05:32:23 -0000 1.1 --- ScanThread.java 30 Mar 2004 14:33:01 -0000 1.2 *************** *** 41,65 **** /** ! * thread to scann ip's */ class ScanThread implements Runnable { ! /** ! * ip iterator ! */ private Iterator i; /** log writer */ private Log log = LogFactory.getLog(ScanThread.class); ! /** ! * parent plugin ! */ ! private ScannerPlugin parent; ! /** ! * thread ! */ private Thread me = null; ! /** ! * id of the thread ! */ private int t; --- 41,61 ---- /** ! * thread to scan ip's ! * @todo perform REAL check for iterator syncronization */ class ScanThread implements Runnable { ! /** ip iterator */ private Iterator i; /** log writer */ private Log log = LogFactory.getLog(ScanThread.class); ! ! /** parent plugin */ ! private Scanner parent; ! ! /** thread */ private Thread me = null; ! ! /** id of the thread */ private int t; *************** *** 67,74 **** * constructor * @param sp calling plugin ! * @param i ip iterator ! * @param t thread id ! */ ! protected ScanThread(ScannerPlugin sp, Iterator i, int t) { this.parent = sp; this.i = i; --- 63,70 ---- * constructor * @param sp calling plugin ! * @param i ip iterator over ALL ip's to scan ! * @param t thread id (no real purpose, just for logging) ! */ ! protected ScanThread(Scanner sp, Iterator i, int t) { this.parent = sp; this.i = i; *************** *** 82,88 **** } /** * run method of the thread ! */ public void run() { log.info("Starting Thread " + t); --- 78,88 ---- } + /** empty standard constructor **/ + private ScanThread() { + } + /** * run method of the thread ! */ public void run() { log.info("Starting Thread " + t); *************** *** 90,97 **** long start = System.currentTimeMillis(); //to get a scanningtime ! int c = 0; ! while (i.hasNext()) { ! c++; String ip = (String) i.next(); //get next IP --- 90,97 ---- long start = System.currentTimeMillis(); //to get a scanningtime ! int c = 0; //count scanned ip's ! while (i.hasNext() && this.parent.isRunning()) { ! c++; //increase ip counter String ip = (String) i.next(); //get next IP *************** *** 102,106 **** } else { //the host is handled as 'offline' ! //et the old values to preserve the name NameOnline no = (NameOnline) this.parent.getHosts().get(ip); no.setOnline(Boolean.FALSE); //set it offline --- 102,106 ---- } else { //the host is handled as 'offline' ! //get the old values to preserve the name NameOnline no = (NameOnline) this.parent.getHosts().get(ip); no.setOnline(Boolean.FALSE); //set it offline --- 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 * */ /* * ScannerConfig.java * * Created on 30. März 2004, 16:34 */ package junk.plugin.scanner; import java.util.Iterator; /** * * @author flow */ public interface ScannerConfig { public long getInterval(); public Iterator getIpSubnetIterator(); public int getNumOfThreads(); public String getSaveMapPath(); public long getSleep(); public int getTcpTimeout(); } --- NEW FILE: ControlThread.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 * */ /* * ControlThread.java * * Created on 30. März 2004, 12:11 */ package junk.plugin.scanner; import junk.util.IpSubnet; import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * @author flow */ public class ControlThread implements Runnable { /** log writer */ private Log log = LogFactory.getLog(ScannerPlugin.class); private Scanner parent; /** the scanner thread */ private Thread controlThread = null; /** whether the thread is running. * has to be maintained by the thread itself */ private boolean isRunning = false; /** Creates a new instance of ControlThread */ public ControlThread(Scanner parent) { this.parent = parent; //start the real thread if (this.controlThread == null) { this.controlThread = new Thread(this); this.controlThread.start(); } else { log.fatal("Scanner thread could not be started. " + "It seems it is already running."); return; } } /** the thread run method. */ public void run() { this.isRunning = true; this.scan(); //scan at startup to provide values as fast as possible Thread runningThread = Thread.currentThread(); while (controlThread == runningThread) { try { Thread.sleep(this.parent.getConf().getSleep()); } catch (InterruptedException e) { ; //wake up, neo } //every 'interval' milliseconds if ((System.currentTimeMillis() % (this.parent.getConf() .getInterval())) <= this.parent.getConf() .getSleep()) { this.scan(); } } this.isRunning = false; } protected boolean isRunning() { return this.isRunning; } protected void destroy() { this.controlThread = null; } /** * scans the net. * @return <CODE>true</CODE> if the scan was performed. */ private synchronized boolean scan() { if (this.parent.isScanning()) { return false; //semaphore, to be sure } this.parent.setScanning(true); //set the semaphore Iterator iterator = this.parent.getConf().getIpSubnetIterator(); //special iterator to iterate over ALL ip addresses Iterator i = new IpIterator(iterator); for (int x = 0; x < this.parent.getConf().getNumOfThreads(); x++) { new ScanThread(this.parent, i, x); } //store the results in the context /* this.actionServlet.getServletContext().setAttribute(KEY_HOSTS_MAP, this.hosts); */ this.parent.saveHosts(); //this is now maintained by the threads /* this.isScanning = false; this.actionServlet.getServletContext().setAttribute(KEY_SCANNER_RUNNING, new Boolean(this.isScanning)); */ return true; } class IpIterator implements Iterator { private Iterator currentIterator = null; private Iterator subnets; protected IpIterator(Iterator subnets) { this.subnets = subnets; if (this.subnets.hasNext()) { this.currentIterator = (Iterator) ((IpSubnet) subnets.next()) .clone(); } } public boolean hasNext() { while (!this.currentIterator.hasNext()) { if (this.subnets.hasNext()) { this.currentIterator = (Iterator) this.subnets.next(); } else { return false; } } return true; } public Object next() { if (this.hasNext()) { return this.currentIterator.next(); } else { throw new NoSuchElementException(); } } /* * @see java.util.Iterator#remove * @throws UnsupportedOperationException this operation is not supported */ public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } } } --- NEW FILE: StandaloneScannerConfig.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 * */ /* * DUMMY, yet */ package junk.plugin.scanner; /** * * @author flow */ public class StandaloneScannerConfig { /** Creates a new instance of StandaloneScannerConfig */ public StandaloneScannerConfig() { } } |