1 23 24 25 package com.sun.enterprise.admin.selfmanagement.event; 26 27 import java.util.Enumeration ; 29 import java.util.List ; 30 import java.util.Set ; 31 import java.util.ArrayList ; 32 import java.util.logging.LogManager ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 import java.util.Hashtable ; 36 37 import javax.management.ObjectName ; 39 import javax.management.MBeanServer ; 40 import javax.management.MBeanInfo ; 41 import javax.management.MBeanAttributeInfo ; 42 import javax.management.MBeanNotificationInfo ; 43 import javax.management.MalformedObjectNameException ; 44 import javax.management.InstanceNotFoundException ; 45 import javax.management.IntrospectionException ; 46 import javax.management.ReflectionException ; 47 48 49 import com.sun.logging.LogDomains; 51 import com.sun.enterprise.config.ConfigException; 52 import com.sun.enterprise.config.ConfigContext; 53 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 54 import com.sun.enterprise.config.serverbeans.Mbean; 55 import com.sun.enterprise.config.serverbeans.Server; 56 import com.sun.enterprise.config.serverbeans.ServerHelper; 57 import com.sun.enterprise.admin.common.MBeanServerFactory; 58 import com.sun.enterprise.admin.server.core.AdminService; 59 import com.sun.enterprise.admin.selfmanagement.event.ManagementRuleConstants; 60 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration; 61 import com.sun.enterprise.management.selfmanagement.RuleManager; 62 import static com.sun.enterprise.admin.selfmanagement.event.ManagementRuleConstants.*; 63 64 import com.sun.enterprise.server.ApplicationServer; 65 import com.sun.enterprise.admin.common.ObjectNames; 66 import com.sun.enterprise.config.serverbeans.Domain; 67 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 68 import com.sun.enterprise.config.serverbeans.ApplicationRef; 69 import com.sun.enterprise.config.serverbeans.Applications; 70 import com.sun.enterprise.config.serverbeans.Clusters; 71 import com.sun.enterprise.config.serverbeans.Cluster; 72 73 public class ManagementRulesMBeanHelper { 74 75 private static Logger _logger = 76 LogDomains.getLogger(LogDomains.SELF_MANAGEMENT_LOGGER); 77 78 private static String instanceName = (ApplicationServer.getServerContext()).getInstanceName(); 79 private static ConfigContext configCtx = (ApplicationServer.getServerContext()).getConfigContext(); 80 81 private static ConfigContext getConfigContext() { 82 return AdminService.getAdminService().getAdminContext().getAdminConfigContext(); 83 } 84 85 92 public static List <String > getAllActionMBeans(boolean enabled) throws ConfigException { 93 List <String > actionMBeanList = new ArrayList <String >(); 94 List <Mbean> mbeanList = ServerBeansFactory.getAllMBeanDefinitions(getConfigContext()); 95 for (Mbean mbean : mbeanList) { 96 if (enabled && !mbean.isEnabled()) 97 continue; 98 String implClassName = mbean.getImplClassName(); 99 if (implementsInterface(implClassName, "javax.management.NotificationListener")) 100 actionMBeanList.add(mbean.getName()); 101 } 102 return actionMBeanList; 103 } 104 105 private static boolean implementsInterface(String implClassName, String interfaceName) { 106 Class ifList[] = null; 107 Class class1 = null; 108 try { 109 ClassLoader mbeanClLoader = getMBeanClassLoader(); 110 if (mbeanClLoader != null) 111 class1 = Class.forName(implClassName,false,mbeanClLoader); 112 else 113 class1 = Class.forName(implClassName); 114 while (class1 != null) { 115 ifList = class1.getInterfaces(); 116 if(ifList != null) { 117 for (int i=0; i<ifList.length; i++) { 118 String canonicalName = ifList[i].getCanonicalName().trim(); 119 if( canonicalName.equals(interfaceName) ) { 120 return true; 121 } 122 } 123 class1 = class1.getSuperclass(); 124 } 125 } 126 } catch (Exception ex) { 127 _logger.log(Level.FINE, " An unexpcted exception occurred " , ex); 128 } 129 return false; 130 } 131 132 private static ClassLoader getMBeanClassLoader() { 133 try { 134 return (ClassLoader ) Class.forName( 135 "com.sun.enterprise.admin.mbeans.custom.loading.MBeanClassLoader"). 136 newInstance(); 137 } catch (Exception ex) { 138 _logger.log(Level.FINE, " An unexpcted exception occurred " , ex); 139 } 140 return null; 141 142 } 143 144 150 public static List <String > getEventTypes(boolean isEE) { 151 List <String > eventTypeList = new ArrayList <String >(); 152 eventTypeList.add(EVENT_LIFECYCLE); 153 eventTypeList.add(EVENT_LOG); 154 eventTypeList.add(EVENT_TIMER); 155 eventTypeList.add(EVENT_TRACE); 156 eventTypeList.add(EVENT_MONITOR); 157 eventTypeList.add(EVENT_NOTIFICATION); 158 if (isEE) { 159 eventTypeList.add(EVENT_CLUSTER); 160 } 161 return eventTypeList; 162 } 163 164 170 public static List <String > getEventProperties(String eventType) { 171 if (eventType == null || "".equals(eventType)) 172 return new ArrayList <String >(0); 173 List <String > valueList = null; 174 if (eventType.equals(EVENT_LIFECYCLE)) { 175 valueList = new ArrayList <String >(1); 176 valueList.add(PROPERTY_LIFECYCLE_NAME); 177 return valueList; 178 } 179 if (eventType.equals(EVENT_MONITOR)) { 180 valueList = new ArrayList <String >(); 181 valueList.add(PROPERTY_MONITOR_OBSERVED_OBJ); 182 valueList.add(PROPERTY_MONITOR_OBSERVED_OBJ_MBEAN_NAME); 183 valueList.add(PROPERTY_MONITOR_OBSERVED_ATTRIBUTE); 184 valueList.add(PROPERTY_MONITOR_GRANULARITY_PERIOD); 185 valueList.add(PROPERTY_MONITOR_NUMBERTYPE); 186 valueList.add(PROPERTY_MONITOR_DIFFERENCEMODE); 187 valueList.add(PROPERTY_MONITOR_INIT_THRESHOLD); 188 valueList.add(PROPERTY_MONITOR_OFFSET); 189 valueList.add(PROPERTY_MONITOR_MODULUS); 190 valueList.add(PROPERTY_MONITOR_LOW_THRESHOLD); 191 valueList.add(PROPERTY_MONITOR_HIGH_THRESHOLD); 192 valueList.add(PROPERTY_MONITOR_STRING_TO_COMPARE); 193 valueList.add(PROPERTY_MONITOR_STRING_NOTIFY); 194 return valueList; 195 } 196 if (eventType.equals(EVENT_TRACE)) { 197 valueList = new ArrayList <String >(1); 198 valueList.add(PROPERTY_TRACE_NAME); 199 return valueList; 200 } 201 if (eventType.equals(EVENT_LOG)) { 202 valueList = new ArrayList <String >(2); 203 valueList.add(PROPERTY_LOG_LOGGERNAME); 204 valueList.add(PROPERTY_LOG_LEVEL); 205 return valueList; 206 } 207 if (eventType.equals(EVENT_TIMER)) { 208 valueList = new ArrayList <String >(5); 209 valueList.add(PROPERTY_TIMER_PATTERN); 210 valueList.add(PROPERTY_TIMER_DATESTRING); 211 valueList.add(PROPERTY_TIMER_PERIOD); 212 valueList.add(PROPERTY_TIMER_NUMBER_OF_OCCURRENCES); 213 valueList.add(PROPERTY_TIMER_MESSAGE); 214 return valueList; 215 } 216 if (eventType.equals(EVENT_NOTIFICATION)) { 217 valueList = new ArrayList <String >(1); 218 valueList.add(PROPERTY_NOTIFICATION_SOURCEMBEAN); 219 valueList.add(PROPERTY_NOTIFICATION_SOURCE_OBJ_NAME); 220 return valueList; 221 } 222 if (eventType.equals(EVENT_CLUSTER)) { 223 valueList = new ArrayList <String >(2); 224 valueList.add(PROPERTY_CLUSTER_NAME); 225 valueList.add(PROPERTY_CLUSTER_SERVERNAME); 226 return valueList; 227 } 228 return new ArrayList <String >(0); 229 } 230 231 232 public static List <String > getEventPropertyValues(String eventType, String propertyName) 233 throws ConfigException { 234 if (eventType == null || "".equals(eventType)) 235 return new ArrayList <String >(0); 236 List <String > valueList = null; 237 if (eventType.equals(EVENT_LIFECYCLE)) { 238 if (PROPERTY_LIFECYCLE_NAME.equals(propertyName)) { 239 valueList = new ArrayList <String >(3); 240 valueList.add("ready"); 241 valueList.add("shutdown"); 242 valueList.add("termination"); 243 return valueList; 244 } 245 } 246 if (eventType.equals(EVENT_MONITOR)) { 247 if (PROPERTY_MONITOR_OBSERVED_OBJ.equals(propertyName)) { 248 return new ArrayList <String >(0); 249 } 250 if (PROPERTY_MONITOR_OBSERVED_OBJ_MBEAN_NAME.equals(propertyName)) { 251 return new ArrayList <String >(0); 252 } 253 if (PROPERTY_MONITOR_OBSERVED_ATTRIBUTE.equals(propertyName)) { 254 return new ArrayList <String >(0); 255 } 256 if (PROPERTY_MONITOR_GRANULARITY_PERIOD.equals(propertyName)) { 257 return new ArrayList <String >(0); 258 } 259 if (PROPERTY_MONITOR_TYPE.equals(propertyName)) { 260 valueList = new ArrayList <String >(3); 261 valueList.add(PROPERTY_MONITOR_COUNTER); 262 valueList.add(PROPERTY_MONITOR_GAUGE); 263 valueList.add(PROPERTY_MONITOR_STRING); 264 return valueList; 265 } 266 if (PROPERTY_MONITOR_NUMBERTYPE.equals(propertyName)) { 267 valueList = new ArrayList <String >(6); 268 valueList.add("long"); 269 valueList.add("int"); 270 valueList.add("short"); 271 valueList.add("double"); 272 valueList.add("float"); 273 valueList.add("byte"); 274 return valueList; 275 } 276 if (PROPERTY_MONITOR_DIFFERENCEMODE.equals(propertyName)) { 277 valueList = new ArrayList <String >(2); 278 valueList.add("true"); 279 valueList.add("false"); 280 return valueList; 281 } 282 if (PROPERTY_MONITOR_INIT_THRESHOLD.equals(propertyName)) { 283 return new ArrayList <String >(0); 284 } 285 if (PROPERTY_MONITOR_OFFSET.equals(propertyName)) { 286 return new ArrayList <String >(0); 287 } 288 if (PROPERTY_MONITOR_MODULUS.equals(propertyName)) { 289 return new ArrayList <String >(0); 290 } 291 if (PROPERTY_MONITOR_LOW_THRESHOLD.equals(propertyName)) { 292 return new ArrayList <String >(0); 293 } 294 if (PROPERTY_MONITOR_HIGH_THRESHOLD.equals(propertyName)) { 295 return new ArrayList <String >(0); 296 } 297 if (PROPERTY_MONITOR_STRING_TO_COMPARE.equals(propertyName)) { 298 return new ArrayList <String >(0); 299 } 300 if (PROPERTY_MONITOR_STRING_NOTIFY.equals(propertyName)) { 301 valueList = new ArrayList <String >(2); 302 valueList.add(PROPERTY_MONITOR_STRING_NOTIFY_MATCH); 303 valueList.add(PROPERTY_MONITOR_STRING_NOTIFY_DIFFER); 304 return valueList; 305 } 306 } 307 if (EVENT_TRACE.equals(eventType)) { 308 if (PROPERTY_TRACE_NAME.equals(propertyName)) { 309 valueList = new ArrayList <String >(6); 310 valueList.add("web_component_method_entry"); 311 valueList.add("web_component_method_exit"); 312 valueList.add("request_start"); 313 valueList.add("request_end"); 314 valueList.add("ejb_component_method_entry"); 315 valueList.add("ejb_component_method_exit"); 316 return valueList; 317 } 318 } 319 if (EVENT_LOG.equals(eventType)) { 320 if (PROPERTY_LOG_LOGGERNAME.equals(propertyName)) { 321 valueList = new ArrayList <String >(); 322 Enumeration <String > loggerNames = LogManager.getLogManager().getLoggerNames(); 323 while (loggerNames.hasMoreElements()) { 324 valueList.add(loggerNames.nextElement()); 325 } 326 return valueList; 327 } 328 if (PROPERTY_LOG_LEVEL.equals(propertyName)) { 329 valueList = new ArrayList <String >(); 330 valueList.add(Level.ALL.getName()); 331 valueList.add(Level.CONFIG.getName()); 332 valueList.add(Level.FINE.getName()); 333 valueList.add(Level.FINER.getName()); 334 valueList.add(Level.FINEST.getName()); 335 valueList.add(Level.INFO.getName()); 336 valueList.add(Level.SEVERE.getName()); 337 valueList.add(Level.WARNING.getName()); 338 return valueList; 339 } 340 } 341 if (EVENT_TIMER.equals(eventType)) { 342 if (PROPERTY_TIMER_PATTERN.equals(propertyName) || 343 PROPERTY_TIMER_DATESTRING.equals(propertyName) || 344 PROPERTY_TIMER_PERIOD.equals(propertyName) || 345 PROPERTY_TIMER_NUMBER_OF_OCCURRENCES.equals(propertyName) || 346 PROPERTY_TIMER_MESSAGE.equals(propertyName) ) { 347 return new ArrayList <String >(0); 348 } 349 } 350 if (EVENT_NOTIFICATION.equals(eventType)) { 351 if (PROPERTY_NOTIFICATION_SOURCEMBEAN.equals(propertyName)) { 352 return new ArrayList <String >(0); 354 } 355 if (PROPERTY_NOTIFICATION_SOURCE_OBJ_NAME.equals(propertyName)) { 356 return new ArrayList <String >(0); 357 } 358 } 359 if (EVENT_CLUSTER.equals(eventType)) { 360 if (PROPERTY_CLUSTER_NAME.equals(propertyName)) { 361 valueList = new ArrayList <String >(3); 362 valueList.add("start"); 363 valueList.add("stop"); 364 valueList.add("fail"); 365 return valueList; 366 } 367 if (PROPERTY_CLUSTER_SERVERNAME.equals(propertyName)) { 368 valueList = new ArrayList <String >(); 369 Server servers[] = ServerHelper.getServersInDomain(getConfigContext()); 370 for (Server server : servers) { 371 valueList.add(server.getName()); 372 } 373 return valueList; 374 } 375 } 376 return new ArrayList <String >(0); 377 } 378 379 public static List <String > getAllNotificationEmitterMbeans(boolean enabled) throws ConfigException { 380 List <String > customMBeanList = new ArrayList <String >(); 381 List <Mbean> mbeanList = ServerBeansFactory.getAllMBeanDefinitions(getConfigContext()); 382 for (Mbean mbean : mbeanList) { 383 if (enabled && !mbean.isEnabled()) 384 continue; 385 String implClassName = mbean.getImplClassName(); 386 if (implementsInterface(implClassName, "javax.management.NotificationEmitter")) 387 customMBeanList.add(mbean.getName()); 388 } 389 return customMBeanList; 390 } 391 392 393 400 public static Set <ObjectName > getRegisteredMBeans(String filter) throws MalformedObjectNameException { 401 ObjectName userFilteredObjectName = null; 402 MBeanServer mbeanServer = MBeanServerFactory.getMBeanServer(); 403 if (filter != null) { 404 userFilteredObjectName = new ObjectName (filter); 405 return mbeanServer.queryNames(userFilteredObjectName, null); 406 } 407 String monitoringObjectNameFilter = "com.sun.appserv:category=monitor,*"; 408 String jsr77ObjectNameFilter = "com.sun.appserv:category=runtime,*"; 409 ObjectName monitoringFilteredObjectName = null; 410 ObjectName jsr77FilteredObjectName = null; 411 try { 412 monitoringFilteredObjectName = new ObjectName (monitoringObjectNameFilter); 413 jsr77FilteredObjectName = new ObjectName (jsr77ObjectNameFilter); 414 } catch (Exception ex) { 415 _logger.log(Level.FINE, " An unexpcted exception occurred " , ex); 416 } 417 Set <ObjectName > setObjNames = mbeanServer.queryNames(monitoringFilteredObjectName, null); 418 setObjNames.addAll(mbeanServer.queryNames(jsr77FilteredObjectName,null)); 419 return setObjNames; 420 } 421 422 429 public static List <String > getAttributes(ObjectName objName) throws InstanceNotFoundException , IntrospectionException , 430 ReflectionException { 431 if (objName == null) 432 return new ArrayList <String >(0); 433 MBeanInfo mbInfo = MBeanServerFactory.getMBeanServer().getMBeanInfo(objName); 434 MBeanAttributeInfo attrInfoArr[] = mbInfo.getAttributes(); 435 List <String > attributes = new ArrayList <String >(); 436 for (MBeanAttributeInfo attrInfo : attrInfoArr) { 437 attributes.add(attrInfo.getName()); 438 } 439 return attributes; 440 } 441 442 public static List <String > getMBeanAttributes(String objectNameStr) throws MalformedObjectNameException , 443 InstanceNotFoundException , IntrospectionException , 444 ReflectionException { 445 ObjectName objName = new ObjectName (objectNameStr); 446 return getAttributes(objName); 447 } 448 449 public static List <String > getNotificationTypes(ObjectName objName) throws InstanceNotFoundException , IntrospectionException , 450 ReflectionException { 451 if (objName == null) 452 return new ArrayList <String >(0); 453 MBeanInfo mbInfo = MBeanServerFactory.getMBeanServer().getMBeanInfo(objName); 454 MBeanNotificationInfo notifInfoArr[] = mbInfo.getNotifications(); 455 List <String > notifications = new ArrayList <String >(); 456 for (MBeanNotificationInfo notifInfo : notifInfoArr) { 457 String notifTypes[] = notifInfo.getNotifTypes(); 458 for (String notifType : notifTypes) 459 notifications.add(notifType); 460 } 461 return notifications; 462 } 463 464 public static List <String > getNotificationTypes(String objectNameStr) throws MalformedObjectNameException , 465 InstanceNotFoundException , IntrospectionException , 466 ReflectionException { 467 ObjectName objName = new ObjectName (objectNameStr); 468 return getNotificationTypes(objName); 469 } 470 471 472 473 public static List <String > getAttributes(String dottedName) { return null; } 474 475 public static List <String > getDottedNames(String dottedName) { return null; } 476 477 public static String getObjName(String mBeanName) { 478 String objName = null; 479 String cascadedObjName = null; 480 if (mBeanName != null) { 481 try { 482 Domain domain = ServerBeansFactory.getDomainBean(configCtx); 483 ApplicationRef appRef = verifyMBean(mBeanName, domain); 484 if (appRef != null) { 485 Applications apps = domain.getApplications(); 486 Mbean definedMBean = apps.getMbeanByName(mBeanName); 487 if(definedMBean != null) { 488 objName = definedMBean.getObjectName(); 489 cascadedObjName = getCascadingAwareObjectName(new ObjectName (objName)).toString(); 490 } 491 } 492 } catch(ConfigException ex) { 493 _logger.log(Level.INFO, "An unexpected exception occured.", ex); 494 } catch (Exception ex) { 495 _logger.log(Level.INFO, "An unexpected exception occured.", ex); 496 } 497 } 498 502 return cascadedObjName; 503 } 504 505 public static ObjectName getCascadingAwareObjectName(final ObjectName configON) throws RuntimeException { 506 try { 507 final String serverNameKey = "server"; 508 final Hashtable properties = configON.getKeyPropertyList(); 510 properties.put(serverNameKey, instanceName); 511 final ObjectName ron = new ObjectName (configON.getDomain(), properties); 512 return ( ron ); 513 } catch(final Exception e) { 514 throw new RuntimeException (e); 515 } 516 } 517 518 535 536 private static ApplicationRef verifyMBean(String mBeanName, Domain domain) { 537 ApplicationRef appRef = null; 538 539 try { 540 Server instanceBean = ServerBeansFactory.getServerBean(configCtx); 541 appRef = instanceBean.getApplicationRefByRef(mBeanName); 542 543 if (appRef == null) { 544 Clusters clusters = domain.getClusters(); 546 Cluster[] cluster = clusters.getCluster(); 547 for (Cluster val : cluster) { 548 if ((val.getServerRefByRef(instanceName)) != null) { 549 appRef = val.getApplicationRefByRef(mBeanName); 550 break; 551 } 552 } 553 } 554 555 if (appRef != null) { 557 Applications apps = domain.getApplications(); 559 Mbean definedMBean = apps.getMbeanByName(mBeanName); 560 if (definedMBean == null ) { 561 appRef = null; 562 } 563 } 564 } catch (ConfigException ex) { 565 _logger.log(Level.INFO, "smgt.config_error", ex); 566 } 567 568 return appRef; 569 } 570 571 } 572 573 | Popular Tags |