1 23 24 38 39 package com.sun.enterprise.admin.monitor.registry.spi; 40 41 import java.util.logging.Logger ; 42 import java.util.Collection ; 43 import java.util.Iterator ; 44 import java.util.ArrayList ; 45 import java.util.List ; 46 import java.util.Map ; 47 import javax.management.MBeanServer ; 48 import javax.management.ObjectName ; 49 import javax.management.MBeanInfo ; 50 import javax.management.Attribute ; 51 import javax.management.AttributeList ; 52 import javax.management.DynamicMBean ; 53 import javax.management.AttributeNotFoundException ; 54 import javax.management.MBeanException ; 55 import javax.management.ReflectionException ; 56 import javax.management.InvalidAttributeValueException ; 57 import javax.management.MBeanRegistration ; 58 import javax.management.MBeanInfo ; 59 import javax.management.MBeanAttributeInfo ; 60 import javax.management.MBeanConstructorInfo ; 61 import javax.management.MBeanNotificationInfo ; 62 import javax.management.MBeanOperationInfo ; 63 import javax.management.MBeanParameterInfo ; 64 65 import javax.management.MBeanRegistrationException ; 66 67 import com.sun.enterprise.admin.monitor.registry.StatsHolder; 68 69 import com.sun.enterprise.admin.common.constant.AdminConstants; 70 import com.sun.enterprise.util.i18n.StringManager; 71 import javax.management.j2ee.statistics.Statistic ; 72 import javax.management.j2ee.statistics.Stats ; 73 74 81 class StatsHolderMBeanImpl implements DynamicMBean , StatsHolderMBean, MBeanRegistration { 82 83 private static Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 84 private static StringManager sm = StringManager.getManager(StatsHolderMBeanImpl.class); 85 private final StatsHolder delegate; 86 private final StatsMediator mediator; 87 private DottedNameRegistrar registrar; 88 private MBeanInfo mi; 89 private int state; 90 private Object lock = new Object (); 91 public static final int INITIALIZED = 0; 92 public static final int REGISTERED = 1; 93 public static final int MBEANINFO_DONE = 2; 94 public static final String JTA_FREEZE = "freeze"; 95 public static final String JTA_UNFREEZE = "unfreeze"; 96 public static final String JTA_ROLLBACK = "rollback"; 97 public static final String JTA_ACTIVE_TRANSACTIONS = "listActiveTransactions"; 98 public static final String DOTTED_NAME = "dotted-name"; 99 100 101 StatsHolderMBeanImpl(StatsHolder delegate) { 102 assert (delegate != null); 103 this.delegate = delegate; 104 this.mediator = new StatsMediatorImpl(delegate.getStats(), delegate.getStatsClass()); 105 changeState(INITIALIZED); 106 } 107 108 public Object getAttribute(String name) throws AttributeNotFoundException , 109 MBeanException , ReflectionException { 110 if(name.equals(DOTTED_NAME)) 111 return delegate.getDottedName(); 112 else 113 return ( mediator.getAttribute(name) ); 114 } 115 116 public AttributeList getAttributes(String [] names) { 117 final AttributeList list = new AttributeList (); 118 for (int i = 0 ; i < names.length ; i++) { 119 try { 120 final Attribute a = new Attribute (names[i], this.getAttribute(names[i])); 121 list.add(a); 122 } 123 catch(Exception e) { 124 logger.finest("Error while accessing an attribute named: " + names[i]); 125 } 127 } 128 return ( list ); 129 } 130 131 public MBeanInfo getMBeanInfo() { 132 synchronized(lock) { 133 if (state == MBEANINFO_DONE) { 134 return mi; 135 } 136 } 137 build(); 138 changeState(MBEANINFO_DONE); 139 return ( mi ); 140 } 141 142 private void build() { 143 final String name = StatsHolderMBeanImpl.class.getName(); 144 final String desc = getDescription(); 145 final MBeanAttributeInfo [] mais = mediator.getAttributeInfos(); 146 final MBeanConstructorInfo [] mcis = this.getConstructorInfos(); 147 final MBeanOperationInfo [] mois = this.getOperationInfos(); 148 final MBeanNotificationInfo [] mnis = this.getNotificationInfos(); 149 mi = new MBeanInfo (name, desc, mais, mcis, mois, mnis); 150 } 151 152 private String getDescription() { 153 return "StatsHolder MBean for: " + StatsHolderMBeanImpl.class.getName(); 154 } 155 156 private MBeanConstructorInfo [] getConstructorInfos() { 157 final MBeanConstructorInfo [] cis = new MBeanConstructorInfo [0]; 158 return ( cis ); } 160 private MBeanOperationInfo [] getOperationInfos() { 161 162 final ArrayList opInfo = new ArrayList (); 163 164 opInfo.add(getChildrenInfo()); 165 opInfo.add(getNameInfo()); 166 opInfo.add(getTypeInfo()); 167 if(delegate.getStats() != null) { 170 opInfo.add(getStatisticNameInfo()); 171 opInfo.add(getStatsInfo()); 172 } 173 174 MBeanOperationInfo [] mos = new MBeanOperationInfo [opInfo.size()]; 175 mos = (MBeanOperationInfo [])opInfo.toArray(mos); 176 177 if(isJta()) 180 return (getJTAOperationInfo(mos)); 181 182 return ( mos ); 183 } 184 185 public Object invoke(String method, Object [] params, String [] sign) throws 186 MBeanException , ReflectionException { 187 if ("getChildren".equals(method)) { 188 return ( this.getChildren() ); 189 } else if ("getName".equals(method)) { 190 return (this.getName()); 191 } else if ("getType".equals(method)) { 192 return (this.getType()); 193 } else if ("getStatisticNames".equals(method)) { 194 return (this.getStatisticNames()); 195 } else if ("getStatistics".equals(method)) { 196 return (this.getStatistics()); 197 } else if(isJTAMethod(method)) { 198 return(mediator.invoke(method, params, sign)); 199 } else { 200 final String msg = sm.getString("smi.no_such_method", method); 201 final Exception ae = new UnsupportedOperationException (msg); 202 throw new MBeanException (ae); 203 } 204 } 205 206 public void setAttribute(Attribute attribute) throws AttributeNotFoundException , 207 InvalidAttributeValueException , MBeanException , ReflectionException { 208 throw new UnsupportedOperationException ("NYI"); 209 } 210 211 public AttributeList setAttributes(AttributeList attributes) { 212 throw new UnsupportedOperationException ("NYI"); 213 } 214 215 public ObjectName [] getChildren() { 216 final Collection c = delegate.getAllChildren(); 217 final ObjectName [] names = new ObjectName [c.size()]; 218 final Iterator it = c.iterator(); 219 int i = 0; 220 while (it.hasNext()) { 221 final StatsHolder s = (StatsHolder) it.next(); 222 names[i++] = s.getObjectName(); 223 } 224 assert (names.length == i) : "Sizes don't match"; 225 return ( names ); 226 } 227 228 private MBeanOperationInfo getChildrenInfo() { 229 final MBeanOperationInfo info = new MBeanOperationInfo ( 230 "getChildren", 231 "Gets the children of this StatsHolder", 232 null, 233 ObjectName [].class.getName(), 234 MBeanOperationInfo.INFO 235 ); 236 return ( info ); 237 } 238 239 private MBeanNotificationInfo [] getNotificationInfos() { 240 final MBeanNotificationInfo [] mns = new MBeanNotificationInfo [0]; 241 return ( mns ); 242 } 243 244 private boolean isJta() { 245 boolean isJta = false; 246 final Class cl = delegate.getStatsClass(); 247 if (cl != null) { 248 if (com.sun.enterprise.admin.monitor.stats.JTAStats.class.getName().equals(cl.getName())) { 249 isJta = true; 250 } 251 } 252 return ( isJta); 253 } 254 private boolean isJTAMethod(String methodName) { 255 return ((JTA_FREEZE.equals(methodName)) || 256 (JTA_UNFREEZE.equals(methodName)) || 257 (JTA_ACTIVE_TRANSACTIONS.equals(methodName)) || 258 (JTA_ROLLBACK.equals(methodName))); 259 } 260 261 public void postDeregister() { 263 } 264 265 public void postRegister(Boolean registered) { 266 if (registered.equals(Boolean.TRUE)) { 267 registrar.registerDottedName(delegate.getDottedName(), 268 delegate.getObjectName()); 269 changeState(REGISTERED); 270 } 271 } 272 273 public void preDeregister() throws Exception { 274 registrar.unregisterDottedName(delegate.getDottedName()); 275 } 276 277 public ObjectName preRegister(MBeanServer mBeanServer, ObjectName objectName) throws 278 Exception { 279 this.registrar = new DottedNameRegistrar(mBeanServer); 280 return objectName; 281 } 282 284 private void changeState(int to) { 285 synchronized(lock) { 286 state = to; 287 } 288 } 289 290 private MBeanOperationInfo [] getJTAOperationInfo(MBeanOperationInfo [] mos) { 291 ArrayList opInfo = new ArrayList (); 292 for(int i = 0; i < mos.length ; i++) 293 opInfo.add(mos[i]); 294 MBeanOperationInfo mInfo = new MBeanOperationInfo (JTA_FREEZE, 298 "Freezes the transaction service", 299 null, 300 void.class.getName(), 301 MBeanOperationInfo.ACTION); 302 303 opInfo.add(mInfo); 304 305 mInfo = new MBeanOperationInfo (JTA_UNFREEZE, 306 "Unfreezes the transaction service", 307 null, 308 void.class.getName(), 309 MBeanOperationInfo.ACTION); 310 311 opInfo.add(mInfo); 312 mInfo = new MBeanOperationInfo (JTA_ROLLBACK, 313 "Rollsback a given transaction", 314 new MBeanParameterInfo [] { 315 new MBeanParameterInfo ("txnId", 316 String .class.getName(), 317 "Id of the transaction to be rolled back" 318 )}, 319 void.class.getName(), 320 MBeanOperationInfo.ACTION); 321 opInfo.add(mInfo); 322 mInfo = new MBeanOperationInfo (JTA_ACTIVE_TRANSACTIONS, 323 "Gets Active Transactions in a Map", 324 null, 325 List .class.getName(), 326 MBeanOperationInfo.ACTION_INFO); 327 opInfo.add(mInfo); 328 MBeanOperationInfo [] jtaOpInfo = new MBeanOperationInfo [opInfo.size()]; 329 return (MBeanOperationInfo [])opInfo.toArray(jtaOpInfo); 330 331 } 332 333 public String getName() { 334 return delegate.getName(); 335 } 336 337 public String getType() { 338 return delegate.getType().getTypeName(); 339 } 340 341 private MBeanOperationInfo getNameInfo() { 342 MBeanOperationInfo mInfo = new MBeanOperationInfo ("getName", 343 "Gets the name of this StatsHolder", 344 null, 345 String .class.getName(), 346 MBeanOperationInfo.INFO); 347 return mInfo; 348 } 349 350 private MBeanOperationInfo getTypeInfo() { 351 MBeanOperationInfo mInfo = new MBeanOperationInfo ("getType", 352 "Gets the type of this StatsHolder", 353 null, 354 String .class.getName(), 355 MBeanOperationInfo.INFO); 356 return mInfo; 357 } 358 359 public String [] getStatisticNames() { 360 Stats stats = delegate.getStats(); 361 if (stats != null) { 362 return stats.getStatisticNames(); 363 } else { 364 return null; 365 } 366 } 367 368 public Statistic [] getStatistics() { 369 Stats stats = delegate.getStats(); 370 if (stats == null) { 371 return null; 372 } 373 374 Statistic [] statArray = stats.getStatistics(); 375 boolean isSerializable = checkSerializability(statArray); 376 if(isSerializable) { 377 final Statistic [] hackedArray = StatisticWorkaround.populateDescriptions(statArray); 378 return hackedArray; 379 } 380 else 381 return null; 382 } 383 384 private MBeanOperationInfo getStatisticNameInfo() { 385 MBeanOperationInfo mInfo = new MBeanOperationInfo ("getStatisticNames", 386 "Gets the names of all the statistics in the given Stats Object", 387 null, 388 String [].class.getName(), 389 MBeanOperationInfo.INFO); 390 return mInfo; 391 } 392 393 private MBeanOperationInfo getStatsInfo() { 394 MBeanOperationInfo mInfo = new MBeanOperationInfo ("getStatistics", 395 "returns the results of all the getXXX methods, in the given Stats object", 396 null, 397 Statistic [].class.getName(), 398 MBeanOperationInfo.INFO); 399 return mInfo; 400 } 401 402 private boolean checkSerializability(Object [] objArray) { 403 boolean isSerializable = true; 404 for(int i = 0; i < objArray.length; i++) { 405 isSerializable = (isSerializable) && (objArray[i] instanceof java.io.Serializable ); 406 } 407 return isSerializable; 408 } 409 410 } 411 | Popular Tags |