1 22 package org.jboss.console.manager.interfaces.impl; 23 24 import org.jboss.console.navtree.AppletBrowser; 25 import org.jboss.console.navtree.AppletTreeAction; 26 import org.jboss.console.navtree.TreeContext; 27 import org.jfree.chart.ChartFactory; 28 import org.jfree.chart.ChartFrame; 29 import org.jfree.chart.JFreeChart; 30 import org.jfree.chart.plot.PlotOrientation; 31 import org.jfree.data.AbstractXYDataset; 32 import org.jfree.data.DatasetChangeEvent; 33 34 import javax.management.ObjectName ; 35 import java.util.ArrayList ; 36 37 52 public class GraphMBeanAttributeAction 53 implements AppletTreeAction 54 { 55 public class MBeanXYDataset extends AbstractXYDataset 56 { 57 58 private ArrayList data = new ArrayList (); 59 60 63 public MBeanXYDataset() 64 { 65 } 66 67 public void clear() 68 { 69 data.clear(); 70 notifyListeners(new DatasetChangeEvent(this, this)); 71 } 72 73 public void add(Object num) 74 { 75 data.add(num); 76 notifyListeners(new DatasetChangeEvent(this, this)); 77 } 78 79 87 public Number getXValue(int series, int item) 88 { 89 return new Integer (item); 90 } 91 92 100 public Number getYValue(int series, int item) 101 { 102 return (Number ) data.get(item); 103 } 104 105 110 public int getSeriesCount() 111 { 112 return 1; 113 } 114 115 122 public String getSeriesName(int series) 123 { 124 return "y = " + attr; 125 } 126 127 134 public int getItemCount(int series) 135 { 136 return data.size(); 137 } 138 } 139 140 public class UpdateThread implements Runnable 141 { 142 MBeanXYDataset data; 143 TreeContext tc; 144 145 public UpdateThread(MBeanXYDataset data, TreeContext tc) 146 { 147 this.data = data; 148 this.tc = tc; 149 } 150 151 public void run() 152 { 153 while (true) 154 { 155 try 156 { 157 if (frame.isShowing()) 158 { 159 Object val = tc.getRemoteMBeanInvoker().getAttribute(targetObjectName, attr); 160 System.out.println("added value: " + val); 161 data.add(val); 162 } 163 Thread.sleep(1000); 164 } 165 catch (Exception ex) 166 { 167 ex.printStackTrace(); 168 } 169 } 170 } 171 } 172 173 174 protected ObjectName targetObjectName = null; 175 protected String attr = null; 176 protected transient ChartFrame frame = null; 177 protected transient MBeanXYDataset dataset = null; 178 179 public GraphMBeanAttributeAction() 180 { 181 } 182 183 public GraphMBeanAttributeAction(ObjectName pName, 184 String attr) 185 { 186 this.targetObjectName = pName; 187 this.attr = attr; 188 } 189 190 public void doAction(TreeContext tc, AppletBrowser applet) 191 { 192 try 193 { 194 if (frame == null) 195 { 196 dataset = new MBeanXYDataset(); 198 JFreeChart chart = ChartFactory.createXYLineChart( 199 "JMX Attribute: " + attr, "count", attr, dataset, 200 PlotOrientation.VERTICAL, 201 true, 202 true, 203 false 204 ); 205 UpdateThread update = new UpdateThread(dataset, tc); 206 207 Thread thread = new Thread (update); 208 thread.start(); 209 frame = new ChartFrame("JMX Attribute: " + attr, chart); 210 frame.getChartPanel().setPreferredSize(new java.awt.Dimension (500, 270)); 211 frame.pack(); 212 } 213 else 214 { 215 dataset.clear(); 216 } 217 frame.show(); 218 frame.requestFocus(); 219 } 220 catch (Exception displayed) 221 { 222 displayed.printStackTrace(); 223 } 224 } 225 226 } 227 | Popular Tags |