KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > jmx > notifications > JmxNotification


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.jmx.notifications;
26
27 import java.io.IOException JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import org.dom4j.Document;
36 import org.dom4j.DocumentHelper;
37 import org.dom4j.Element;
38 import org.dom4j.io.OutputFormat;
39 import org.dom4j.io.SAXReader;
40 import org.dom4j.io.XMLWriter;
41
42 /**
43  * This class defines a JmxNotification class. This class is used by the
44  * <code>RmiConnector</code> on the controller to send a JMXNotification. This
45  * is done by the following code:
46  *
47  * <pre>
48  * JmxNotification cjdbcNotification = new JmxNotification(priority,
49  * &quot;&quot; + sequence, type, description, &quot;&quot;
50  * + time, controllerName, mbean
51  * .getClass().getName(), &quot;mbeanName&quot;,
52  * hostName, &quot;&quot; + port, data);
53  * </pre>
54  *
55  * This create an instance of this JmxNotification class, specific to CJDBC. We
56  * then create a new instance of the Notification object as specified in the
57  * javax.management package
58  *
59  * <pre>
60  * Notification notification = new Notification(type, mbean, sequence, myDate
61  * .getTime(), description);
62  * </pre>
63  *
64  * This class accepts a userData object. We wanted to set this user object to a
65  * specific CJDBC class but this forces generic JMX client to have this class in
66  * their classpath. We just serialize the JmxNotification into an XML string and
67  * feed it in the notification.
68  *
69  * <pre>
70  * notification.setUserData(cjdbcNotification.toString());
71  * </pre>
72  *
73  * This can be retrieved on any jmx client, and on C-JDBC specific clients, the
74  * xml is transformed into an instance of this class again for easier
75  * notification handling.
76  *
77  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
78  */

