SV: Strange error when calling into jruby1.2 from java using javax.scripting?


#1

Just found out myself by more testing that the problem is a bug in the
javax.script implementation for JRuby 1.2.The javax.script standard
specifies that to convert a returned object from ScriptEngine.eval to a
java interface, the Invocable.getInterface method should be called.
Unfortunately, this does not work with JRuby, but the
non-scripting-standard feature of converting the returned eval object
directly to the interface, using a normal java type conversion feature,
works.

/Morten

— Den lør 11/4/09 skrev M C removed_email_address@domain.invalid:

Fra: M C removed_email_address@domain.invalid
Emne: [jruby-user] Strange error when calling into jruby1.2 from java
using javax.scripting ??
Til: removed_email_address@domain.invalid
Dato: lørdag 11. april 2009 23.01

I am trying to get jruby1.2 working with the new scripting api on JDK1.6
(JSR 223) but I have run into a strange error with the returned jruby
object not properly implementing a specified java interface. Can anyone
on the list help?

The error I get is an undefined method error as shown below about a
undefined method which is on an interface that the jruby object has been
asked to implement??? As can be shown further below, the jruby code
does implement the interface so it is a bit strange.

First the error that I get:

javax.script.ScriptException: org.jruby.exceptions.RaiseException:
undefined method `clear’ for #Java::JavaObject:0x2b1d8f11
   at
com.sun.script.jruby.JRubyScriptEngine.invokeImpl(JRubyScriptEngine.java:518)
   at
com.sun.script.jruby.JRubyScriptEngine.access$100(JRubyScriptEngine.java:88)
   at
com.sun.script.jruby.JRubyScriptEngine$1.invoke(JRubyScriptEngine.java:170)
   at $Proxy14.clear(Unknown Source)
   at
com.rv.testsupport.ServiceTestHarness.clear(ServiceTestHarness.java:198)

My jruby 1.2 script is as follows:
"
require ‘java’
module Example
 import java.util.Properties # type of config arg
 import com.rv.Service # implementing interface
 import com.rv.Store # type of store arg
 import com.rv.TaskCoordinator # type of coordinaterCallback arg
Â
 class RubyService
     include com.rv.Service
      Â
     def restore(store, config, coordinaterCallback)
       puts “Restore
called”
     end
    Â
     def clear(config, coordinaterCallback)
       puts “Clear called”
     end
 end
end

return Example::RubyService.new
"

And the java script engine code extract:
"
ScriptEngine engine = scriptManager.getEngineByName(“jruby”)
Invocable invocable = (Invocable)engine;
ScriptContext context = engine.getContext();
BufferedReader scriptReader = …

Object rawResult=engine.eval(scriptReader, context);

com.rv.Service rubyService=invocable.getInterface(rawResult,
com.rv.Service.class);
rubyService.clear(null, null); # This line fails with the exception
shown above!!
"

Finally, here is the java interface with the clear method:
"
package com.rv;
public interface Service
{
   void
restore(Store store, Properties config, TaskCoordinator
coordinaterCallback) throws CoreException, InterruptedException,
IOException;

   void clear(Properties config, TaskCoordinator tc) throws
CoreException, InterruptedException, IOException;
}
"

Any idea why this happens?

P.S: The code extracts here are just extracts from a large piece of SW.

Audi, Fiat, Peugeot, Skoda, Porsche, Toyota, Ford - Kelkoo har brugte
biler til en hver smag! Klik her for at sammenligne priser.

  Trænger du til at se det store billede? Kelkoo giver dig gode 

tilbud på LCD TV! Se her http://dk.yahoo.com/r/pat/lcd