anurak
anurak

Reputation: 11

Can not get log service from osgi-over-slf4j 1.7.7

I try to get log service from osgi-over-slf4j (SLF4j v1.7.7) bundle from another bundle but only got null when i call getServiceReference(...).

Here is code in another bundle that try to get a log service.

public void start(BundleContext context) throws Exception {
    System.out.println(" ID State Name");
    Bundle[] bundles = context.getBundles();
    for (Bundle bundle : bundles) {
        printBundle(bundle.getBundleId(),
        getStateString(bundle.getState()), (String) bundle
            .getHeaders().get(Constants.BUNDLE_NAME),
        bundle.getLocation(), bundle.getSymbolicName());
    }
    ServiceReference ref = context.getServiceReference(LogService.class.getName());
    if (ref == null) {
        System.out.println("service ref. is null");
    } else {
        LogService ls  = (LogService) context.getService(ref);
             if (ls == null) {
               System.out.println(" Log is Null");
            } else {
               ls.log(LogService.LOG_DEBUG, "aaaaHahahahahaha");
             }
    }
}

Here is some part of MANIFEST.MF of that bundle

   Import-Package: org.osgi.framework;version="1.5.0",
                   org.osgi.service.log;version="1.3.0",
                   org.osgi.util.tracker;version="1.4.0"
   Bundle-Activator: com.ktbcs.cjmf.log.factory.activator.LogServiceFactoryActivator

I sure that osgi-over-slf4j already ACTIVE. My environment is Websphere 8.5 on windows 8.

Any suggestions are welcome.

Thanks,

anurak

Upvotes: 0

Views: 399

Answers (1)

Balazs Zsoldos
Balazs Zsoldos

Reputation: 6046

Your bundle starts earlier then osgi-over-slf4j. In that case, the service cannot be found in the start function of the Activator.

To avoid this issue, you can use Declarative Services or one of its alternative instead of an Activator.

Upvotes: 1

Related Questions