KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > jmx > notifications > JmxNotification


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk
20  * Contributor(s): ______________________.
21  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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