TOTD #125 showed how to create an OSGi bundle using NetBeans and deploy in GlassFish. This Tip Of The Day (TOTD) shows how to do the same using Eclipse. OSGi replaced the plug-in technologies starting with Eclipse 3.0 and Equinox is used for all the modularity in Eclipse now.Anyway, lets get started!
  1. In Eclipse 3.5.1, create a new Plug-in project by right-clicking in Package Explorer, selecting "New", Project ..." as shown below:



    and choose the "Plug-in Project":

  2. Enter the project name as "HelloOSGi"



    and take all other defaults. Make sure "Generate an activator ..." checkbox is selected and click on "Next >".
  3. Pick a project template as shown below:



    This creates a template "Activator" class that prints the messages when the bundle is started / stopped. Click on "Next >".
  4. Take the default start and stop message as:



    and click on "Finish".
  5. The generated directory structure looks like:



    and the generated source code looks like:

    package helloosgi;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class Activator implements BundleActivator { /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi. framework.BundleContext) */ public void start(BundleContext context) throws Exception { System.out.println("Hello World!!"); } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.f ramework.BundleContext) */ public void stop(BundleContext context) throws Exception { System.out.println("Goodbye World!!"); }}Notice the imports are from standard org.osgi.framework.* package. The generated manifest looks like:

    Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: HelloOSGiBundle-SymbolicName: HelloOSGiBundle-Version: 1.0.0.qualifierBundle-Activator: helloosgi.ActivatorImport-Package: org.osgi.framework;version="1.3.0"Bundle-RequiredExecutionEnvironment: J2SE-1.5
  6. Right-click on your project, select "Run As", "OSGi Framework" as shown below:



    This launches the OSGi concole within Eclipse, starts the bundle, and prints the "Hello World!!" message from our Activator class in the console:



    This runs our newly created OSGi bundle using the Equinox framework within Eclipse. Our goal however is to run this bundle in GlassFish and so lets do that.
  7. Select "File", "Export ...", "Plug-in Development", and select "Deployable Plug-ins and fragments":



    and click on "Next >".
  8. Make sure "HelloOSGi" bundle is selected and specify the "domains/domain/autodeploy/bundles" directory of your GlassFish installation as specified below:



    GlassFish can be downloaded from here, unzipped, and started as "asadmin start-domain". And then you see the following message in your GlassFish logs:

    [#|2010-04-08T10:25:10.895-0700|INFO|glassfishv3.0|javax.enterprise.system.st d.com.sun.enterprise.v3.services.impl|_ThreadID=24 ;_ThreadName={felix.fileinstall.poll=5000, felix.fileinstall.bundles.new.start=true, service.pid=org.apache.felix.fileinstall.b3dcd962-8b41-4669-858b-7c2e7d32d5c8, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/v3/final/glassfishv3/glassfish/domains/domain1/autodeploy/bundles/, felix.fileinstall.filename=org.apache.felix.filein stall-autodeploy-bundles.cfg, service.factorypid=org.apache.felix.fileinstall, felix.fileinstall.debug=1};|Hello World!!|#]

    Again, the same message from Activator class.
TOTD #118 shows other ways to manage OSGi bundles in GlassFish.

This blog showed how to create an OSGi bundle using Eclipse and deployed in GlassFish v3.

Also read other OSGi entries on this blog.

Technorati: totd eclipse glassfish osgi equinox






Read More about [TOTD #126: Creating an OSGi bundles using Eclipse and deploying in GlassFish...