简体   繁体   中英

Access a pom property in OSGi bundle at run time

I don't know if what I am doing is the OSGi and Maven way, so first some background:

I have two bundles web and client . In the client bundle, I want to access a service that the web bundle registers. I have already checked that I can implement BundleContextAware in the client bundle and if the web bundle's ArtifactId is project.web then the following works:

public void setBundleContext(BundleContext bc) {
    bc.getServiceReferences(clazz, "(Bundle-SymbolicName=project.web)")

What I would like to is not hard-code the symbolic name of the web bundle. The symbolic name comes from the artifactId declared in pom.xml , I believe by some gemini-blueprint magic. So my current thought is

  • create a property web.artifactId in parent pom
  • in web bundle's pom use <artifactId>${web.artifactId}</artifactId>
  • somehow access that property in client bundle's BundleContextAware.setBundleContext

How do I do this last step? I guess it might be possible to generate a java file at run-time that assigns the property value to some known identifier, so I can use WebBundleInfo.ARTIFACT_ID ; but I don't know how to do that and wonder if that is an overkill.

Another thought is if there is a way that the client bundle can use System.getProperty to access this value? I saw some references to accessing a property file but I am not very keen on it -- neither on reading a property file, nor on generating it.

So my questions are: - Is this is a good way to share a bundle's symbolic-name like this? - Is there a away to automatically make some properties created in pom file available to the run-time execution environment? - I guess I could figure out how to generate such a source file, but is that a good way to solve this? Is there a way to generate such a file without a source file, ie from some text in the pom.xml itself? Further, is it possible to overwrite the existing file only if it has changed so as to avoid recompilation, because that will end up in a new snapshot version after every compile?

The easiest way to set variables at compile time is to make sure they end up in the manifest of the bundle. Like you say, the Bundle-SymbolicName already does. For the client bundle, you should probably create a new, custom header that contains your variable. Then, from your client bundle, you could do something like this:

public void setBundleContext(BundleContext bc) {
  String filter = (String) bc.getBundle().getHeaders().get("MyCustomHeader");
  bc.getServiceReferences(clazz, filter);
}

That being said, could you explain a bit more about what you're trying to do (at a higher level) because I fail to see why you're not just using OSGi services and dependencies (using Declarative Services, Blueprint, or some other dependency injection framework)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM