From: <do...@hy...> - 2008-04-17 22:33:58
|
Author: dougm Date: 2008-04-17 15:33:43 -0700 (Thu, 17 Apr 2008) New Revision: 8986 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=8986 Modified: trunk/src/org/hyperic/hq/product/ProductPlugin.java trunk/src/org/hyperic/hq/product/pluginxml/EmbedTag.java Log: [HHQ-1996] support groovy in product plugins Modified: trunk/src/org/hyperic/hq/product/ProductPlugin.java =================================================================== --- trunk/src/org/hyperic/hq/product/ProductPlugin.java 2008-04-17 22:33:00 UTC (rev 8985) +++ trunk/src/org/hyperic/hq/product/ProductPlugin.java 2008-04-17 22:33:43 UTC (rev 8986) @@ -25,7 +25,11 @@ package org.hyperic.hq.product; +import groovy.lang.GroovyClassLoader; + +import java.io.ByteArrayInputStream; import java.io.File; +import java.io.InputStream; import java.util.List; import org.hyperic.hq.product.pluginxml.PluginData; @@ -116,6 +120,49 @@ return (String[])cp.toArray(new String[0]); } + public static boolean isGroovyScript(String name) { + return name.endsWith(".groovy"); + } + + private static Class loadGroovyClass(GenericPlugin plugin, + String name, TypeInfo info) { + + ClassLoader loader = plugin.getClass().getClassLoader(); + GroovyClassLoader cl = new GroovyClassLoader(loader); + + File file = new File(name); //XXX pdk/work/ + if (file.exists()) { + try { + return cl.parseClass(file); + } catch (Exception e) { + plugin.getLog().error("Failed to load: " + name, e); + return null; + } + } + else { + InputStream is; + is = loader.getResourceAsStream(name); //embedded in plugin.jar + if (is == null) { + //in memory server-side + String code = plugin.data.getProperty(name); + if (code == null) { + plugin.getLog().error("No code found for: " + name); + return null; + } + is = new ByteArrayInputStream(code.getBytes()); + } + + try { + return cl.parseClass(is); + } catch (Exception e) { + plugin.getLog().error("Failed to parse: " + name, e); + return null; + } finally { + try { is.close(); } catch (Exception e) {} + } + } + } + private static Class loadClass(ClassLoader loader, String name) throws ClassNotFoundException { @@ -135,23 +182,20 @@ throw nfe; } } - - static GenericPlugin getPlugin(GenericPlugin plugin, - String name, - String type, TypeInfo info) { - Class pluginClass; + private static Class loadJavaClass(GenericPlugin plugin, + String name, TypeInfo info) { ClassLoader loader = plugin.getClass().getClassLoader(); try { - pluginClass = loadClass(loader, name); + return loadClass(loader, name); } catch (ClassNotFoundException e) { //we get here if the server's implementation is a class loaded //from hq-product.jar rather than the plugin's ClassLoader try { plugin.getLog().debug("Trying data ClassLoader to load: " + name + " for plugin " + info.getName()); - pluginClass = loadClass(plugin.data.getClassLoader(), name); + return loadClass(plugin.data.getClassLoader(), name); } catch (ClassNotFoundException e2) { String msg = "Unable to load " + name + @@ -167,6 +211,25 @@ return null; } } + } + + static GenericPlugin getPlugin(GenericPlugin plugin, + String name, + String type, TypeInfo info) { + + Class pluginClass; + + if (isGroovyScript(name)) { + pluginClass = loadGroovyClass(plugin, name, info); + } + else { + pluginClass = loadJavaClass(plugin, name, info); + } + + if (pluginClass == null) { + return null; + } + try { return (GenericPlugin)pluginClass.newInstance(); } catch (Exception e) { Modified: trunk/src/org/hyperic/hq/product/pluginxml/EmbedTag.java =================================================================== --- trunk/src/org/hyperic/hq/product/pluginxml/EmbedTag.java 2008-04-17 22:33:00 UTC (rev 8985) +++ trunk/src/org/hyperic/hq/product/pluginxml/EmbedTag.java 2008-04-17 22:33:43 UTC (rev 8986) @@ -29,6 +29,7 @@ import java.io.IOException; import org.hyperic.hq.product.ClientPluginDeployer; +import org.hyperic.hq.product.ProductPlugin; import org.hyperic.util.xmlparser.XmlTagException; import org.hyperic.util.xmlparser.XmlTextHandler; @@ -82,7 +83,10 @@ void write() throws XmlTagException { String name = getAttribute(ATTR_NAME); - + if (ProductPlugin.isGroovyScript(name)) { + this.data.setProperty(name, this.text); //XXX + return; + } String pdk = this.data.getPdkDir(); if (pdk == null) { |