Don
Don

Reputation: 1458

How do I get the symbolic name of the bundle, which serivce i use?

I'm implementing a journalling service which is needed to map the flow of data throughout an osgi framework.

Each time a clientbundle wants to use a service form another bundle it will first call a function from my journalling bundle which goes a little like this:

JournalService.Journal(args);
NeededService.neededfunction(args);

I need the following things for my journal service:

I know how to get the last 2 (I pass as arguments to my Journal: String functionname and Object[] args). But I don't know what needs to be passed to get the first 3, with the least amount of needed arguments.

Upvotes: 0

Views: 296

Answers (2)

Balazs Zsoldos
Balazs Zsoldos

Reputation: 6046

Here is an article that shows similar functionality: http://java.dzone.com/articles/osgi-service-hook-log-all

It logs out every method call with function name and parameters but it still does not log out the bundle names. I think it would be possible if the code had been changed in the following way:

  • Not proxies services but ServiceFactory implementations would be registered when a new service registration event is caughed
  • The ServiceFactory would instantiate a custom proxy for each bundle request (requesting the service) that does the logging for each method call

In that case there would be a custom proxy object for each Bundle-Service-Bundle relationships so they could log out the information you needed.

Well, in this case there is no need to log from your production code at all.

For more information about serviceFactory please see http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/ServiceFactory.html.

In the Hooks you could filter the services (that you re-register with the ServiceFactory) based on packge name or something. Proxying every service in the system may cause a bit of performance drawback.

Upvotes: 1

Sheena Artrip
Sheena Artrip

Reputation: 2010

If you have the ServiceReference, you can get all that information off of it. I believe most frameworks, deep down, use ServiceReferences to reference individual services from bundles.

http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/ServiceReference.html

You can get the bundle of the service you are referencing. If bundle A is referencing Service B from bundle B, then instance method ServiceReference.getBundle will get you Bundle B, and the current BundleContext will get you Bundle A.

That said, your best bet may to be use something like ReferenceListeners (though you may not be using blueprint). That will let you know when services get referenced or dereferenced and get all the contextual data related to it. Another way may be to use ServiceEvent, which lets you listen for register/deregister events. I apologize if you already knew this.

http://www.ibm.com/developerworks/xml/library/os-osgiblueprint/index.html#srms

http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/ServiceEvent.html

As far as tracking individual methods, you can proxy your references once they get pulled out of osgi. So bundle A will have a reference to Service B from Bundle B. Bundle A then gets a Proxy of Service B which, when called, delegates to the original method AND calls your JournalService.

Upvotes: 1

Related Questions