Implementing Java Interfaces

404 views
Skip to first unread message

PatrickCD

unread,
Feb 27, 2012, 10:21:51 AM2/27/12
to mozilla-rhino
Hi,

Firstly, apologies, this is basically a lazy question, the answer to
which I'm sure is provided by existing docs. However, I'm pushed for
time and and suspect that a quick pointer from this group could save
me a lot of research.

I'm working with a Java / Spring Framework application. There is an
existing Spring bean that implements an interface, "DirectoryService".
I would like to re-implement this interface with a rhino script, and
hook it back up as a Spring managed bean.

Is this possible/convenient/sensible? If so, what's a good way to
approach it? Plan A is to produce a java implementation that
delegates to a rhino script, but this would be annoyingly verbose.

Thanks

Jeb Beich

unread,
Feb 27, 2012, 11:08:58 AM2/27/12
to mozill...@googlegroups.com
There's a section of the docs which describes implementing java interfaces:

http://www.mozilla.org/rhino/ScriptingJava.html (half way down)

That's something to get you started at least.
--
Jeb Beich
http://www.red-source.net/jeb

PatrickCD

unread,
Feb 27, 2012, 1:05:19 PM2/27/12
to mozilla-rhino
Thanks for the pointer.

Actually, apologies, my question / the title of this thread was badly
put.

I can see how to implement an interface, and I'm comfortable running
rhino scripts and calling out to Java.

My problem is that I need the application container (Spring) to be
able to instanciate and register an object that implements an
interface. I want to implement the interface via a rhino script.

One idea I had was to compile the script, using the -implements option
as described here :

http://www.mozilla.org/rhino/jsc.html

This looks like a good option for a production environment, but at
least for development I'd like the script to be revaluated
dynamically.


On Feb 27, 4:08 pm, Jeb Beich <jebbe...@gmail.com> wrote:
> There's a section of the docs which describes implementing java interfaces:
>
> http://www.mozilla.org/rhino/ScriptingJava.html(half way down)

Hannes Wallnoefer

unread,
Feb 28, 2012, 4:10:17 AM2/28/12
to mozilla-rhino
So if I understand you correctly you don't want to write any custom
embedding code, but you want your JS files to be compiled on the fly.

This sounds very much like the problem I'm just trying to solve for
using Rhino on Android. My plan is to create a custom build step that
triggers the .js -> .class compilation.

What I'm still unsure is how to specify what classes or interfaces
should be implemented by script files. One way is to have some sort of
external config file, the other would be to have a directive in the
source code similar to the "use strict" of ES5 strict mode:

"extends org.foo.MyClass";
"implements com.bar.MyInterface";

If you're interested in this you can follow its development over in
the jsdroid google group: http://groups.google.com/group/jsdroid

Hannes

On Feb 27, 7:05 pm, PatrickCD <patrick.do...@gmail.com> wrote:
> Thanks for the pointer.
>
> Actually, apologies, my question / the title of this thread was badly
> put.
>
> I can see how to implement an interface, and I'm comfortable running
> rhino scripts and calling out to Java.
>
> My problem is that I need the application container (Spring) to be
> able to instanciate and register an object that implements an
> interface. I want to implement the interface via a rhino script.
>
> One idea I had was to compile the script, using the -implements option
> as described here :
>
> http://www.mozilla.org/rhino/jsc.html
>
> This looks like a good option for a production environment,  but at
> least for development I'd like the script to be revaluated
> dynamically.
>
> On Feb 27, 4:08 pm, Jeb Beich <jebbe...@gmail.com> wrote:
>
>
>
>
>
>
>
> > There's a section of the docs which describes implementing java interfaces:
>
> >http://www.mozilla.org/rhino/ScriptingJava.html(halfway down)

PatrickCD

unread,
Mar 5, 2012, 4:28:47 PM3/5/12
to mozill...@googlegroups.com
Hannes,

Thanks for your reply, and sorry to take so long to answer. 

In the meantime I've come up with a solution which combines some convenience methods for calling rhino functions with a Java implementation of the interface which delegates to js functions manually. The only alternative I could find was to use a dynamic proxy in the style of Spring's support for JRuby or Groovy beans.

Your dynamic compilation approach for Android does sound interesting, and it is addressing a very similar problem. I'll keep an eye on the group you mentioned. Thanks.

By the way, I'm using Rhino for interfacing with json based webservices, so I'm benefiting greatly from the JSON parser which I think you wrote - thank you! 

Patrick
Reply all
Reply to author
Forward
0 new messages