1

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.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283

2 Answers2

0

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.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • Chirstian, thanks for the reply, it seems not a race condition between spring-dm and the activator, but rather it's a plain bug in the library is was using. Opening the resource with proper API allow me to get the proper content even if the API is still failing. Thanks for you help. – Giovanni Meo Sep 28 '12 at 11:31
0

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.

Peter Kriens
  • 15,196
  • 1
  • 37
  • 55
  • Peter, thanks for the reply, indeed you are right, i just did more troubleshooting on the issue and if in the same spot where i get the failure i retrieve the resource using the this.getClass().getClassLoader().getResourceAsStream("/config/infinispan-config.xml"); and i dump the content i can see it all. The API failing is part of the DefaultCacheManager in Infinispan package (rev 5.1.4) and that seem to have trouble doing the proper lookup for the file. I should have done more debugging, put i was not really sure of what are proper operation to be done in activator. Thanks! – Giovanni Meo Sep 28 '12 at 11:28