title: OSGI HttpService couldn't be initialized / injected properly
body: <p>I'd like to use declarative service feature in my OSGI project for my servlets. In details: user is able to install module manually on the main application selecting some *.jar file, main application has embedded OSGI server and when the OSGI module is activated it should be registered for some requests
  For example in this way:</p>
  
  <pre><code>protected void startup() {
    MyServlet servlet = new MyServlet();
    if (httpService != null)  {
      httpService.registerServlet("/req", servlet, null, null); **//OK!**
    }
  }
  </code></pre>
  
  <p>Unfortunately, I can't have httpService instance initialized and that's the problem. Here's the problem servlet module code listing:</p>
  
  <pre><code>@Component(name="app", immediate=true)
  @Service(value=App.class)
  public class App {                
          @Reference(name = "httpService", referenceInterface = HttpService.class, bind = "bindHttpService", unbind = "unbindHttpService")
          private HttpService httpService;
  
          @Activate
          protected void startup() {                    
              MyServlet servlet = new MyServlet();
              if (httpService != null)    {
                  httpService.registerServlet("/req", servlet, null, null);
                  //OK!
              } else {
                  //Not OK! 
              }
          }
  
          protected void bindHttpService(HttpService httpService) {               
              this.httpService = httpService;
          }
  
          protected void unbindHttpService(HttpService httpService) {         
              this.httpService = null;
          }
          // some more code.........              
  }
  </code></pre>
  
  <p>Maven bundle plugin settings pom.xml code </p>
  
  <pre><code>&lt;plugin&gt;
      &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
      &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
      &lt;extensions&gt;true&lt;/extensions&gt;
      &lt;configuration&gt;
          &lt;instructions&gt;
              &lt;Bundle-SymbolicName&gt;app&lt;/Bundle-SymbolicName&gt;
              &lt;Embed-Dependency&gt;!org.osgi.core&lt;/Embed-Dependency&gt;
              &lt;Embed-Transitive&gt;true&lt;/Embed-Transitive&gt;                                               
              &lt;Import-Package&gt;                        
               &lt;![CDATA[ 
                 org.osgi*,
                 com.mynamespace                        
              ]]&gt;                     
              &lt;/Import-Package&gt;                       
          &lt;/instructions&gt;
      &lt;/configuration&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>And here is simplified code for embedded OSGI server on main application:</p>
  
  <pre><code>public static void main(String[] args) 
  {
      FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
      Map&lt;String, String&gt; config = new HashMap&lt;String, String&gt;();
      config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "onFirstInit");
      config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "javax.microedition.io");
      Framework framework = frameworkFactory.newFramework(config);
      framework.start();
      BundleContext context = framework.getBundleContext();
      List&lt;Bundle&gt; installedBundles = new LinkedList&lt;Bundle&gt;();
      // install prerequisites
      installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.util_1.0.500.v20130404-1337.jar"));
      installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.osgi.services_3.4.0.v20131120-1328.jar"));
      installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.ds_1.4.200.v20131126-2331.jar"));
      //install servlet module
      installedBundles.add(context.installBundle("file:///MY_PATH\\app.jar"));
      for (Bundle bundle : installedBundles) {
          bundle.start();
      }
  }
  </code></pre>
  
  <p>I was inspired by example from Peter Fries blog (<a href="http://www.peterfriese.de/osgi-servlets-flexibility-by-simplicity/" rel="nofollow">http://www.peterfriese.de/osgi-servlets-flexibility-by-simplicity/</a>), but I can't have httpService instance initialized.
  The App module even could be started only if @Reference annotation is removed or covered with comment, in other case module will not be activated.</p>
  
  <p>I suspect that my embedded OSGI doesn't have some required module but can't get the details about it - log console doens't say anything.</p>
  
  <h2>UPDATE</h2>
  
  <p>Thanks for the hint Balasz!<br>
  There are my mistakes:<br>
  0. When <code>&lt;Embed-Dependency&gt;!org.osgi.core&lt;/Embed-Dependency&gt;</code> is declared httpService can't be automatically binded through @Reference annotatoin, so <code>bind = "bindHttpService", unbind = "unbindHttpService"</code> is useless. I removed <code>&lt;Embed-Dependency&gt;</code> declaration and it helps to bind httpService like I want to have it (via bindHttpService).</p>
  
  <ol>
  <li><p>Before installing bundles on OSGI server jetty settings should be specified:</p>
  
  <blockquote>
    <p>System.setProperty("jetty.port","8080");<br>
    System.setProperty("jetty.home.bundle", "org.eclipse.jetty.osgi.boot");</p>
  </blockquote></li>
  <li><p>There are too many jars must be installed on embedded OSGI server, in my case</p>
  
  <p>// Mandatory bundles            installedBundles.add(context.installBundle("file:///MY_PATH\org.eclipse.equinox.util_1.0.500.v20130404-1337.jar"));        installedBundles.add(context.installBundle("file:///MY_PATH\org.eclipse.equinox.ds_1.4.200.v20131126-2331.jar"));
  installedBundles.add(context.installBundle("file:///MY_PATH\org.eclipse.osgi.services_3.4.0.v20131120-1328.jar"));
  installedBundles.add(context.installBundle("file:///MY_PATH\javax.servlet-api-3.0.1.jar"));</p>
  
  <pre><code>        // Container bundles
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.jetty.security_8.1.12.v20130726.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.jetty.servlet_8.1.12.v20130726.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.jetty.continuation_8.1.12.v20130726.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.jetty.server_8.1.12.v20130726.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.jetty.util_8.1.12.v20130726.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.jetty.io_8.1.12.v20130726.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.jetty.http_8.1.12.v20130726.jar"));
  
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.http.servlet_1.1.400.v20130418-1354.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.http.jetty_3.0.200.v20131021-1843.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.http.servletbridge_1.0.300.v20130327-1442.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.servletbridge_1.3.0.v20130927-1541.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.common_3.6.200.v20130402-1505.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.registry_3.5.400.v20130717-1325.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.eclipse.equinox.http.registry_1.1.300.v20130402-1529.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\org.osgi.service.obr-1.0.2.jar"));
  
          // Service bundles
          installedBundles.add(context.installBundle("file:///MY_PATH\\jetty-httpservice-8.0.4.v20111024.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\ow2-httpservice-1.2-spec-1.0.0.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\jetty-osgi-boot-8.0.4.v20111024.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\jetty-deploy-8.0.4.v20111024.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\jetty-webapp-8.0.4.v20111024.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\jetty-security-8.0.4.v20111024.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\jetty-http-8.0.4.v20111024.jar"));
          installedBundles.add(context.installBundle("file:///MY_PATH\\jetty-xml-8.0.4.v20111024.jar"));
  
          // Finally, required OSGI bundle
          installedBundles.add(context.installBundle("file:///MY_PATH\\app.jar"));
  </code></pre></li>
  </ol>
  
  <p>So with these changes I have an initialized httpService object, and now I want to register my servlet instance</p>
  
  <pre><code>@Activate
  protected void startup() {                    
     MyServlet servlet = new MyServlet();
     if (httpService != null) {
          System.out.println("Greetings! I just want to be sure that service isn't null!")
          //httpService.registerServlet("/req", servlet, null, null); // BAD PLACE
     } else {
          System.out.println("Something goes wrong")
     }
  }
  </code></pre>
  
  <p>The code snippet above will show the 'Greetings' message but if I try to register servlet and uncomment 'BAD PLACE' the mesage won't be shown, nothing is happening. Exception is also couldn't be catched; looks like the 'startup' method is ignored. Any ideas what's wrong?</p>