1 23 24 38 39 package com.sun.enterprise.admin.monitor.registry.spi; 40 41 import com.sun.enterprise.admin.monitor.registry.*; 42 import javax.management.j2ee.statistics.*; 43 import javax.management.*; 44 import java.util.ArrayList ; 45 import java.util.Hashtable ; 46 import java.util.Enumeration ; 47 import java.util.Collection ; 48 import java.util.logging.*; 49 import com.sun.enterprise.admin.common.constant.AdminConstants; 50 import com.sun.enterprise.util.i18n.StringManager; 51 56 public class StatsHolderImpl implements StatsHolder { 57 private final String name; 58 private final Hashtable children; 59 private final MBeanServer server; 60 private Class statsClass = null; 61 private String statsClassName = null; 62 63 MonitoredObjectType type = null; 64 Stats stats = null; 65 ObjectName objectName = null; 66 String dottedName = null; 67 68 private static final Logger logger = Logger.getLogger(AdminConstants.kLoggerName); private static final StringManager sm = StringManager.getManager(StatsHolderImpl.class); 70 71 public StatsHolderImpl(String name) { 72 this.name = name; 73 this.children = new Hashtable (); 74 this.server = findMBeanServer(); 75 logger.fine("StatsHolderImpl initialized, name = " + name); 76 } 77 78 public StatsHolderImpl(String name, MonitoredObjectType type) { 79 this(name); 80 this.setType(type); 81 } 82 83 86 private MBeanServer findMBeanServer() { 87 MBeanServer server = null; 88 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 89 if(!servers.isEmpty()){ 90 server = (MBeanServer)servers.get(0); 91 } 92 return server; 93 } 94 95 99 private StatsHolder addChild(StatsHolder sh) { 100 assert (sh != null) : "Null StatsHolder to be added"; 102 if(!children.containsKey(sh.getName())){ 103 children.put(sh.getName(), sh); 104 logger.fine("StatsHolder.addChild: New key created with statsHolder key name = " + sh.getName() + " type = " + sh.getType().toString()); 105 } 106 else { 107 sh = this.getChild(sh.getName()); 108 logger.fine("Not adding, StatsHolder.addChild: Child exists, name = " + sh.getName() + " type = " + sh.getType().toString()); 109 } 111 return ( sh ); 112 } 113 114 120 public StatsHolder addChild(String name, MonitoredObjectType type) { 121 assert ( name != null && type != null) : "Asked to add a null name-type child"; 122 StatsHolder child = this.getChild(name); 123 if (child == null) 124 child = this.addChild(new StatsHolderImpl(name, type)); 125 126 return ( child ); 127 } 128 129 134 public Collection getAllChildren() { 135 return children.values(); 136 } 137 138 141 public void removeAllChildren() { 142 if (children.isEmpty()) { 144 this.unregisterMBean(); 145 } 146 else { 147 final Enumeration e = children.elements(); 148 while (e.hasMoreElements()) { 149 final StatsHolder sh = (StatsHolder)e.nextElement(); 150 sh.removeAllChildren(); 151 } 152 children.clear(); 153 } 154 } 155 156 161 public void removeChild(String name) { 162 children.remove(name); 163 } 164 165 168 public String getName(){ 169 return name; 170 } 171 172 175 public MonitoredObjectType getType(){ 176 return type; 177 } 178 179 184 public void setStats(Stats stats) { 185 this.stats=stats; 186 } 187 188 public Stats getStats() { 189 return ( this.stats ); 190 } 191 194 public void setObjectName(ObjectName name) { 195 this.objectName = name; 196 } 197 198 public ObjectName getObjectName(){ 199 return objectName; 200 } 201 202 public void setDottedName(String dottedName) { 203 this.dottedName = dottedName; 204 } 205 206 public String getDottedName() { 207 return dottedName; 208 } 209 210 213 public void registerMBean() { 214 try { 215 if (server.isRegistered(this.objectName)) { 216 logger.fine("ObjectName obj is already registered - this is an error, ignoring for now : " + this.objectName); 217 return; 218 } 219 final StatsHolderMBean mbean = new StatsHolderMBeanImpl(this); 222 server.registerMBean(mbean, this.objectName); 223 logger.finer("Registered the MBean for this StatsHolder: " + this.objectName); 224 } 225 catch(Exception e) { 226 logger.fine("SH.registerMBean: Exception for object-name " + this.objectName); 227 logger.throwing(StatsHolderImpl.class.getName(), "registerMBean()", e); 228 } 230 } 231 232 235 public void unregisterMBean() { 236 String msg = null; 237 try { 238 if (server.isRegistered(this.objectName)) { 239 server.unregisterMBean(this.objectName); 240 logger.fine("SH.unregisterMonitoringMBean(): unregistered - " + objectName); 241 } 242 else { 243 logger.fine("SH.unregisterMonitoringMBean(): never registered - so not unregistering" + objectName); 244 } 245 } 246 catch(Exception e){ 247 logger.fine("SH.unregisterMBeanUnregistration failed, objectName = " + objectName); 248 } 250 } 251 252 public void setType(MonitoredObjectType type) { 253 this.type = type; 254 } 255 256 public StatsHolder getChild(String name) { 257 final StatsHolder child = (StatsHolder) children.get(name); 258 if (child == null) { 259 logger.fine("SH.getChild - child is null with name = " + name); 260 } 261 return ( child ); 262 } 263 264 265 void write() { 266 } 268 269 public Class getStatsClass() { 270 return ( this.statsClass ); 271 } 272 273 public void setStatsClass(Class c) { 274 if (! javax.management.j2ee.statistics.Stats .class.isAssignableFrom(c)) { 275 final String msg = sm.getString("sh.not_a_stats_interface", c.getName()); 276 throw new IllegalArgumentException (msg); 277 } 278 if (! c.isInterface()) { 280 final String msg = sm.getString("sh.should_be_an_interface", c.getName()); 281 throw new IllegalArgumentException (msg); 282 } 283 284 this.statsClass = c; 285 this.statsClassName = c.getName(); 286 } 287 288 public String getStatsClassName() { 289 return ( this.statsClassName ); 290 } 291 292 public void setStatsClassName(String cName) { 293 Class c = null; 294 try { 295 c = Class.forName(cName); 296 } catch (ClassNotFoundException cnfe) { 297 final String msg = sm.getString("invalidclassname.statsholder", 298 cName); 299 throw new IllegalArgumentException (msg); 300 } 301 302 if (! javax.management.j2ee.statistics.Stats .class.isAssignableFrom(c)) { 303 final String msg = sm.getString("sh.not_a_stats_interface", c.getName()); 304 throw new IllegalArgumentException (msg); 305 } 306 if (! c.isInterface()) { 308 final String msg = sm.getString("sh.should_be_an_interface", c.getName()); 309 throw new IllegalArgumentException (msg); 310 } 311 312 this.statsClass = c; 313 this.statsClassName = cName; 314 } 315 316 private class DumpThread extends Thread { 317 public void run() { 318 while (true) { 319 logger.fine("Start Element: " + name); 320 if (children.isEmpty()) { 321 logger.fine("Leaf Element"); 322 } 323 else { 324 final Enumeration e = children.elements(); 325 while (e.hasMoreElements()) { 326 final StatsHolderImpl c = (StatsHolderImpl) e.nextElement(); 327 c.write(); 328 } 329 } 330 logger.fine("End Element: " + name); 331 try { 332 Thread.sleep(5000); 333 } 334 catch(InterruptedException e) {} 335 } 336 } 337 } 338 } 339 | Popular Tags |