Quantcast

How can I call Beanshell methods from Java?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How can I call Beanshell methods from Java?

Sid Kar
How can I call a method scripted in Beanshell script (externally saved file
loaded in Java using Interpreter)?

I wish to define a PDF file structure (using iText) in a beanshell script,
in different methods, such as PDF Title, Table Header, Table Rows, Trailer
and so on, and then call them as needed from my Java bean. This will
basically allow me to define my PDF file structure externally in the
beanshell script, but will allow me to plugin the data from my Java bean
(more secure).

Thank you
Sid
------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I call Beanshell methods from Java?

David Lee
http://www.beanshell.org/manual/quickstart.html#Calling_BeanShell_From_Your_Application




> How can I call a method scripted in Beanshell script (externally saved
> file
> loaded in Java using Interpreter)?
>
> I wish to define a PDF file structure (using iText) in a beanshell script,
> in different methods, such as PDF Title, Table Header, Table Rows, Trailer
> and so on, and then call them as needed from my Java bean. This will
> basically allow me to define my PDF file structure externally in the
> beanshell script, but will allow me to plugin the data from my Java bean
> (more secure).
>
> Thank you
> Sid
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> High Quality Requirements in a Collaborative Environment.
> Download a free trial of Rational Requirements Composer Now!
> http://p.sf.net/sfu/www-ibm-com
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>


------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I call Beanshell methods from Java?

Sid Kar
Thank you David,

If I have  for example 10 methods in my script file, after loading it, can I
call a specific method?

Sid

On Wed, Apr 15, 2009 at 9:47 AM, David A. Lee <[hidden email]> wrote:

>
> http://www.beanshell.org/manual/quickstart.html#Calling_BeanShell_From_Your_Application
>
>
>
>
>  How can I call a method scripted in Beanshell script (externally saved
>> file
>> loaded in Java using Interpreter)?
>>
>> I wish to define a PDF file structure (using iText) in a beanshell script,
>> in different methods, such as PDF Title, Table Header, Table Rows, Trailer
>> and so on, and then call them as needed from my Java bean. This will
>> basically allow me to define my PDF file structure externally in the
>> beanshell script, but will allow me to plugin the data from my Java bean
>> (more secure).
>>
>> Thank you
>> Sid
>>
>> ------------------------------------------------------------------------------
>> This SF.net email is sponsored by:
>> High Quality Requirements in a Collaborative Environment.
>> Download a free trial of Rational Requirements Composer Now!
>> http://p.sf.net/sfu/www-ibm-com
>> _______________________________________________
>> Beanshell-users mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/beanshell-users
>>
>>
>
------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I call Beanshell methods from Java?

Alexey Zinger

You can.  Just like in the example given, you'll need to set up an instance of Interpreter, call the source method to load your bsh file and then manipulate variables and invoke methods:

import bsh.Interpreter;
Interpreter i = new Interpreter();  // Construct an interpreter
i.source("somefile.bsh");
i.set("foo", 5);                    // Set variables
i.set("date", new Date() );
Object result = i.eval("doSomething(foo, date)"); // invoke doSomething


Alexey
2001 Honda CBR600F4i (CCS)
1992 Kawasaki EX500
http://azinger.blogspot.com
http://bsheet.sourceforge.net
http://wcollage.sourceforge.net



--- On Wed, 4/15/09, Sid Kar <[hidden email]> wrote:

> From: Sid Kar <[hidden email]>
> Subject: Re: [Beanshell-users] How can I call Beanshell methods from Java?
> To: "David A. Lee" <[hidden email]>
> Cc: [hidden email]
> Date: Wednesday, April 15, 2009, 9:51 AM
> Thank you David,
>
> If I have  for example 10 methods in my script file, after
> loading it, can I
> call a specific method?
>
> Sid
>
> On Wed, Apr 15, 2009 at 9:47 AM, David A. Lee
> <[hidden email]> wrote:
>
> >
> >
> http://www.beanshell.org/manual/quickstart.html#Calling_BeanShell_From_Your_Application
> >
> >
> >
> >
> >  How can I call a method scripted in Beanshell script
> (externally saved
> >> file
> >> loaded in Java using Interpreter)?
> >>
> >> I wish to define a PDF file structure (using
> iText) in a beanshell script,
> >> in different methods, such as PDF Title, Table
> Header, Table Rows, Trailer
> >> and so on, and then call them as needed from my
> Java bean. This will
> >> basically allow me to define my PDF file structure
> externally in the
> >> beanshell script, but will allow me to plugin the
> data from my Java bean
> >> (more secure).
> >>
> >> Thank you
> >> Sid
> >>
> >>
> ------------------------------------------------------------------------------
> >> This SF.net email is sponsored by:
> >> High Quality Requirements in a Collaborative
> Environment.
> >> Download a free trial of Rational Requirements
> Composer Now!
> >> http://p.sf.net/sfu/www-ibm-com
> >> _______________________________________________
> >> Beanshell-users mailing list
> >> [hidden email]
> >>
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
> >>
> >>
> >
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> High Quality Requirements in a Collaborative Environment.
> Download a free trial of Rational Requirements Composer
> Now!
> http://p.sf.net/sfu/www-ibm-com
> _______________________________________________
> Beanshell-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/beanshell-users


     

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I call Beanshell methods from Java?

