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: Using the ObjectRegistry
Date Thu, 25 Jan 2007 18:09:16 GMT
I continued to drill down on this issue. From reading of the BSF source code, I found that
the "bsf" object that was put into the javascript global context is actually a object of BSFFunctions
object which only contains a subset of BSFManager methods. The BSFFunctions doesn't have a
getObjectRegistry method.
  This explains the reason of the exception I ran into. 
  Now my question is
  (1) Why BSFFunctions doesn't support the getObjectRegistry method?
  (2) How can I retrieve the bean objects from the ObjectRegiestry inside the script code?

Daling Xu <daling_xu@yahoo.com> wrote:
  Hi Rony,

I think I am following the steps you described, here is my code:

final String BEAN_ANME = "myBeanName";
BSFManager manager = new BSFManager();

ObjectRegistry objReg = new ObjectRegistry( manager.getObjectRegistry() );
objReg.register(BEAN_NAME , new MyBeanClass("my String by objReg")); 

manager.setObjectRegistry( objReg );

manager.exec("javascript", "test", 0, 0, "bsf.getObjectRegistry().lookup(\"" + BEAN_NAME +"\")"+
".myMethod();" ); //try case 1

//manager.exec("javascript", "test", 0, 0, "bsf.lookupBean(\"" + BEAN_NAME +"\")"+ ".myMethod();"
); //try case 2

//manager.exec("javascript", "test", 0, 0, BEAN_NAME + ".myMethod();" ); //try case 3

None of the three tries worked. 

For the try case 1, I got this error:
TypeError: Cannot find function getObjectRegistry. although I can directly call 
Although I can run the following code successfully in my java code:
Object obj = manager.getObjectRegistry().lookup( BEAN_NAME );

For the case 2, I got a java.lang.ClassCastException, here is part of my printstacktrace:
at org.apache.bsf.BSFManager.lookupBean(BSFManager.java:736)
at org.apache.bsf.util.BSFFunctions.lookupBean(BSFFunctions.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

For the case 3, I know conceptly it not the right way to do, just use it to confirm my understanding.
As expected, it gave me exception of "myBeanName" is not defined.

Conceptually, I think the case 1 here is the right way to do it, but why it didn't work?



"Rony G. Flatscher" wrote:
bm=new org.apache.bsf.BSFManager();
// ... do whatever you need to do (setup, invoking scripsts, maybe
nothing, whatever)

or=new org.apache.bsf.ObjectRegistry();
// or if you want to use bm's ObjectRegistry object as a parent:
or=new org.apache.bsf.ObjectRegistry(bm.getObjectRegistry());
// do whatever you need to do with "or" (registering objects for the

bm.setObjectRegistry(or); // now set "bm" to the new
ObjectRegistry object "or"
// continue to invoke scripts



Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.

Everyone is raving about the all-new Yahoo! Mail beta.
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message