Re: Scriptable objects with BeanShell

2003-11-24 Thread Bertrand Delacretaz
Le Lundi, 24 nov 2003, à 22:02 Europe/Zurich, Upayavira a écrit :
...Then I've effectively created a class. But it seems a bit around 
the houses to me...
ok, see your point now, thanks.

Don't close the door on scripting stuff too soon though, there's a lot 
happening with Groovy [1] (not to mention LOAF;-) these days, even 
though it is very new it looks very interesting for writing test cases.

-Bertrand

[1] http://groovy.codehaus.org



Re: Scriptable objects with BeanShell

2003-11-24 Thread Upayavira
Bertrand Delacretaz wrote:

Le Lundi, 24 nov 2003, à 21:05 Europe/Zurich, Upayavira a écrit :

...From looking at the BeanShell docs, the Java is definitely 
'scripted' java, and lacks Java's object orientedness...


But what about this example from the BeanShell docs:

buttonHandler = new ActionListener() {
actionPerformed( event ) {
print(event);
}
};
button = new JButton();
button.addActionListener( buttonHandler );
frame(button);
A new behaviour for ActionListener is indeed created in a script.
Isn't this a good example of object orientedness, do you have a 
counterexample?
Yup. But I can't code a class called User, and then invoke it with 'new 
User()', I have to do it inline.

I guess I could do:

newUser() {
   user = new Object() {
   setUsername(username) {
   this.username = username;
}
validatePassword(password) {
...etc
}
}
And then do:
myUser = newUser();
myUser.setUsername("upayavira");
if (myUser.validatePassword("xyz")) {
 cocoon.sendPage(".");
} else {
  ...
}
Then I've effectively created a class. But it seems a bit around the 
houses to me.

Regards, Upayavira



Re: Scriptable objects with BeanShell

2003-11-24 Thread Bertrand Delacretaz
Le Lundi, 24 nov 2003, à 21:05 Europe/Zurich, Upayavira a écrit :
...From looking at the BeanShell docs, the Java is definitely 
'scripted' java, and lacks Java's object orientedness...
But what about this example from the BeanShell docs:

buttonHandler = new ActionListener() {
actionPerformed( event ) {
print(event);
}
};
button = new JButton();
button.addActionListener( buttonHandler );
frame(button);
A new behaviour for ActionListener is indeed created in a script.
Isn't this a good example of object orientedness, do you have a 
counterexample?

-Bertrand



Re: Scriptable objects with BeanShell

2003-11-24 Thread Upayavira
Bertrand Delacretaz wrote:

Le Vendredi, 21 nov 2003, à 22:20 Europe/Zurich, Upayavira a écrit :

Bertrand Delacretaz wrote:

...This means that BeanShell (dunno if BSF does this too) scripts  
could be called from flow *and* interpreted *and* implement Avalon 
or  Cocoon interfaces. I haven't thought about all implications but 
it  might be a nice intermediate solution between limited 
interpreted  stuff and full-blown java coding.


Can you say more about how you do this? Give some examples? I would  
find that very useful.


Still in wildthoughts mode here, I haven't tested this:

1) A BeanShellWrapper Component initializes BeanShell and provides 
some  useful beans that scripts can access (similar to what the 
current  ScriptGenerator does).

Maybe BSF instead of BeanShell (as BSF can use BeanShell as a script  
engine anyway), but I don't know if BSF does all this.

2) BeanShellWrapper provides methods for flowscripts to retrieve  
scriptable objects, something like:

  IMyBizObject =  
(IMyBizObject)BSFWrapper.createObject(IMyBizObject.class,"MyBusinessScri 
pt.bsl");

At this point the BeanShell "dynamic adapter" feature [1] [2] is used  
to provide an object of the specified interface, something like:

  // in BeanShellWrapper:
  public Object createObject(Class clazz,String scriptFilename) {
 // first load scriptFilename in the (or a new) BeanShell  
Interpreter
 ...
// then cast the interpreter as required, it will call the  
appropriate methods in
// the script if they exist and complain if not
return interpreter.getInterface(clazz);
  }

See what I mean? This is only a rough sketch, there are certainly a 
lot  of "details" to consider. But it looks like all the essential 
elements  are here.

I'm also not sure if one of the BeanShell licenses [3] are ok for  
distributing with Cocoon, but mocks could be used if it is not the case.

Hope this helps,
-Bertrand
[1]  http://www.beanshell.org/javadoc/bsh/ 
Interpreter.html#getInterface(java.lang.Class)

[2]  http://www.beanshell.org/manual/ 
bshmanual.html#%22Dummy%22_Adapters_and_Incomplete_Interfaces

[3] http://www.beanshell.org/license.html

From looking at the BeanShell docs, the Java is definitely 'scripted' 
java, and lacks Java's object orientedness. Therefore, whilst you're 
coding in more or less Java, I can't see a great advantage of using 
BeanShell over using Rhino. They're both scripted, and thus not able to 
create clear interfaces. (In BeanShell you can implement interfaces, but 
to do so you have to use 'anonymous classes', which IMO doesn't produce 
attractive or easy to read code.

Therefore, I think, for my case, I'm just going to have to use Java for 
my objects, to use Unit Tests to test them (rather than bothering to 
test them in a servlet environment), and hope for the day when someone 
gets Christopher's CompilingClassLoader working so that I can easily 
debug Java code from within a servlet.

Regards, Upayavira