Sid Kar
That worked!

Thank you very much!
Sid

On Wed, Apr 15, 2009 at 10:10 AM, Alexey Zinger <[hidden email]>wrote:

>
> You can.  Just like in the example given, you'll need to set up an instance
> of Interpreter, call the source method to load your bsh file and then
> manipulate variables and invoke methods:
>
> import bsh.Interpreter;
> Interpreter i = new Interpreter();  // Construct an interpreter
> i.source("somefile.bsh");
> i.set("foo", 5);                    // Set variables
> i.set("date", new Date() );
> Object result = i.eval("doSomething(foo, date)"); // invoke doSomething
>
>
> Alexey
> 2001 Honda CBR600F4i (CCS)
> 1992 Kawasaki EX500
> http://azinger.blogspot.com
> http://bsheet.sourceforge.net
> http://wcollage.sourceforge.net
>
>
>
> --- On Wed, 4/15/09, Sid Kar <[hidden email]> wrote:
>
> > From: Sid Kar <[hidden email]>
> > Subject: Re: [Beanshell-users] How can I call Beanshell methods from
> Java?
> > To: "David A. Lee" <[hidden email]>
> > Cc: [hidden email]
> > Date: Wednesday, April 15, 2009, 9:51 AM
> > Thank you David,
> >
> > If I have  for example 10 methods in my script file, after
> > loading it, can I
> > call a specific method?
> >
> > Sid
> >
> > On Wed, Apr 15, 2009 at 9:47 AM, David A. Lee
> > <[hidden email]> wrote:
> >
> > >
> > >
> >
> http://www.beanshell.org/manual/quickstart.html#Calling_BeanShell_From_Your_Application
> > >
> > >
> > >
> > >
> > >  How can I call a method scripted in Beanshell script
> > (externally saved
> > >> file
> > >> loaded in Java using Interpreter)?
> > >>
> > >> I wish to define a PDF file structure (using
> > iText) in a beanshell script,
> > >> in different methods, such as PDF Title, Table
> > Header, Table Rows, Trailer
> > >> and so on, and then call them as needed from my
> > Java bean. This will
> > >> basically allow me to define my PDF file structure
> > externally in the
> > >> beanshell script, but will allow me to plugin the
> > data from my Java bean
> > >> (more secure).
> > >>
> > >> Thank you
> > >> Sid
> > >>
> > >>
> >
> ------------------------------------------------------------------------------
> > >> This SF.net email is sponsored by:
> > >> High Quality Requirements in a Collaborative
> > Environment.
> > >> Download a free trial of Rational Requirements
> > Composer Now!
> > >> http://p.sf.net/sfu/www-ibm-com
> > >> _______________________________________________
> > >> Beanshell-users mailing list
> > >> [hidden email]
> > >>
> > https://lists.sourceforge.net/lists/listinfo/beanshell-users
> > >>
> > >>
> > >
> >
> ------------------------------------------------------------------------------
> > This SF.net email is sponsored by:
> > High Quality Requirements in a Collaborative Environment.
> > Download a free trial of Rational Requirements Composer
> > Now!
> > http://p.sf.net/sfu/www-ibm-com
> > _______________________________________________
> > Beanshell-users mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/beanshell-users
>
>
>
>
------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I call Beanshell methods from Java?

alexander_a_sokolov
In reply to this post by Sid Kar

If you need to call BeanShell from Java
please look to beanshell manual for example
http://www.beanshell.org/manual/bshmanual.html#Calling_BeanShell_From_Java

