Hi,

How can we version the service(s)?
(While registering service the into service registry)

I am having two bundles running, bundle names (bundle2) are same but they both different versions are 1.0 and 2.0. Both buldes implemnets interface in Bundle1

Another bundle called bundle4, which is currently calling the budle2 version 1.0.

Now I want to change some of the information in manifest file or xml file (Spring - OSGi), it should reflect budle4 to pick bundle2 version 2.0.

Bundle1 manifest file: (Whicg has interface)

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bundle1 Plug-in
Bundle-SymbolicName: Bundle1
Bundle-Version: 1.0.0
Export-Package: com.shan.osgi.example;version="1.0.0"


HellloService.java

package com.shan.osgi.example;

public interface HelloService {

public void speak();

}


Budle2 Version 1.0 Manifest file:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bundle2 Plug-in
Bundle-SymbolicName: Bundle2
Bundle-Version: 1.0.0
Bundle-Activator: com.shan.osgi.example.bundle2.Activator
Import-Package: com.shan.osgi.example;version="1.0.0",
org.osgi.framework;version="1.3.0"


Implementation of HelloService Interface:

HelloServiceImpl1.java

package com.shan.osgi.example.bundle2;

import com.shan.osgi.example.HelloService;

public class HelloServiceImpl1 implements HelloService {

public void speak() {
System.out.println("Inside implementation 1.0...");
}

}


Service Registration:

Activator.java


package com.shan.osgi.example.bundle2;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.shan.osgi.example.HelloService;

public class Activator implements BundleActivator {

private HelloService service;

public void start(BundleContext context) throws Exception {

service = new HelloServiceImpl1();

Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("Version", "1.0");

// register the service
context.registerService(HelloService.class.getName (), service,
hashtable);

}

public void stop(BundleContext context) throws Exception {
service = null;
}

}


Bundle2 Version 2.0 manifest file:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bundle2 Plug-in
Bundle-SymbolicName: Bundle2
Bundle-Version: 2.0.0
Bundle-Activator: com.shan.osgi.example.bundle2.Activator
Import-Package: com.shan.osgi.example;version="1.0.0",
org.osgi.framework;version="1.3.0",
org.osgi.util.tracker;version="1.3.1"



Implementation of HelloService Interface:

HelloServiceImpl2.java


package com.shan.osgi.example.bundle2;

import com.shan.osgi.example.HelloService;

public class HelloServiceImpl2 implements HelloService {

public void speak() {
System.out.println("Inside implementation 2.0...");
}

}



Service Registration:

Activator.java


package com.shan.osgi.example.bundle2;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.shan.osgi.example.HelloService;

public class Activator implements BundleActivator {

private HelloService service;

public void start(BundleContext context) throws Exception {

service = new HelloServiceImpl2();

Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("Version", "2.0");

// register the service
context.registerService(HelloService.class.getName (), service,
hashtable);

}

public void stop(BundleContext context) throws Exception {
service = null;
}

}


Bundle4 manifest file:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Bundle4 Plug-in
Bundle-SymbolicName: Bundle4
Bundle-Version: 1.0.0
Bundle-Activator: com.shan.osgi.example.bundle4.Activator
Import-Package: com.shan.osgi.example;version="1.0.0",
org.osgi.framework;version="1.3.0"


Getttng Service from the servic Registry:

Activator.java


[I]package com.shan.osgi.example.bundle4;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import com.shan.osgi.example.HelloService;

public class Activator implements BundleActivator {

public void start(BundleContext context) throws Exception {

HelloService service = null;

ServiceReference[] references = null;

references = context.getAllServiceReferences(HelloService.class
.getName(), "(&(Version=1.0))");

if (references != null) {
for (int i = 0; i < references.length; i++) {
service = (HelloService) context.getService(references);
if (service != null)
service.speak();
service = null;
}
}

}

public void stop(BundleContext context) throws Exception {

}

}








Advance Thanks,
Shan.