KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > mysql > Daemon


1 package net.sf.jasperreports.mysql;
2
3 import org.xml.sax.Attributes JavaDoc;
4 import org.xml.sax.InputSource JavaDoc;
5 import org.xml.sax.XMLReader JavaDoc;
6 import org.xml.sax.helpers.DefaultHandler JavaDoc;
7 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
8
9 import java.io.FileReader JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Stack JavaDoc;
13 import java.util.Timer JavaDoc;
14
15 /**
16  * Command line data acquisition daemon for collecting statistics from multiple
17  * MySQL servers and storing the data collected into a 'destination' MySQL
18  * database.
19  * <p/>
20  * To run the Daemon, everything is included if you are using JRE 1.4. Simply
21  * type:
22  * java -jar jasper-mysql.jar configuration.xml
23  * <p/>
24  * Where configuration.xml is the path to the configuration file to use. The
25  * Daemon will just sit and run forever, collecting stats as needed.
26  * <p/>
27  * The stats collected from this process can then be reported on.
28  */

29 public class Daemon extends DefaultHandler JavaDoc {
30
31     //the list of sources
32
public List JavaDoc sources = new ArrayList JavaDoc();
33
34     //list of timers
35
private List JavaDoc timers = new ArrayList JavaDoc();
36
37     //the destination for report information
38
private Destination destination = new Destination();
39
40     //temporary vars for loading the config file with
41
private Database currentSource;
42     private Timer JavaDoc currentTimer;
43     private Task currentTask;
44
45     //global for setConf
46
private String JavaDoc dbType = null;
47
48     //keepd track of the current tag for the character event handler
49
private Stack JavaDoc elementStack = new Stack JavaDoc();
50
51     /**
52      * The main method of jasper-mysql
53      * loads the configuration info and runs start the timer thread(s).
54      * One background thread is created for each source of data.
55      *
56      * @param args - the filename of the XML config.
57      */

58     public static void main(String JavaDoc args[]) {
59         Daemon daemon = new Daemon();
60         //parse the config file given on the command line
61
//Note that loading the config creates AND starts the timers.
62
try {
63             daemon.loadConfig(args[0]);
64         } catch (Exception JavaDoc 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     /**
73      * parses an xml file with the given filename using SAX.
74      * For each datasource, a Timer object is created to capture statistics
75      * at the necessary intervals.
76      *
77      * @param filename - the file to be parsed.
78      */

79     public void loadConfig(String JavaDoc filename) {
80         try {
81
82             //set the driver path
83
System.setProperty("org.xml.sax.driver",
84                     "org.apache.crimson.parser.XMLReaderImpl");
85
86             //create a parser and parse the file
87
XMLReader JavaDoc xr = XMLReaderFactory.createXMLReader();
88             xr.setContentHandler(this);
89             xr.setErrorHandler(this);
90             FileReader JavaDoc r = new FileReader JavaDoc(filename);
91             xr.parse(new InputSource JavaDoc(r));
92
93         } catch (Exception JavaDoc e) {
94             //Doh! probably bad filename
95
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     /* Event handlers for xml parsing.
104        Initializes the Database object based on which tag it sees.
105        tag:
106         source - creates a new source and adds it to the source list.
107                - creates a new timer and adds it to the timer list.
108                - tells setConf to fill in a source object.
109         destination - tells setConf to fill in the destination object.
110         status - creates a new status task and schedules it to the timer created by the source tag.
111         tableStatus - creates a new table status task and schedules it to the timer created by the source tag.
112         processList - creates a new process list task and schedules it to the timer created by the source tag.
113     */

114
115     /**
116      * @see DefaultHandler#startElement(String, String, String, Attributes)
117      */

118     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc qName,
119                              Attributes JavaDoc atts) {
120         //create new objects depending on which tag is encounterd
121

122         //push the current element onto the stack - used in character
123
elementStack.push(qName);
124         //source
125
if (qName == "source") {
126             //System.out.println(" adding new source");
127
//set the global for setConf
128
dbType = "source";
129
130             //create a new source
131
currentSource = new Database();
132             //add it tot he source list
133
sources.add(currentSource);
134
135             //create a new timer
136
currentTimer = new Timer JavaDoc();
137             //add it to the timer list
138
timers.add(currentTimer);
139         }
140         //destination
141
if (qName == "destination") {
142             //System.out.println(" setting destination");
143
//set the global for setConf
144
dbType = "destination";
145         }
146         //status
147
if (qName == "status") {
148             //System.out.println(" adding new status task");
149

150             //creat a new status task
151
currentTask = new StatusTask(currentSource, destination);
152
153             //get the time in milliseconds
154
long period = Long.parseLong(atts.getValue("period"));
155             currentTimer.scheduleAtFixedRate(currentTask, 0, period);
156         }
157         //tablestatus
158
if (qName == "tableStatus") {
159             //System.out.println(" adding new table status task");
160

161             //create a new table status task
162
currentTask = new TableStatusTask(currentSource, destination);
163
164             //get the time in milliseconds
165
long period = Long.parseLong(atts.getValue("period"));
166             currentTimer.scheduleAtFixedRate(currentTask, 0, period);
167         }
168         //process list
169
if (qName == "processList") {
170             //System.out.println(" adding new processList task");
171

172             //create a new process list task
173
currentTask = new ProcessListTask(currentSource, destination);
174
175             //get the time in milliseconds
176
long period = Long.parseLong(atts.getValue("period"));
177             currentTimer.scheduleAtFixedRate(currentTask, 0, period);
178         }
179     }
180
181     /**
182      * called by SAX when each element ends.
183      * pops an element off of the element stack.
184      *
185      * @see DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
186      */

187     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc qName) {
188         Object JavaDoc popped = elementStack.pop();
189         //were done setting config stuff for this...
190
if (name == "destination") {
191             //...so now we can connect and create the tables
192
destination.createTables();
193         }
194     }
195
196     /**
197      * called by SAX when the parser encounters characters outside a tag.
198      * Gets the values to be put in the Database objects.
199      *
200      * @see DefaultHandler#characters(char[], int, int)
201      */

202     public void characters(char ch[], int start, int length) {
203         //convert the char array to a string
204
String JavaDoc value = new String JavaDoc(ch, start, length);
205
206         //ignore whitespace
207
if (!value.startsWith("\n") && !value.startsWith(" ") &&
208                 !value.startsWith("\t")) {
209             //fill the cureent dbType database with a value from tag elementstack.peek()
210
setConf((String JavaDoc) elementStack.peek(), value);
211         }
212     }
213
214     /**
215      * Enters data into Database objects based on element.
216      * dbType is set in startElement to either source or destination,
217      * depending on which tag was encountered
218      *
219      * @param element - the current tag name
220      * @param value - the String inside the current tag.
221      */

222     public void setConf(String JavaDoc element, String JavaDoc value) {
223
224         Database database = null;
225         //check which type of database to fill
226
if (dbType == "source") {
227             database = (Database) sources.get(sources.size() - 1);
228         } else {
229             database = destination;
230         }
231
232         //System.out.println("\tsetting " + element + " to " + value);
233

234         //check which tag we're in
235
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