1 package net.sf.jasperreports.mysql; 2 3 import org.xml.sax.Attributes ; 4 import org.xml.sax.InputSource ; 5 import org.xml.sax.XMLReader ; 6 import org.xml.sax.helpers.DefaultHandler ; 7 import org.xml.sax.helpers.XMLReaderFactory ; 8 9 import java.io.FileReader ; 10 import java.util.ArrayList ; 11 import java.util.List ; 12 import java.util.Stack ; 13 import java.util.Timer ; 14 15 29 public class Daemon extends DefaultHandler { 30 31 public List sources = new ArrayList (); 33 34 private List timers = new ArrayList (); 36 37 private Destination destination = new Destination(); 39 40 private Database currentSource; 42 private Timer currentTimer; 43 private Task currentTask; 44 45 private String dbType = null; 47 48 private Stack elementStack = new Stack (); 50 51 58 public static void main(String args[]) { 59 Daemon daemon = new Daemon(); 60 try { 63 daemon.loadConfig(args[0]); 64 } catch (Exception e) { 65 System.err.println( 66 "Error: please enter a valid filename for configuration."); 67 } 68 System.out.println( 69 "Data acquisition daemon running... (Press Ctrl+C to stop)"); 70 } 71 72 79 public void loadConfig(String filename) { 80 try { 81 82 System.setProperty("org.xml.sax.driver", 84 "org.apache.crimson.parser.XMLReaderImpl"); 85 86 XMLReader xr = XMLReaderFactory.createXMLReader(); 88 xr.setContentHandler(this); 89 xr.setErrorHandler(this); 90 FileReader r = new FileReader (filename); 91 xr.parse(new InputSource (r)); 92 93 } catch (Exception e) { 94 e.printStackTrace(); 96 System.err.println("Exception: " + e.getMessage()); 97 System.err.println("You probably enterd a bad filename."); 98 System.exit(1); 99 } 100 101 } 102 103 114 115 118 public void startElement(String uri, String name, String qName, 119 Attributes atts) { 120 122 elementStack.push(qName); 124 if (qName == "source") { 126 dbType = "source"; 129 130 currentSource = new Database(); 132 sources.add(currentSource); 134 135 currentTimer = new Timer (); 137 timers.add(currentTimer); 139 } 140 if (qName == "destination") { 142 dbType = "destination"; 145 } 146 if (qName == "status") { 148 150 currentTask = new StatusTask(currentSource, destination); 152 153 long period = Long.parseLong(atts.getValue("period")); 155 currentTimer.scheduleAtFixedRate(currentTask, 0, period); 156 } 157 if (qName == "tableStatus") { 159 161 currentTask = new TableStatusTask(currentSource, destination); 163 164 long period = Long.parseLong(atts.getValue("period")); 166 currentTimer.scheduleAtFixedRate(currentTask, 0, period); 167 } 168 if (qName == "processList") { 170 172 currentTask = new ProcessListTask(currentSource, destination); 174 175 long period = Long.parseLong(atts.getValue("period")); 177 currentTimer.scheduleAtFixedRate(currentTask, 0, period); 178 } 179 } 180 181 187 public void endElement(String uri, String name, String qName) { 188 Object popped = elementStack.pop(); 189 if (name == "destination") { 191 destination.createTables(); 193 } 194 } 195 196 202 public void characters(char ch[], int start, int length) { 203 String value = new String (ch, start, length); 205 206 if (!value.startsWith("\n") && !value.startsWith(" ") && 208 !value.startsWith("\t")) { 209 setConf((String ) elementStack.peek(), value); 211 } 212 } 213 214 222 public void setConf(String element, String value) { 223 224 Database database = null; 225 if (dbType == "source") { 227 database = (Database) sources.get(sources.size() - 1); 228 } else { 229 database = destination; 230 } 231 232 234 if (element == "name") { 236 database.name = value; 237 } else if (element == "login") { 238 database.login = value; 239 } else if (element == "pass") { 240 database.pass = value; 241 } else if (element == "url") { 242 database.url = value; 243 } else { 244 System.out.println( 245 "Warning: configuration tag not recognized: " + element); 246 } 247 } 248 249 } | Popular Tags |