Refresh Package Bundles

500 Views Asked by At

I've stumbled upon a problem, that can be summarized as follows:

I have an application which is embedded with OSGI. In this OSGI container, I have installed a bundle A of version v1 which registers that service object. This service object is used by the host application. while the service object is in use, I uninstalled the bundle A and installed a new version(v2) of the bundle A. Here are my findings.

  • Old service object works fine even if we uninstall the bundle A(v1).
  • The new service object gives the new functionality of bundle A(v2).

My question here is when we refresh the bundle packages by using

List<Bundle> refreshbundles = new ArrayList<Bundle>();
Bundle local; // points to the bundle
refreshbundles.add(local);
m_felix.adapt(FrameworkWiring.class).refreshBundles(refreshbundles)

and when we try to get the service object registered by the new version of the bundle A, it is returning null. I tried looking at the source code but it didn't help. can you please help me with this?

EDIT:

HostApplication

public Felix m_felix = null;
m_felix = new Felix(config); //sending some config to felix.
m_felix.start();

//installing bundle
Bundle bundle = m_felix.getBundleContext().installBundle("file:/bundleA-1.0.jar")    
bundle.start()

//getting the service object registered by bundle A.
ServiceReference sr = m_felix.getBundleContext()
                        .getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();

//now uninstalling the bundle installing the new version of it say version 1.1
bundle.uninstall()
bundle = m_felix.getBundleContext().installBundle("file:/bundleA-1.1.jar")    
bundle.start()

//getting the service object registered by bundleA1.1

ServiceReference sr = m_felix.getBundleContext()
                    .getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();

//the above line is working fine but after refreshing the packages, Service object is returned as null    

List<Bundle> refreshbundles = new ArrayList<Bundle>();
Bundle bundle; // points to the bundle
refreshbundles.add(bundle);
m_felix.adapt(FrameworkWiring.class).refreshBundles(refreshbundles)

//refresh done
ServiceReference sr = m_felix.getBundleContext()
                    .getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();

//throwing null pointer exception because getService() is returning null.

what exactly is happening when we refresh the bundles?

BundleA_Activator.java

public class BundleA_Activator extends BundleActivator{
   public class BundleActivatorInterfaces implements  BundleActivator{
    ServiceRegistration SR;
    @Override
    public void start(BundleContext bundleContext) {

        SR = bundleContext.registerService(SampleInterface.class.getName(), new ExposedClass(), null);
    }

    @Override
    public void stop(BundleContext bundleContext) {
            SR.unregister();
    }
}
0

There are 0 best solutions below