Sunday, 6 December 2009

OSGi - introduction

OSGi - Open Services Gateway initiative helps to develop and deploy applications/libraries which are modular or need to be modular.This specification was started by OSGi alliance in 1999.
To develop OSGi application, the OSGi specification defines two things: first the set of services that the OSGi container needs to implement and second a contract between the OsGi container and the application. So in short, you need a OSGi container to run your OSGi complaint application. As of now there are 3 containers that are OSGi complaint; Knopflerfish,Equinox and Apache Felix.
OSGi offers several advantages
  1. one can install,uninstall,start, stop any modules of the application at runtime.
  2. Helps to build service-oriented applications
  3. Application can have more than one version of a module running at the same time.

In the next article I will show you how each of these advantages translates into code rather than just plain english sentences in your tech vocab.

Tuesday, 2 June 2009

NullPointerException in static factory-method in Spring

For one project, I was trying to test db connection at startup using some method like
ConnectionTest
{
public static void testConnection(DataSource ds){...}
..
}

which I happily configured in Spring using the factory-method attribute. To my surprise I got NullPointerException in populateMethod of Spring class AbstractAutowireCapableBeanFactory.

The reason for this was, Spring was expecting a object to be returned as it's a factory-method and fatory method will always return some object. Since my use case was to only test the connection and leave it I didnt return anything.

Hope this helps.

Monday, 1 June 2009

Binary To Text using Base64 encoding

If you need to convert binary data to text one of the recommended approach is to use base64 encoding and not direct string conversion like
byte[] byteArray = //get byte array from any image file
String str = new String(byteArray);

This is not recommended as it will not necessarily represent a valid character string in any encoding. Base64 will make sure that the byte array represents a valid ASCII character string
Ex byte[] byteArray = //get byte array from any image file
String str = new Base64Encoder().encode(byteArray);

To support this point lets take an XML String with the str String padded between some xml element like
String xmlString = <imagedata>;
xmlString+=str;
xmlString+=</imagedata>;

This xml when parsed for the image
InputSource inputSource = new InputSource(new StringReader(xmlString));
DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder();
Document document = builder.parse(inputSource);

will throw an error in the first case ie using String saying
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the element content of the document.

So a cleaner approach is to use Base64 encoding.

Sunday, 10 May 2009

GWT

Was excited to check Google GWT 1.6, I decided to try an example application using the same. But hit a road block when I got stuck when I needed more advanced features, so had to look at ExtGWT or GWT-Ext, was confused as to what to use. GWT-Ext is maintained by Sanjiv Jivan and uses ExtJS, and ExtGWT (GXT) is from the makers of ExtJS.I decided to move on ExtGWT, I made this choice based on that I would always put a bet on some company product than an individual,purely my decision. Though, GWT-Ext seemed so viable and tempting option.