'Dynamic Bundle loading OSGI

I am new to OSGi so please help me out here.

**I have 2 independent modules A and B for addition and multiplication respectively. Now in clientmodule i.e., i want to take user input for which bundle to install and input for addition or multiplication.

**for eg:** if i give bundle A as input, i want 4+3=7 , 
            if i give bundle B as input, i want 4*3=12**

Bundle A:

public class AdditionImpl implements Addition, BundleActivator {

  private ServiceReference<Addition> reference;
  private ServiceRegistration<Addition> registration;

  @Override
  public void start(BundleContext bundleContext) throws Exception {
    System.out.println("Registering service for addition.");
    registration = bundleContext.registerService(
        Addition.class,
        new AdditionImpl(),
        new Hashtable<String, String>());
    reference = registration
        .getReference();
  }

  @Override
  public void stop(BundleContext bundleContext) throws Exception {
    System.out.println("Unregistering service for addition.");
    registration.unregister();
  }

  @Override
  public int add(int a, int b) {
    return a + b;
  }
}

Bundle B:

public class MultiplicationImpl implements Multiplication, BundleActivator {

  private ServiceReference<Multiplication> reference;
  private ServiceRegistration<Multiplication> registration;

  @Override
  public int multiply(int a, int b) {
    return a + b;
  }

  @Override
  public void start(BundleContext context) throws Exception {
    System.out.println("Registering service for multiplication.");
    registration = context.registerService(
        Multiplication.class,
        new MultiplicationImpl(),
        new Hashtable<String, String>());
    reference = registration
        .getReference();
  }

  @Override
  public void stop(BundleContext context) throws Exception {
    System.out.println("Unregistering service for multiplication.");
    registration.unregister();
  }
}

ClientService(Bundle C:)

I am not able to figure out how to do that.
//logic for activating / install bundle according to user input
I am using **karaf** terminal to execute bundles manually. Not sure about user input.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source