1 22 package org.jboss.console.plugins.helpers; 23 24 import org.jboss.console.manager.PluginManager; 25 import org.jboss.console.manager.interfaces.ConsolePlugin; 26 import org.jboss.console.manager.interfaces.ManageableResource; 27 import org.jboss.console.manager.interfaces.ResourceTreeNode; 28 import org.jboss.console.manager.interfaces.TreeAction; 29 import org.jboss.console.manager.interfaces.TreeNode; 30 import org.jboss.console.manager.interfaces.TreeNodeMenuEntry; 31 import org.jboss.console.manager.interfaces.impl.HttpLinkTreeAction; 32 import org.jboss.console.manager.interfaces.impl.MBeanResource; 33 import org.jboss.console.manager.interfaces.impl.SeparatorTreeNodeMenuEntry; 34 import org.jboss.console.manager.interfaces.impl.SimpleFolderResource; 35 import org.jboss.console.manager.interfaces.impl.SimpleResourceTreeNode; 36 import org.jboss.console.manager.interfaces.impl.SimpleTreeNode; 37 import org.jboss.console.manager.interfaces.impl.SimpleTreeNodeMenuEntryImpl; 38 import org.jboss.logging.Logger; 39 import org.jboss.mx.util.MBeanServerLocator; 40 import org.jboss.system.Registry; 41 42 import javax.management.MBeanServer ; 43 import javax.management.MalformedObjectNameException ; 44 import javax.management.ObjectInstance ; 45 import javax.management.ObjectName ; 46 import javax.management.Query ; 47 import javax.management.QueryExp ; 48 import javax.servlet.ServletConfig ; 49 import java.util.HashMap ; 50 import java.util.Set ; 51 52 67 public abstract class AbstractPluginWrapper 68 implements PluginWrapper, ConsolePlugin 69 { 70 72 public static final String OBJECT_NAME_PARAM = "ObjectName"; 73 public static final String FOLDER_NAME_PARAM = "FolderName"; 74 public static final String MBEAN_CLASS_PARAM = "MBeanClass"; 75 public static final String WRAPPER_CLASS_PARAM = "WrapperClass"; 76 public static final String SCRIPT_NAME_PARAM = "ScriptName"; 77 public static final String IS_ROOT_NODE_PARAM = "IsRootNode"; 78 79 81 protected MBeanServer mbeanServer = null; 82 protected PluginManager pm = null; 83 84 protected String pluginName = null; 85 protected String pluginVersion = null; 86 87 protected String objectName = null; 88 protected String mbeanClass = null; 89 protected String folderName = null; 90 91 protected String rootContextName = null; 92 93 protected Logger log = org.jboss.logging.Logger.getLogger(this.getClass()); 94 95 protected InternalResourceChecker checker = null; 96 97 99 101 public AbstractPluginWrapper () {} 102 103 105 107 109 public void init (ServletConfig servletConfig) throws Exception 110 { 111 findJBossMBeanServer (); 112 findPluginManager (); 113 readConfigurationParameters (servletConfig); 114 115 this.pm.registerPlugin(this); 116 } 117 118 public void destroy () 119 { 120 if( pm != null ) 121 pm.unregisterPlugin(this); 122 } 123 124 125 public void readConfigurationParameters (ServletConfig config) 126 { 127 this.pluginName = config.getInitParameter("PluginName"); 128 this.pluginVersion = config.getInitParameter("PluginVersion"); 129 130 this.folderName = config.getInitParameter(FOLDER_NAME_PARAM); 131 this.objectName = config.getInitParameter(OBJECT_NAME_PARAM); 132 this.mbeanClass = config.getInitParameter(MBEAN_CLASS_PARAM); 133 this.rootContextName = config.getInitParameter("ContextName"); 134 135 String tmp = this.objectName; 136 if (tmp != null && !"".equals(tmp)) 137 { 138 checker = new SingleMBeanChecker (); 142 } 143 144 tmp = this.folderName; 145 if (tmp != null && !"".equals(tmp)) 146 { 147 checker = new SubFolderChecker (); 150 } 151 152 tmp = config.getInitParameter(IS_ROOT_NODE_PARAM); 153 if (tmp != null && !"".equals(tmp) && "true".equalsIgnoreCase(tmp)) 154 { 155 checker = new RootTreeChecker (); 158 } 159 160 tmp = this.mbeanClass; 161 if (tmp != null && !"".equals(tmp)) 162 { 163 checker = new StandardMBeanChecker (); 167 } 168 169 } 170 171 173 public String getIdentifier() 174 { 175 if (this.pluginName != null) 176 { 177 return this.pluginName + " (Wrapped by ServletPluginHelper)"; 178 } 179 else 180 { 181 return getPluginIdentifier(); 182 } 183 } 184 185 public String getVersion() 186 { 187 if (this.pluginVersion != null) 188 { 189 return this.pluginVersion; 190 } 191 else 192 { 193 return getPluginVersion (); 194 } 195 } 196 197 public String [] getSupportedProfiles() 198 { 199 return new String [] {ConsolePlugin.WEB_PROFILE}; 200 } 201 202 public TreeNode getSubTreeForResource( 203 PluginManager master, 204 String profile, 205 ManageableResource resource) 206 { 207 if (!ConsolePlugin.WEB_PROFILE.equalsIgnoreCase(profile)) 208 { 209 return null; 210 } 211 else 212 { 213 if (isResourceToBeManaged (resource)) 214 { 215 return getTreeForResource( 216 profile, 217 resource); 218 } 219 else 220 { 221 return null; 222 } 223 } 224 } 225 226 227 229 protected boolean isResourceToBeManaged (ManageableResource resource) 230 { 231 if (checker == null) 232 { 233 return false; 234 } 235 else 236 { 237 return checker.isResourceToBeManaged(resource); 238 } 239 } 240 241 protected abstract TreeNode getTreeForResource( 242 String profile, 243 ManageableResource resource); 244 245 protected String getPluginIdentifier() 246 { 247 return "AbstractPluginWrapper (" + this.getClass() + ")"; 248 } 249 250 protected String getPluginVersion() 251 { 252 return "unknown version"; 253 } 254 255 257 259 260 protected void findJBossMBeanServer() 261 { 262 this.mbeanServer = MBeanServerLocator.locateJBoss(); 263 } 264 265 protected void findPluginManager () 266 { 267 this.pm = (PluginManager) Registry.lookup (PluginManager.PLUGIN_MANAGER_NAME); 268 } 269 270 protected MBeanServer getMBeanServer () 271 { 272 return this.mbeanServer; 273 } 274 275 protected String fixUrl (String source) 276 { 277 if (source == null) 278 { 279 return null; 280 } 281 else if (source.toLowerCase().startsWith("http://") || 282 source.toLowerCase().startsWith("https://")) 283 { 284 return source; 285 } 286 else if (source.startsWith("/")) 287 { 288 return source; } 290 else 291 { 292 return this.rootContextName + "/" + source; 293 } 294 } 295 296 protected ObjectInstance [] getMBeansForClass(String scope, String className) 297 { 298 try 299 { 300 Set result = mbeanServer.queryMBeans(new ObjectName (scope), 301 Query.eq (Query.classattr(), Query.value(className))); 302 303 return (ObjectInstance [])result.toArray(new ObjectInstance [result.size()]); 304 } 305 catch (MalformedObjectNameException e) 306 { 307 log.debug (e); 308 return new ObjectInstance [0]; 309 } 310 311 } 312 313 protected ObjectInstance [] getMBeansForQuery(String scope, QueryExp query) 314 { 315 try 316 { 317 Set result = mbeanServer.queryMBeans(new ObjectName (scope), query); 318 return (ObjectInstance [])result.toArray(new ObjectInstance [result.size()]); 319 } 320 catch (MalformedObjectNameException e) 321 { 322 log.debug (e); 323 return new ObjectInstance [0]; 324 } 325 326 } 327 328 protected SimpleTreeNode createTreeNode (String name, 329 String description, 330 String iconUrl, 331 String defaultUrl, 332 TreeNodeMenuEntry[] menuEntries, 333 TreeNode[] subNodes, 334 ResourceTreeNode[] subResNodes) throws Exception 335 { 336 TreeAction action = new HttpLinkTreeAction (fixUrl(defaultUrl)); 337 return new SimpleTreeNode (name, description, fixUrl(iconUrl), action, menuEntries, subNodes, subResNodes); 338 } 339 340 protected SimpleResourceTreeNode createResourceNode (String name, 341 String description, 342 String iconUrl, 343 String defaultUrl, 344 TreeNodeMenuEntry[] menuEntries, 345 TreeNode[] subNodes, 346 ResourceTreeNode[] subResNodes, 347 String jmxObjectName, 348 String jmxClassName) throws Exception 349 { 350 TreeAction action = new HttpLinkTreeAction (fixUrl(defaultUrl)); 351 ManageableResource res = new MBeanResource (new ObjectName (jmxObjectName), jmxClassName); 352 return new SimpleResourceTreeNode (name, description, fixUrl(iconUrl), action, menuEntries, subNodes, subResNodes, res); 353 } 354 355 protected SimpleResourceTreeNode createResourceNode (String name, 356 String description, 357 String iconUrl, 358 String defaultUrl, 359 TreeNodeMenuEntry[] menuEntries, 360 TreeNode[] subNodes, 361 ResourceTreeNode[] subResNodes, 362 ManageableResource resource) throws Exception 363 { 364 TreeAction action = new HttpLinkTreeAction (fixUrl(defaultUrl)); 365 return new SimpleResourceTreeNode (name, description, fixUrl(iconUrl), action, menuEntries, subNodes, subResNodes, resource); 366 } 367 368 protected TreeNodeMenuEntry[] createMenus (String [] content) throws Exception 369 { 370 371 TreeNodeMenuEntry[] menuEntries = null; 372 373 if (content != null && content.length > 0) 374 { 375 menuEntries = new TreeNodeMenuEntry[content.length]; 376 int i=0; 377 while (i< content.length) 378 { 379 if (content[i] == null) 380 { 381 menuEntries[i] = new SeparatorTreeNodeMenuEntry(); 382 i++; 383 } 384 else 385 { 386 String text = content[i]; 387 TreeAction action = new HttpLinkTreeAction(fixUrl(content[i+1])); 388 menuEntries[i] = new SimpleTreeNodeMenuEntryImpl ( text, action ); 389 i+=2; 390 } 391 } 392 } 393 else 394 { 395 menuEntries = new TreeNodeMenuEntry[0]; 396 } 397 return menuEntries; 398 } 399 400 protected String encode (String source) 401 { 402 try 403 { 404 return java.net.URLEncoder.encode(source); 405 } 406 catch (Exception e) 407 { 408 return source; 409 } 410 } 411 412 414 416 public interface InternalResourceChecker 417 { 418 boolean isResourceToBeManaged (ManageableResource resource); 419 } 420 421 public class StandardMBeanChecker 422 implements InternalResourceChecker 423 { 424 425 protected Class targetClass = null; 426 public HashMap knownAnswers = new HashMap (); 427 428 public StandardMBeanChecker () 429 { 430 try 431 { 432 targetClass = Thread.currentThread().getContextClassLoader().loadClass(mbeanClass); 433 } 434 catch (Exception displayed) 435 { 436 displayed.printStackTrace(); 437 } 438 } 439 440 public boolean isResourceToBeManaged (ManageableResource resource) 441 { 442 if (resource instanceof MBeanResource) 443 { 444 MBeanResource mbr = (MBeanResource)resource; 445 446 Boolean result = (Boolean )knownAnswers.get(mbr.getClassName ()); 447 if (result == null) 448 { 449 try 452 { 453 Class resourceClass = Thread.currentThread().getContextClassLoader().loadClass(mbr.getClassName ()); 455 result = new Boolean (targetClass.isAssignableFrom(resourceClass)); 456 } 458 catch (Exception e) 459 { 460 result = Boolean.FALSE; 461 } 462 knownAnswers.put(mbr.getClassName(), result); 463 464 } 465 return result.booleanValue(); 466 } 467 else 468 return false; 469 } 470 } 471 472 473 public class RootTreeChecker 474 implements InternalResourceChecker 475 { 476 477 public boolean isResourceToBeManaged (ManageableResource resource) 478 { 479 if (resource == null) 480 return false; 481 else 482 return resource.equals (pm.getBootstrapResource ()); 483 } 484 } 485 486 public class SingleMBeanChecker 487 implements InternalResourceChecker 488 { 489 490 public boolean isResourceToBeManaged (ManageableResource resource) 491 { 492 if (objectName != null && resource instanceof MBeanResource) 493 { 494 MBeanResource mbr = (MBeanResource)resource; 495 return objectName.equals(mbr.getObjectName().toString()); 496 497 } 498 else 499 return false; 500 } 501 502 } 503 504 public class SubFolderChecker 505 implements InternalResourceChecker 506 { 507 public boolean isResourceToBeManaged (ManageableResource resource) 508 { 509 if (resource == null || !(resource instanceof SimpleFolderResource)) 510 { 511 return false; 512 } 513 else 514 { 515 return folderName.equals(resource.getId()); 516 } 517 } 518 } 519 520 } 521 | Popular Tags |