79 public class JmxNotification
80 {
81   /**
82    * All the tags and attributes used to parse the notification
83    */

84   private static final String JavaDoc ELT_jmxevent = "jmxevent";
85   private static final String JavaDoc ELT_info = "info";
86   private static final String JavaDoc ELT_source = "source";
87   private static final String JavaDoc ELT_data = "data";
88   private static final String JavaDoc ELT_priority = "priority";
89   private static final String JavaDoc ELT_sequence = "sequence";
90   private static final String JavaDoc ELT_type = "type";
91   private static final String JavaDoc ELT_description = "description";
92   private static final String JavaDoc ELT_time = "time";
93   private static final String JavaDoc ELT_controller = "controller";
94   private static final String JavaDoc ELT_mbean = "mbean";
95   private static final String JavaDoc ELT_class = "class";
96   private static final String JavaDoc ELT_server = "server";
97   private static final String JavaDoc ELT_value = "value";
98
99   private static final String JavaDoc ATT_ip = "ip";
100   private static final String JavaDoc ATT_port = "port";
101   private static final String JavaDoc ATT_name = "name";
102
103   String JavaDoc priority;
104   String JavaDoc sequence;
105   String JavaDoc type;
106   String JavaDoc description;
107   String JavaDoc controllerName;
108   String JavaDoc mbeanClass;
109   String JavaDoc mbeanName;
110   String JavaDoc mbeanServerIP;
111   String JavaDoc mbeanServerPort;
112   String JavaDoc time;
113   Hashtable JavaDoc dataList;
114
115   /**
116    * Create a new JmxNotification object
117    *
118    * @param priority notification priority
119    * @param sequence sequence number
120    * @param type notification type
121    * @param description notification description
122    * @param time time the notification was issued
123    * @param controllerName name of the controller issuing the notification
124    * @param mbeanClass class of the mbean issuing the notification
125    * @param mbeanName name of the mbean issuing the notification
126    * @param mbeanServerIP IP address of the mbean
127    * @param mbeanServerPort Port of the mbean
128    * @param dataList Additional data
129    */

130   public JmxNotification(String JavaDoc priority, String JavaDoc sequence, String JavaDoc type,
131       String JavaDoc description, String JavaDoc time, String JavaDoc controllerName,
132       String JavaDoc mbeanClass, String JavaDoc mbeanName, String JavaDoc mbeanServerIP,
133       String JavaDoc mbeanServerPort, Hashtable JavaDoc dataList)
134   {
135     this.priority = priority;
136     this.sequence = sequence;
137     this.type = type;
138     this.description = description;
139     this.controllerName = controllerName;
140     this.mbeanClass = mbeanClass;
141     this.mbeanName = mbeanName;
142     this.mbeanServerIP = mbeanServerIP;
143     this.mbeanServerPort = mbeanServerPort;
144     this.time = time;
145     this.dataList = dataList;
146   }
147
148   /**
149    * Parse the given xml to create a notification
150    *
151    * @param xml the xml to use to create a <code>JmxNotification</code>
152    * instance
153    * @return a <code>JmxNotification</code> instance which xml version is that
154    * of the given xml arg
155    * @throws Exception if cannot create an instance (the xml is invalid)
156    */

157   public static JmxNotification createNotificationFromXmlString(String JavaDoc xml)
158       throws Exception JavaDoc
159   {
160     StringReader JavaDoc sreader = new StringReader JavaDoc(xml);
161     SAXReader reader = new SAXReader();
162     Document document = reader.read(sreader);
163     return createNotificationFromXml(document);
164   }
165
166   /**
167    * @return Returns the dataList.
168    */

169   public Hashtable JavaDoc getDataList()
170   {
171     return dataList;
172   }
173
174   /**
175    * Returns the first value of an entry in the data list of values
176    *
177    * @param key value of a data parameter in the list
178    * @return <code>String</code> value of the corresponding key
179    */

180   public String JavaDoc getDataValue(String JavaDoc key)
181   {
182     if (!dataList.containsKey(key))
183       return null;
184     else
185       return (String JavaDoc) ((ArrayList JavaDoc) dataList.get(key)).get(0);
186   }
187
188   /**
189    * @return Returns the controllerName.
190    */

191   public String JavaDoc getControllerName()
192   {
193     return controllerName;
194   }
195
196   /**
197    * Return the controller jmx name
198    *
199    * @return <code>IP:Port</code>
200    */

201   public String JavaDoc getControllerJmxName()
202   {
203     return getMbeanServerIP() + ":" + getMbeanServerPort();
204   }
205
206   /**
207    * @param controllerName The controllerName to set.
208    */

209   public void setControllerName(String JavaDoc controllerName)
210   {
211     this.controllerName = controllerName;
212   }
213
214   /**
215    * @return Returns the description.
216    */

217   public String JavaDoc getDescription()
218   {
219     return description;
220   }
221
222   /**
223    * @param description The description to set.
224    */

225   public void setDescription(String JavaDoc description)
226   {
227     this.description = description;
228   }
229
230   /**
231    * @return Returns the mbeanClass.
232    */

233   public String JavaDoc getMbeanClass()
234   {
235     return mbeanClass;
236   }
237
238   /**
239    * @param mbeanClass The mbeanClass to set.
240    */

241   public void setMbeanClass(String JavaDoc mbeanClass)
242   {
243     this.mbeanClass = mbeanClass;
244   }
245
246   /**
247    * @return Returns the mbeanName.
248    */

249   public String JavaDoc getMbeanName()
250   {
251     return mbeanName;
252   }
253
254   /**
255    * @param mbeanName The mbeanName to set.
256    */

257   public void setMbeanName(String JavaDoc mbeanName)
258   {
259     this.mbeanName = mbeanName;
260   }
261
262   /**
263    * @return Returns the mbeanServerIP.
264    */

265   public String JavaDoc getMbeanServerIP()
266   {
267     return mbeanServerIP;
268   }
269
270   /**
271    * @param mbeanServerIP The mbeanServerIP to set.
272    */

273   public void setMbeanServerIP(String JavaDoc mbeanServerIP)
274   {
275     this.mbeanServerIP = mbeanServerIP;
276   }
277
278   /**
279    * @return Returns the mbeanServerPort.
280    */

281   public String JavaDoc getMbeanServerPort()
282   {
283     return mbeanServerPort;
284   }
285
286   /**
287    * @param mbeanServerPort The mbeanServerPort to set.
288    */

289   public void setMbeanServerPort(String JavaDoc mbeanServerPort)
290   {
291     this.mbeanServerPort = mbeanServerPort;
292   }
293
294   /**
295    * @return Returns the priority.
296    */

297   public String JavaDoc getPriority()
298   {
299     return priority;
300   }
301
302   /**
303    * @param priority The priority to set.
304    */

305   public void setPriority(String JavaDoc priority)
306   {
307     this.priority = priority;
308   }
309
310   /**
311    * @return Returns the sequence.
312    */

313   public String JavaDoc getSequence()
314   {
315     return sequence;
316   }
317
318   /**
319    * @param sequence The sequence to set.
320    */

321   public void setSequence(String JavaDoc sequence)
322   {
323     this.sequence = sequence;
324   }
325
326   /**
327    * @return Returns the type.
328    */

329   public String JavaDoc getType()
330   {
331     return type;
332   }
333
334   /**
335    * @param type The type to set.
336    */

337   public void setType(String JavaDoc type)
338   {
339     this.type = type;
340   }
341
342   /**
343    * Used as a factory to create an instance of this class from a xml document
344    *
345    * @param document a dom4j document
346    * @return an instance of this class with the corresponding parameters
347    */

348   public static JmxNotification createNotificationFromXml(Document document)
349   {
350     String JavaDoc priority = document.selectSingleNode(
351         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_priority).getText();
352     String JavaDoc sequence = document.selectSingleNode(
353         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_sequence).getText();
354     String JavaDoc type = document.selectSingleNode(
355         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_type).getText();
356     String JavaDoc description = document.selectSingleNode(
357         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_description).getText();
358     String JavaDoc time = document.selectSingleNode(
359         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_time).getText();
360
361     String JavaDoc controllerName = document.selectSingleNode(
362         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_controller)
363         .getText();
364     String JavaDoc mbeanclass = document.selectSingleNode(
365         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/"
366             + ELT_class).getText();
367     String JavaDoc mbeanname = document.selectSingleNode(
368         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean).valueOf(
369         "@" + ATT_name);
370     String JavaDoc serverip = document.selectSingleNode(
371         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/"
372             + ELT_server).valueOf("@" + ATT_ip);
373     String JavaDoc serverport = document.selectSingleNode(
374         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/"
375             + ELT_server).valueOf("@" + ATT_port);
376
377     Element root = document.getRootElement();
378
379     Hashtable JavaDoc dataList = new Hashtable JavaDoc();
380     for (Iterator JavaDoc i = root.elementIterator(ELT_data); i.hasNext();)
381     {
382       Element data = (Element) i.next();
383       ArrayList JavaDoc list = new ArrayList JavaDoc();
384       for (Iterator JavaDoc j = data.elementIterator(ELT_value); j.hasNext();)
385       {
386         Element value = (Element) j.next();
387         list.add(value.getTextTrim());
388       }
389       dataList.put(data.valueOf("@" + ATT_name), list);
390     }
391     JmxNotification notif = new JmxNotification(priority, sequence, type,
392         description, time, controllerName, mbeanclass, mbeanname, serverip,
393         serverport, dataList);
394     return notif;
395   }
396
397   /**
398    * Convert the object to the corresponding xml document instance
399    *
400    * @return <code>Document</code> object with the proper values
401    */

402   public Document toXmlDocument()
403   {
404     Document document = DocumentHelper.createDocument();
405     Element root = document.addElement(ELT_jmxevent);
406
407     // Describe info
408
Element info = root.addElement(ELT_info);
409     info.addElement(ELT_priority).addText(priority);
410     info.addElement(ELT_sequence).addText(sequence);
411     info.addElement(ELT_type).addText(type);
412     info.addElement(ELT_description).addText(description);
413     info.addElement(ELT_time).addText(time);
414
415     // Describe source
416
Element source = root.addElement(ELT_source);
417     source.addElement(ELT_controller).addText(controllerName);
418
419     // Describe mbean
420
Element mbean = source.addElement(ELT_mbean).addAttribute(ATT_name,
421         mbeanName);
422     mbean.addElement(ELT_class).addText(mbeanClass);
423     mbean.addElement(ELT_server).addAttribute(ATT_ip, mbeanServerIP)
424         .addAttribute(ATT_port, mbeanServerPort);
425
426     // Describe data
427
Enumeration JavaDoc keys = dataList.keys();
428     while (keys.hasMoreElements())
429     {
430       String JavaDoc key = (String JavaDoc) keys.nextElement();
431       Element data = root.addElement(ELT_data).addAttribute(ATT_name, key);
432
433       Object JavaDoc entry = dataList.get(key);
434       if (entry instanceof ArrayList JavaDoc)
435       {
436         ArrayList JavaDoc list = (ArrayList JavaDoc) entry;
437         for (int i = 0; i < list.size(); i++)
438           data.addElement(ELT_value).addText((String JavaDoc) list.get(i));
439       }
440       else if (entry instanceof String JavaDoc[])
441       {
442         String JavaDoc[] list = (String JavaDoc[]) entry;
443         for (int i = 0; i < list.length; i++)
444           data.addElement(ELT_value).addText(list[i]);
445       }
446       else if (entry instanceof String JavaDoc)
447       {
448         data.addElement(ELT_value).addText((String JavaDoc) entry);
449       }
450     }
451     return document;
452   }
453
454   /**
455    * @return <code>String</code> version in xml formatted text
456    */

457   public String JavaDoc toString()
458   {
459     StringWriter JavaDoc swriter = new StringWriter JavaDoc();
460     OutputFormat format = OutputFormat.createCompactFormat();
461     XMLWriter writer = new XMLWriter(swriter, format);
462     try
463     {
464       writer.write(toXmlDocument());
465     }
466     catch (IOException JavaDoc e)
467     {
468       // ignore
469
}
470     return swriter.getBuffer().toString();
471   }
472
473   /**
474    * @return Returns the time.
475    */

476   public String JavaDoc getTime()
477   {
478     return time;
479   }
480
481   /**
482    * @param time The time to set.
483    */

484   public void setTime(String JavaDoc time)
485   {
486     this.time = time;
487   }
488
489   /**
490    * @param dataList The dataList to set.
491    */

492   public void setDataList(Hashtable JavaDoc dataList)
493   {
494     this.dataList = dataList;
495   }
496 }
Popular Tags