title: Injecting components into a POJO using OSGi
body: <p>I'm new to OSGi and I'm interested in retrofitting some of my jars as OSGi bundles.
  However I do not want to introduce additional dependencies to any osgi-specific libraries.</p>
  
  <p>As such annotations are out of the question as are programmatic calls to bundle contexts and what not.</p>
  
  <p>I have found a near match to my requirements in declarative services which allows me to expose my lower level bundles without impacting dependencies however at the higher level (where i actually need to consume the services) i'm still a bit stuck.</p>
  
  <p>I understand that the component xml can be used to declare implementations of services (which i already use for my lower level jars) but also to inject service instances into a specific POJO.</p>
  
  <p>Now my question: how do I get access to the osgi-managed POJO which has the services injected into it? Is it at all possible without introducing new dependencies or do I have to do it programmatically? </p>
  
  <p>If the latter is the case can someone point me in the direction of some code to do it, in other words the component-equivalent of bundleContext.getServiceReference()?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>To clarify, if you take the fifth part of this tutorial: <a href="http://www.vogella.com/articles/OSGiServices/article.html" rel="nofollow">http://www.vogella.com/articles/OSGiServices/article.html</a></p>
  
  <p>He declares a component.xml file which uses reference binding to inject a service into the object QuoteConsumer.
  Great, now how do I get an instance of QuoteConsumer that has the necessary services injected into it, I can't very well do "new QuoteConsumer()" right?</p>
  
  <p><strong>UPDATE2</strong></p>
  
  <p>Currently I am registering the instance created by osgi as a static variable which can be requested, I'm thinking this is not the best method especially because I can't set the constructor to private. (the latter would at least result in a true singleton)</p>
  
  <p>Basically the Factory class has:</p>
  
  <pre><code>private void activate() {
      instance = this;
  }
  </code></pre>
  
  <p><strong>UPDATE3</strong></p>
  
  <p>A full example of a factory:</p>
  
  <pre><code>public class Factory {
  
      private static Factory instance;
  
      public static Factory getInstance() {
          if (instance == null)
              instance = new Factory();
          return instance;
      }
  
      private MyInterface implementation;
  
      public void setMyInterface(MyInterface implementation) {
          this.implementation = implementation;
      }
  
      public void unsetMyInterface(MyInterface implementation) {
          implementation = null;
      }
  
      public MyInterface getMyInterface() {
          if (implementation == null) {
              ServiceLoader&lt;MyInterface&gt; serviceLoader = ServiceLoader.load(MyInterface.class);
              Iterator&lt;MyInterface&gt; iterator = serviceLoader.iterator();
              if (iterator.hasNext())
                  implementation = iterator.next();
              else
                  implementation = new MyInterfaceStub();
          }
          return implementation;
      }
  
      @SuppressWarnings("unused")
      private void activate() {
          instance = this;
      }
      @SuppressWarnings("unused")
      private void deactivate() {
          instance = null;
      }
  }
  </code></pre>
  
  <p>Any client code can then do:</p>
  
  <pre><code>Factory.getInstance().getMyInterface();
  </code></pre>
  
  <p>and receive the OSGi loaded service, the SPI loaded one or a stub.
  You can still manually set the service instance if necessary.</p>
  
  <p><strong>UPDATE4</strong></p>
  
  <p>To clarify further: this pattern is not meant for applications that are designed from the ground up to be run in an OSGi container but rather for low level libraries that have to run everywhere and even when on an OSGi container must not assume that all consumers are actually using OSGi.</p>