1 22 package org.jboss.console.manager; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.Notification ; 31 import javax.management.NotificationFilter ; 32 import javax.management.NotificationListener ; 33 import javax.management.ObjectName ; 34 import javax.naming.InitialContext ; 35 36 import org.jboss.console.manager.interfaces.ConsolePlugin; 37 import org.jboss.console.manager.interfaces.ManageableResource; 38 import org.jboss.console.manager.interfaces.ResourceTreeNode; 39 import org.jboss.console.manager.interfaces.TreeInfo; 40 import org.jboss.console.manager.interfaces.TreeNode; 41 import org.jboss.console.manager.interfaces.TreeNodeMenuEntry; 42 import org.jboss.console.manager.interfaces.impl.DefaultTreeInfo; 43 import org.jboss.console.manager.interfaces.impl.HttpLinkTreeAction; 44 import org.jboss.console.manager.interfaces.impl.MBeanAction; 45 import org.jboss.console.manager.interfaces.impl.MBeanResource; 46 import org.jboss.console.manager.interfaces.impl.SeparatorTreeNodeMenuEntry; 47 import org.jboss.console.manager.interfaces.impl.SimpleTreeNodeMenuEntryImpl; 48 import org.jboss.console.navtree.RefreshTreeAction; 49 import org.jboss.jmx.adaptor.rmi.RMIRemoteMBeanProxy; 50 import org.jboss.system.Registry; 51 import org.jboss.system.ServiceMBeanSupport; 52 53 56 public class PluginManager 57 extends ServiceMBeanSupport 58 implements PluginManagerMBean, NotificationListener 59 { 60 61 63 public static String PLUGIN_MANAGER_NAME = null; 64 65 67 protected ArrayList plugins = new ArrayList (); 68 protected ManageableResource bootstrapResource = null; 69 70 public String jndiName = "console/PluginManager"; 71 72 protected long treeVersion = 0; 73 protected HashMap currentTrees = new HashMap (); 74 75 protected String mainLogoUrl = "/web-console/images/jboss.gif"; 76 protected String mainLinkUrl = "http://www.jboss.org/forums/"; 77 protected boolean enableShutdown = true; 78 79 81 83 public PluginManager () 84 { 85 } 86 87 89 public void createService () throws Exception 90 { 91 this.bootstrapResource = new MBeanResource (this.getServiceName (), this.getClass ().toString ()); 92 } 93 94 public void startService () throws Exception 95 { 96 bindProxyInJndi (); 97 PLUGIN_MANAGER_NAME = this.getServiceName().toString(); 98 Registry.bind(PLUGIN_MANAGER_NAME, this); 99 100 initNotificationReception (); 101 } 102 103 public void stopService () 104 { 105 Registry.unbind(this.getServiceName().toString()); 106 } 107 108 112 public void registerPlugin (String consolePluginClassName) throws Exception 113 { 114 Class pluginClass = Thread.currentThread ().getContextClassLoader (). 115 loadClass (consolePluginClassName); 116 ConsolePlugin plugin = (ConsolePlugin)pluginClass.newInstance (); 117 this.registerPlugin (plugin); 118 } 119 120 124 public synchronized void registerPlugin (ConsolePlugin plugin) 125 { 126 plugins.add (plugin); 127 regenerateAdminTree(); 128 } 129 130 134 public synchronized void unregisterPlugin (ConsolePlugin plugin) 135 { 136 plugins.remove (plugin); 137 regenerateAdminTree(); 138 } 139 140 143 public synchronized void regenerateAdminTree () 144 { 145 currentTrees.clear(); 148 } 149 150 153 public synchronized void regenerateAdminTreeForProfile (String profile) 154 { 155 currentTrees.remove(profile); 158 } 159 160 163 public synchronized TreeInfo getTreeForProfile (String profile) 164 { 165 TreeInfo currentTree = (TreeInfo)currentTrees.get(profile); 166 167 if (currentTree == null) 168 { 169 HashSet resourcesToManage = new HashSet (); 170 TreeInfo result = new DefaultTreeInfo (); 171 ArrayList pluginsSubset = getPluginsSubsetForProfile (profile); 172 HashSet resourcesAlreadyScanned = new HashSet (); 173 174 result.setRootResources (new ManageableResource[] {bootstrapResource}); 175 176 resourcesToManage.add (bootstrapResource); 179 180 while (resourcesToManage.size () > 0) 181 { 182 ManageableResource currentResource = (ManageableResource)resourcesToManage.iterator ().next (); 183 184 resourcesToManage.remove (currentResource); 187 resourcesAlreadyScanned.add (currentResource); 188 189 Iterator iter = getTreesForResource(currentResource, profile, pluginsSubset); 190 while (iter.hasNext ()) 191 { 192 TreeNode subTree = (TreeNode)iter.next (); 193 result.addTreeToResource (currentResource, subTree); 194 HashSet subResources = findSubResources (subTree); 195 if (subResources != null && subResources.size () > 0) 196 { 197 Iterator subsRes = subResources.iterator (); 198 while (subsRes.hasNext ()) 199 { 200 ManageableResource subRes = (ManageableResource)subsRes.next (); 201 if (!resourcesAlreadyScanned.contains (subRes)) 202 resourcesToManage.add (subRes); 203 } 204 } 205 206 207 } 208 } 209 210 this.treeVersion++; 211 result.setTreeVersion (this.treeVersion); 212 try 213 { 214 TreeNodeMenuEntry[] base = new TreeNodeMenuEntry[] 215 { 216 new SimpleTreeNodeMenuEntryImpl ("Update tree", new RefreshTreeAction (false)), 217 new SimpleTreeNodeMenuEntryImpl ("Force update tree", new RefreshTreeAction (true)), 218 }; 219 220 if (enableShutdown) 221 { 222 result.setRootMenus (new TreeNodeMenuEntry[] 223 { 224 base[0], 225 base[1], 226 new SeparatorTreeNodeMenuEntry (), 227 new SimpleTreeNodeMenuEntryImpl ("Shutdown JBoss instance", 228 new MBeanAction (new ObjectName ("jboss.system:type=Server"), 229 "shutdown", new Object [0], new String [0]) 230 ), 231 new SimpleTreeNodeMenuEntryImpl ("Shutdown and Restart JBoss instance", 232 new MBeanAction (new ObjectName ("jboss.system:type=Server"), 233 "exit", new Object [] {new Integer (10)}, 234 new String [] {"int"}) 235 ), 236 new SimpleTreeNodeMenuEntryImpl ("HALT and Restart JBoss instance", 237 new MBeanAction (new ObjectName ("jboss.system:type=Server"), 238 "halt", new Object [] {new Integer (10)}, 239 new String [] {"int"}) 240 ) 241 } 242 ); 243 } 244 else 245 { 246 result.setRootMenus (base); 247 } 248 249 result.setHomeAction(new HttpLinkTreeAction (this.mainLinkUrl)); 250 result.setIconUrl (this.mainLogoUrl); 251 } 252 catch (Exception bla) {} 253 254 currentTree = result; 255 256 currentTrees.put(profile, currentTree); 257 258 } 259 260 return currentTree; 261 } 262 263 267 public synchronized TreeInfo getUpdateTreeForProfile (String profile, long knownVersion) 268 { 269 TreeInfo currentTree = (TreeInfo)currentTrees.get(profile); 270 271 if (this.treeVersion > knownVersion || currentTree==null) 272 return getTreeForProfile (profile); 273 else 274 return null; 275 } 276 277 280 public MBeanServer getMBeanServer() 281 { 282 return this.server; 283 } 284 285 288 public ManageableResource getBootstrapResource() 289 { 290 return this.bootstrapResource; 291 } 292 293 296 public String getJndiName() 297 { 298 return jndiName; 299 } 300 301 304 public void setJndiName(String jndiName) 305 { 306 this.jndiName = jndiName; 307 } 308 309 312 public boolean isEnableShutdown() 313 { 314 return enableShutdown; 315 } 316 317 320 public void setEnableShutdown(boolean enableShutdown) 321 { 322 this.enableShutdown = enableShutdown; 323 treeVersion++; 324 } 325 326 329 public String getMainLinkUrl() 330 { 331 return mainLinkUrl; 332 } 333 334 337 public void setMainLinkUrl(String mainLinkUrl) 338 { 339 this.mainLinkUrl = mainLinkUrl; 340 treeVersion++; 341 } 342 343 346 public String getMainLogoUrl() 347 { 348 return mainLogoUrl; 349 } 350 351 354 public void setMainLogoUrl(String mainLogoUrl) 355 { 356 this.mainLogoUrl = mainLogoUrl; 357 treeVersion++; 358 } 359 360 362 364 public void handleNotification (Notification notif, Object handback) 365 { 366 regenerateAdminTree (); 370 } 371 372 374 376 378 protected Iterator getTreesForResource(ManageableResource res, String profile, ArrayList pluginsSubset) 379 { 380 ArrayList result = new ArrayList (); 381 382 383 for (int i = 0; i < pluginsSubset.size(); i++) 384 { 385 ConsolePlugin cp = (ConsolePlugin)pluginsSubset.get(i); 386 TreeNode node = null; 387 try 388 { 389 node = cp.getSubTreeForResource (this, profile, res); 390 } 391 catch (Throwable t) 392 { 393 t.printStackTrace(); 394 } 395 396 if (node != null) 397 result.add (node); 398 } 399 400 return result.iterator (); 401 } 402 403 protected ArrayList getPluginsSubsetForProfile (String profile) 404 { 405 ArrayList result = new ArrayList (); 406 407 for (int i = 0; i < plugins.size(); i++) 408 { 409 ConsolePlugin cp = (ConsolePlugin)plugins.get(i); 410 String [] set = cp.getSupportedProfiles (); 411 if (java.util.Arrays.asList (set).contains (profile)) 412 result.add (cp); 413 } 414 415 return result; 416 } 417 418 protected HashSet findSubResources (TreeNode tree) 419 { 420 HashSet result = new HashSet (); 421 422 if (tree instanceof ResourceTreeNode) 426 { 427 result.add (((ResourceTreeNode)tree).getResource ()); 428 } 429 430 ResourceTreeNode[] rns = tree.getNodeManagableResources (); 433 if (rns != null && rns.length > 0) 434 { 435 for (int i=0; i<rns.length; i++) 438 { 439 result.add (rns[i].getResource ()); 440 HashSet subResult = findSubResources (rns[i]); 441 if (subResult != null && subResult.size () > 0) 442 result.addAll (subResult); 443 } 444 } 445 446 TreeNode[] ns = tree.getSubNodes (); 449 if (ns != null && ns.length > 0) 450 { 451 for (int i=0; i<ns.length; i++) 452 { 453 HashSet subResult = findSubResources (ns[i]); 454 if (subResult != null && subResult.size () > 0) 455 result.addAll (subResult); 456 } 457 } 458 459 return result; 460 } 461 462 protected void bindProxyInJndi () throws Exception 463 { 464 InitialContext ctx = new InitialContext (); 465 Object proxy = RMIRemoteMBeanProxy.create (PluginManagerMBean.class, this.getServiceName (), this.getServer ()); 466 org.jboss.naming.Util.rebind (ctx, this.jndiName, proxy); 467 } 468 469 protected void initNotificationReception () throws Exception 470 { 471 ObjectName mbsDelegate = 472 new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 473 474 NotificationFilter filter = new NotificationFilter () 475 { 476 public boolean isNotificationEnabled (Notification n) 477 { 478 return ( n.getType().equals("JMX.mbean.registered") || 479 n.getType().equals("JMX.mbean.unregistered") ); 480 } 481 }; 482 483 this.getServer().addNotificationListener(mbsDelegate, this, filter, null); 484 } 485 486 488 490 } 491 | Popular Tags |