Question

I have defined an OSGi bundle (cluster_implementation) with a Declarative Service (DS) component definition, specifying one service exported, one activate method and one deactivate method.

In the body of the Activate method i need to access one file that is not present in the file-system but is bundled in the OSGi bundle itself. During the activate method, the file content is retrieved as an InputStream using java.lang.ClassLoader:getResourceAsStream.

This mostly works, but there is one case where it doesn't work. In my application the service exported by the cluster_implementation can be referenced by other bundles either via the Service Registry or Declarative Service or spring-dm. The activation policy of the component is delayed so it gets activated when the first reference to the service happens.

Now if the activation happens because of a reference from Declarative Service component the file content is read fine, if instead the activation happens because a spring-dm component is in the need for the service, then the InputStream for the resource is NULL! For now i have solved the problem by making the component to activate immediately by setting immediate="true" in the component property, however my requests are these:

  • Is it allowed to fetch the content of a resource as a Stream during DS component activation?
  • If this is legal, why activating via spring-dm could cause the resource as Stream not to be accessible? BTW the resource is there - if I do a Bundle.findEntries I can see it!

The OSGi framework I'm using is Equinox 3.6.

Was it helpful?

Solution

I think you must have another problem than you think. Access to resources is available to any resolved bundle and is completely unrelated to DS. Since your class is loaded, your code can be loaded from the JAR.

The behavior seems puzzling, and maybe an error in the framework. I could only imagine this going wrong when you use fragments? But even that does not seem to make sense. Did you try another framework, Felix has very good diagnostics.

OTHER TIPS

I guess you have a timing issue between your activator and spring dm. Spring dm uses and extender to watch for bundles with spring contexts and initializes them. This might run in parallel to the activator. Honestly I would have expected the activator would run first but it seems this is not the case.

To solve this you could make sure the file is created by the side that accesses the file first but be careful to make it thread safe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top