1 7 package org.ejtools.management.browser.ui; 8 9 import java.awt.GridBagConstraints ; 10 import java.awt.Insets ; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.ActionListener ; 13 import java.util.ArrayList ; 14 import java.util.Hashtable ; 15 import java.util.Iterator ; 16 17 import javax.management.MBeanAttributeInfo ; 18 import javax.management.j2ee.statistics.Statistic ; 19 import javax.management.j2ee.statistics.Stats ; 20 import javax.swing.JButton ; 21 22 import org.apache.log4j.Logger; 23 import org.ejtools.graph.service.GraphConsumer; 24 import org.ejtools.graph.service.GraphConsumerMediator; 25 import org.ejtools.graph.service.GraphConsumerSelector; 26 import org.ejtools.management.browser.model.ManagedObject; 27 import org.ejtools.management.browser.ui.statistics.StatisticCustomizer; 28 import org.ejtools.management.browser.ui.statistics.StatisticCustomizerFactory; 29 30 36 public class ManagedObjectStatsViewer extends ManagedObjectViewer implements GraphConsumerMediator 37 { 38 39 private Hashtable customizers; 40 41 private static Logger logger = Logger.getLogger(ManagedObjectStatsViewer.class); 42 43 44 45 public ManagedObjectStatsViewer() 46 { 47 super(); 48 } 49 50 51 52 57 public ManagedObjectStatsViewer(Object bean) 58 { 59 super(bean); 60 } 61 62 63 67 public void addStatisticToGraph(String statisticName, String statisticProperty) 68 { 69 GraphConsumer consumer = GraphConsumerSelector.selectWithDialog(this); 70 if (consumer != null) 71 { 72 this.object.registerForGraph(consumer, statisticName, statisticProperty); 73 } 74 } 75 76 77 81 public MBeanAttributeInfo [] filter(MBeanAttributeInfo [] infos) 82 { 83 ArrayList filteredInfos = new ArrayList (); 84 ArrayList includes = new ArrayList (); 85 includes.add(MO_ATTRIBUTE_STATS); 86 87 for (int i = 0; i < infos.length; i++) 88 { 89 String name = infos[i].getName(); 90 if (includes.contains(name.toLowerCase())) 91 { 92 filteredInfos.add(infos[i]); 93 } 94 } 95 96 return (MBeanAttributeInfo []) filteredInfos.toArray(new MBeanAttributeInfo [0]); 97 } 98 99 100 105 public GraphConsumer[] getGraphConsumers() 106 { 107 return this.object.getGraphConsumers(); 108 } 109 110 111 116 public void setObject(Object bean) 117 { 118 try 119 { 120 this.panel.removeAll(); 121 122 if (bean == null) 123 { 124 this.validate(); 125 this.repaint(); 126 return; 127 } 128 129 this.object = (ManagedObject) bean; 130 132 GridBagConstraints gridbagconstraints = new GridBagConstraints (); 133 gridbagconstraints.insets = new Insets (1, 1, 1, 1); 134 gridbagconstraints.anchor = GridBagConstraints.NORTH; 135 gridbagconstraints.weightx = 1.0D; 136 gridbagconstraints.weighty = 0.0D; 137 gridbagconstraints.fill = GridBagConstraints.HORIZONTAL; 138 gridbagconstraints.gridwidth = GridBagConstraints.REMAINDER; 139 140 Stats stats = this.getStats(); 141 String [] names = stats.getStatisticNames(); 142 for (int i = 0; i < names.length; i++) 143 { 144 String name = names[i]; 145 146 Statistic statistic = stats.getStatistic(name); 147 StatisticCustomizer sc = StatisticCustomizerFactory.create(statistic); 148 149 if (sc != null) 150 { 151 this.panel.add(sc, gridbagconstraints); 152 this.getCustomizers().put(name, sc); 153 sc.setMediator(this); 154 sc.refresh(); 155 } 156 } 157 158 JButton button = new JButton (resources.getString("customizer.tab.statistics.button.refresh")); 159 button.addActionListener( 160 new ActionListener () 161 { 162 165 public void actionPerformed(ActionEvent e) 166 { 167 try 168 { 169 ManagedObjectStatsViewer.this.update(); 170 } 171 catch (Exception ex) 172 { 173 logger.error("Error while updating" + ex); 174 } 175 } 176 } 177 ); 178 gridbagconstraints.weighty = 1.0D; 179 this.panel.add(button, gridbagconstraints); 180 181 this.validate(); 182 this.repaint(); 183 } 184 catch (Exception e) 185 { 186 logger.error("Error while setting object" + e); 187 } 188 } 189 190 191 196 protected Hashtable getCustomizers() 197 { 198 if (this.customizers == null) 199 { 200 this.customizers = new Hashtable (); 201 } 202 return this.customizers; 203 } 204 205 206 212 protected Stats getStats() 213 throws Exception 214 { 215 return this.object.getStats(); 216 } 217 218 219 224 protected void update() 225 throws Exception 226 { 227 Stats stats = this.getStats(); 228 Iterator it = this.getCustomizers().keySet().iterator(); 229 while (it.hasNext()) 230 { 231 String name = (String ) it.next(); 232 StatisticCustomizer sc = (StatisticCustomizer) this.getCustomizers().get(name); 233 Statistic statistic = stats.getStatistic(name); 234 sc.setStatistic(statistic); 235 sc.refresh(); 236 } 237 } 238 } 239 | Popular Tags |