You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bsf-user@jakarta.apache.org by Ludovic Drolez <ld...@debian.org> on 2007/04/14 21:51:44 UTC

BSF + Jython: intercepting the access to the variables ?

Hi !

I'm currently trying to add BSF+Jython support in the ZK framework
(www.zkoss.org). To make this I need to have a method called, each
time the scripting language tried to get the value of a variable.

For example the Groovy support in ZK is done this way:

=======================================
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
...
public void init(Page owner, String zslang) {
  super.init(owner, zslang);

  _global = new Binding(new Variables());
  _ip = new GroovyShell(_global);
}
//An inner class of GroovyInterpreter
private class Variables extends HashMap {
  public Object get(Object key) {
    Object val = super.get(key);
    if (val != null || containsKey(key) || !(key instanceof String))
      return val;
    return getFromNamespace((String)key); //provided by GenericInterpreter
  }
}
=======================================
(More can be read here:
http://www.zkoss.org/smalltalks/addinterpreter/addinterpreter.dsp)

So with groovy, you can register an 'object container' to intercept
accesses. If the object is already set in the scripting language, then
return it, but if it is not set, it tries to return a ZK object with the
same name (using the ZK method getFromNamespace()).

I do not see how it could be done with BSF. I cannot register new BSF Beans,
because, new ZK objects can be created at any time, so I do not know their
names to register them. I need something dynamic, something which is called
just before the scripting language tried to use an object.

Did I miss something in BSF, or do I need to use the Jython API instead ?

Best regards,

-- 
Ludovic Drolez.

http://zaurus.palmopensource.com    - The Zaurus Open Source Portal
http://www.drolez.com      - Personal site - Linux and PalmOS stuff

---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-user-help@jakarta.apache.org


How to call function in another .js file?

Posted by Daling Xu <da...@yahoo.com>.
Hi,
   
  I am using BSF to execute a javascript file from my java application.
   
  Because my logic is complicate, I would like to separate them into functions and put functions into different .js files. 
   
  I started to execute my main javascript file like:
   
  BSFManager bsfManager = new BSFManager();
  bsfManager.exec( "javascript",
  "" , 0, 0, 
  IOUtils.getStringFromReader(new FileReader("main.js") ));
   
  From the main.js, how can I call a function myfunc1() which is declared in anotherFile.js?
   
  Thanks
   
  Linda

       
---------------------------------
Ahhh...imagining that irresistible "new car" smell?
 Check outnew cars at Yahoo! Autos.

Re: BSF + Jython: intercepting the access to the variables ?

Posted by Vladimir Vivien <vm...@gmail.com>.
I have a project that does similar things as what you are looking for.
However, my objects implements an interface that has a public Object
get(Object key).  Therefore, I can intersect the get() call in objects that
implements that interface.

BSF really has no need to have such mechanism since BSF itself does not
parse, execute, or evaluate scripts.  When objects are registered with BSF,
they are just passed to the script engine that is using BSF.

My experience with BeanShell, JXEL, Groovy, and others is that they all have
a mechanism (as you are looking for)  where they use a HashMap to store
values that are evaluated using associative array notations (i.e.
hash.keyor hash[key]).

vmatters

On 4/15/07, Ludovic Drolez <ld...@debian.org> wrote:
>
> Vladimir Vivien wrote:
> > Ludovic,
> > Lookup documentation for the BSFManager.  Beanshell manager will allow
> you
> > to register objects that are passed to the scripting language (as long
> as
>
> Vladimir,
>
> As I said before, I cannot use declareBean, because java objects are
> created
> dynamically. In the Groovy example below, there is a mecanism to intercept
> the get() method of the scripting language objects. But I cannot find one
> in BSF (e.g. instead of throwing a "name error" in jython, my custom get()
> method should be called to try to return an object external to the
> interpreter).
>
> Any ideas ?
>
>   Ludovic.
>
>
> > On 4/14/07, Ludovic Drolez <ld...@debian.org> wrote:
> >
> >>
> >> Hi !
> >>
> >> I'm currently trying to add BSF+Jython support in the ZK framework
> >> (www.zkoss.org). To make this I need to have a method called, each
> >> time the scripting language tried to get the value of a variable.
> >>
> >> For example the Groovy support in ZK is done this way:
> >>
> >> =======================================
> >> import groovy.lang.Binding;
> >> import groovy.lang.GroovyShell;
> >> ...
> >> public void init(Page owner, String zslang) {
> >>   super.init(owner, zslang);
> >>
> >>   _global = new Binding(new Variables());
> >>   _ip = new GroovyShell(_global);
> >> }
> >> //An inner class of GroovyInterpreter
> >> private class Variables extends HashMap {
> >>   public Object get(Object key) {
> >>     Object val = super.get(key);
> >>     if (val != null || containsKey(key) || !(key instanceof String))
> >>       return val;
> >>     return getFromNamespace((String)key); //provided by
> >> GenericInterpreter
> >>   }
> >> }
> >> =======================================
> >> (More can be read here:
> >> http://www.zkoss.org/smalltalks/addinterpreter/addinterpreter.dsp)
> >>
> >> So with groovy, you can register an 'object container' to intercept
> >> accesses. If the object is already set in the scripting language, then
> >> return it, but if it is not set, it tries to return a ZK object with
> the
> >> same name (using the ZK method getFromNamespace()).
> >>
> >> I do not see how it could be done with BSF. I cannot register new BSF
> >> Beans,
> >> because, new ZK objects can be created at any time, so I do not know
> >> their
> >> names to register them. I need something dynamic, something which is
> >> called
> >> just before the scripting language tried to use an object.
> >>
> >> Did I miss something in BSF, or do I need to use the Jython API instead
> ?
> >>
> >> Best regards,
> >>
> >> --
> >> Ludovic Drolez.
> >>
> >> http://zaurus.palmopensource.com    - The Zaurus Open Source Portal
> >> http://www.drolez.com      - Personal site - Linux and PalmOS stuff
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
> >> For additional commands, e-mail: bsf-user-help@jakarta.apache.org
> >>
> >>
> >
>
> --
> Ludovic Drolez.
>
> http://zaurus.palmopensource.com    - The Zaurus Open Source Portal
> http://www.drolez.com      - Personal site - Linux and PalmOS stuff
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bsf-user-help@jakarta.apache.org
>
>

Re: BSF + Jython: intercepting the access to the variables ?

Posted by Ludovic Drolez <ld...@debian.org>.
Vladimir Vivien wrote:
> Ludovic,
> Lookup documentation for the BSFManager.  Beanshell manager will allow you
> to register objects that are passed to the scripting language (as long as

Vladimir,

As I said before, I cannot use declareBean, because java objects are created
dynamically. In the Groovy example below, there is a mecanism to intercept
the get() method of the scripting language objects. But I cannot find one
in BSF (e.g. instead of throwing a "name error" in jython, my custom get()
method should be called to try to return an object external to the interpreter).

Any ideas ?

  Ludovic.


> On 4/14/07, Ludovic Drolez <ld...@debian.org> wrote:
> 
>>
>> Hi !
>>
>> I'm currently trying to add BSF+Jython support in the ZK framework
>> (www.zkoss.org). To make this I need to have a method called, each
>> time the scripting language tried to get the value of a variable.
>>
>> For example the Groovy support in ZK is done this way:
>>
>> =======================================
>> import groovy.lang.Binding;
>> import groovy.lang.GroovyShell;
>> ...
>> public void init(Page owner, String zslang) {
>>   super.init(owner, zslang);
>>
>>   _global = new Binding(new Variables());
>>   _ip = new GroovyShell(_global);
>> }
>> //An inner class of GroovyInterpreter
>> private class Variables extends HashMap {
>>   public Object get(Object key) {
>>     Object val = super.get(key);
>>     if (val != null || containsKey(key) || !(key instanceof String))
>>       return val;
>>     return getFromNamespace((String)key); //provided by
>> GenericInterpreter
>>   }
>> }
>> =======================================
>> (More can be read here:
>> http://www.zkoss.org/smalltalks/addinterpreter/addinterpreter.dsp)
>>
>> So with groovy, you can register an 'object container' to intercept
>> accesses. If the object is already set in the scripting language, then
>> return it, but if it is not set, it tries to return a ZK object with the
>> same name (using the ZK method getFromNamespace()).
>>
>> I do not see how it could be done with BSF. I cannot register new BSF
>> Beans,
>> because, new ZK objects can be created at any time, so I do not know
>> their
>> names to register them. I need something dynamic, something which is
>> called
>> just before the scripting language tried to use an object.
>>
>> Did I miss something in BSF, or do I need to use the Jython API instead ?
>>
>> Best regards,
>>
>> -- 
>> Ludovic Drolez.
>>
>> http://zaurus.palmopensource.com    - The Zaurus Open Source Portal
>> http://www.drolez.com      - Personal site - Linux and PalmOS stuff
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: bsf-user-help@jakarta.apache.org
>>
>>
> 

-- 
Ludovic Drolez.

http://zaurus.palmopensource.com    - The Zaurus Open Source Portal
http://www.drolez.com      - Personal site - Linux and PalmOS stuff

---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-user-help@jakarta.apache.org


Re: BSF + Jython: intercepting the access to the variables ?

Posted by Vladimir Vivien <vm...@gmail.com>.
Ludovic,
Lookup documentation for the BSFManager.  Beanshell manager will allow you
to register objects that are passed to the scripting language (as long as
the language is using BSF).  For instance:

BSFManager mgr = new BSFManager();
mgr.declareBean(name, val, val.class);

This code will register an object val with a key name in the BSF object
registry.  If your dynamic language supports hash (i.e. "map.Key" or
"map[key]") such as your groovy example you should be able to access your
values at runtime.

Your issue may be with Jython.  Make sure that Jython supports Map (hash)
variable access.

vmatters.

On 4/14/07, Ludovic Drolez <ld...@debian.org> wrote:
>
> Hi !
>
> I'm currently trying to add BSF+Jython support in the ZK framework
> (www.zkoss.org). To make this I need to have a method called, each
> time the scripting language tried to get the value of a variable.
>
> For example the Groovy support in ZK is done this way:
>
> =======================================
> import groovy.lang.Binding;
> import groovy.lang.GroovyShell;
> ...
> public void init(Page owner, String zslang) {
>   super.init(owner, zslang);
>
>   _global = new Binding(new Variables());
>   _ip = new GroovyShell(_global);
> }
> //An inner class of GroovyInterpreter
> private class Variables extends HashMap {
>   public Object get(Object key) {
>     Object val = super.get(key);
>     if (val != null || containsKey(key) || !(key instanceof String))
>       return val;
>     return getFromNamespace((String)key); //provided by GenericInterpreter
>   }
> }
> =======================================
> (More can be read here:
> http://www.zkoss.org/smalltalks/addinterpreter/addinterpreter.dsp)
>
> So with groovy, you can register an 'object container' to intercept
> accesses. If the object is already set in the scripting language, then
> return it, but if it is not set, it tries to return a ZK object with the
> same name (using the ZK method getFromNamespace()).
>
> I do not see how it could be done with BSF. I cannot register new BSF
> Beans,
> because, new ZK objects can be created at any time, so I do not know their
> names to register them. I need something dynamic, something which is
> called
> just before the scripting language tried to use an object.
>
> Did I miss something in BSF, or do I need to use the Jython API instead ?
>
> Best regards,
>
> --
> Ludovic Drolez.
>
> http://zaurus.palmopensource.com    - The Zaurus Open Source Portal
> http://www.drolez.com      - Personal site - Linux and PalmOS stuff
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bsf-user-help@jakarta.apache.org
>
>