2 Replies Latest reply on Jan 14, 2004 7:14 AM by James Cooley

    Scripting Nukes modules with Beanshell

    James Cooley Newbie

      Sacha checked in a BeanShell JBoss sub-deployer to JBoss some time back and it looks like an easy way to add scripting to Nukes. You can get his instructions on how to register BeanShell scripts here:

      https://sourceforge.net/tracker/?func=detail&atid=381174&aid=701282&group_id=22866

      It would be nice to build on Sacha's work and add full BSF support to get other languages like jython but that's another days work.

      All you need to do is copy the beanshell script and a jar containing a Java interface defining the jmx interface to your deploy directory and invoke using something like

      http://localhost:8080/index.html?module=myService

      You get some errors as Nukes trys to register a class (as the current version of BeanShell doesn't generate one).
      +++ error +++
      java.lang.ClassNotFoundException: No ClassLoaders found for: file:/backup/1/nukes/3.2.3_nukes-1.0.0RC1/server/default/deploy/myNukesService.bsh
      at org.jboss.mx.loading.LoadMgr3.beginLoadTask(LoadMgr3.java:161)
      +++ error +++

      But it works anyway. BeanShell 2.0 may fix this problem.

      The code below isn't pretty but you'll get the general idea.

      James

      Use something like this to deploy
      # you can find nukes-lib.jar in the nukes.ear
      javac -classpath $JBOSS_HOME/lib/jboss-system.jar:nukes-lib.jar NukesBshInterface.java
      jar cvf nukes-bsh.jar NukesBshInterface.class
      cp nukes-bsh.jar myNukesService.bsh $JBOSS_HOME/server/default/deploy
      Invoke by calling ...
      http://localhost:8080/index.html?module=myService
      You can see the myService JMX module interface on
      http://localhost:8080/jmx-console/

      ################### Beanshell script start ##########################
      /**
      A simple nukes service to print out the current time

      */
      import java.util.Collections;
      import org.jboss.nukes.html.Page;
      import org.jboss.nukes.module.ModuleMetaData;

      String objectName () { return "nukes.modules:name=myService"; }

      // The interface used by JMX
      Class[] getInterfaces () { return new Class[] {NukesBshInterface.class}; }

      create () { System.out.println ("Create called on me"); }

      ResourceBundle getLanguage(Object locale){
      System.out.println("getLanguage called");
      return null; //new java.util.ListResourceBundle();
      }

      main(Page page){
      System.out.println("main called.");
      page.openTable();
      page.print("My first bsh nukes module!");
      page.closeTable();
      }
      action(Page page){
      System.out.println("action called.");
      page.openTable();
      page.print("You called - " + page.getParameter("name", "Not Defined") );
      page.closeTable();
      }

      process(Page page){
      System.out.println("process called. "+ page.getParameter("module", "Not Defined"));
      page.process();
      System.out.println("process ended.");
      }

      head(Page page)
      {
      System.out.println("head called.");
      }


      render(Page page)
      {
      System.out.println("render called.");
      page.openTable();
      page.print("The time is now " + new java.util.Date() );
      page.closeTable();
      }


      getMetaData()
      {
      System.out.println("getMetaData called");
      return new ModuleMetaData("myService", Collections.EMPTY_LIST);
      }



      ################### Beanshell script end ##########################

      ################### Interface start ##########################
      /**
      * NukesBshInterface.java
      *
      */

      import org.w3c.dom.Element;
      import org.jboss.nukes.module.ModuleMetaData;
      import java.util.Locale;
      import java.util.ResourceBundle;
      import org.jboss.nukes.resources.ResourceRequest;
      import org.jboss.nukes.resources.ResourceResponse;
      import org.jboss.nukes.resources.Resource;
      import org.jboss.nukes.html.Page;

      import org.jboss.system.Service;
      import org.jboss.system.ServiceMBeanSupport;

      public interface NukesBshInterface extends org.jboss.system.Service {

      public String objectName ();
      public Class[] getInterfaces ();
      public void setCtx (ServiceMBeanSupport wrapper);
      public ResourceBundle getLanguage(Locale locale);
      public ModuleMetaData getMetaData();
      public void main(Page page);
      public void process(Page page);
      public void action(Page page);
      public void head(Page page);
      public void render(Page page);

      } // NukesBshInterface

      ################### Interface end ##########################