Saturday, December 12, 2009

Using OSGi Service Registration & Consumption with Service Properties

When I was looking in to resource which tells about OSGi Service Properties I found very little useful articles. Most of the resource just describe how to write services and consume them and they just mention we can use properties to varies different services. You might not know what OSGi properties yet,first I will show you how to write a typical OSGi service and register it in to the framework, then let's look in to how do we have to deal with Service Properties.

  • Before writing a real service we always have to have an interface of that particular service, then we have to implement our interface.Let's assume the Interface name is Calculator and implementation class name is CalculatorImpl.
  • Then we have to register our service using a class which implements BundleActivator class. Normally when our bundle get resolved start method of that class get called. So normally we have to register our services during the activation of our bundle. So we have to do the registration inside the start method of the Activator class of our bundle.
public class CalculatorAcivator implements BundleActivator{
public void start(BundleContext context){
// Registering the Calculator Service as an OSGi Service
Calculator calc = new CalculatorImpl();
public void stop(){

  • Then we have to write another class which is in another bundle and get the service reference. In order to do that OSGi API provide number of ways, here I will be showing the very basic way of consuming a known service.

public class CalculatorConsumer implements BundleActivator{
public void start(BundleContext context){
// Registering the Calculator Service as an OSGi Service
ServiceReference ref = context.getServiceReference(Calculator.class.getName);
if(ref != null){
Calculator calc = (Calculator)ref.getService(ref)
// now you have a reference to the Calculator service
public void stop(){

Here the registration happens with registerService method of BundleContext object which is the object we always use to talk to the OSGi environment. In here you can see we are registering the service by giving the interface name, object of the service class and a null value. Second argument is the Service object which is going to use by the consumer. Before going in to the last argument I would like to remind you something which I have mentioned in my blog post about of Introduction to OSGi Services. Here I have mentioned that we can register different services which implement the same interface, as an example we can write another class which implements the same Calculator class. So during registration of process we give the same full qualified class name. So you may get confused how do we the exactly the same require object when we get the service because when we get a service we again pass the interface name rather parsing the exactly implementation class name. So that is the place where we have to use Service Properties to get the differentiation of the services which implement the same interface.

As the third argument for the registerService method we have to give a Dictionary type object and normally what we are doing is create a Properties object and put required values to identify our service from the services which implement the same interface.

Properties props = new Properties();

So once you give the Service properties, during the consumption you can check these properties using Service Filters to get the the exactly the required Service object using getService method by passing the filter. As an example if we want to get the service object of the registered service above we have to give the filter string like this.

ServiceReference ref =context.getServiceReference(Calculator.class.getName(),"(name=Calculator)");
Calculator calc =(Calculator) ref.getService(ref);

This will simply return the actual CalculatorImpl object. When you give the filter Strings you have to obay certain rules whey you deal with complex filters and I will be writing more about OSGi Service Filters which will be helpful for you to use OGSi services in effective manner.

Friday, December 11, 2009

Introduction to OSGi Services

Recently I'm doing some work at WSO2 with OSGi Services and I'm reading some OSGi books which is very interesting for a typical Java developer. So I thought of blogging little by little about what I'm learning by reading about OSGi since there are little number of resources to read about OSGi in the internet.

In simple OSGi Service is again a service which we expose using a Java class as we do in a typical Web Service, but here we have our consumers run in the same JVM and message parsing happens through the same JVM, so no remote calls at all. With OSGi services it contains it's own way of handling clients and it's own way of handling consumers of the services like we have in Web Services. In web services we should have a framework to process web service calls and with OSGi services we have to have the an OSGi framework running to consume and expose OSGi services.
We can write a simple service interface first and implement it in a class and register the service.You can register different number of services by creating different implementation of the same interface. In OSGi we keep all the service information in a Service Registry and when someone wants to consume a given service it actually talk to the Service Broker which is aware of the OSGi registry. Through the service Broker it connect with the service and give a reference of the service object, so that we can call appropriate methods in the service class. Instead of just consuming service operations OSGi services provide set of features like monitoring service registration and unregistration using different approaches. So we can do certain things while a particular service register and while a service is unregistering. I will be writing more on how to register a service, consume a service and how to track service life-cycle, how to use service properties to select required and how to use filters with OSGi Services. I hope to write on those things in few blog posts rather writing all together.

Sunday, December 6, 2009

Standard Java class loading hierarchy

I think before we write complex Java programs we should have a basic understanding about how JVM works and class loading is one of the important thing which JVM does during running of Java programs. In typical Java programs we are using hierarchical class loading concept. In berief class loading is basically contains two operations which are locating the class in the file system and convert physical bytes in to Class object. We can overwrite the first operation by extending java.lang.ClassLoader but we cannot change the way conversion of physical bytes in to Class object.

When we run a normal Java application we have to give the classpath value for our Java command and it's the place where we have our Jar files. So that is the place where we start our class loading. JVM reads paths according to the given order and it's directly goes to the first place which is called Application Class Loader. Since the Java class loading is heirarchical, before loading the class by itself it delegate the work to the upper layer called Extension class Loader and before doing any work Extension class loader delegate the work to Bootstrap class Loader. This concept is called parent first class loading.
So when we try to load our own Jar, Bootstrap and Extension is failing to load the class because Bootstrap is aware of classes in JRE library or extension of JRE class and Extension class loader is aware of libraries in extension directory and finally class loading happens in Application class loader.

Amazon Kindle for Sri Lankans

Recently I bought an amazon kindle since Amazon recently started on shipping to Sri Lanka and I thought of writing some information on behalf of the Sri Lankans who are going to buy a kindle from Amazon.
First I would say that shipping service is excellent and it shipped directly to home on exact date they mentioned, but if flights get delayed it will prolly delayed and you can monitor the shipping progress via amazon since they send us an email once they ship it from there. Sad news is once we take the kindle from DHL we have to pay a tax of 2602 Rs and if you are buying a kindle Leather cover you have to pay another 319 bucks as well.

I would like to first talk about the price, if you are buying a new amazon kindle you have to pay 259$ for the kindle and if you are thinking of shipping it'll cost you 20$ and if you are buying a leather cover it will cost 24$. so including taxes from Sri Lanka total cost would be roughly 332$. I would recommend to buy a kindle leather cover since it will be really really useful for smooth usage for the device.

When we talk about features first I should say that if you live in Sri Lanka you will not be able to access internet since Kindle network is not support in Sri Lanka because they do not have a partnership with any Sri Lankan 3G service providers yet. But I cannot say this is impossible since you can try to replace the sim card in the kindle (Kindle is having a built in HSDPA modem with a built in AT&T Sim card) modem and change the Access Point some how (that is what I'm searching these days) and use it in the normal way we use our 3G internet connection. But I still do not have a solution since I'm bit scared to open up the kindle back cover :). Still I love it once I feel i should open up and try to do something I will do it but I hope amazon will support the Wireless support to Sri Lanka soon.

But even though you can still use kindle to read books which is a very cool experience and I would say that it will prolly increase the number of books you read. What you have to do is you can get the books pdf versions or any other kindle supporting file format and convert it in to kindle version by sending an email to "username" with attaching the document. It will send the document converting to the kindle version to you email, in few seconds so you just have to get the document and copy that to you kindle using the kindle USB cable.

The new kindle is supporting the PDF files but I'm not that happy about it, because of the font size and page seperation issues. So the best option is to use free amazon conversion service but some of the images get completely black during the convesion and you have to be bit patient in those situations. But if you are reading story books instead of techie books it will work fine for you.But still I would say this will be a cool experience and good chance for you to read more and more books, during you travel by bus and when you feel unhappy reading at the computer.

Reading experience with the kindle is great and it's screan is very much like a typical paper, since it doesn't have back light. No pain for eyes even if you keep on reading for hours. Important thing I'm seeing here is you can read in any place. If you fed up staying in your chair you can go to bed and start reading which is hard to do with your laptop,and if you want you can read in any place at your home and it will be a good hobby for you to use it during weekends. If you travel long way to your job you can keep on reading in the bus of if you are a person who travel during weekends it would be great to bring your kindle with you and read a bit. If you are a person who hate reading ebooks this is the ideal solution for you to buy an amazon kindle even thought it's an ebook reader, you never feel that you are reading an ebook, it's very much like a normal book.

I will be writing some more about the kindle in future, hopefully if I find how to crack the kindle HSDPA modem to work in Sri Lanka, I will prolly blogging, so keep in touch. Still it's amazing for me and it's really improving my reading !