KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > manager > PluginManager


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.manager;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.Notification JavaDoc;
31 import javax.management.NotificationFilter JavaDoc;
32 import javax.management.NotificationListener JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
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 /**
54  * @jmx:mbean extends="org.jboss.system.ServiceMBean"
55  */

56 public class PluginManager
57    extends ServiceMBeanSupport
58    implements PluginManagerMBean, NotificationListener JavaDoc
59 {
60
61    // Constants -----------------------------------------------------
62

63    public static String JavaDoc PLUGIN_MANAGER_NAME = null;
64
65    // Attributes ----------------------------------------------------
66

67    protected ArrayList JavaDoc plugins = new ArrayList JavaDoc ();
68    protected ManageableResource bootstrapResource = null;
69
70    public String JavaDoc jndiName = "console/PluginManager";
71
72    protected long treeVersion = 0;
73    protected HashMap JavaDoc currentTrees = new HashMap JavaDoc();
74
75    protected String JavaDoc mainLogoUrl = "/web-console/images/jboss.gif";
76    protected String JavaDoc mainLinkUrl = "http://www.jboss.org/forums/";
77    protected boolean enableShutdown = true;
78
79    // Static --------------------------------------------------------
80

81    // Constructors --------------------------------------------------
82

83    public PluginManager ()
84    {
85    }
86
87    // Public --------------------------------------------------------
88

89    public void createService () throws Exception JavaDoc
90    {
91       this.bootstrapResource = new MBeanResource (this.getServiceName (), this.getClass ().toString ());
92    }
93
94    public void startService () throws Exception JavaDoc
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    /**
109     * send a message
110     * @jmx:managed-operation
111     */

112    public void registerPlugin (String JavaDoc consolePluginClassName) throws Exception JavaDoc
113    {
114       Class JavaDoc pluginClass = Thread.currentThread ().getContextClassLoader ().
115                            loadClass (consolePluginClassName);
116       ConsolePlugin plugin = (ConsolePlugin)pluginClass.newInstance ();
117       this.registerPlugin (plugin);
118    }
119
120    /**
121     * send a message
122     * @jmx:managed-operation
123     */

124    public synchronized void registerPlugin (ConsolePlugin plugin)
125    {
126       plugins.add (plugin);
127       regenerateAdminTree();
128    }
129
130    /**
131     * send a message
132     * @jmx:managed-operation
133     */

134    public synchronized void unregisterPlugin (ConsolePlugin plugin)
135    {
136       plugins.remove (plugin);
137       regenerateAdminTree();
138    }
139
140    /**
141     * @jmx:managed-operation
142     */

143    public synchronized void regenerateAdminTree ()
144    {
145       // remove all cached trees
146
//
147
currentTrees.clear();
148    }
149
150    /**
151     * @jmx:managed-operation
152     */

153    public synchronized void regenerateAdminTreeForProfile (String JavaDoc profile)
154    {
155       // remove cached tree for profile (if any)
156
//
157
currentTrees.remove(profile);
158    }
159
160    /**
161     * @jmx:managed-operation
162     */

163    public synchronized TreeInfo getTreeForProfile (String JavaDoc profile)
164    {
165       TreeInfo currentTree = (TreeInfo)currentTrees.get(profile);
166
167       if (currentTree == null)
168       {
169          HashSet JavaDoc resourcesToManage = new HashSet JavaDoc ();
170          TreeInfo result = new DefaultTreeInfo ();
171          ArrayList JavaDoc pluginsSubset = getPluginsSubsetForProfile (profile);
172          HashSet JavaDoc resourcesAlreadyScanned = new HashSet JavaDoc ();
173
174          result.setRootResources (new ManageableResource[] {bootstrapResource});
175
176          // Bootstrap tree creation
177
//
178
resourcesToManage.add (bootstrapResource);
179
180          while (resourcesToManage.size () > 0)
181          {
182             ManageableResource currentResource = (ManageableResource)resourcesToManage.iterator ().next ();
183
184             // pre-clean resources environment
185
//
186
resourcesToManage.remove (currentResource);
187             resourcesAlreadyScanned.add (currentResource);
188
189             Iterator JavaDoc iter = getTreesForResource(currentResource, profile, pluginsSubset);
190             while (iter.hasNext ())
191             {
192                TreeNode subTree = (TreeNode)iter.next ();
193                result.addTreeToResource (currentResource, subTree);
194                HashSet JavaDoc subResources = findSubResources (subTree);
195                if (subResources != null && subResources.size () > 0)
196                {
197                   Iterator JavaDoc 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 JavaDoc("jboss.system:type=Server"),
229                                           "shutdown", new Object JavaDoc[0], new String JavaDoc[0])
230                      ),
231                      new SimpleTreeNodeMenuEntryImpl ("Shutdown and Restart JBoss instance",
232                         new MBeanAction (new ObjectName JavaDoc("jboss.system:type=Server"),
233                                           "exit", new Object JavaDoc[] {new Integer JavaDoc (10)},
234                                           new String JavaDoc[] {"int"})
235                      ),
236                      new SimpleTreeNodeMenuEntryImpl ("HALT and Restart JBoss instance",
237                         new MBeanAction (new ObjectName JavaDoc("jboss.system:type=Server"),
238                                           "halt", new Object JavaDoc[] {new Integer JavaDoc (10)},
239                                           new String JavaDoc[] {"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 JavaDoc bla) {}
253
254          currentTree = result;
255
256          currentTrees.put(profile, currentTree);
257
258       }
259
260       return currentTree;
261    }
262
263    /**
264     * Only return the tree if the actual version is bigger than the known version
265     * @jmx:managed-operation
266     */

267    public synchronized TreeInfo getUpdateTreeForProfile (String JavaDoc 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    /**
278     *@jmx:managed-attribute
279     */

280    public MBeanServer JavaDoc getMBeanServer()
281    {
282       return this.server;
283    }
284
285    /**
286     *@jmx:managed-attribute
287     */

288    public ManageableResource getBootstrapResource()
289    {
290       return this.bootstrapResource;
291    }
292
293    /**
294     *@jmx:managed-attribute
295     */

296    public String JavaDoc getJndiName()
297    {
298       return jndiName;
299    }
300
301    /**
302     *@jmx:managed-attribute
303     */

304    public void setJndiName(String JavaDoc jndiName)
305    {
306       this.jndiName = jndiName;
307    }
308
309    /**
310     *@jmx:managed-attribute
311     */

312    public boolean isEnableShutdown()
313    {
314       return enableShutdown;
315    }
316
317    /**
318     *@jmx:managed-attribute
319     */

320    public void setEnableShutdown(boolean enableShutdown)
321    {
322       this.enableShutdown = enableShutdown;
323       treeVersion++;
324    }
325
326    /**
327     *@jmx:managed-attribute
328     */

329    public String JavaDoc getMainLinkUrl()
330    {
331       return mainLinkUrl;
332    }
333
334    /**
335     *@jmx:managed-attribute
336     */

337    public void setMainLinkUrl(String JavaDoc mainLinkUrl)
338    {
339       this.mainLinkUrl = mainLinkUrl;
340       treeVersion++;
341    }
342
343    /**
344     *@jmx:managed-attribute
345     */

346    public String JavaDoc getMainLogoUrl()
347    {
348       return mainLogoUrl;
349    }
350
351    /**
352     *@jmx:managed-attribute
353     */

354    public void setMainLogoUrl(String JavaDoc mainLogoUrl)
355    {
356       this.mainLogoUrl = mainLogoUrl;
357       treeVersion++;
358    }
359
360    // Z implementation ----------------------------------------------
361

362    // NotificationListener implementation ----------------------------------------------
363

364    public void handleNotification (Notification JavaDoc notif, Object JavaDoc handback)
365    {
366       // Very simple implementation: could be optimized to minimize tree regeneration
367
// (local invalidation for example)//
368
//
369
regenerateAdminTree ();
370    }
371
372    // Y overrides ---------------------------------------------------
373

374    // Package protected ---------------------------------------------
375

376    // Protected -----------------------------------------------------
377

378    protected Iterator JavaDoc getTreesForResource(ManageableResource res, String JavaDoc profile, ArrayList JavaDoc pluginsSubset)
379    {
380       ArrayList JavaDoc result = new ArrayList JavaDoc ();
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 JavaDoc 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 JavaDoc getPluginsSubsetForProfile (String JavaDoc profile)
404    {
405       ArrayList JavaDoc result = new ArrayList JavaDoc ();
406
407       for (int i = 0; i < plugins.size(); i++)
408       {
409          ConsolePlugin cp = (ConsolePlugin)plugins.get(i);
410          String JavaDoc [] 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 JavaDoc findSubResources (TreeNode tree)
419    {
420       HashSet JavaDoc result = new HashSet JavaDoc ();
421
422       // first add the tree node itself if it is an instance
423
// of an ResourceTreeNode
424
//
425
if (tree instanceof ResourceTreeNode)
426       {
427          result.add (((ResourceTreeNode)tree).getResource ());
428       }
429
430       // then add local resources
431
//
432
ResourceTreeNode[] rns = tree.getNodeManagableResources ();
433       if (rns != null && rns.length > 0)
434       {
435          // Then travel to sub-nodes resources...
436
//
437
for (int i=0; i<rns.length; i++)
438          {
439             result.add (rns[i].getResource ());
440             HashSet JavaDoc subResult = findSubResources (rns[i]);
441             if (subResult != null && subResult.size () > 0)
442                result.addAll (subResult);
443          }
444       }
445
446       // ..and to other sub-nodes (which are not resources)
447
//
448
TreeNode[] ns = tree.getSubNodes ();
449       if (ns != null && ns.length > 0)
450       {
451          for (int i=0; i<ns.length; i++)
452          {
453             HashSet JavaDoc 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 JavaDoc
463    {
464       InitialContext JavaDoc ctx = new InitialContext JavaDoc ();
465       Object JavaDoc 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 JavaDoc
470    {
471       ObjectName JavaDoc mbsDelegate =
472          new ObjectName JavaDoc ("JMImplementation:type=MBeanServerDelegate");
473
474       NotificationFilter JavaDoc filter = new NotificationFilter JavaDoc ()
475       {
476          public boolean isNotificationEnabled (Notification JavaDoc 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    // Private -------------------------------------------------------
487

488    // Inner classes -------------------------------------------------
489

490 }
491
Popular Tags