codice:
package jwebmonitor; /** * Imports */ import java.util.*; import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; import java.net.*; import mailer.Mailer; /** * */ // Monitor and Notify public class JWebMonitor { static ArrayList sitelist = new ArrayList(); static String emailmethod = new String(); static String emailfrom = new String(); static ArrayList emailto = new ArrayList(); static ArrayList emailcc = new ArrayList(); static String emailsubject = new String(); static String emailmessage = new String(); static String emailserver = new String(); static String emailsmtpport = new String(); static String emailsmtpssl = new String(); static String emailsmtpauth = new String(); static String emailuser = new String(); static String emailpw = new String(); // Creates a new instance of JWebMonitor public JWebMonitor() { } // Open connections to above websites and report if down public static void main(String[] args) { System.out.println("" + "=====================================================\n" + "=== Java Web Monitor (JWebMonitor)\n" + "=====================================================" ); File file; // Get values from the config file try{ file = new File(args[0]); } catch(Exception e){ System.out.println("!!! Unable to find config.xml. Please specify the config.xml location as a parameter."); return; } // Get data from XML try { DocumentBuilder builder = DocumentBuilderFactory .newInstance() .newDocumentBuilder(); Document doc = builder.parse(file); // site info System.out.println("\n[Adding Sites...]"); sitelist = createArray(doc,"data","site"); // get list of websites // Type of notification System.out.println("\n[Adding email data...]"); emailmethod = createString(doc,"data", "emailmethod"); emailsmtpauth = createString(doc,"data", "emailsmtpauth"); // email info emailfrom = createString(doc,"data","emailfrom"); // Get emailfrom addresses emailto = createArray(doc,"data","emailto"); // Get emailto addresses emailcc = createArray(doc,"data","emailcc"); // Get emailcc addresses emailsubject = createString(doc,"data","emailsubject"); // Get emailsubject addresses emailmessage = createString(doc,"data","emailmsg"); // Get emailmsg addresses // email server info if (emailsmtpauth.equals("false")){ emailserver = createString(doc,"data","emailsmtpserver"); // Get sendmail server }else{ emailserver = createString(doc,"data","emailsmtpserver"); // Get smtp server emailuser = createString(doc,"data","emailsmtpuser"); // Get stmp user emailpw = createString(doc,"data","emailsmtppw"); // Get smtp pw } emailsmtpport = createString(doc,"data","emailsmtpport"); // Get smtp port emailsmtpssl = createString(doc,"data","emailsmtpssl"); // Get smtp use ssl? } catch (Exception e) { System.out.println("!!! Problem Reading XML !!! - " + e); } // Quit app if no websites listed if (sitelist.isEmpty()){ System.out.println("!!! There are no websites to scan. Please add a site element to config.xml!!!"); return; } // connect to website and if problem (exception) then run notifier else do nothing System.out.println("\n[Testing websites...]"); for (int i=0; i<sitelist.size(); i++){ try { URL url = new URL(sitelist.get(i).toString()); System.out.println("*** Testing " + url); long begin = System.currentTimeMillis(); // Record start time URLConnection uc = url.openConnection(); // Open connection to website InputStream ic = uc.getInputStream(); // Get input stream to try and cause exception long end = System.currentTimeMillis(); // Record end time float timeToLoad = (((float)(end - begin))/1000); // Convert to floating number System.out.println(">>> " + url + " is OK! Time: " + timeToLoad + " seconds.\n"); } catch (Exception e) { String emailmsg = emailmessage + " " + sitelist.get(i) + "\n\nDetected Problem:\n" + e; String theError = "" + e; Mailer.sendMessage(emailmethod, emailsmtpauth, emailserver, emailsmtpport, emailsmtpssl, emailuser, emailpw, emailfrom, emailto, emailcc, emailsubject, emailmsg, theError); } } } // return the string of the requested child public static String createString(Document doc, String roottag, String childsection) { String thestring = new String(); NodeList nodes = doc.getElementsByTagName(roottag); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); // Process the lines NodeList lines = element.getElementsByTagName(childsection); Element line = (Element) lines.item(0); // Collect the text from the <Line> element StringBuffer sb = new StringBuffer(); Node child = line.getFirstChild(); if (child instanceof CharacterData && child != null) { CharacterData cd = (CharacterData) child; sb.append(cd.getData()); } String text = sb.toString().trim(); thestring = text; System.out.println(">>> Added " + childsection + ": " + text); } return thestring; }// End createString method // return an array of similar child nodes public static ArrayList createArray(Document doc, String roottag, String childsection) { ArrayList thearray = new ArrayList(); NodeList nodes = doc.getElementsByTagName(roottag); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); // Process the lines NodeList lines = element.getElementsByTagName(childsection); for (int j = 0; j < lines.getLength(); j++) { Element line = (Element) lines.item(j); // Collect the text from the <Line> element StringBuffer sb = new StringBuffer(); for (Node child = line.getFirstChild(); child != null; child = child.getNextSibling()){ if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; sb.append(cd.getData()); } } String text = sb.toString().trim(); thearray.add(text); System.out.println(">>> Added " + childsection + ": " + text); } } return thearray; }// End createArray method } // End JWebMonitor class
qualunque aiuto è più che gradito anche perché non sono un gran programmatore specialmente java.