KickJava   Java API By Example, From Geeks To Geeks.

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


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 bsh.Interpreter;
25 import org.jboss.console.manager.PluginManager;
26 import org.jboss.console.manager.interfaces.ManageableResource;
27 import org.jboss.console.manager.interfaces.ResourceTreeNode;
28 import org.jboss.console.manager.interfaces.TreeNode;
29 import org.jboss.console.manager.interfaces.TreeNodeMenuEntry;
30 import org.jboss.logging.Logger;
31
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.ObjectInstance JavaDoc;
34 import javax.servlet.ServletConfig JavaDoc;
35 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
36 import java.net.URL JavaDoc;
37
38 /**
39  * <description>
40  *
41  * @see <related>
42  *
43  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
44  * @version $Revision: 37459 $
45  *
46  * <p><b>Revisions:</b>
47  *
48  * <p><b>23 dec 2002 Sacha Labourey:</b>
49  * <ul>
50  * <li> First implementation </li>
51  * </ul>
52  */

53 public class BasePluginWrapper
54    extends AbstractPluginWrapper
55 {
56    
57    // Constants -----------------------------------------------------
58

59    // Attributes ----------------------------------------------------
60

61    protected Interpreter interpreter = null;
62
63    protected String JavaDoc pluginName = null;
64    protected String JavaDoc pluginVersion = null;
65    
66    protected String JavaDoc scriptName = null;
67    
68    protected String JavaDoc scriptContent = null;
69    
70    protected ScriptPlugin script = null;
71    protected PluginContext pluginCtx = null;
72    
73    // Static --------------------------------------------------------
74

75    // Constructors --------------------------------------------------
76

77    public BasePluginWrapper () { super (); }
78    
79    // Public --------------------------------------------------------
80

81    // Z implementation ----------------------------------------------
82

83    // PluginWrapper overrides ---------------------------------------
84

85    public void init (ServletConfig JavaDoc servletConfig) throws Exception JavaDoc
86    {
87       super.init (servletConfig);
88       
89       loadScript (this.scriptName);
90       pluginCtx = new SimplePluginContext ();
91
92    }
93
94    public void readConfigurationParameters (ServletConfig JavaDoc config)
95    {
96       /*
97       try
98       {
99          this.rootContextName = config.getServletContext().getServletContextName();
100          log.info ("XXXXXXXXXXXXXXXXXX* * " + rootContextName);
101          log.info ("XXXXXXXXXXXXXXXXXX* * " + rootContextName);
102       }
103       catch (Exception ignored) {}
104       getRealPath("/");
105       */

106       
107       super.readConfigurationParameters(config);
108       
109       this.scriptName = config.getInitParameter("ScriptName");
110    }
111    
112    // ConsolePlugin overrides ---------------------------------------
113

114    protected String JavaDoc getPluginIdentifier()
115    {
116       try
117       {
118          return script.getName (pluginCtx);
119       }
120       catch (UndeclaredThrowableException JavaDoc ute)
121       {
122          return "ServletPluginHelper Wrapping script '" + this.scriptName + "'";
123       }
124    }
125
126    protected String JavaDoc getPluginVersion()
127    {
128       try
129       {
130          System.out.println ("Version : " + script.getVersion (pluginCtx));
131          return script.getVersion (pluginCtx);
132       }
133       catch (UndeclaredThrowableException JavaDoc ute)
134       {
135          return "unknown version";
136       }
137    }
138    
139    protected TreeNode getTreeForResource(
140       String JavaDoc profile,
141       ManageableResource resource)
142    {
143       try
144       {
145          TreeNode result = script.getTreeForResource (resource, pluginCtx);
146          // result = fixUrls (result); // no really necessary now!
147
return result;
148       }
149       catch (UndeclaredThrowableException JavaDoc ute)
150       {
151          ute.printStackTrace(); // TODO CHANGE TO LOG.DEBUG!!!
152
return null; // we decide for the plugin: we don't provide content
153
}
154    }
155
156    protected boolean isResourceToBeManaged (ManageableResource resource)
157    {
158       if (checker != null)
159          return super.isResourceToBeManaged(resource);
160       else
161       {
162          try
163          {
164             return isResourceToBeManaged_Script (pm, resource);
165          }
166          catch (UndeclaredThrowableException JavaDoc ute)
167          {
168             ute.printStackTrace();
169             return false; // we decide for the plugin (not implemented by it)
170
}
171       }
172    }
173    
174    // Package protected ---------------------------------------------
175

176    // Protected -----------------------------------------------------
177

178    protected boolean isResourceToBeManaged_Script (PluginManager master,
179                                               ManageableResource resource)
180                                               throws UndeclaredThrowableException JavaDoc
181    {
182       return script.isResourceToBeManaged(resource, pluginCtx);
183    }
184         
185    protected void loadScript (String JavaDoc scriptName) throws Exception JavaDoc
186    {
187       URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource(scriptName);
188       if (url == null)
189          throw new IllegalArgumentException JavaDoc("Resource not found: " + scriptName);
190
191       interpreter = new Interpreter ();
192       //System.out.println(Thread.currentThread().getContextClassLoader());
193
interpreter.setClassLoader(Thread.currentThread().getContextClassLoader());
194       //interpreter.eval (new java.io.InputStreamReader (url.openStream()), new NameSpace (this.rootContextName), this.rootContextName);
195
interpreter.eval (new java.io.InputStreamReader JavaDoc (url.openStream()));
196       //interpreter.source (url.getFile(), new bsh.NameSpace(this.rootContextName));
197

198       script = (ScriptPlugin)interpreter.getInterface(ScriptPlugin.class);
199       
200    }
201
202    // Private -------------------------------------------------------
203

204    // Inner classes -------------------------------------------------
205

206    public class SimplePluginContext implements PluginContext
207    {
208       public String JavaDoc localizeUrl (String JavaDoc source)
209       {
210          return fixUrl (source);
211       }
212       
213       public MBeanServer JavaDoc getLocalMBeanServer()
214       {
215          return mbeanServer;
216       }
217
218       public ObjectInstance JavaDoc[] getMBeansForClass(String JavaDoc scope, String JavaDoc className)
219       {
220          return BasePluginWrapper.this.getMBeansForClass (scope, className);
221       }
222
223       public Logger getLogger()
224       {
225          return log;
226       }
227
228       public TreeNode createTreeNode (String JavaDoc name,
229                                                String JavaDoc description,
230                                                String JavaDoc iconUrl,
231                                                String JavaDoc defaultUrl,
232                                                TreeNodeMenuEntry[] menuEntries,
233                                                TreeNode[] subNodes,
234                                                ResourceTreeNode[] subResNodes) throws Exception JavaDoc
235       {
236          return BasePluginWrapper.this.createTreeNode (name, description, iconUrl, defaultUrl, menuEntries, subNodes, subResNodes);
237       }
238    
239       public ResourceTreeNode createResourceNode (String JavaDoc name,
240                                                String JavaDoc description,
241                                                String JavaDoc iconUrl,
242                                                String JavaDoc defaultUrl,
243                                                TreeNodeMenuEntry[] menuEntries,
244                                                TreeNode[] subNodes,
245                                                ResourceTreeNode[] subResNodes,
246                                                String JavaDoc jmxObjectName,
247                                                String JavaDoc jmxClassName) throws Exception JavaDoc
248       {
249          return BasePluginWrapper.this.createResourceNode (name,
250                                                description,
251                                                iconUrl,
252                                                defaultUrl,
253                                                menuEntries,
254                                                subNodes,
255                                                subResNodes,
256                                                jmxObjectName,
257                                                jmxClassName);
258       }
259    
260       public ResourceTreeNode createResourceNode (String JavaDoc name,
261                                                String JavaDoc description,
262                                                String JavaDoc iconUrl,
263                                                String JavaDoc defaultUrl,
264                                                TreeNodeMenuEntry[] menuEntries,
265                                                TreeNode[] subNodes,
266                                                ResourceTreeNode[] subResNodes,
267                                                ManageableResource resource) throws Exception JavaDoc
268       {
269          return BasePluginWrapper.this.createResourceNode (name,
270                                                description,
271                                                iconUrl,
272                                                defaultUrl,
273                                                menuEntries,
274                                                subNodes,
275                                                subResNodes,
276                                                resource);
277       }
278    
279       public TreeNodeMenuEntry[] createMenus (String JavaDoc[] content) throws Exception JavaDoc
280       {
281          return BasePluginWrapper.this.createMenus (content);
282       }
283       
284       public String JavaDoc encode (String JavaDoc source)
285       {
286          return BasePluginWrapper.this.encode (source);
287       }
288
289
290    }
291
292 }
293
Popular Tags