JRuby Class with Java Interface in OSGi

Hi folks,

I try to implement a Java Interface in a (J)Ruby Class.
(I read many posts about this here on Nabble - but they were really old
and
didn’t solve my prob)

JAVA INTERFACE:

package org.codingkata.unit.api;

public interface IKataSolution {
String reply();
}
#######################

RUBY CLASS:

class MyKata
include org.codingkata.unit.api.IKataSolution

def reply
return “hello world”
end
end
#######################

It compiles fine to a JAR - but when I load that JAR into an OSGi
container
via Spring DM - Spring throws the exception
Exported service object does not implement the given interface: class
org.codingkata.unit.MyKata is not assignable to interface
org.codingkata.unit.api.IKataSolution

I already successfully ran the same interface in the same OSGI
environment
in Java, Scala and Groovy. So it must be the Ruby class.

Any ideas?

kind regards,
stephanos

View this message in context:
http://old.nabble.com/JRuby-Class-with-Java-Interface-in-OSGi-tp26707257p26707257.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I’m still fighting with this - it makes it impossible for me to run
JRuby on
OSGi - ANY ideas? pls?

stephanos wrote:

public interface IKataSolution {
end
in Java, Scala and Groovy. So it must be the Ruby class.

Any ideas?

kind regards,
stephanos


View this message in context:
http://old.nabble.com/JRuby-Class-with-Java-Interface-in-OSGi-tp26707257p27391456.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Wed, Dec 9, 2009 at 3:21 AM, stephanos [email protected] wrote:

Hi folks,

I try to implement a Java Interface in a (J)Ruby Class.
(I read many posts about this here on Nabble - but they were really old and
didn’t solve my prob)

Sorry for the lack of responses on this one…OSGi scares many of us :slight_smile:

 include org.codingkata.unit.api.IKataSolution
org.codingkata.unit.MyKata is not assignable to interface
org.codingkata.unit.api.IKataSolution

This is a peculiar error. If this were coming from the Ruby class,
your error message would not say “org.codingkata.unit.MyKata” because
the Ruby class does not specify any Java package and would be
generated into our own synthetic namespaces. Are you sure about this
error?

I already successfully ran the same interface in the same OSGI environment
in Java, Scala and Groovy. So it must be the Ruby class.

I would like to help…so let’s figure this out :slight_smile:

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Well, I investigated this a little more - here is what I found by using
a
decompiler.
PS: Meanwhile the interface ‘IKataSolution’ was changed to a class
‘BaseKataSolution’ with still the same error

######### Decompiled Groovy version #########
public class MyKata extends BaseKataSolution
implements GroovyObject
{
public MyKata()
{
MyKata this;
[…]
}

public String reply()
{
for (CallSite[] arrayOfCallSite = $getCallSiteArray(); ; return
((String)ScriptBytecodeAdapter.castToType(“hello world”,
$get$$class$java$lang$String())));
}
[…]
}
####################################

######### Decompiled JRuby version #########
public class MyKata extends AbstractScript
{
private final Class $class =
Class.forName(“org.codingkata.unit.MyKata”);

public MyKata()
{
[…]
}

@JRubyMethod(name=“reply”, frame=true, required=0, optional=0,
rest=-1)
public static IRubyObject method__1$RUBY$reply(MyKata paramMyKata,
ThreadContext paramThreadContext, IRubyObject paramIRubyObject, Block
paramBlock) { Ruby localRuby = paramThreadContext.getRuntime();
IRubyObject
localIRubyObject = localRuby.getNil(); setPosition(paramThreadContext,
5);
return paramMyKata.getString1(localRuby);
}

[…]
}
####################################

######### Spring Beans context ###########
[…]
<osgi:service
ref=“kataSolution”
interface=“org.codingkata.unit.api.BaseKataSolution”/>
[…]
####################################

Obviously the generated/compiled groovy version does compile to a
‘BaseKataSolution’ type, but the JRuby version does not!

The whole JSR 223 is a black box to me - so I can’t figure out how to
fix
this. Spring is looking for a class implementing ‘BaseKataSolution’, no
wonder it doesn’t find one. Maybe my whole design is flawed then, hm :frowning:

Any ideas?

Cheers,
stephanos

Charles Oliver N.-4 wrote:

Sorry for the lack of responses on this one…OSGi scares many of us :slight_smile:

class MyKata
via Spring DM - Spring throws the exception

http://xircles.codehaus.org/manage_email


View this message in context:
http://old.nabble.com/JRuby-Class-with-Java-Interface-in-OSGi-tp26707257p27415656.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I do not know whether this works for you or not - so it is just an idea:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Proxy.html

the proxy can be really generic and just pass through what it gets.
and I am quite sure spring can handle it.

the question is whether you can build such an extra layer in your code.

regards,
Kristian

On Tue, Feb 2, 2010 at 10:49 AM, stephanos [email protected] wrote:

