Including additional resources with OSGi bundles

淺唱寂寞╮ 提交于 2019-11-30 14:45:47

Do these additional files need to be writeable by the native code? If not, there's nothing stopping you putting any files you like inside a bundle.

The usual problem you have in OSGi is working out the path to the file as OSGi does not assume a file system is available (it's not as strange as it sounds as OSGi started out in embedded devices).

How do you control where the native code looks for its related files? Do you need to pass it a path?

If you want a directory to copy or unpack stuff, then use:

org.eclipse.core.runtime.Platform.getStateLocation()

Which gives you the working directory for the bundle.

If you want to find the path for a particular file in your bundle, you can do:

org.eclipse.core.runtime.FileLocator.toFileURL((context.getBundle().getEntry("/etc/readme.txt")))

Which, in this case, will return an file URL to the /etc/readme.txt in the current bundle.

Both pieces of code assume they are inside the activator's start() method.

Your solution works, of course. But you have to be careful to also stop and remove any resource that you extracted and started during the installation. This might especially difficult to track in case an executable also created any kind of working files.

You should do this because one of OSGi's strength is the lifecycle management, which allows you to also remove bundles and services without a trace. For this, the framework tracks everything a bundle does. If you keep an execuable running after you removed the bundle that installed and started it, the connection is lost and it may keep running until the machine is rebooted (often not an option for embedded systems).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!