Jump to: navigation, search

Difference between revisions of "Jetty/Tutorial/Jetty-OSGi SDK"

m
m
Line 8: Line 8:
 
We will provision a new Target Platform and develop a simple bundle that executes a Testunit.
 
We will provision a new Target Platform and develop a simple bundle that executes a Testunit.
 
A Target Platform defines the OSGi environment in which your bundles are compiled and debugged.
 
A Target Platform defines the OSGi environment in which your bundles are compiled and debugged.
<br clear="all" />
+
[[Image:01-JOT-new-target-platform.png|Create a new OSGi Project]]
[[Image:01-JOT-new-target-platform.png|thumb|left|Create a new OSGi Project]]
+
 
* Launch Eclipse-SDK-3.6.
 
* Launch Eclipse-SDK-3.6.
 
* Define a new Target Platform "EclipseRT-Tutorial"
 
* Define a new Target Platform "EclipseRT-Tutorial"
Line 15: Line 14:
 
Click on "Add..." and choose the option "Nothing:" start with an empty Target Platform"
 
Click on "Add..." and choose the option "Nothing:" start with an empty Target Platform"
 
* Name the Target Platform "EclipseRT Tutorial"
 
* Name the Target Platform "EclipseRT Tutorial"
<br clear="all" />
 
 
* Click on Next and choose "Add..." then "Select Software Site"
 
* Click on Next and choose "Add..." then "Select Software Site"
 
* Select the "Helios" download site as the source of the features to install in the Target Platform
 
* Select the "Helios" download site as the source of the features to install in the Target Platform

Revision as of 21:44, 7 June 2010



Introduction

Jetty-OSGi is a packaging of jetty where jetty is run as an OSGi bundle. It supports the deployment of traditional J2EE web-applications and also web-bundles where the web application is contained in a bundle.

This tutorial introduces the development and testing of web-bundles in PDE.

Provision a Target Platform and run a simple Test Unit with PDE

We will provision a new Target Platform and develop a simple bundle that executes a Testunit. A Target Platform defines the OSGi environment in which your bundles are compiled and debugged. Create a new OSGi Project

  • Launch Eclipse-SDK-3.6.
  • Define a new Target Platform "EclipseRT-Tutorial"

Open the Preferences and choose the node "Plugins-Development/Target Platform" Click on "Add..." and choose the option "Nothing:" start with an empty Target Platform"

  • Name the Target Platform "EclipseRT Tutorial"
  • Click on Next and choose "Add..." then "Select Software Site"
  • Select the "Helios" download site as the source of the features to install in the Target Platform

(Currently use: http://download.eclipse.org/jetty/7.1.3.v20100526/repository/)

  • Look for the category "Eclipse RT Target Platform" and select the PDE JUnit Support feature. Click on Finish.
  • Select the new Target Platform as the active platform.
  • Create a new OSGI Bundle project: "New Project.../Plugin-Project"
  • Select a pure OSGi bundle.
  • Generate a bundle activator.
  • Click on Finish.
  • Open the META-INF/MANIFEST.MF editor and choose the tab "dependencies"
  • In the "Imported packages" section, click on "Add..." and select the package "org.junit"
  • Open the META-INF/MANIFEST.MF editor and on the tab "Overview, select the checkbox "Activate this plugin when one of its classes is loaded"
  • Create a new class ActivatorTest
  • Make a method that will be run by JUnit to test that the activator is started:
package org.eclipse.jetty.rt.example.test;
 
import org.junit.Assert;
import org.junit.Test;
 
/**
 * Tests that the activator was indeed loaded
 */
public class ActivatorTest {
	@Test public void testActivator() throws Exception {
		Assert.assertNotNull("The activator was not started", Activator.getContext());
	}	
}
    • Right-click on the Activator-Test Class and choose "Run as .../JUnit Plugin Test"


First web-application defined in an OSGi bundle (RFC66)


Create a new OSGi Project

Create a new Plug-in Project

OSGi Project name

Choose 'OSGi bundle'

OSGi Project parameters

Choose 'Next' instead of 'Finish' to show the project templates.

OSGi Project RFC66 template

Choose the 'Jetty RFC66' template.

Web-application parameters

The web-bundle generated.

Right-click and choose run as...

Right-click on the project and choose "Run as... Jetty-on-OSGi"

Open a web-browser and check.

Open a web-browser and check that the webapp is running. Everything is configured by default at this point: jetty is running on localhost at port 8080.

Launch configuration

Run Configuration.

To customize the configuration of jetty used to run this web-application, choose the menu "Run configuration..." and select the "Launch Jetty in OSGi" node. The first tab "Jetty Configuration" points to the default jetty.home folder. Jetty-on-OSGi uses a folder hierarchy inside which it locates the configuration file(s) (${jetty.home}/etc/jetty.xml), the configuration for the central logging (${jetty.home}/resources/logback.xml). By default the SDK will generate the jetty.home folder in the PDE runtime workspace and will place the default configuration files identical to the ones distributed with jetty.

The wizard gives direct access to jetty.xml but other settings should be done by accessing the file system. (TODO: improve; for example provide a wizard to import as a project a jetty configuration so that everything can be done from eclipse.)



Debugging web-applications defined in standard java projects

CAS a java project

A very common situation is to have a set of existing java projects that define a web-application. The SDK is able to deploy them and execute them just like an RFC66 web-bundle.

Let's take a java project that defines a web-application: it contains a folder 'webapp' inside which jsp pages and WEB-INF/web.xml are located.

Jetty-OSGi won't run this as an OSGi bundle: it depends on a set of jars that are outside of the platform and eventually on other java projects.

Jetty-OSGi only needs to recognize the webapp folder and to know what is the servlet context to use. The current approach consists of adding a META-INF/MANIFEST.MF file to the project and to use the headers that jetty-osgi would use for an OSGi web-application:

Web-ContextPath: /cas
will set the context path to "/cas"
Jetty-WarFolderPath: /src/main/webapp

In fact by default Jetty-WarFolderPath is assumed to be '/src/main/webapp' and the context path is the name of the eclipse project.

It would be nice to support more sophisticated ways of identifying a java project that contains a web-application: WTP and maven's pom.xml file come to mind.


Choose the java projects to deploy as webapps

The Jetty Configuration tab displays the java projects that are identified as web-applications. The checkbox selects the projects to be debugged as web-applications. Libraries and java projects on which they eventually depend don't need to be configured. A list of the OSGi web-bundles is also displayed.

TODO: more doc. Although the java project is not an OSGi project, it is possible to inject OSGi dependencies: Use the Require-Bundle or the Import-Package in the manifest and add to the .classpath the line:

<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>

This is very useful to gradually migrate java projects to OSGi bundles: not all libraries need to be OSGi ready. Partially migrated libs are fine during development.


Developing Jetty-OSGi in PDE

Use the jetty-sdk setup; identical to the one used for the development of web-applications. Import as project the bundles to work on: [1]

Start hacking using the PDE just like any other OSGi bundle or eclipse plugin. Launch using the jetty configuration. The PDE will use the bundles as defined in the workspace as a replacement for the jetty bundles.

Additional Resources

Jetty-OSGi, RFC66, PDE We are working on migrating all building an update site and executing the build on eclipse with the rest of the jetty@eclipse project. In the mean time, the latest code is here: [http://github.com/intalio/hightide-on-osgi