BSF and NetRexx -- was Announcing general availability (GA) of BSF4ooRexx, version 4.10 as of 20120618

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

BSF and NetRexx -- was Announcing general availability (GA) of BSF4ooRexx, version 4.10 as of 20120618

measel

Yep, BSF is pretty neat. 

 

I’ve used it from NetRexx to allow plugins from other languages to return values to a control program (NetRexx of course).

 

/* just an example – it exposes a metric reporter (printstream) and sleep timer to the called script */

import org.apache.bsf

import org.apache.bsf.util

 

import java.text

 

package com.wily.introscope.epagent

 

class bsController 

              properties public

                             args = String[]

                             epaPrinter = PrintStream

 

method main(eparg = String[], epprt = java.io.PrintStream) public static

             

              bsController(eparg, epprt)

             

method bsController(arg = String[], prtWrt = java.io.PrintStream)    signals InterruptedException     

              fd = SimpleDateFormat("MM/dd/yy hh:mm:ss a z")

              dt = fd.format(Date())

              epaPrinter = prtWrt

              ct = Thread.currentThread()

              args = arg

         say dt '[INFO] [EPAClass bsController] BSF Controller - v0.2'

              say dt '[INFO] [EPAClass bsController] Started'

        -- cfg = Rexx ' '

         fdir = "scripts"

                                           do

                                                             f1 = File(fdir)                       -- create file object

                                                             if f1.exists() = 0 then do

                                                                say dt '[ERROR] [EPAClass bsController] Scripts directory:' fdir 'does not exist.'

                                                                exit 8

                                                             end

                                                             if f1.isDirectory() then do

                                                               list1 = f1.list()

                                                               if list1.length = 0

                                                                        then say dt '[ERROR] [EPAClass bsController] scripts directory is empty'

                                                                        else

                                                                          loop i = 0 to list1.length -1

                                                                             f2 = File(f1.getAbsolutePath()''f1.separator''list1[i])

                                                                             if f2.isDirectory() then iterate

                                                                                                                   else do

                                                                                                                                  say "  Executing File:" f2

                                                                                                                                  sfile = f2.toString()

                                                                                                                                  bsc = getMetrics(sfile,epaPrinter)

                                                                                                                                  Thread(bsc,sfile).start()

                                                                                                                                  end

                                                                         end

                                                             end

                                                            

                                                             

                                                             

                                                             

                                                          end

 

                                           loop forever

                                                          --sleep for a minute

                                                          ct.sleep(60000)

                                                          newfiles = check4New()

                                                          if newfiles = "0" then iterate

                                                                        else

                                                                                      do   

                                                                                                    

                                                                                                     loop i = 1 to newfiles

                                                                                                                   sfile = newfiles[i]

                                                                                                                   bsc = getMetrics(sfile,epaPrinter)

                                                                                                                                  Thread(bsc,sfile).start()

                                                                                                                                 

                                                                                                     end

                                                                                      end

                                                          end

                                           --catch f1=FileNotFoundException

                                           --say 'Exception caught:' f1 '\n  ' f1.getMessage()

                                          

                            

method check4New() returns Rexx

              newfiles = 0

              return newfiles

             

             

class getMetrics implements Runnable

              Properties inheritable

                            

scriptfile = String

prtWrt = printStream

 

                            

method getMetrics(sfile = String, p = java.io.PrintStream) --- signals  IOException, BSFException    

              scriptfile = sfile

              prtWrt = p

             

--

method run()

                            

 

              fd = SimpleDateFormat("MM/dd/yy hh:mm:ss a z")

              dt = fd.format(Date())

              say dt '[INFO] [EPAClass bsController] Executing ' scriptfile

              ct = Thread.currentThread()

             

          do                           

                               manager =  BSFManager()

                               manager.declareBean("metricReporter", this, this.getClass())

                               manager.registerBean("metricReporter",prtWrt)

                               manager.declareBean("sleepTimer",this, this.getClass())

                               manager.registerBean("sleepTimer",ct)

                  do

                                             fname = IOUtils.getStringFromReader(FileReader(scriptfile))

                                             -- say "filename " fname

                                             manager.exec(manager.getLangFromFilename(scriptfile), scriptfile, 0, 0,fname)

                                          

                                             catch fn1 = FileNotFoundException

                                                            say dt '[ERROR] [EPAClass bsController] File Not Found' fn1

                                       catch io1 = IOException

                                                            say dt '[ERROR] [EPAClass bsController] IO exception' io1

                                           end

                                           catch be1 = BSFException

                                                                say dt '[ERROR] [EPAClass bsController] BSFException' be1 '\n ' be1.getMessage()

                             end

 

             

 

 

             

             

              /* catch e5 = InterruptedException

                                           say 'Exception caught:' e5 '\n  ' e5.getMessage()

              */

 

 

return

 

 


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: BSF and NetRexx -- was Announcing general availability (GA) of BSF4ooRexx, version 4.10 as of 20120618

Rony G. Flatscher (wu-wien)

On 19.06.2012 14:53, Measel, Mike wrote:

Yep, BSF is pretty neat.

Yep!

