简体   繁体   English

在Ruby中包装现有Java库的最佳方法(JRuby)

[英]Best approach to wrap an existing Java library in Ruby (JRuby)

I'm looking to putting a ruby (JRuby) wrapper over a medium sized Java Library and am looking for advice and articles on best practices for everything from packaging to strategy. 我希望将一个ruby(JRuby)包装器放在一个中等大小的Java库上,我正在寻找有关从包装到策略的各种最佳实践的建议和文章。

I found a relatively dated (2009) discussion on this topic here: http://www.ruby-forum.com/topic/188447 . 我在这里找到了一个关于这个主题的相对过时的(2009)讨论: http//www.ruby-forum.com/topic/188447

I'm looking to use the newest version of JRuby. 我想使用最新版本的JRuby。

I've written a few wrappers for Java libraries ( Eurydice , HotBunnies , MessagePack , Rubydoop and Mikka , among others). 我为Java库编写了一些包装器( EurydiceHotBunniesMessagePackRubydoopMikka等)。 Some of them are just JRuby code adapting the Java library, and some of them include Java code that interfaces with the JRuby runtime ( MessagePack is in fact written completely in Java). 其中一些只是适应Java库的JRuby代码,其中一些包括与JRuby运行时接口的Java代码( MessagePack实际上完全用Java编写)。

The thing that I've found is not solved very well is how to ship the JAR file for the Java library. 我发现的事情并没有得到很好的解决,那就是如何为Java库提供JAR文件。 You don't want to include it in the gem, because then you'll end up in JAR hell eventually. 你不想把它包含在gem中,因为那样你最终会最终进入JAR地狱。 Common Java libraries like Netty will be bundled in many gems and eventually you will have gems that bundle incompatible versions. 像Netty这样的常见Java库将捆绑在许多宝石中,最终你会拥有捆绑不兼容版本的宝石。 I've solved this problem by packaing gems that only contain the Java library JAR (see for example scala-library-jars and ning-compress-jars ) and then having my wrapper depend on that gem. 我通过打包只包含Java库JAR的gems来解决这个问题(例如参见scala-library-jarsning-compress-jars ),然后让我的包装器依赖于那个gem。 It's not a particularly scalable solution, but at least it will be more manageable than if you bundle JAR in the wrapper gem. 它不是一个特别可扩展的解决方案,但至少它比在包装器gem中捆绑JAR更易于管理。

Your question isn't very clear on exactly what it is you want to know, I suggest you revise and be more specific. 你的问题不清楚你想要知道什么,我建议你修改并更具体。 However, here are a few random things off the top of my head 然而,这里有一些随意的东西

  • A quick way of making a Java API more accessible to Ruby is to add methods to Java classes and interfaces. 使Ruby更易于访问Java API的一种快速方法是向Java类和接口添加方法。 It's as easy as opening up a Ruby class (but Java interfaces are modules), and can make working with Java objects in Ruby much easier. 它就像打开Ruby类一样简单(但Java接口是模块),并且可以更容易地在Ruby中使用Java对象。 These methods will only be seen by Ruby, they will not be visible to Java code. 这些方法只能由Ruby看到,它们对Java代码不可见。
  • Don't java_import into the global scope. 不要将java_import放入全局范围。 It will cause lots of trouble for everyone. 这会给每个人带来很多麻烦。 Remember that java_import isn't like import in Java where it's only local to the file, it's actually creating an alias in the scope. 请记住, java_import与Java中的import ,它只是文件的本地,它实际上是在作用域中创建别名。 Wrap java_import and include_package in modules. java_importinclude_package包装在模块中。
  • Don't use import -- it is incompatible with Rake. 不要使用import - 它与Rake不兼容。

The wiki page: Scripting Java from JRuby (jruby 1.0+) contains excellent advice and ideas that you may leverage to get your wrapper 维基页面: 来自JRuby的脚本Java(jruby 1.0+)包含了很好的建议和想法,您可以利用这些建议和想法来获取包装器

I think interesting the use of the Module name to scope access to the imported Java class 我觉得有趣的是使用Module名称来扩展对导入的Java类的访问

For Example: create a Ruby Module called JavaLangDemo that includes the classes in the Java package java.lang. 例如:创建一个名为JavaLangDemo的Ruby模块,其中包含Java包java.lang中的类。

 module JavaLangDemo
   include_package "java.lang"
   # alternately, use the #import method
   import "java.lang"
 end

Now you can prefix the desired Java Class name with JavaLangDemo:: to access the included Classes: 现在,您可以使用JavaLangDemo ::为所需的Java类名称添加前缀,以访问包含的类:

 version = JavaLangDemo::System.getProperties["java.runtime.version"]
 => "1.5.0_13-b05-237"
 processors = JavaLangDemo::Runtime.getRuntime.availableProcessors
 => 2

The article also explains the following topics: 本文还解释了以下主题:

  • implementing a Java interface in Ruby 在Ruby中实现Java接口
  • class name ambiguity between Java and Ruby Java和Ruby之间的类名歧义
  • "mixing-in" more than one Java interface to a Jruby modules in JRuby 将一个以上的Java接口“混入”到JRuby中的Jruby模块
  • Exception Handling 异常处理
  • Synchronization of objects for thread safety 用于线程安全的对象同步

and includes a list of usefull links to "Related Articles" for further information about Java integration layer 并包含一系列有关“相关文章”的有用链接,以获取有关Java集成层的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM