public class Activator implements BundleActivator {The Activator of the application bundle uses the service:
public void start(final BundleContext context) throws Exception {
context.registerService(IUserManager.class.getName(), new UserManager(), null);
System.out.println("Service registered.");
}
public void stop(final BundleContext context) throws Exception {}
}
public class Activator implements BundleActivator {Now you have to verify that R-OSGi is installed on your local Eclipse. Activate the view "Plug-ins" and search for a bundle named ch.ethz.iks.r_osgi.remote. If it is already there, you may skip the following installation procedure.
public void start(final BundleContext context) throws Exception {
final ServiceReference serviceRef = context.getServiceReference(IUserManager.class.getName());
if (serviceRef == null) {
System.out.println("Service not found!");
} else {
final IUserManager userManager = (IUserManager) context.getService(serviceRef);
try {
System.out.println("All user names:");
final List<String> names = userManager.getUserNames();
for (final String name : names) {
System.out.println("Name = " + name);
}
} finally {
context.ungetService(serviceRef);
}
}
}
public void stop(final BundleContext context) throws Exception {}
}
The easiest way to install R-OSGi into your Eclipse is to install the Eclipse Communication Framework (ECF). Use one of the following update sites:
- Eclipse 3.4: http://download.eclipse.org/rt/ecf/2.0/3.4/updateSite/site.xml
- Eclipse 3.3: http://download.eclipse.org/rt/ecf/2.0/updateSite
First, you have to add the bundle ch.ethz.iks.r_osgi.remote to the list of required plug-ins. Do this for both the application and the service bundle. Then you have to mark the published OSGi service as remote service (inside the Activator of the service bundle):
public void start(final BundleContext context) throws Exception {That was easy! Using the remote service requires a little bit more code. Instead of getting the user manager service directly from the service registry, you have to get the R-OSGi service from the service registry. Then you have to query the R-OSGi service for the remote user manager services. The code inside the Activator of the application bundle looks like this:
final Hashtable properties = new Hashtable();
properties.put(RemoteOSGiService.R_OSGi_REGISTRATION, Boolean.TRUE);
context.registerService(IUserManager.class.getName(), new UserManager(), properties);
System.out.println("Service registered.");
}
public void start(final BundleContext context) throws Exception {Now you should test the remote OSGi service. Create a run configuration for the service and run it. Then create a run configuration for the application and run it. Depending on your user manager implementation you should see something like this (Console of the application):
final ServiceReference serviceRef = context.getServiceReference(RemoteOSGiService.class.getName());
if (serviceRef == null) {
System.out.println("R-OSGi service not found!");
} else {
final RemoteOSGiService remote = (RemoteOSGiService) context.getService(serviceRef);
try {
remote.connect(new URI("r-osgi://205.207.25.9:9278"));
final RemoteServiceReference[] references = remote.getRemoteServiceReferences(
new URI("r-osgi://205.207.25.9:9278"), IUserManager.class.getName(), null);
if (references == null) {
System.out.println("Service not found!");
} else {
final IUserManager userManager = (IUserManager) remote.getRemoteService(references[0]);
System.out.println("All user names:");
final List<String> names = userManager.getUserNames();
for (final String name : names) {
System.out.println("Name = " + name);
}
}
} finally {
context.ungetService(serviceRef);
}
}
}
osgi> All user names:You may download the source code from here.
Name = Charly
Name = Dennis
Name = Ed
3 comments:
Dear Stefan Diener,
Thank you for the tutorial on to to create a remote OSGI service. It is very useful for a beginner like me.
I did managed to connect to the remote machine which is within the same subnet but could not able to connect to a machine which located in different subnet.
How do should I do to connect to remote machine which is in different subnet?
Thanks
Sonam
Hi Sonam,
I guess you have to add a route to your network configuration to reach the subnet. Here's how to add a persistent route on Windows XP:
http://www.labtestproject.com/using_windows/add_persistent_route_on_windows_xp
If you're using another operating system, please search Google for proper instructions for your OS.
Best regards,
Stefan
Dear Stefan,
Thank you very much for your help. I look forward to getting help from you in future too.
BR,
Sonam
Post a Comment