0

Does an OSGi-Bundle consider its changed class files when restarting? My intention is to stop a Bundle, change a java file of it, compile it, an then restart the bundle. Does it have the new class included or do I need to update the bundle somehow?

These two threads made me come up with this idea: Editing/Modifying a .java file programmatically? (not the .class file) and How do I programmatically compile and instantiate a Java class?

Would be glad for help. Thanks.

Community
  • 1
  • 1
Chaoz77
  • 37
  • 7
  • Why would you want or need to do this? Do it the way _everyone_ else does, use [semantic versioning](http://www.osgi.org/wiki/uploads/Links/SemanticVersioning.pdf‎) and just install the newer version, uninstall old version and refresh. (If you were using Karaf and Maven, you could simply [watch](http://karaf.apache.org/manual/latest-2.2.x/commands/dev-watch.html) the bundle and it would be updated automatically for you as it changes). – earcam Jun 15 '13 at 17:04
  • The goal of my project is to do this automatically. Due to performance requirements a dynamic mapper class is built up automatically. I know this is a very special case but becomes necessary for this undertaking. Every time there is a new party in the event based system the class needs to be updated... You don't want to do this by hand... Your semantic versioning Link gave me a 404 btw. – Chaoz77 Jun 16 '13 at 07:27
  • re: 404, [www.osgi.org/wiki/uploads/Links/SemanticVersioning.pdf](http://www.osgi.org/wiki/uploads/Links/SemanticVersioning.pdf). If I were worried about performance I would not choose a design that required modification and compilation at runtime. – earcam Jun 16 '13 at 14:35
  • **"Every time there is a new party in the event based system the class needs to be updated"** This is a major design smell, your code violates the [Open/Close Principle](http://en.wikipedia.org/wiki/Open/closed_principle). – earcam Jun 16 '13 at 14:41
  • Well maybe there was a misunderstanding. There are not many parties joining after the system has been started. Thus, mapping with a method from a class is faster than looking up the information from a db every time a event occurs... Anyways, would be interested what your approach would be then... – Chaoz77 Jun 16 '13 at 14:58
  • Register each individual mapping as service - then have a listener coordinate the delegation mapping to the correct service (you can use service properties to add as much metadata as needed). – earcam Jun 17 '13 at 13:07
  • Can I create the mapping / service then on-the-fly? How would I do that with java?! It has to be done automatically and the problem about your service idea is, that the mapping fuctions are called and distingquished by annotations. How would I solve that?! I would be really thankful if you could explain your idea in more detail. – Chaoz77 Jun 18 '13 at 06:32

1 Answers1

1

A bundle must be refreshed or updated to get a new class loader. Simply restarting a bundle will use the current class loader which will still have the original class loaded.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27
  • Thanks for your answer. So even if the class file is replaced it still has the old class file in memory? How does the process work exactly? Would it then be a solution to update the bundle programmatically? (Stop bundle, update the content programmatically, update bundle, start bunde) Is this possible? – Chaoz77 Jun 16 '13 at 09:59
  • Just replace the bundle jar and update the bundle programmatically. – Christian Schneider Jun 16 '13 at 17:05