First, you need to create bsh interpteter in your java code:

Interpreter interpreter = new Interpreter();

Then you need to execute your script:

interpreter.source("your_script_name.bsh");

And at the end, You can create any java objects inside beanshell scripts and
try to get back to java such objects using:

Your object_type your_object = interpreter.get("variable_name");
 

Sid Kar wrote
How can I call a method scripted in Beanshell script (externally saved file
loaded in Java using Interpreter)?

I wish to define a PDF file structure (using iText) in a beanshell script,
in different methods, such as PDF Title, Table Header, Table Rows, Trailer
and so on, and then call them as needed from my Java bean. This will
basically allow me to define my PDF file structure externally in the
beanshell script, but will allow me to plugin the data from my Java bean
(more secure).

Thank you
Sid
------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Beanshell-users mailing list
Beanshell-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How can I call Beanshell methods from Java?

dbekkerman
This post was updated on .
In reply to this post by Alexey Zinger
Hey Alexey,

I read the forum and online references but something does not square with the execution, hence I wonder if you may help me

My goal - pass the Java code for the execution on the fly. Hence I need a method that accepts Java code line by line and executes it. Lets call it executeMethod that accepts String representing a line of Java code to be executed. I am making this up so please forgive for typos. Say you call the method withing your main as executeMethod("Integer.parseInt(12)")

Can you add the body to the method that would force Java interpretation/execution of the passed to the method String

public void executeMethod(String s) {


}

Appreciate your help,


- Dmitriy
Alexey Zinger wrote
You can.  Just like in the example given, you'll need to set up an instance of Interpreter, call the source method to load your bsh file and then manipulate variables and invoke methods:

import bsh.Interpreter;
Interpreter i = new Interpreter();  // Construct an interpreter
i.source("somefile.bsh");
i.set("foo", 5);                    // Set variables
i.set("date", new Date() );
Object result = i.eval("doSomething(foo, date)"); // invoke doSomething


Alexey
2001 Honda CBR600F4i (CCS)
1992 Kawasaki EX500
http://azinger.blogspot.com
http://bsheet.sourceforge.net
http://wcollage.sourceforge.net



--- On Wed, 4/15/09, Sid Kar <eclipsetracker@gmail.com> wrote:

> From: Sid Kar <eclipsetracker@gmail.com>
> Subject: Re: [Beanshell-users] How can I call Beanshell methods from Java?
> To: "David A. Lee" <dlee@calldei.com>
> Cc: beanshell-users@lists.sourceforge.net
> Date: Wednesday, April 15, 2009, 9:51 AM
> Thank you David,
>
> If I have  for example 10 methods in my script file, after
> loading it, can I
> call a specific method?
>
> Sid
>
> On Wed, Apr 15, 2009 at 9:47 AM, David A. Lee
> <dlee@calldei.com> wrote:
>
> >
> >
> http://www.beanshell.org/manual/quickstart.html#Calling_BeanShell_From_Your_Application
> >
> >
> >
> >
> >  How can I call a method scripted in Beanshell script
> (externally saved
> >> file
> >> loaded in Java using Interpreter)?
> >>
> >> I wish to define a PDF file structure (using
> iText) in a beanshell script,
> >> in different methods, such as PDF Title, Table
> Header, Table Rows, Trailer
> >> and so on, and then call them as needed from my
> Java bean. This will
> >> basically allow me to define my PDF file structure
> externally in the
> >> beanshell script, but will allow me to plugin the
> data from my Java bean
> >> (more secure).
> >>
> >> Thank you
> >> Sid
> >>
> >>
> ------------------------------------------------------------------------------
> >> This SF.net email is sponsored by:
> >> High Quality Requirements in a Collaborative
> Environment.
> >> Download a free trial of Rational Requirements
> Composer Now!
> >> http://p.sf.net/sfu/www-ibm-com
> >> _______________________________________________
> >> Beanshell-users mailing list
> >> Beanshell-users@lists.sourceforge.net
> >>
> https://lists.sourceforge.net/lists/listinfo/beanshell-users
> >>
> >>
> >
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> High Quality Requirements in a Collaborative Environment.
> Download a free trial of Rational Requirements Composer
> Now!
> http://p.sf.net/sfu/www-ibm-com
> _______________________________________________
> Beanshell-users mailing list
> Beanshell-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/beanshell-users


     

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Beanshell-users mailing list
Beanshell-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Loading...