And if you supplied a program "abc.rex" then the Rexx interpreter would get automatically loaded and the program "abc.rex" executed which could access the beans named "metricReporter" and "sleepTimer".

---rony

P.S.: Whoever implemented the NetRexx engine in BSF at IBM did a very nice job!

 

I’ve used it from NetRexx to allow plugins from other languages to return values to a control program (NetRexx of course).

 

/* just an example – it exposes a metric reporter (printstream) and sleep timer to the called script */

import org.apache.bsf

import org.apache.bsf.util

 

import java.text

 

package com.wily.introscope.epagent

 

class bsController 

              properties public

                             args = String[]

                             epaPrinter = PrintStream

 

method main(eparg = String[], epprt = java.io.PrintStream) public static

             

              bsController(eparg, epprt)

             

method bsController(arg = String[], prtWrt = java.io.PrintStream)    signals InterruptedException     

              fd = SimpleDateFormat("MM/dd/yy hh:mm:ss a z")

              dt = fd.format(Date())

              epaPrinter = prtWrt

              ct = Thread.currentThread()

              args = arg

         say dt '[INFO] [EPAClass bsController] BSF Controller - v0.2'

              say dt '[INFO] [EPAClass bsController] Started'

        -- cfg = Rexx ' '

         fdir = "scripts"

                                           do

                                                             f1 = File(fdir)                       -- create file object

                                                             if f1.exists() = 0 then do

                                                                say dt '[ERROR] [EPAClass bsController] Scripts directory:' fdir 'does not exist.'

                                                                exit 8

                                                             end

                                                             if f1.isDirectory() then do

                                                               list1 = f1.list()

                                                               if list1.length = 0

                                                                        then say dt '[ERROR] [EPAClass bsController] scripts directory is empty'

                                                                        else

                                                                          loop i = 0 to list1.length -1

                                                                             f2 = File(f1.getAbsolutePath()''f1.separator''list1[i])

                                                                             if f2.isDirectory() then iterate

                                                                                                                   else do

                                                                                                                                  say "  Executing File:" f2

                                                                                                                                  sfile = f2.toString()

                                                                                                                                  bsc = getMetrics(sfile,epaPrinter)

                                                                                                                                  Thread(bsc,sfile).start()

                                                                                                                                  end

                                                                         end

                                                             end

                                                            

                                                             

                                                             

                                                             

                                                          end

 

                                           loop forever

                                                          --sleep for a minute

                                                          ct.sleep(60000)

                                                          newfiles = check4New()

                                                          if newfiles = "0" then iterate

                                                                        else

                                                                                      do   

                                                                                                    

                                                                                                     loop i = 1 to newfiles

                                                                                                                   sfile = newfiles[i]

                                                                                                                   bsc = getMetrics(sfile,epaPrinter)

                                                                                                                                  Thread(bsc,sfile).start()

                                                                                                                                 

                                                                                                     end

                                                                                      end

                                                          end

                                           --catch f1=FileNotFoundException

                                           --say 'Exception caught:' f1 '\n  ' f1.getMessage()

                                          

                            

method check4New() returns Rexx

              newfiles = 0

              return newfiles

             

             

class getMetrics implements Runnable

              Properties inheritable

                            

scriptfile = String

prtWrt = printStream

 

                            

method getMetrics(sfile = String, p = java.io.PrintStream) --- signals  IOException, BSFException    

              scriptfile = sfile

              prtWrt = p

             

--

method run()

                            

 

              fd = SimpleDateFormat("MM/dd/yy hh:mm:ss a z")

              dt = fd.format(Date())

              say dt '[INFO] [EPAClass bsController] Executing ' scriptfile

              ct = Thread.currentThread()

             

          do                           

                               manager =  BSFManager()

                               manager.declareBean("metricReporter", this, this.getClass())

                               manager.registerBean("metricReporter",prtWrt)

                               manager.declareBean("sleepTimer",this, this.getClass())

                               manager.registerBean("sleepTimer",ct)

                  do

                                             fname = IOUtils.getStringFromReader(FileReader(scriptfile))

                                             -- say "filename " fname

                                             manager.exec(manager.getLangFromFilename(scriptfile), scriptfile, 0, 0,fname)

                                          

                                             catch fn1 = FileNotFoundException

                                                            say dt '[ERROR] [EPAClass bsController] File Not Found' fn1

                                       catch io1 = IOException

                                                            say dt '[ERROR] [EPAClass bsController] IO exception' io1

                                           end

                                           catch be1 = BSFException

                                                                say dt '[ERROR] [EPAClass bsController] BSFException' be1 '\n ' be1.getMessage()

                             end

 

             

 

 

             

             

              /* catch e5 = InterruptedException

                                           say 'Exception caught:' e5 '\n  ' e5.getMessage()

              */

 

 

return

 

 



_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/


-- 
--
__________________________________________________________________________________

Prof. Dr. Rony G. Flatscher
Department Informationsverarbeitung und Prozessmanagement
Institut für Betriebswirtschaftslehre und Wirtschaftsinformatik
WU Wien
Augasse 2-6
A-1090  Wien/Vienna, Austria/Europe

http://www.wu.ac.at (English: http://www.wu.ac.at/start/en)
__________________________________________________________________________________








_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/