Monday Apr 12, 2010

TOTD #127: Embedding GlassFish in an existing OSGi runtime - Eclipse Equinox

GlassFish has a modular architecture based upon OSGi and bundles Apache Felix as the OSGi runtime. TOTD #103 explains how to run GlassFish with two other OSGi runtimes (Equinox and Knopflerfish). However you may already have an OSGi runtime in your environment and like to run GlassFish within it. Sahoo blogged about how to embed GlassFish in Equinox.

This Tip Of The Day (TOTD) provides complete set of steps with Equinox and the different download options.

  1. There are two download options for Equinox - SDK or Framework. The framework is an easier option to start with as its just a single JAR. So create a top-level directory as "equinox-3.5.2" and download the latest framework JAR from here. Complete details about the starting and configuring the framework are described here.
  2. Start the framework and check its status as:
    ~/tools/equinox-3.5.2 >java -jar org.eclipse.osgi_3.5.2.R35x_v20100126.jar -console
    osgi> ss
    Framework is launched.
    id State Bundle
    0 ACTIVE org.eclipse.osgi_3.5.2.R35x_v20100126
    Notice only one bundle, i.e. Equinox main bundle, is currently active.
  3. Download and unzip the latest GlassFish 3.1 nightly build.
  4. Shutdown the framework by typing "exit" on the console or Ctrl+C. Copy "glassfish/osgi/equinox/configuration/config.ini" to "equinox-3.5.2/configuration" directory. This is required because the version number of some packages exported by JRE is not uniform across frameworks. This will hopefully be fixed in future versions of the frameworks.
  5. Sahoo's blog describes an OSGi bundle that controls the lifecycle of GlassFish. Download and unzip the OSGi bundle's Maven project.
  6. Edit "src/main/java/sahoo/embeddedgf/" file and change the value of "installRoot" and "instanceRoot" on line 73 and 74 to match the location of your GlassFish install. Build the bundle as:
    mvn package

    which generates "target/embeddedgf-1.0-SNAPSHOT.jar". The bundle performs three tasks:
    1. Install all the bundles from "glassfish/modules" directory.
    2. Creates a configuration with "com.sun.aas.installRoot" and "com.sun.aas.instanceRoot" properties.
    3. Starts the main GlassFish bundle.
  7. Start the framework again, install the bundle, and check its status as:
    osgi> install file:///Users/arungupta/samples/v3/osgi/embeddedgf/target/embeddedgf-1.0-SNAPSHOT.jar
    Bundle id is 1
    osgi> ss
    Framework is launched.
    id      State       Bundle
    0       ACTIVE      org.eclipse.osgi_3.5.2.R35x_v20100126
    1       INSTALLED   sahoo.embeddedgf_1.0.0.SNAPSHOT

    Now GlassFish bundle is installed but not started. Optionally, the framework may be started with "-clean" switch that clears out the cache and starts with a clean slate.
  8. Start the newly installed bundle and see the log messages as:
    osgi> start 1
    osgi> [#|2010-04-10T10:46:04.616-0700|INFO|glassfishv3.0|com.sun.grizzly.config.GrizzlyServiceListener|_ThreadID=10;_ThreadName=Configuration Updater;|Perform lazy SSL initialization for the listener 'http-listener-2'|#]
    [#|2010-04-10T10:46:04.700-0700|INFO|glassfishv3.0|null|_ThreadID=11;_ThreadName=Thread-13;|Grizzly Framework 1.9.19-beta1 started in: 26ms listening on port 4848|#]
    [#|2010-04-10T10:46:04.700-0700|INFO|glassfishv3.0|null|_ThreadID=12;_ThreadName=Thread-9;|Grizzly Framework 1.9.19-beta1 started in: 50ms listening on port 8181|#]
    [#|2010-04-10T10:46:04.709-0700|INFO|glassfishv3.0|null|_ThreadID=13;_ThreadName=Thread-8;|Grizzly Framework 1.9.19-beta1 started in: 135ms listening on port 8080|#]
    . . .
    [#|2010-04-10T10:46:11.625-0700|INFO|glassfishv3.0|null|_ThreadID=21;_ThreadName={felix.fileinstall.poll=5000,,, felix.fileinstall.dir=/Users/arungupta/tools/glassfish/3.1/apr9/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};|{felix.fileinstall.poll (ms) = 5000, felix.fileinstall.dir = /Users/arungupta/tools/glassfish/3.1/apr9/glassfishv3/glassfish/domains/domain1/autodeploy/bundles, felix.fileinstall.debug = 1, = true, felix.fileinstall.tmpdir = /var/folders/+E/+E6YtSvGGEKNwOA77I-9Fk+++TI/-Tmp-/fileinstall--1798045578591636699, felix.fileinstall.filter = null}|#]
  9. Check the status of installed bundle as:
    osgi> ss
    Framework is launched.
    id      State       Bundle
    0       ACTIVE      org.eclipse.osgi_3.5.2.R35x_v20100126
                        Fragments=89, 106, 109
    1       ACTIVE      sahoo.embeddedgf_1.0.0.SNAPSHOT
    2       RESOLVED    org.glassfish.appclient.acc-config_3.1.0.SNAPSHOT
    3       RESOLVED    org.glassfish.admin.cli_3.1.0.SNAPSHOT
    4       RESOLVED    org.glassfish.admin.core_3.1.0.SNAPSHOT
    5       RESOLVED    org.glassfish.admin.util_3.1.0.SNAPSHOT
    . . .
    220     ACTIVE      org.glassfish.web.weld-integration_3.1.0.SNAPSHOT
    221     RESOLVED    org.jboss.weld.osgi-bundle_1.0.1.SP1
    222     RESOLVED    com.ctc.wstx_0.0.0
    223     RESOLVED

    So now all the GlassFish modules are installed and the required ones are started. "http://localhost:8080" shows the default web page and "asadmin" can be used to issue the standard commands.

If you downloaded Eclipsed SDK, then the framework needs to be started as:

java -jar plugins/org.eclipse.osgi_3.5.2.R35x_v20100126.jar -console

and the configuration file needs to be copied to "plugins/configuration" directory.

A future blog will show how to run GlassFish inside Eclipse as an RCP, however this requires issue #11782 to be fixed.

There are several other OSGi entries on this blog or on Sahoo's blog.

How are you using GlassFish's OSGi capabilities ?

Technorati: glassfish osgi embedded eclipse equinox

Thursday Apr 08, 2010

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||_ThreadID=24;_ThreadName={felix.fileinstall.poll=5000,,, 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

Monday Sep 14, 2009

TOTD #103: GlassFish v3 with different OSGi runtimes – Felix, Equinox, and Knoplerfish

GlassFish v3 uses standard OSGi APIs to achieve modularity. It bundles Apache Felix 2.0 as the OSGi runtime and can run on any other OSGi R4.1 compliant runtimes as well. This blog will use previously described instructions and show how to run GlassFish v3 on Eclipse Equinox and Knoplerfish. The complete details about different OSGi runtimes is described here, this TOTD provide detailed steps and overrides the instructions defined here (and here and here).

This blog is using a CI build from last week but you should try the instructions on a promoted build.

GlassFish with Felix - Felix 2.0.0 is bundled with GlassFish v3 and requires no additional configurations.

  1. Start GlassFish as:
    ~/tools/glassfish/v3/2269/glassfishv3/glassfish >./bin/asadmin start-domain --verbose

    and see the following messages in the log:
    Sep 11, 2009 4:41:16 PM com.sun.enterprise.glassfish.bootstrap.ASMain main
    INFO: Launching GlassFish on Felix platform
    Welcome to Felix
    . . .
    Sep 11, 2009 4:41:30 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: GlassFish v3  startup time : Felix(9669ms) startup services(4453ms) total(14122ms)
  2. Optionally, connect to Felix runtime using Apache Felix Remote Shell (described here) as:

    ~/tools/glassfish/v3/2269/glassfishv3 >telnet localhost 6666
    Trying ::1...
    telnet: connect to address ::1: Connection refused
    Trying fe80::1...
    telnet: connect to address fe80::1: Connection refused
    Connected to localhost.
    Escape character is '\^]'.
    Felix Remote Shell Console:

    and type "help" to see the list of commands available as:

    -> help
    Use 'help ' for more information.
  3. See the list of OSGi modules installed as:
    -> ps
       ID   State         Level  Name
    [   0] [Active     ] [    0] System Bundle (2.0.0)
    [   1] [Active     ] [    1] HK2 OSGi Main Bundle (0.3.91)
    [   2] [Installed  ] [    1] EclipseLink Core (2.0.0.v20090821-r4934)
    [   3] [Resolved   ] [    1] Hibernate validator library and Validation API (JSR 303) repackaged as OSGi and HK2 bundle (3.0.0.JBoss-400Beta3)
    [   4] [Installed  ] [    1] Connector (gluecode) for GlassFish Java EE Connector Architecture Runtime (3.0.0.SNAPSHOT)
    [   5] [Active     ] [    1] GlassFish Flashlight Framework (3.0.0.SNAPSHOT)
    . . .
    [ 201] [Resolved   ] [    1] glassfish-corba-asm (3.0.0.b024)
    [ 202] [Resolved   ] [    1] MIME streaming extension (1.4)
    [ 203] [Installed  ] [    1] Admin Console Connectors Plugin (3.0.0.SNAPSHOT)
    [ 204] [Installed  ] [    1] Apache Felix Shell TUI (1.4.0)
    [ 205] [Resolved   ] [    1] org.glassfish.core.glassfish-extra-jre-packages
  4. Use "headers" command to display the bundle header properties for any bundle. "help <command>" shows more information about each command displayed in the output of "help". Finally type "exit" to quit the shell.

GlassFish on Equinox

  1. Download Equinox latest release from (3.5 as of this writing).
  2. Unzip the contents and copy the Equinox jar file to "osgi/equinox" directory of GlassFish as shown below:
    ~/tools/glassfish/v3/2269/glassfishv3/glassfish >unzip ~/Downloads/ -d ~/tools/equinox-3.5
    ~/tools/glassfish/v3/2269/glassfishv3/glassfish >cp ~/tools/equinox-3.5/plugins/org.eclipse.osgi_3.5.0.v20090520.jar ./osgi/equinox
  3. Start GlassFish as:
    ~/tools/glassfish/v3/2269/glassfishv3 >GlassFish_Platform=Equinox ./bin/asadmin start-domain --verbose

    and see the following messages in the log:
    Sep 11, 2009 5:11:13 PM com.sun.enterprise.glassfish.bootstrap.ASMain main
    INFO: Launching GlassFish on Equinox platform
    Listening on port 6667 ...
    . . .
    Sep 11, 2009 5:11:19 PM com.sun.enterprise.v3.server.AppServerStartup run
    INFO: GlassFish v3  startup time : Equinox(3649ms) startup services(1843ms) total(5492ms)

    Note, Equinox platform started on port 6667 for remote management.
  4. Optionally, connect to Equinox runtime as:

    ~/tools/glassfish/v3/2269/glassfishv3/glassfish >telnet localhost 6667
    Trying ::1...
    Connected to localhost.
    Escape character is '\^]'.

    Make sure to use the port number specified during the startup. Type "help" to see the list of commands available as:
    osgi> help
    ---Controlling the OSGi framework---
            launch - start the OSGi Framework
            shutdown - shutdown the OSGi Framework
            close - shutdown and exit
            exit - exit immediately (System.exit)
            init - uninstall all bundles
            setprop = - set the OSGi property
    ---Controlling Bundles---
    . . . 
    ---Controlling the Console---
            more - More prompt for console output
            disconnect - Disconnects from telnet session
  5. See the list of OSGi modules installed as:
    osgi> ss
    Framework is launched.
    id      State       Bundle
    0       ACTIVE      org.eclipse.osgi_3.5.0.v20090520
                        Fragments=62, 128, 205
    1       ACTIVE      com.sun.enterprise.osgi-main_0.3.91
    2       RESOLVED    org.eclipse.persistence.core_2.0.0.v20090821-r4934
    . . . 
    203     RESOLVED    org.glassfish.admingui.console-jca-plugin_3.0.0.SNAPSHOT
    204     RESOLVED
    205     RESOLVED    org.glassfish.core.glassfish-extra-jre-packages_0.0.0

    Typing "help" shows a comprehensive list of commands. Finally type "disconnect" to terminate the shell. Do not type "exit" as it terminates the Equinox shell and killing the GlassFish process as well.

GlassFish with Knoplerfish - As mentioned here, GlassFish v3 requires at least R4.1 APIs. All Knopflerfish releases prior to 3.0.0 are R4 compliant and so will not work with the GlassFish v3 latest builds. Knopflerfish 3.0.0 beta1 is the first build that is R4.2 compliant but it does not start on Mac OS X 10.5.8 as described in issue #2858798. So until then, you you can follow the instructions documented at  GlassFish v3 on OSGi KnopflerFish and Using KnopflerFish Desktop in GlassFish v3 to get them running on GlassFish v3 TP2 or Prelude.

Some additional details missing form the entries above

  1. Download Knoplerfish 2.3.3 and install.
  2. In "knopflerfish_osgi_2.3.3/", copy "props.xargs" to "gf.xargs" and add the lines mentioned here. Make sure to copy the Knopflerfish related files to the modules directory and change the location accordingly.
  3. Create "gfpackages.txt" by copying "felix/conf/" from the GlassFish directory. List each package specified in the "gf-packages" property on a single line. There should be no other character such as ";" or "," or "/" on a line, only the package name.
  4. Start GlassFish as:
    ~/tools/glassfish/v3/2269/glassfishv3/glassfish >KNOPFLERFISH_HOME=/Users/arungupta/tools/knopflerfish_osgi_2.3.3/ GlassFish_Platform=Knopflerfish ./bin/asadmin start-domain --verbose
    and see the following messages in the log:
    Knopflerfish OSGi framework, version 4.1.10
    Copyright 2003-2009 Knopflerfish. All Rights Reserved.
    See for more information.
    Loading xargs url file:/Users/arungupta/tools/knopflerfish_osgi_2.3.3/
    Removed existing fwdir /Users/arungupta/tools/knopflerfish_osgi_2.3.3/
    Installed: file:/Users/arungupta/tools/glassfish/v3/glassfishv3-tp2/glassfish/modules/tiger-types-osgi-0.2.1.jar (id#1)
    Installed: file:/Users/arungupta/tools/glassfish/v3/glassfishv3-tp2/glassfish/modules/auto-depends-0.2.1.jar (id#2)
    Installed: file:/Users/arungupta/tools/glassfish/v3/glassfishv3-tp2/glassfish/modules/config-0.2.1.jar (id#3)
    . . .
    Started: file:/Users/arungupta/tools/glassfish/v3/glassfishv3-tp2/glassfish/modules/cm_all-2.0.1.jar (id#7)
    Started: file:/Users/arungupta/tools/glassfish/v3/glassfishv3-tp2/glassfish/modules/console_all-2.0.1.jar (id#8)
    Started: file:/Users/arungupta/tools/glassfish/v3/glassfishv3-tp2/glassfish/modules/desktop_all-2.3.11.jar (id#11)

    and finally see the Knopflerfish console as:

The instructions will be updated after a more stable release of Knopflerfish is available.

A complete archive of all the tips is available here.

Technorati: totd glassfish v3 osgi apache felix eclipse equinox knopflerfish


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


« July 2016