title: Dynamic OSGi service replacement as development aid
body: <p>We are using Apache Felix annotations to handle all the OSGi stuff in our application. I have a provider class that talks to a server. I have a consumer class that does stuff with data from the server. What I want is to create another provider instance (new class implementing interface) that is for debug purposes only that returns canned responses to requests by the consumer. Ideally I would like the consumer to be unaware of this handoff. It's provider service reference would simply be replaced. </p>
  
  <p><strong>The use case</strong>: When the developer is running on a machine without access to the actual server, he presses a button in our running app to switch from the real provider instance to our debug provider instance.</p>
  
  <p>What is the recommended way to accomplish this?</p>
  
  <p>Example code:</p>
  
  <pre><code>public interface IProvider{
      public String getDataFromServer();
  }
  
  
  @Component
  @Service(value=IProvider.class)
  public class RealProvider implements IProvider{
      @Override
      public String getDataFromServer(){
          ...
      }
  }
  
  
  @Component
  @Service(value=IProvider.class)
  public class DebugProvider implements IProvider{
      @Override
      public String getDataFromServer(){
          return "Hello World";
      }
  }
  
  
  @Component
  public class Consumer{
      private @Reference IProvider provider;
  
      public void doSomething(){
          provider.getDataFromServer();
      }
  }
  </code></pre>