Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

JmxNotification.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.jmx.notifications;
00026 
00027 import java.io.IOException;
00028 import java.io.StringReader;
00029 import java.io.StringWriter;
00030 import java.util.ArrayList;
00031 import java.util.Enumeration;
00032 import java.util.Hashtable;
00033 import java.util.Iterator;
00034 
00035 import org.dom4j.Document;
00036 import org.dom4j.DocumentHelper;
00037 import org.dom4j.Element;
00038 import org.dom4j.io.OutputFormat;
00039 import org.dom4j.io.SAXReader;
00040 import org.dom4j.io.XMLWriter;
00041 
00042 /**
00043  * This class defines a JmxNotification class. This class is used by the
00044  * <code>RmiConnector</code> on the controller to send a JMXNotification. This
00045  * is done by the following code:
00046  * 
00047  * <pre>
00048  * JmxNotification cjdbcNotification = new JmxNotification(priority,
00049  *                                       &quot;&quot; + sequence, type, description, &quot;&quot;
00050  *                                           + time, controllerName, mbean
00051  *                                           .getClass().getName(), &quot;mbeanName&quot;,
00052  *                                       hostName, &quot;&quot; + port, data);
00053  * </pre>
00054  * 
00055  * This create an instance of this JmxNotification class, specific to CJDBC. We
00056  * then create a new instance of the Notification object as specified in the
00057  * javax.management package
00058  * 
00059  * <pre>
00060  * Notification notification = new Notification(type, mbean, sequence, myDate
00061  *     .getTime(), description);
00062  * </pre>
00063  * 
00064  * This class accepts a userData object. We wanted to set this user object to a
00065  * specific CJDBC class but this forces generic JMX client to have this class in
00066  * their classpath. We just serialize the JmxNotification into an XML string and
00067  * feed it in the notification.
00068  * 
00069  * <pre>
00070  * notification.setUserData(cjdbcNotification.toString());
00071  * </pre>
00072  * 
00073  * This can be retrieved on any jmx client, and on C-JDBC specific clients, the
00074  * xml is transformed into an instance of this class again for easier
00075  * notification handling.
00076  * 
00077  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00078  */
00079 public class JmxNotification
00080 {
00081   /**
00082    * All the tags and attributes used to parse the notification
00083    */
00084   private static final String ELT_jmxevent    = "jmxevent";
00085   private static final String ELT_info        = "info";
00086   private static final String ELT_source      = "source";
00087   private static final String ELT_data        = "data";
00088   private static final String ELT_priority    = "priority";
00089   private static final String ELT_sequence    = "sequence";
00090   private static final String ELT_type        = "type";
00091   private static final String ELT_description = "description";
00092   private static final String ELT_time        = "time";
00093   private static final String ELT_controller  = "controller";
00094   private static final String ELT_mbean       = "mbean";
00095   private static final String ELT_class       = "class";
00096   private static final String ELT_server      = "server";
00097   private static final String ELT_value       = "value";
00098 
00099   private static final String ATT_ip          = "ip";
00100   private static final String ATT_port        = "port";
00101   private static final String ATT_name        = "name";
00102 
00103   String                      priority;
00104   String                      sequence;
00105   String                      type;
00106   String                      description;
00107   String                      controllerName;
00108   String                      mbeanClass;
00109   String                      mbeanName;
00110   String                      mbeanServerIP;
00111   String                      mbeanServerPort;
00112   String                      time;
00113   Hashtable                   dataList;
00114 
00115   /**
00116    * Create a new JmxNotification object
00117    * 
00118    * @param priority notification priority
00119    * @param sequence sequence number
00120    * @param type notification type
00121    * @param description notification description
00122    * @param time time the notification was issued
00123    * @param controllerName name of the controller issuing the notification
00124    * @param mbeanClass class of the mbean issuing the notification
00125    * @param mbeanName name of the mbean issuing the notification
00126    * @param mbeanServerIP IP address of the mbean
00127    * @param mbeanServerPort Port of the mbean
00128    * @param dataList Additional data
00129    */
00130   public JmxNotification(String priority, String sequence, String type,
00131       String description, String time, String controllerName,
00132       String mbeanClass, String mbeanName, String mbeanServerIP,
00133       String mbeanServerPort, Hashtable dataList)
00134   {
00135     this.priority = priority;
00136     this.sequence = sequence;
00137     this.type = type;
00138     this.description = description;
00139     this.controllerName = controllerName;
00140     this.mbeanClass = mbeanClass;
00141     this.mbeanName = mbeanName;
00142     this.mbeanServerIP = mbeanServerIP;
00143     this.mbeanServerPort = mbeanServerPort;
00144     this.time = time;
00145     this.dataList = dataList;
00146   }
00147 
00148   /**
00149    * Parse the given xml to create a notification
00150    * 
00151    * @param xml the xml to use to create a <code>JmxNotification</code>
00152    *          instance
00153    * @return a <code>JmxNotification</code> instance which xml version is that
00154    *         of the given xml arg
00155    * @throws Exception if cannot create an instance (the xml is invalid)
00156    */
00157   public static JmxNotification createNotificationFromXmlString(String xml)
00158       throws Exception
00159   {
00160     StringReader sreader = new StringReader(xml);
00161     SAXReader reader = new SAXReader();
00162     Document document = reader.read(sreader);
00163     return createNotificationFromXml(document);
00164   }
00165 
00166   /**
00167    * @return Returns the dataList.
00168    */
00169   public Hashtable getDataList()
00170   {
00171     return dataList;
00172   }
00173 
00174   /**
00175    * Returns the first value of an entry in the data list of values
00176    * 
00177    * @param key value of a data parameter in the list
00178    * @return <code>String</code> value of the corresponding key
00179    */
00180   public String getDataValue(String key)
00181   {
00182     if (!dataList.containsKey(key))
00183       return null;
00184     else
00185       return (String) ((ArrayList) dataList.get(key)).get(0);
00186   }
00187 
00188   /**
00189    * @return Returns the controllerName.
00190    */
00191   public String getControllerName()
00192   {
00193     return controllerName;
00194   }
00195 
00196   /**
00197    * Return the controller jmx name
00198    * 
00199    * @return <code>IP:Port</code>
00200    */
00201   public String getControllerJmxName()
00202   {
00203     return getMbeanServerIP() + ":" + getMbeanServerPort();
00204   }
00205 
00206   /**
00207    * @param controllerName The controllerName to set.
00208    */
00209   public void setControllerName(String controllerName)
00210   {
00211     this.controllerName = controllerName;
00212   }
00213 
00214   /**
00215    * @return Returns the description.
00216    */
00217   public String getDescription()
00218   {
00219     return description;
00220   }
00221 
00222   /**
00223    * @param description The description to set.
00224    */
00225   public void setDescription(String description)
00226   {
00227     this.description = description;
00228   }
00229 
00230   /**
00231    * @return Returns the mbeanClass.
00232    */
00233   public String getMbeanClass()
00234   {
00235     return mbeanClass;
00236   }
00237 
00238   /**
00239    * @param mbeanClass The mbeanClass to set.
00240    */
00241   public void setMbeanClass(String mbeanClass)
00242   {
00243     this.mbeanClass = mbeanClass;
00244   }
00245 
00246   /**
00247    * @return Returns the mbeanName.
00248    */
00249   public String getMbeanName()
00250   {
00251     return mbeanName;
00252   }
00253 
00254   /**
00255    * @param mbeanName The mbeanName to set.
00256    */
00257   public void setMbeanName(String mbeanName)
00258   {
00259     this.mbeanName = mbeanName;
00260   }
00261 
00262   /**
00263    * @return Returns the mbeanServerIP.
00264    */
00265   public String getMbeanServerIP()
00266   {
00267     return mbeanServerIP;
00268   }
00269 
00270   /**
00271    * @param mbeanServerIP The mbeanServerIP to set.
00272    */
00273   public void setMbeanServerIP(String mbeanServerIP)
00274   {
00275     this.mbeanServerIP = mbeanServerIP;
00276   }
00277 
00278   /**
00279    * @return Returns the mbeanServerPort.
00280    */
00281   public String getMbeanServerPort()
00282   {
00283     return mbeanServerPort;
00284   }
00285 
00286   /**
00287    * @param mbeanServerPort The mbeanServerPort to set.
00288    */
00289   public void setMbeanServerPort(String mbeanServerPort)
00290   {
00291     this.mbeanServerPort = mbeanServerPort;
00292   }
00293 
00294   /**
00295    * @return Returns the priority.
00296    */
00297   public String getPriority()
00298   {
00299     return priority;
00300   }
00301 
00302   /**
00303    * @param priority The priority to set.
00304    */
00305   public void setPriority(String priority)
00306   {
00307     this.priority = priority;
00308   }
00309 
00310   /**
00311    * @return Returns the sequence.
00312    */
00313   public String getSequence()
00314   {
00315     return sequence;
00316   }
00317 
00318   /**
00319    * @param sequence The sequence to set.
00320    */
00321   public void setSequence(String sequence)
00322   {
00323     this.sequence = sequence;
00324   }
00325 
00326   /**
00327    * @return Returns the type.
00328    */
00329   public String getType()
00330   {
00331     return type;
00332   }
00333 
00334   /**
00335    * @param type The type to set.
00336    */
00337   public void setType(String type)
00338   {
00339     this.type = type;
00340   }
00341 
00342   /**
00343    * Used as a factory to create an instance of this class from a xml document
00344    * 
00345    * @param document a dom4j document
00346    * @return an instance of this class with the corresponding parameters
00347    */
00348   public static JmxNotification createNotificationFromXml(Document document)
00349   {
00350     String priority = document.selectSingleNode(
00351         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_priority).getText();
00352     String sequence = document.selectSingleNode(
00353         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_sequence).getText();
00354     String type = document.selectSingleNode(
00355         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_type).getText();
00356     String description = document.selectSingleNode(
00357         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_description).getText();
00358     String time = document.selectSingleNode(
00359         "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_time).getText();
00360 
00361     String controllerName = document.selectSingleNode(
00362         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_controller)
00363         .getText();
00364     String mbeanclass = document.selectSingleNode(
00365         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/"
00366             + ELT_class).getText();
00367     String mbeanname = document.selectSingleNode(
00368         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean).valueOf(
00369         "@" + ATT_name);
00370     String serverip = document.selectSingleNode(
00371         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/"
00372             + ELT_server).valueOf("@" + ATT_ip);
00373     String serverport = document.selectSingleNode(
00374         "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/"
00375             + ELT_server).valueOf("@" + ATT_port);
00376 
00377     Element root = document.getRootElement();
00378 
00379     Hashtable dataList = new Hashtable();
00380     for (Iterator i = root.elementIterator(ELT_data); i.hasNext();)
00381     {
00382       Element data = (Element) i.next();
00383       ArrayList list = new ArrayList();
00384       for (Iterator j = data.elementIterator(ELT_value); j.hasNext();)
00385       {
00386         Element value = (Element) j.next();
00387         list.add(value.getTextTrim());
00388       }
00389       dataList.put(data.valueOf("@" + ATT_name), list);
00390     }
00391     JmxNotification notif = new JmxNotification(priority, sequence, type,
00392         description, time, controllerName, mbeanclass, mbeanname, serverip,
00393         serverport, dataList);
00394     return notif;
00395   }
00396 
00397   /**
00398    * Convert the object to the corresponding xml document instance
00399    * 
00400    * @return <code>Document</code> object with the proper values
00401    */
00402   public Document toXmlDocument()
00403   {
00404     Document document = DocumentHelper.createDocument();
00405     Element root = document.addElement(ELT_jmxevent);
00406 
00407     // Describe info
00408     Element info = root.addElement(ELT_info);
00409     info.addElement(ELT_priority).addText(priority);
00410     info.addElement(ELT_sequence).addText(sequence);
00411     info.addElement(ELT_type).addText(type);
00412     info.addElement(ELT_description).addText(description);
00413     info.addElement(ELT_time).addText(time);
00414 
00415     // Describe source
00416     Element source = root.addElement(ELT_source);
00417     source.addElement(ELT_controller).addText(controllerName);
00418 
00419     // Describe mbean
00420     Element mbean = source.addElement(ELT_mbean).addAttribute(ATT_name,
00421         mbeanName);
00422     mbean.addElement(ELT_class).addText(mbeanClass);
00423     mbean.addElement(ELT_server).addAttribute(ATT_ip, mbeanServerIP)
00424         .addAttribute(ATT_port, mbeanServerPort);
00425 
00426     // Describe data
00427     Enumeration keys = dataList.keys();
00428     while (keys.hasMoreElements())
00429     {
00430       String key = (String) keys.nextElement();
00431       Element data = root.addElement(ELT_data).addAttribute(ATT_name, key);
00432 
00433       Object entry = dataList.get(key);
00434       if (entry instanceof ArrayList)
00435       {
00436         ArrayList list = (ArrayList) entry;
00437         for (int i = 0; i < list.size(); i++)
00438           data.addElement(ELT_value).addText((String) list.get(i));
00439       }
00440       else if (entry instanceof String[])
00441       {
00442         String[] list = (String[]) entry;
00443         for (int i = 0; i < list.length; i++)
00444           data.addElement(ELT_value).addText(list[i]);
00445       }
00446       else if (entry instanceof String)
00447       {
00448         data.addElement(ELT_value).addText((String) entry);
00449       }
00450     }
00451     return document;
00452   }
00453 
00454   /**
00455    * @return <code>String</code> version in xml formatted text
00456    */
00457   public String toString()
00458   {
00459     StringWriter swriter = new StringWriter();
00460     OutputFormat format = OutputFormat.createCompactFormat();
00461     XMLWriter writer = new XMLWriter(swriter, format);
00462     try
00463     {
00464       writer.write(toXmlDocument());
00465     }
00466     catch (IOException e)
00467     {
00468       // ignore
00469     }
00470     return swriter.getBuffer().toString();
00471   }
00472 
00473   /**
00474    * @return Returns the time.
00475    */
00476   public String getTime()
00477   {
00478     return time;
00479   }
00480 
00481   /**
00482    * @param time The time to set.
00483    */
00484   public void setTime(String time)
00485   {
00486     this.time = time;
00487   }
00488 
00489   /**
00490    * @param dataList The dataList to set.
00491    */
00492   public void setDataList(Hashtable dataList)
00493   {
00494     this.dataList = dataList;
00495   }
00496 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1