Multiple OSGi services
<p>I'm new to OSGi and trying to develop an application with OSGi. I have a OSGi service which has one interface and two implementations. </p>

<p>Interface: <code>ExportService</code></p>

<p>Implementations: <code>ExcelExportServiceImpl</code>, <code>PdfExportServiceImpl</code></p>

<p><code>ExportService</code> is my interface and <code>ExcelExportServiceImpl</code>, <code>PdfExportServiceImpl</code> are the implementations of <code>ExportService</code>.</p>

<p>I want <code>ExcelExportServiceImpl</code> and <code>PdfExportServiceImpl</code> as two different services.</p>

<p>From my application bundle, if I want to use excel export, I should be able to call the <code>ExcelExportServiceImpl</code> service without involving <code>PdfExportServiceImpl</code>.</p>

<p>How to register two different services which has same interface?</p>

<pre><code>@Override
public void start(BundleContext context) throws Exception {
        context.registerService(ExportService.class.getName(), new ExcelExportServiceImpl(), null);
        context.registerService(ExportService.class.getName(), new PdfExportServiceImpl(), null);
    }
}
</code></pre>

<p>For now, I came up with the above code in my activator and it doesn't seem to work since both the service has <code>ExportService.class.getName()</code> as the class name. How to achieve two different services in same bundle with one interface?</p>

<p><strong>Update/Solution:</strong></p>

<p>I changed the code as below in my service bundle's activator,</p>

<pre><code>@Override
public void start(BundleContext context) throws Exception {
        Hashtable excelProperty = new Hashtable();
        excelProperty.put("type", "excel");
        excelServiceRegistration = context.registerService(ExportService.class.getName(), new ExcelExportServiceImpl(), excelProperty);
        Hashtable pdfProperty = new Hashtable();
        pdfProperty.put("type", "pdf");
        pdfServiceRegistration = context.registerService(ExportService.class.getName(), new PdfExportServiceImpl(), pdfProperty);
}
</code></pre>

<p>And in my application bundle, I added the below filter</p>

<pre><code>public static void startBundle(BundleContext context) throws InvalidSyntaxException {
    String EXCEL_FILTER_STRING = "(&amp;(" + Constants.OBJECTCLASS + "=com.stpl.excel.api.ExportService)" + "(type=excel))";
    String PDF_FILTER_STRING = "(&amp;(" + Constants.OBJECTCLASS + "=com.stpl.excel.api.ExportService)" + "(type=pdf))";
    Filter excelFilter = context.createFilter(EXCEL_FILTER_STRING);
    Filter pdfFilter = context.createFilter(PDF_FILTER_STRING);
    ServiceTracker excelService = new ServiceTracker(context, excelFilter, null);
    ServiceTracker pdfService = new ServiceTracker(context, pdfFilter, null);
    excelService.open();
    pdfService.open();
}
</code></pre>