[英]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库编写了一些包装器( Eurydice , HotBunnies , MessagePack , Rubydoop和Mikka等)。 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-jars和ning-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
然而,这里有一些随意的东西
java_import
into the global scope. java_import
放入全局范围。 It will cause lots of trouble for everyone. 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_import
和include_package
包装在模块中。 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: 本文还解释了以下主题:
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.