使用 OSGi 将 JMenuItems 附加到 GUI
我已经创建了一个应用程序,现在我希望它准备好一个插件。我决定使用 Apache Felix,在阅读教程后,我不确定这是否是我想要的选择。
我想更改我的应用程序,以便它从 /plugins 文件夹开始加载 Bundle,并将每个 Bundle 作为 JMenuItem 添加到 JMenu。我本来可以使用 ClassLoader 来完成它,但我认为 OSGi 正是我想要的,但后来我遇到了一些问题:
OSGi 有一个命令行,但我需要它在我的应用程序中实现,无需任何用户交互,在代码中硬编码其默认行为。
Bundle 的行为(从
org.osgi.framework.BundleActivator
继承的start()
和stop()
方法) Bundle 在开始时调用,但我实际上希望我的应用程序使用 Bundle 执行某些操作,而不是使用 Bundle 执行应用程序执行某些操作。
那么你能告诉我,是否可以使用 OSGi(等 Apache Felix)来实现它,或者我应该使用 ClassLoader 来实现它,然后将 .jar 插件的主类显式转换为 JMenuItem 并将其添加到我的 JMenu 中(这更糟糕) ,因为它不支持应用程序的动态更改,并且不给我学习OSGi的机会)。
编辑:我正在考虑这样的事情: http://karussell.wordpress.com/2009/09/16/plugable-swing-a-hello-world-osgi-example/,但我找不到任何教程。
I have created an application and now I want it to make a plugin ready. I have decided to use Apache Felix and after reading tutorial, I am not sure, whether it was a choice that I wanted to.
I want to change my application, so that it loads at the beginning Bundles from /plugins folder and adds every Bundle as a JMenuItem to JMenu. I could have done it using ClassLoader, but I thought OSGi was exactly what I wanted, but then I encountered some problems:
there is a command line for OSGi, but I need it to be implemented in my application, without any user interaction, hard code its default behaviour in code.
there is behaviour of a Bundle (
start()
andstop()
methods inherited fromorg.osgi.framework.BundleActivator
) that Bundle invokes at the beginning, but I actually want my application, to do something with a Bundle, not a Bundle to do something with an application.
So could you tell me, whether it is possible to make it using OSGi (etc. Apache Felix) or should I implement it using ClassLoader and then explicitly convert a .jar plugin's Main Class to JMenuItem and add it to my JMenu (it is worse, because it doesn't support dynamic change of application and doesn't give me a chance to learn OSGi).
EDIT: I am thinking about sth like: http://karussell.wordpress.com/2009/09/16/plugable-swing-a-hello-world-osgi-example/ , but I cannot find any tutorial.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要为视图中存在的每个包创建一个菜单项?这确实没有意义。
您/可以/通过创建 GUI 应用程序来完成此操作,然后在 start() 方法中保留上下文,然后使用它来查询系统中所有包的列表。您可能希望在显示菜单时按需执行此操作(以便您看到当前列表)。
然而,你的想法确实是错误的。您要做的就是寻找/services/。当服务进入时,您可以使用该服务来填充菜单项。这样,每个捆绑包就可以有多个服务,并且这些服务可以进行某种形式的交互。
Neil Bartlett 和我本人就基于 Swing 的应用程序的这种方法做了一次演讲(尽管使用注册的 Action 对象)。演示和演示文稿仍可从此位置获取:
http://www.eclipsezone.com/files/jsig /
请注意,这是在 OSGi v4 时完成的,因此您可能会发现安装因导入框架版本不匹配而失败。如果是这样,请破解文件并确保它没有显示
Import-Package: org.osgi.framework;version="[1.3.0,1.4.0)"
或类似的内容 - 获取去掉版本号,今天应该仍然可以正常运行。Why would you create a menu item for every bundle that exists in the view? That doesn't really make sense.
You /could/ do it by creating your GUI app, and then in the start() method, persist the context and then use that to query a list of all bundles in the system. You would probably want to do that on demand when your menu was shown (so you see a current list).
However, you're really thinking in the wrong direction. What you want to do is look for /services/. When a service comes in, you use that service to populate your menu item. That way, you can have multiple services per bundle and the services can have some form of interaction.
Neil Bartlett and myself did a talk on exactly this approach for a Swing-based application (though using registered Action objects). The demo and presentation are still available from this location:
http://www.eclipsezone.com/files/jsig/
Note that this was done at the time of OSGi v4, so you might find the install fails with a mismatched version of the import framework. If so, crack open the file and ensure that it doesn't say
Import-Package: org.osgi.framework;version="[1.3.0,1.4.0)"
or some such - get rid of the version numbers and it should still run fine today.