Tuesday, January 03, 2012

Introduction to OSGi - 2 (OSGi Services)

There are so many OSGi tutorials, blogs, and samples. I am going to add another OSGi sample/tutorial with my OSGi leanings. Here is my previous article regarding OSGi(Introduction to OSGi).

An OSGi Service is a java object instance which is registered with OSGi framework with set of attributes. Services can be accessed via service registry(performed via the class BundleContext). BundleActivator is to be invoked on start and stop. When BundleActivator call start method we are going to register our service. After that any bundle can access that service.

Service Bundle:

In service bundle you need to export your service and need to register it via service registry. When we are exporting service we export interface package only. As usual that is to hide the implementation from the other bundles.

I have created a sample OSGi project called HelloServcie

MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloService
Bundle-SymbolicName: com.chandana.hello.HelloService
Bundle-Version: 1.0.0
Bundle-Activator: com.chandana.hello.helloservice.Activator
Bundle-Vendor: CHANDANA
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0"
Export-Package: com.chandana.hello.service
Bundle-ActivationPolicy: lazy

Service Interface:
public interface  HelloService {    
    public String helloMethods();
}

Service Implementation:
public class HelloServiceImpl implements HelloService {
    @Override
    public String helloMethods() {
        String retValue = "Inside Hello Service method";
        return retValue;
    }
}

Boundle Activater:
public class Activator implements BundleActivator {
    ServiceRegistration serviceRegistration;
    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        System.out.println("Bundle Started.....!!!!!");
        HelloService service = new HelloServiceImpl();
        serviceRegistration = context.registerService(HelloService.class.getName(), service,null);
    }
    
    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        System.out.println("Bundle Stoped.....!!!!!");
        serviceRegistration.unregister();
    }
}


When we are using published services, we can import it from another Bundle. So need to create another Plug-In-Project for HelloClient

Bundle Context

Bundle context is the context of a single bundle within the OSGi runtime and it is created when Bundle get started. Bundle context can be used to Install new bundles, Obtain registered services by other bundles and Register services in the framework.

MANIFEST.MF
Import-Package: org.osgi.framework;version="1.3.0",com.chandana.hello.service

After importing the bundle, you can access the service. Important thing is service can be accessed only through bundle context. You can get actual service object via BundleContext.getService() method .

Activator class:
public class Activator implements BundleActivator {
     ServiceReference serviceReference;     
    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        serviceReference= context.getServiceReference(HelloService.class.getName());
        HelloService helloService =(HelloService)context.getService(serviceReference);
        System.out.println(helloService.helloMethods());
    }
    
    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        context.ungetService(serviceReference);
    }
}

context.getServiceReference() method return the HelloService OSGi service reference  and Using that service reference can access the actual service object.

For Run this project click on Run --> Run Configuration , In OSGi Framework  Right click and create new Run Configuration. Make sure HelloService and HelloClient .

Issues:
What happened if the service is not started when client is accessing the service?
What happened if you have stopped the service bundle?

Code Repo:
http://code.google.com/p/osgi-world/source/browse/#svn/trunk/com.chandana.hello.HelloService
http://code.google.com/p/osgi-world/source/browse/#svn/trunk/com.chandana.hello.HelloClient

Tuesday, December 13, 2011

How to Ignore Files or Directories in Subversion

We are facing a common problem when trying to commit some changes to the SVN. That is each time we have to ignore some JAR files, because those JAR files are user run time specific and if they are committed, it would not work on other local runtimes.

Here is a pretty simple solution for this problem. We can tell subversion to ignore directories or specific files as follows.

Ignore directories or specific files.
   svn propedit svn:ignore ./some_path

It will ignore all files in the specified directory. Here only wildcard indication is supported, and regular expressions are not supported.

Example wildcard supprt:  
 svn propedit svn:ignore *

Example ingnore class files with wildcard:
  svn propedit svn:ignore *.class

If you are using Eclipse SubEclispe plugin,

Right Click file or folder --> Team --> Then click on svn:ignore

Thursday, November 10, 2011

How to debug using Maven Jetty

Debugging is a better approach to find out bugs in our programs. Here is a way to debug a maven project, and I am going to run my web application in the Jetty server.

First you need to add the following environmental variables on your Windows/Linux environment.

MAVEN_OPTS = -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=y

Now go to your maven web project location through command line and start the jetter server -> mvn jetty:run



Now in eclipse debug configuration, add a remote Java application instance on port 4000. Then Add some debug points to your codes  and start the remote app instance on debug mode. You can see the steps from following screen shots.





In this manner you can debug your java web apps and find out the bugs. With this way you can debug your java web app.

if you are using maven latest version( Maven 2.0.8 or later)

run the
 mvnDebug  
command instead of
 mvn 
and attach a debugger on port 8000.

Love to hear from you! feedback(comments) are always welcome 

Tuesday, October 25, 2011

Google Cloud SQL, Hibernate JPA Support

With my last post about Google Cloud SQL(Google App Engine) with Spring Hibernate, many people had encountered the same issue about HIbenrate JPA. If you are developing Hibernate JPA based applications,there is some issues with Hibernate JPA support which you need to be awared.

When you are running the application you may get an error like this:

java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;

This is because Hibernate used JPA 2.0 specification. But Google App Engine only support JPA 1.0 specification. I also got the same issue, and therefore I used Spring Hibernate support (instead of Hibernate JPA).

"The App Engine Java SDK includes an implementation of JPA 1.0 for the App Engine datastore"
Google App Engine JPA : http://code.google.com/appengine/docs/java/datastore/jpa/


Love to hear from you! feedback(comments) are always welcome