KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > plugins > helpers > AbstractPluginWrapper


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.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 JavaDoc;
43 import javax.management.MalformedObjectNameException JavaDoc;
44 import javax.management.ObjectInstance JavaDoc;
45 import javax.management.ObjectName JavaDoc;
46 import javax.management.Query JavaDoc;
47 import javax.management.QueryExp JavaDoc;
48 import javax.servlet.ServletConfig JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Set JavaDoc;
51
52 /**
53  * <description>
54  *
55  * @see <related>
56  *
57  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
58  * @version $Revision: 37459 $
59  *
60  * <p><b>Revisions:</b>
61  *
62  * <p><b>2 janv. 2003 Sacha Labourey:</b>
63  * <ul>
64  * <li> First implementation </li>
65  * </ul>
66  */

67 public abstract class AbstractPluginWrapper
68    implements PluginWrapper, ConsolePlugin
69 {
70    // Constants -----------------------------------------------------
71

72    public static final String JavaDoc OBJECT_NAME_PARAM = "ObjectName";
73    public static final String JavaDoc FOLDER_NAME_PARAM = "FolderName";
74    public static final String JavaDoc MBEAN_CLASS_PARAM = "MBeanClass";
75    public static final String JavaDoc WRAPPER_CLASS_PARAM = "WrapperClass";
76    public static final String JavaDoc SCRIPT_NAME_PARAM = "ScriptName";
77    public static final String JavaDoc IS_ROOT_NODE_PARAM = "IsRootNode";
78
79    // Attributes ----------------------------------------------------
80

81    protected MBeanServer JavaDoc mbeanServer = null;
82    protected PluginManager pm = null;
83
84    protected String JavaDoc pluginName = null;
85    protected String JavaDoc pluginVersion = null;
86    
87    protected String JavaDoc objectName = null;
88    protected String JavaDoc mbeanClass = null;
89    protected String JavaDoc folderName = null;
90      
91    protected String JavaDoc rootContextName = null;
92    
93    protected Logger log = org.jboss.logging.Logger.getLogger(this.getClass());
94    
95    protected InternalResourceChecker checker = null;
96    
97    // Static --------------------------------------------------------
98

99    // Constructors --------------------------------------------------
100

101    public AbstractPluginWrapper () {}
102    
103    // Public --------------------------------------------------------
104

105    // Z implementation ----------------------------------------------
106

107    // PluginWrapper overrides ---------------------------------------
108

109    public void init (ServletConfig JavaDoc servletConfig) throws Exception JavaDoc
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 JavaDoc 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 JavaDoc tmp = this.objectName;
136       if (tmp != null && !"".equals(tmp))
137       {
138          // this kind of plugin is associated with a single MBean
139
// which has a give JMX ObjectName
140
//
141
checker = new SingleMBeanChecker ();
142       }
143
144       tmp = this.folderName;
145       if (tmp != null && !"".equals(tmp))
146       {
147          // this kind of plugins is associated with one of the static folder of the tree
148
//
149
checker = new SubFolderChecker ();
150       }
151       
152       tmp = config.getInitParameter(IS_ROOT_NODE_PARAM);
153       if (tmp != null && !"".equals(tmp) && "true".equalsIgnoreCase(tmp))
154       {
155          // this kind of plugins is associated with the root of the tree
156
//
157
checker = new RootTreeChecker ();
158       }
159       
160       tmp = this.mbeanClass;
161       if (tmp != null && !"".equals(tmp))
162       {
163          // this kind of plugins is associated with all MBean
164
// that share a given interface
165
//
166
checker = new StandardMBeanChecker ();
167       }
168
169    }
170    
171    // ConsolePlugin overrides ---------------------------------------
172

173    public String JavaDoc 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 JavaDoc 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 JavaDoc[] getSupportedProfiles()
198    {
199       return new String JavaDoc[] {ConsolePlugin.WEB_PROFILE};
200    }
201
202    public TreeNode getSubTreeForResource(
203       PluginManager master,
204       String JavaDoc 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    // Abstract Methods ---------------------------------------------
228

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 JavaDoc profile,
243       ManageableResource resource);
244
245    protected String JavaDoc getPluginIdentifier()
246    {
247       return "AbstractPluginWrapper (" + this.getClass() + ")";
248    }
249
250    protected String JavaDoc getPluginVersion()
251    {
252       return "unknown version";
253    }
254
255    // Package protected ---------------------------------------------
256

