PDA

View Full Version : Jython bean factory?


kent37
Aug 25th, 2004, 04:18 PM
I see the thread about Groovy integration. Has anyone worked on a Jython version of this? I would like to be able to configure Jython beans from Spring.

Preferably the JythonFactory would understand Jython introspection so the bean would not have to implement an interface in order to be configurable from Spring.

Rod Johnson
Aug 25th, 2004, 05:24 PM
Currently it assumes that the script can implement one or more interfaces, or be cast to an interface (in the case of Beanshell, where it may not formally implement the interface).

The approach is pretty flexible, so I would like to know more about the capabilities of Jython in this respect. It would be great to be able to support Jython in 1.2 also.

I did have a quick look at Jython material when working on it a few weeks ago, but I haven't used Jython before and didn't have enough time to really get into it.

R

kent37
Aug 25th, 2004, 06:07 PM
Jython classes can implement Java interfaces, so the current approach would work. It is more restrictive than I would like, though.

There are two aspects to this - the interface the Jython bean presents to the BeanFactory (the setters that are configured by Spring) and the interface the bean presents to clients.

In the first case, it would be very nice if the Jython bean did not have to present a Java interface for Spring to be able to set properties on it. This requires that the Spring factory class understands how to use Jython's introspection. This is not hard to do with the Jython api, it is just different from native Java introspection.

In the second case, if the bean is going to be given to Java clients, it will have to implement a Java interface. If it is going to be given to Jython clients, no interface is needed. I'm not sure if Spring needs to care about this at all?

Rod Johnson
Aug 26th, 2004, 02:12 AM
Yes, I don't thing that Spring needs to worry about the second case. The aim of this mechanism is to provide beans actually written in a scripting language so that they look like Java objects to beans that depends on them,

The first case: using Jython introspection, might need to be handled in the core IoC container. However, a relatively simple extensibility mechanism could provide this.

If you are interesting in contributing a JythonScript and JythonScriptFactory implementation (initially not supporting DI for except by Java properties) I'll look at the issues with Jython introspection when I get time.

kent37
Aug 26th, 2004, 06:50 AM
I'll give it a try.

kent37
Aug 26th, 2004, 08:25 PM
I have successfully made JythonFactory and JythonScript classes by cloning GroovyFactory and GroovyScript. I also cloned the Groovy unit tests and accompanying scripts. The tests all pass.

Setter injection is extremely awkward for Jython scripts. To get the propertySingleton and propertyPrototype beans to work, I had to define a Message interface (containing setMessage() and getMessage() methods) and have PropertyHello implement Message. This is because Jython methods are not visible to Java introspection unless they are implementing a Java interface.

So, although the code is working, I don't think it is usable in its present form. It's a bit much to expect that beans implement interfaces for their configuration methods. If the IoC container could be extended to use Jython introspection this would be much more usable.

The classes depend on jython.jar in your classpath, and the Jython Lib directory in the same directory as jython.jar.

Where should I go from here? How can I contribute the code?

Kent

herbyderby
Aug 31st, 2004, 03:36 AM
This may also be of interest:
http://jdocs.com/cglib/2.0.2/api/net/sf/cglib/proxy/InterfaceMaker.html

Chris

Rod Johnson
Sep 16th, 2004, 04:54 AM
Kent

I'd be interested to see the Jython introspection code. Please email it to me. I'll think about whether the core container could be opened up a little to allow non-Java reflection population strategies.

Rgds
Rod

Rod Johnson
Sep 16th, 2004, 08:13 AM
Chris

Thanks--InterfaceMaker is cool. I've just used it to create a config interface with the necessary setters for any bean definitions for BeanShell. The code is generic, and should work with other scripting languages also. With Groovy it's not necessary, as the Groovy class will actually implement the setters, but of course with BeanShell you just get a dynamic proxy.

Rgds
Rod

sethladd
Mar 31st, 2005, 01:20 PM
Just doing a ping on this topic. Would it be possible to get a copy of the Jython integration code? Any chance it could get committed to the sandbox?

Our beanshell code is working great, and would love to compare to the Jython code.

Rod Johnson
Apr 5th, 2005, 11:48 AM
I haven't actually done Jython integration, and I think Kent moved to a pure Jython solution... But it would be great to see it and the work I did already should provide a good basis.

sethladd
Apr 5th, 2005, 02:23 PM
OK, sounds great. The beanshell code is working really well for us so far. We are experimenting with a BeanFactoryPostProcessor that reads in a directory of scripts and puts them all into the app context. This way, we don't have to manually define each script.