/* Author: Julie Romm * Course: COM3336 * Professor: Karl Lieberherr * Homework 3, part 2 */ /* File: URLDiffThread.java * Classes implemented: URLDiffThread * * Purpose: * This class compares to files using UNIX diff command. * The result of the comparison is written into a file. * In case diff produces output, the EMail object is * instantiated, and the diff file is sent to the * RECIPIENT (a constant defined in WebWatch.java) * After that, the diff file is removed. */ import java.io.*; public class URLDiffThread extends Thread{ private String oldname; private String newname; private String diffname; private String filename; private Process p; private Runtime r; private BufferedWriter bw; public URLDiffThread(String filename) { this.filename = filename; oldname = filename + ".old"; newname = filename + ".new"; diffname = filename + ".diff"; } public void run() { r = Runtime.getRuntime(); try {p = r.exec("diff " + oldname + " " + newname);} catch (IOException e) { System.err.println("URLDiffThread: error: " + e); } try { bw = new BufferedWriter(new FileWriter(diffname));} catch (IOException e){}; //need to capture the output of the command into the file InputStream is = p.getInputStream(); try { int i = is.read(); while (i != -1) { bw.write(i); i = is.read(); } bw.close(); } catch (IOException e){}; File difffile = new File(diffname); //for debug: System.out.println(difffile.length()); if (difffile.length() != 0) { sendEMail(filename); System.out.println("Mail was sent to " + WebWatch.RECIPIENT); } else System.out.println("WebWatch detected no changes in http://" + filename); //remove the diff file difffile.delete(); } private void sendEMail(String filename) { try { BufferedReader br = new BufferedReader(new FileReader(filename + ".diff")); EMail email = new EMail(); System.out.println("WebWatch detected changes in http://" + filename); email.sendMail( WebWatch.MAILSERVER, WebWatch.SENDER, WebWatch.RECIPIENT, "WebWatch detected changes in http://" + filename, br); } catch (FileNotFoundException e) { System.out.println("Error: File not found: " + filename); } } //sendEMail } //URLDiffThread