TOTD #126: Creating an OSGi bundles using Eclipse and deploying in GlassFish
[URL="http://blogs.sun.com/arungupta/entry/totd_125_creating_an_osgi"]TOTD #125[/URL] showed how to create an OSGi bundle using [URL="http://netbeans.org"]NetBeans[/URL] and deploy in [URL="http://glassfish.org"]GlassFish[/URL]. This [B]T[/B]ip [B]O[/B]f [B]T[/B]he [B]D[/B]ay (TOTD) shows how to do the same using [URL="http://eclipse.org"]Eclipse[/URL]. OSGi replaced the plug-in technologies starting with Eclipse 3.0 and [URL="http://www.eclipse.org/equinox/"]Equinox[/URL] is used for all the modularity in Eclipse now.Anyway, lets get started!
[LIST=1] [*]In Eclipse 3.5.1, create a new Plug-in project by right-clicking in Package Explorer, selecting "New", Project ..." as shown below:
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-create-new-project.png[/IMG]
and choose the "Plug-in Project":
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-plugin-project.png[/IMG] [*]Enter the project name as "HelloOSGi"
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-project-name.png[/IMG]
and take all other defaults. Make sure "Generate an activator ..." checkbox is selected and click on "Next >". [*]Pick a project template as shown below:
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-plugin-template.png[/IMG]
This creates a template "Activator" class that prints the messages when the bundle is started / stopped. Click on "Next >". [*]Take the default start and stop message as:
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-activator-messages.png[/IMG]
and click on "Finish". [*]The generated directory structure looks like:
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-directory-structure.png[/IMG]
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.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 [*]Right-click on your project, select "Run As", "OSGi Framework" as shown below:
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-runas-osgi-framework.png[/IMG]
This launches the OSGi concole within Eclipse, starts the bundle, and prints the "Hello World!!" message from our Activator class in the console:
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-console-output.png[/IMG]
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. [*]Select "File", "Export ...", "Plug-in Development", and select "Deployable Plug-ins and fragments":
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-deployable-plugin.png[/IMG]
and click on "Next >". [*]Make sure "HelloOSGi" bundle is selected and specify the "domains/domain/autodeploy/bundles" directory of your GlassFish installation as specified below:
[IMG]http://blog.arungupta.me/wp-content/uploads/2010/04/totd126-export-bundle-directory.png[/IMG]
GlassFish can be downloaded from [URL="https://glassfish.dev.java.net/downloads/v3-final.html"]here[/URL], 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};|[B]Hello World!![/B]|#]
Again, the same message from Activator class.[/LIST][URL="http://blogs.sun.com/arungupta/entry/totd_118_managing_osgi_bundles"]TOTD #118[/URL] 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 [URL="http://blogs.sun.com/arungupta/tags/osgi"]OSGi entries[/URL] on [URL="http://blogs.sun.com/arungupta"]this blog[/URL].
Technorati: [URL="http://technorati.com/tags/totd"]totd[/URL] [URL="http://technorati.com/tags/eclipse"]eclipse[/URL] [URL="http://technorati.com/tags/glassfish"]glassfish[/URL] [URL="http://technorati.com/tags/osgi"]osgi[/URL] [URL="http://technorati.com/tags/equinox"]equinox[/URL]
[url=http://blogs.sun.com/arungupta/entry/totd_126_creating_an_osgi]Read More about [TOTD #126: Creating an OSGi bundles using Eclipse and deploying in GlassFish...[/url]