TOTD #126: Creating an OSGi bundles using Eclipse and deploying in GlassFish

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.framework.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.0
    Bundle-ManifestVersion: 2
    Bundle-Name: HelloOSGi
    Bundle-SymbolicName: HelloOSGi
    Bundle-Version: 1.0.0.qualifier
    Bundle-Activator: helloosgi.Activator
    Import-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.std.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.fileinstall-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

Comments:

I noticed that the sun is setting in your photo.

http://blogs.sun.com/arungupta/resource/personal/arun-jan1-2010-cropped.png

Posted by Wesley on June 02, 2010 at 04:12 AM PDT #

I noticed that the sun is setting in your photo. I hope Oracle is treating the Sun folks well.

Posted by Wesley on June 02, 2010 at 04:14 AM PDT #

Wesley,

The "Sun" has indeed set and that's why this photograph was taken with that background. Anyway Oracle is taking very good care of us and most importantly it makes money :-)

Posted by Arun Gupta on June 02, 2010 at 05:33 AM PDT #

I would like to use glassfish4 & eclipse(juno/kepler)
because there is not alot info about GF4 on the web I will try here :)

I want to build a osgi bundle that depent on bundels running in GF4
GF tools for eclipse work fine for dyn Web app but I do not find how I can tell eclipse to use GF4 as target platform for OSGI.

Also it would be nice to be able to have the "Debug As on server" for a GF osgi bundle
or we need a "Dyn Web + bundle (WAB)project"

Posted by guest on September 06, 2013 at 04:27 AM PDT #

Read about GlassFish and OSGi in a white paper published at:

http://glassfish.java.net/public/GF-OSGi-Features.pdf

Feel free to follow up on GlassFish Forums after that:

http://www.java.net/forums/glassfish/glassfish

Posted by Arun Gupta on September 18, 2013 at 10:44 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today