jakarta-bsf-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daling Xu <daling...@yahoo.com>
Subject Re: overhead of new BSFManager()
Date Thu, 25 Jan 2007 15:39:52 GMT
I tried to use both ObjectRegistry and manager.registerBean, neither of them works. 
   
  I am using JDK1.4.1,  BSF2.4 and Rhino 1.6R5.
   
  First here is my bean class defined in java:
  public class MyBeanClazz {
      private String str;
      public MyBeanClazz( String val ) {
         str = val;
      }
   
      public void myMethod() {
         System.out.println( str );
      }
  }
   
  Then I need to create an instance of MyBeanClazz and pass it to the javascript code, so
I can call myMethod from javascript code.
   
  I first tried:
   
  BSFManager manager = new BSFManager();
  manager.registerBean( "myBeanName", new MyBeanClazz( "myString")  );
  manager.exec( "javascript" , "test" , 0, 0, "myBeanName.myMethod();");
   
  This gives me a 
  SEVERE: Exception :
  java.security.PrivilegedActionException: org.apache.bsf.BSFException: JavaScript Error:
Internal Error: org.mozilla.javascript.EcmaError: ReferenceError: "myBeanName" is not defined.
   
  Then I tried:
  BSFManager manager = new BSFManager();
  ObjectRegistery objReg = manager.getObjectRegistry();
  objReg.register( "myBeanName", new MyBeanClazz("string1"));
  manager.exec( "javascript", "test", 0, 0, "myBeanName.myMethod()" );
   
  This gives me the same exception. 
   
  I finally tried this one:
   
  BSFManager manager = new BSFManager();
  manager.declareBean( "myBeanName", new MyBeanClazz("string2"), MyBeanClazz.class);
  manager.exec( "javascript", "test", 0, 0, "myBeanName.myMethod()" );
   
  This one run successfully and printed the "string2" in the console.
  
But why the first two cases didn't work? Especially the case 1, according to the api documentation,
it should work? Anything I did wrongly here? ( I double checked, it's not because any typo
in the bean name string  when I registered it).  
   
  And base on my understanding to the api doc, the case one is conceptly the right way to
do it, right?
   
  Thanks
   
  Linda
   

"Rony G. Flatscher" <Rony.Flatscher@wu-wien.ac.at> wrote:
  
> In the JDK 6's script supporting API, I can bind beans into a specific ScriptContext,
then evaluate the script code insed that context. By doing this, I can bind different bean
objects for different scripts using the same name. Can I do the similar thing using BSF?
> 
Yes, this should be possible with the ObjectRegistry, something like:

* create an ObjectRegistry object, register the beans that you want
to share among different scripts with it,
o while creating an ObjectRegistry you could also supply an
existing ObjectRegistry possessing the registered objects to
the constructor. That passed ObjectRegistry object will
serve as a "parent" which is looked up, if the new
ObjectRegistry does not have the sought entry, serving as a
fall-back registry.
* set the BSFManager's ObjectRegistry using setObjectRegistry().

Also, you could get the ObjectRegistry object from a BSFManager with
getObjectRegistry() and supply it to a different instance of BSFManager,
in the case that scripts created entries there which should be referred
to in other scripts.

Just try it out.

HTH,

---rony



 
---------------------------------
Don't get soaked.  Take a quick peak at the forecast 
 with theYahoo! Search weather shortcut.
Mime
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message