1 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 ; 33 import javax.management.ObjectInstance ; 34 import javax.servlet.ServletConfig ; 35 import java.lang.reflect.UndeclaredThrowableException ; 36 import java.net.URL ; 37 38 53 public class BasePluginWrapper 54 extends AbstractPluginWrapper 55 { 56 57 59 61 protected Interpreter interpreter = null; 62 63 protected String pluginName = null; 64 protected String pluginVersion = null; 65 66 protected String scriptName = null; 67 68 protected String scriptContent = null; 69 70 protected ScriptPlugin script = null; 71 protected PluginContext pluginCtx = null; 72 73 75 77 public BasePluginWrapper () { super (); } 78 79 81 83 85 public void init (ServletConfig servletConfig) throws Exception 86 { 87 super.init (servletConfig); 88 89 loadScript (this.scriptName); 90 pluginCtx = new SimplePluginContext (); 91 92 } 93 94 public void readConfigurationParameters (ServletConfig config) 95 { 96 106 107 super.readConfigurationParameters(config); 108 109 this.scriptName = config.getInitParameter("ScriptName"); 110 } 111 112 114 protected String getPluginIdentifier() 115 { 116 try 117 { 118 return script.getName (pluginCtx); 119 } 120 catch (UndeclaredThrowableException ute) 121 { 122 return "ServletPluginHelper Wrapping script '" + this.scriptName + "'"; 123 } 124 } 125 126 protected String getPluginVersion() 127 { 128 try 129 { 130 System.out.println ("Version : " + script.getVersion (pluginCtx)); 131 return script.getVersion (pluginCtx); 132 } 133 catch (UndeclaredThrowableException ute) 134 { 135 return "unknown version"; 136 } 137 } 138 139 protected TreeNode getTreeForResource( 140 String profile, 141 ManageableResource resource) 142 { 143 try 144 { 145 TreeNode result = script.getTreeForResource (resource, pluginCtx); 146 return result; 148 } 149 catch (UndeclaredThrowableException ute) 150 { 151 ute.printStackTrace(); return null; } 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 ute) 167 { 168 ute.printStackTrace(); 169 return false; } 171 } 172 } 173 174 176 178 protected boolean isResourceToBeManaged_Script (PluginManager master, 179 ManageableResource resource) 180 throws UndeclaredThrowableException 181 { 182 return script.isResourceToBeManaged(resource, pluginCtx); 183 } 184 185 protected void loadScript (String scriptName) throws Exception 186 { 187 URL url = Thread.currentThread().getContextClassLoader().getResource(scriptName); 188 if (url == null) 189 throw new IllegalArgumentException ("Resource not found: " + scriptName); 190 191 interpreter = new Interpreter (); 192 interpreter.setClassLoader(Thread.currentThread().getContextClassLoader()); 194 interpreter.eval (new java.io.InputStreamReader (url.openStream())); 196 198 script = (ScriptPlugin)interpreter.getInterface(ScriptPlugin.class); 199 200 } 201 202 204 206 public class SimplePluginContext implements PluginContext 207 { 208 public String localizeUrl (String source) 209 { 210 return fixUrl (source); 211 } 212 213 public MBeanServer getLocalMBeanServer() 214 { 215 return mbeanServer; 216 } 217 218 public ObjectInstance [] getMBeansForClass(String scope, String 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 name, 229 String description, 230 String iconUrl, 231 String defaultUrl, 232 TreeNodeMenuEntry[] menuEntries, 233 TreeNode[] subNodes, 234 ResourceTreeNode[] subResNodes) throws Exception 235 { 236 return BasePluginWrapper.this.createTreeNode (name, description, iconUrl, defaultUrl, menuEntries, subNodes, subResNodes); 237 } 238 239 public ResourceTreeNode createResourceNode (String name, 240 String description, 241 String iconUrl, 242 String defaultUrl, 243 TreeNodeMenuEntry[] menuEntries, 244 TreeNode[] subNodes, 245 ResourceTreeNode[] subResNodes, 246 String jmxObjectName, 247 String jmxClassName) throws Exception 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 name, 261 String description, 262 String iconUrl, 263 String defaultUrl, 264 TreeNodeMenuEntry[] menuEntries, 265 TreeNode[] subNodes, 266 ResourceTreeNode[] subResNodes, 267 ManageableResource resource) throws Exception 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 [] content) throws Exception 280 { 281 return BasePluginWrapper.this.createMenus (content); 282 } 283 284 public String encode (String source) 285 { 286 return BasePluginWrapper.this.encode (source); 287 } 288 289 290 } 291 292 } 293 | Popular Tags |