开发者

How does the OSGi framework set the Bundle ID?

开发者 https://www.devze.com 2023-04-06 16:07 出处:网络
I am trying to run OSGi framework (Equinox) in a main method. Each time I start the framework, when I print BundleContext.getBundles().length, it says the framework has only 1 Bundle installed on (tha

I am trying to run OSGi framework (Equinox) in a main method. Each time I start the framework, when I print BundleContext.getBundles().length, it says the framework has only 1 Bundle installed on (that is certainly the system bundle). Whe开发者_如何学编程n I install my first bundle the bundle ID will continue from the last session. let's say if I had 4 bundles last session (and I have stopped and uninstalled all of them before stopping the system bundle), the first Bundle ID is set 5. Now, I want to know how does the framework choose the bundle ID? Why and how does the framework remembers the last session, even though I had uninstalled all of the bundles? Is it because of Bundle Cache? And if it is, how can I clear the cache (to restart numbering from 1)?


The framework has the last used bundle id somewhere in the persistent store it manages. What this store looks like is a framework implementation detail. When you launch the framework, you can specify the org.osgi.framework.storage.clean framework configuration property. This will clear all installed bundles but I am not sure if it will reset the last used bundle id.


Deleting the equinox/org.eclipse.osgi folder resets the numbering. Before the delete make sure that your bundles don't have any important data under this folder.

The bundle command with a valid bundle id can show the absolute path of the equinox/org.eclipse.osgi folder:

osgi> bundle 7
slf4j.api_1.6.1 [7]
  Id=7, Status=ACTIVE      Data Root=D:\temp\test\equinox\org.eclipse.osgi\bundles\7\data
...


Here's what OSGI Core Release 8 specification says about bundle identifiers:

Bundle identifier - A long that is a Framework assigned unique
identifier for the full lifetime of a bundle, even if it is updated
or the Framework is restarted. Its purpose is to distinguish
bundles in a Framework. Bundle identifiers are assigned in
ascending order to bundles when they are installed. The method 
getBundleId() returns a bundle's identifier.

So that at tells us two important properties about bundle identifiers: 1.) the relationship between bundleId and bundle is stable from the time that it is installed to the time that it is uninstalled, and 2.) that bundle identifiers are assign in ascending order to when they were installed.

Going back to the original question, trying to get bundles identifier number assignments to start over works against the OSGI specification for bundle identity, and you shouldn't mess around with trying to control bundle identity. For example, let's assume you have three bundles installed:

b1(0), b2(1), b3(2)

Now lets assume you uninstall bundle b2 (with id = 1):

b1(0),b3(2)

Now lets assume you re-install b2:

b1(0),b3(2),b2(3)

Notice that b2 gets a new id assigned to it. That id is stable for referencing b2 as long as b2 is not uninstalled. And if you notice, the id of 1 is no longer used. Bundle id is an identifier, not an index into an array. The bundleId of b3 can't be changed because it wasn't uninstalled, b2 could not be assigned the bundleId of 1, since that would violate the rule the bundle identifiers are assigned in ascending order to when they were installed (the OSGI framework doesn't remember that b2 was previously installed and uninstalled - it looks like its just being installed for the first time). Given that the bundleId is a long, that means we would need to do 9,223,372,036,854,775,807 bundle uninstalls before OSGI's bundle id number assignment scheme would run out of id's.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号