257    // Protected -----------------------------------------------------
258

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 JavaDoc getMBeanServer ()
271    {
272       return this.mbeanServer;
273    }
274    
275    protected String JavaDoc fixUrl (String JavaDoc 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; // already absolute
289
}
290       else
291       {
292          return this.rootContextName + "/" + source;
293       }
294    }
295
296    protected ObjectInstance JavaDoc[] getMBeansForClass(String JavaDoc scope, String JavaDoc className)
297    {
298       try
299       {
300          Set JavaDoc result = mbeanServer.queryMBeans(new ObjectName JavaDoc(scope),
301             Query.eq (Query.classattr(), Query.value(className)));
302          
303          return (ObjectInstance JavaDoc[])result.toArray(new ObjectInstance JavaDoc[result.size()]);
304       }
305       catch (MalformedObjectNameException JavaDoc e)
306       {
307          log.debug (e);
308          return new ObjectInstance JavaDoc[0];
309       }
310          
311    }
312
313    protected ObjectInstance JavaDoc[] getMBeansForQuery(String JavaDoc scope, QueryExp JavaDoc query)
314    {
315       try
316       {
317          Set JavaDoc result = mbeanServer.queryMBeans(new ObjectName JavaDoc(scope), query);
318          return (ObjectInstance JavaDoc[])result.toArray(new ObjectInstance JavaDoc[result.size()]);
319       }
320       catch (MalformedObjectNameException JavaDoc e)
321       {
322          log.debug (e);
323          return new ObjectInstance JavaDoc[0];
324       }
325          
326    }
327
328    protected SimpleTreeNode createTreeNode (String JavaDoc name,
329                                             String JavaDoc description,
330                                             String JavaDoc iconUrl,
331                                             String JavaDoc defaultUrl,
332                                             TreeNodeMenuEntry[] menuEntries,
333                                             TreeNode[] subNodes,
334                                             ResourceTreeNode[] subResNodes) throws Exception JavaDoc
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 JavaDoc name,
341                                             String JavaDoc description,
342                                             String JavaDoc iconUrl,
343                                             String JavaDoc defaultUrl,
344                                             TreeNodeMenuEntry[] menuEntries,
345                                             TreeNode[] subNodes,
346                                             ResourceTreeNode[] subResNodes,
347                                             String JavaDoc jmxObjectName,
348                                             String JavaDoc jmxClassName) throws Exception JavaDoc
349    {
350       TreeAction action = new HttpLinkTreeAction (fixUrl(defaultUrl));
351       ManageableResource res = new MBeanResource (new ObjectName JavaDoc(jmxObjectName), jmxClassName);
352       return new SimpleResourceTreeNode (name, description, fixUrl(iconUrl), action, menuEntries, subNodes, subResNodes, res);
353    }
354
355    protected SimpleResourceTreeNode createResourceNode (String JavaDoc name,
356                                             String JavaDoc description,
357                                             String JavaDoc iconUrl,
358                                             String JavaDoc defaultUrl,
359                                             TreeNodeMenuEntry[] menuEntries,
360                                             TreeNode[] subNodes,
361                                             ResourceTreeNode[] subResNodes,
362                                             ManageableResource resource) throws Exception JavaDoc
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 JavaDoc[] content) throws Exception JavaDoc
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 JavaDoc 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 JavaDoc encode (String JavaDoc source)
401    {
402       try
403       {
404          return java.net.URLEncoder.encode(source);
405       }
406       catch (Exception JavaDoc e)
407       {
408          return source;
409       }
410    }
411
412    // Private -------------------------------------------------------
413

414    // Inner classes -------------------------------------------------
415

416    public interface InternalResourceChecker
417    {
418       boolean isResourceToBeManaged (ManageableResource resource);
419    }
420    
421    public class StandardMBeanChecker
422       implements InternalResourceChecker
423    {
424    
425       protected Class JavaDoc targetClass = null;
426       public HashMap JavaDoc knownAnswers = new HashMap JavaDoc ();
427       
428       public StandardMBeanChecker ()
429       {
430          try
431          {
432             targetClass = Thread.currentThread().getContextClassLoader().loadClass(mbeanClass);
433          }
434          catch (Exception JavaDoc 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 JavaDoc result = (Boolean JavaDoc)knownAnswers.get(mbr.getClassName ());
447             if (result == null)
448             {
449                // find answer and cache it
450
//
451
try
452                {
453                   //System.out.println("CHECK: " +
454
Class JavaDoc resourceClass = Thread.currentThread().getContextClassLoader().loadClass(mbr.getClassName ());
455                   result = new Boolean JavaDoc (targetClass.isAssignableFrom(resourceClass));
456                   //result = new Boolean (resourceClass.isAssignableFrom(targetClass));
457
}
458                catch (Exception JavaDoc 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