 public MyKata()
 }
 {

####################################
Cheers,

(I read many posts about this here on Nabble - but they were really old
}
#######################
the Ruby class does not specify any Java package and would be

  • Charlie


Kristian Meier + Saumya Sharma + Sanuka Meier
Vadakkethu House,
Edayanmula West PO - 689532,
Pathanamthitta District, Kerala, INDIA

tel: +91 468 2319577

protect your privacy while searching the net: www.ixquick.com

         _=_
       q(-_-)p
        '_) (_`
        /__/  \
     _(<_   / )_
  (__\_\_|_/__)

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Mon, Feb 1, 2010 at 11:19 PM, stephanos [email protected] wrote:

Well, I investigated this a little more - here is what I found by using a
decompiler.
PS: Meanwhile the interface ‘IKataSolution’ was changed to a class
‘BaseKataSolution’ with still the same error

######### Decompiled Groovy version #########
public class MyKata extends BaseKataSolution
 implements GroovyObject

OT: Interesting that they have GroovyObject as their equivalent to our
“IRubyObject” interface and stuff it into Groovy classes that extend
other Java classes. This is the same in the new JRuby “real java
class” support.

######### Decompiled JRuby version #########
public class MyKata extends AbstractScript

The classes produced by JRuby are certainly not “real” classes in
JRuby 1.4. On the contrary, they’re just a “bag of methods” bound
entirely at runtime.

Obviously the generated/compiled groovy version does compile to a
‘BaseKataSolution’ type, but the JRuby version does not!

Yes :slight_smile: This is known, and has been a limitation of JRuby forever. But
there’s hope ahead!!!

JRuby 1.5 has started to introduce to compile (ahead of time) to
normal Java classes. For any case like this where you have to specify
a class before runtime, you will need the JRuby 1.5 feature (or you
could cobble something together with the ruby2java gem).

Here’s how your example would work in JRuby 1.5

RUBY CODE

java_import org.codingkata.unit.api.BaseKataSolution

class MyKata
java_implements BaseKataSolution

java_signature [] => String
def reply
# your reply code here
end
end
######################

Command line session

$ jrubyc --java my_kata.rb
<generates a .class for the script and a .java and .class for the MyKata
class>
##########################

The resulting class file could then be configured in your Spring
config exactly like the Groovy version!

Now the jruby --java support is a work in progress. I need more peopel
to experiment with it and offer suggestions. But all the logic to
generate this “real” Java class ends up being only a few hundred lines
of Ruby code, in lib/ruby/site_ruby/shared/jruby/compiler.rb. It’s
very simple:

  1. Reads in the source of the file(s) being compiled
  2. Processes them for java_ calls (java_import, java_signature, etc),
    classes, and methods
  3. Generates .java source for all classes encountered
  4. Compiles the Ruby scripts to “bag of methods” .class files and uses
    javac to compile the .java sources to .class files

When it’s all done, you have precompiled Ruby code plus all the “real”
Java classes you need!

Maybe you should pull a JRuby 1.5 snapshot and start playing with it?
I really do need help working out the bugs, writing tests, adding
features, and basically making it do what users want it to do.

  • Charlie

Charles Oliver N.-4 wrote:

Sorry for the lack of responses on this one…OSGi scares many of us :slight_smile:

class MyKata
via Spring DM - Spring throws the exception


To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

@Charles
Thank you very much for your detailed message. JRuby 1.5 sounds very
promising. I will checkout the 1.5-SNAPSHOT soon and try it. I’d like to
help, but my knowledge of (J)Ruby is extremely limited, I just want to
offer
it as a programming language for my online exercise site - and it was
the
last of the “big 4” that was missing. So I’m not actually working with
it.

Probably you and Kristian should work collaborate because his maven
plugin
(I use) is the best (and only properly working) solution to compile
jruby
scripts easily.

Charles Oliver N.-4 wrote:

 implements GroovyObject
JRuby 1.4. On the contrary, they’re just a “bag of methods” bound
a class before runtime, you will need the JRuby 1.5 feature (or you
java_signature [] => String
##########################

  1. Reads in the source of the file(s) being compiled
    I really do need help working out the bugs, writing tests, adding

Cheers,

(I read many posts about this here on Nabble - but they were really old
}
#######################
the Ruby class does not specify any Java package and would be

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


View this message in context:
http://old.nabble.com/JRuby-Class-with-Java-Interface-in-OSGi-tp26707257p27432879.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Charlie,

The java_implements keyword works great for interfaces, but curious how
to do the same when extending a class or an abstract class. For example:

java_import org.codingkata.unit.api.BaseKataSolution
class MyKata < BaseKataSolution
java_signature [] => String
def reply
# your reply code here
end
end

but then in Java if a parent library I’m using tries to cast it back to
a BaseKataSolution as follows:

myKata = new MyKata();

BaseKataSolution solution = (BaseKataSolution) myKata;

the cast fails because the generated source code MyKata.java actually
extends RubyObject and NOT BaseKataSolution.

Thanks,
Todd

Charles Nutter wrote:

RUBY CODE

java_import org.codingkata.unit.api.BaseKataSolution

class MyKata
java_implements BaseKataSolution

java_signature [] => String
def reply
# your reply code here
end
end
######################

Command line session

$ jrubyc --java my_kata.rb
<generates a .class for the script and a .java and .class for the MyKata
class>
##########################

@Kristian
Thank you for the hint to the Proxy class. I will have a look at it in
order
to see if it’s feasible to add this extra layer and how to get it
working
with Spring+JRuby. Of course this would be a much better approach to
solve
the dynamic language issue as I doubt every single one will implement a
java
bytecode compiler.

PS: Did you already consider extending your maven plugin to support the
‘real compile feature’ of 1.5 as Charles described it? (That would
rock
:slight_smile:

Cheers,
Stephan

kristian-2 wrote:

Kristian

 implements GroovyObject
((String)ScriptBytecodeAdapter.castToType(“hello world”,
Class.forName(“org.codingkata.unit.MyKata”);
IRubyObject
 […]
this. Spring is looking for a class implementing ‘BaseKataSolution’, no

On Wed, Dec 9, 2009 at 3:21 AM, stephanos [email protected] wrote:

JAVA INTERFACE:

org.codingkata.unit.MyKata is not assignable to interface
environment


Kristian Meier + Saumya Sharma + Sanuka Meier
') (`


View this message in context:
http://old.nabble.com/JRuby-Class-with-Java-Interface-in-OSGi-tp26707257p27432953.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email