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

From Eclipsepedia

Jump to: navigation, search
m
m
Line 76: Line 76:
 
[[Media:Org.eclipse.rt.example.test-1.0.0.qualifier.zip|Archive for this project]]
 
[[Media:Org.eclipse.rt.example.test-1.0.0.qualifier.zip|Archive for this project]]
  
== First web-application defined in an OSGi bundle (RFC66) ==
+
== Setup Jetty and a first Web-bundle ==
<br clear="all" />
+
[[Image:Jetty-sdk-01-create-plugin-project.png|thumb|left|Create a new OSGi Project]]
+
  
Create a new Plug-in Project
+
=== Setup Jetty Target Component ===
<br clear="all" />
+
* Choose Preferences and Edit the "EclipseRT Tutorial" target platform definition
[[Image:Jetty-sdk-02-name-plugin-project.png|thumb|left|OSGi Project name]]
+
* Select the Helios site and choose "Edit"
 +
* Add the "Jetty Target Component" feature to the list of features provisioned
 +
[[Image:13-JOT-Target-Platform-add-jetty.png|center|Lazy Activation policy]]
  
Choose 'OSGi bundle'
+
=== Optional: install Jetty tooling ===
<br clear="all" />
+
The Jetty tooling feature contains 2 templates for Pluin projects that make it easy to get started developing web-bundles.
[[Image:Jetty-sdk-03-set-plugin-project.png|thumb|left|OSGi Project parameters]]
+
  
Choose 'Next' instead of 'Finish' to show the project templates.
+
=== Create a first web-bundle ===
<br clear="all" />
+
Create a new OSGi bundle. Make sure it is not an eclipse plugin. Name it "org.eclipse.jetty.example.webapp"
[[Image:Jetty-sdk-04-b-use-rfc66-template.png|thumb|left|OSGi Project RFC66 template]]
+
  
Choose the 'Jetty RFC66' template.
+
If you have installed the Jetty tooling feature, click next until you can choose a template. Everything will be generated.
<br clear="all" />
+
[[Image:Jetty-sdk-05-webapp-project.png|thumb|left|Web-application parameters]]
+
  
The web-bundle generated.
+
If you are creating the bundle with the jetty template you will need to add to the MANIFEST.MF:
<br clear="all" />
+
* The import-package: <source>javax.servlet;version="2.5.0"</source>
 +
* A new line that defines the context path for the web-application: <source>Web-ContextPath: /test</source>
 +
It should look like this:
 +
<source>
 +
Manifest-Version: 1.0
 +
Bundle-ManifestVersion: 2
 +
Bundle-Name: Webapp
 +
Bundle-SymbolicName: org.eclipse.jetty.example.webapp
 +
Bundle-Version: 1.0.0.qualifier
 +
Bundle-Activator: org.eclipse.jetty.example.webapp.Activator
 +
Import-Package: org.osgi.framework;version="1.3.0",
 +
javax.servlet;version="2.5.0"
 +
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 +
Web-ContextPath: /test
 +
</source>
  
[[Image:Jetty-sdk-06-run-webapp.png|thumb|left|Right-click and choose run as...]]
+
* Now create a new file WEB-INF/web.xml at the root of the project.
Right-click on the project and choose "Run as... Jetty-on-OSGi"
+
And define a simple servlet-mapping:
<br clear="all"/>
+
  
[[Image:Jetty-sdk-07-hello-in-browser.png|thumb|left|Open a web-browser and check.]]
+
<source lang="xml">
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.
+
<br clear="all"/>
+
  
=== Launch configuration ===
+
</source>
  
[[Image:Jetty-sdk-08-run-config.png|thumb|left|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.)
 
 
<br clear="all"/>
 
 
 
=== Debugging web-applications defined in standard java projects ===
 
[[Image:Jetty-sdk-10-java-project.png|thumb|left|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.
 
<br clear="all"/>
 
 
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:
 
<pre>Web-ContextPath: /cas</pre> will set the context path to "/cas"
 
<pre>Jetty-WarFolderPath: /src/main/webapp</pre>
 
 
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.
 
 
<br clear="all"/>
 
[[Image:Debug-crm-cas-and-more-webapps.png|thumb|left|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.
 
<br clear="all"/>
 
 
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:
 
<pre>&lt;classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/&gt;</pre>
 
 
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: [http://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk/jetty-osgi/]
 
 
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.
 
  
 
| more = [[Jetty/Feature/Jetty_OSGi|Jetty-OSGi]], RFC66, PDE
 
| more = [[Jetty/Feature/Jetty_OSGi|Jetty-OSGi]], RFC66, PDE

Revision as of 23:32, 7 June 2010



Contents

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.

Configure 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.

Target Platform with JUnit Support

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"

Create a new OSGi Project
  • Name the Target Platform "EclipseRT Tutorial"
  • Click on Next and choose "Add..." then "Select Software Site"
Name it
  • Select the "Helios" download site as the source of the features to install in the Target Platform
  • Look for the category "Eclipse RT Target Platform" and select the PDE JUnit Support feature. Click on Finish.

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

Select the JUnit Support Feature


  • Select the new Target Platform as the active platform.
Activate the Target Platform

OSGi bundle with a test unit

  • Create a new OSGI Bundle project: "New Project.../Plugin-Project"
  • Name the project "org.eclipse.jetty.rt.example.test"
  • Choose a pure OSGi bundle.
Activate the Target Platform


  • Generate a bundle activator.
  • Click on Finish.
Activate the Target Platform


  • 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"
Lazy Activation policy


  • 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"
Lazy Activation policy


  • 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"
  • The test should pass
Lazy Activation policy

Archive for this project

Setup Jetty and a first Web-bundle

Setup Jetty Target Component

  • Choose Preferences and Edit the "EclipseRT Tutorial" target platform definition
  • Select the Helios site and choose "Edit"
  • Add the "Jetty Target Component" feature to the list of features provisioned
Lazy Activation policy

Optional: install Jetty tooling

The Jetty tooling feature contains 2 templates for Pluin projects that make it easy to get started developing web-bundles.

Create a first web-bundle

Create a new OSGi bundle. Make sure it is not an eclipse plugin. Name it "org.eclipse.jetty.example.webapp"

If you have installed the Jetty tooling feature, click next until you can choose a template. Everything will be generated.

If you are creating the bundle with the jetty template you will need to add to the MANIFEST.MF:

  • The import-package:

    Invalid language.

    You need to specify a language like this: <source lang="html4strict">...</source>

    Supported languages for syntax highlighting:

    4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, otj, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


    javax.servlet;version="2.5.0"
  • A new line that defines the context path for the web-application:

    Invalid language.

    You need to specify a language like this: <source lang="html4strict">...</source>

    Supported languages for syntax highlighting:

    4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, otj, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


    Web-ContextPath: /test

It should look like this:

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, otj, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Webapp
Bundle-SymbolicName: org.eclipse.jetty.example.webapp
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: org.eclipse.jetty.example.webapp.Activator
Import-Package: org.osgi.framework;version="1.3.0",
 javax.servlet;version="2.5.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Web-ContextPath: /test
  • Now create a new file WEB-INF/web.xml at the root of the project.

And define a simple servlet-mapping:

 

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