Do i need to supply a custom ClassLoader ? Thinking out loud this does not appear to be the right approach because inside the classloader one does not know the required version.
You could always download them from database (or whatever) into some local location and then install them dynamically into the OSGi Framework. All you need to give to the framework is the filesystem path of the bundle. Of course, you will have to write all the glue code for this yourself. On the other hand, if you are using Eclipse P2, you might have some more flexibility thanks to the automated provisioning.
Have a look at the PAX URL project: http://wiki.ops4j.org/display/paxurl/Documentation
That demonstrates the separation of concerns of loading an OSGi bundle from how it is stored. In theory, I don't see any major impediment to using a database as the bundle store, though I also don't see any obvious advantages.
To your point on the bundle versions, you need some mechanism to identify which bundles (and versions) to load. You would store the bundles in the database with version information, otherwise how would you store different versions of the same bundle?
A simple solution is to create your own bootstrap bundle that will provision all other bundles from a database (or other location). OSGi allows you to install bundles from an arbitrary InputStream
so it should be reasonably easy to plug this into whatever source you want (e.g. JDBC).
See BundleContext.installBundle()
method for more details.
You should not be looking for a "callback" through which the framework would notify you "when it needs to load a required dependency" since automatic dependency management (ala Maven) is not part of the core OSGi functionality (although can be achieved by using services like OBR).
Given that the loading mechanism essentially works with an URL, I would suggest to try a custom URL handler. Logically you will need to load and activate it before loading any bundles using this handler, otherwise I think it should work.
See the "URL Handlers Service Specification", R4 OSGi Core Specification.
Glassfish is using this approach to install webapp bundles btw.