Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Servlet with Jetty 7 and OSGI

derheld42 edited this page Dec 6, 2013 · 1 revision

UPDATE: I learned that the org.apache.felix.http.jetty-2.2.0 (from bndhub bundles) or 2.2.1 bundle from felix distribution contain Jetty 7. Currently Jetty 9 is the version that contains support for WebSockets. Look at [WebSocket with Jetty 9 and OSGI](WebSocket with Jetty 9 and OSGI) for more details relating to WebSockets.

My goal -- Attempt to use a Servlet (then WebSocket) with jetty and OSGI.

I tried to follow this: http://www.ralfebert.de/archive/java/osgi_server/

However, I ran into this: http://www.mail-archive.com/users@felix.apache.org/msg14746.html

And here's my example solution:

bnd.bnd -- added org.apache.felix.http.jetty to buildpath.

run.bndrun -- My runrequires:

-runrequires: \
	osgi.identity;filter:='(osgi.identity=org.apache.felix.gogo.shell)',\
	osgi.identity;filter:='(osgi.identity=org.apache.felix.gogo.command)',\
	osgi.identity;filter:='(osgi.identity=org.apache.felix.webconsole)',\
	osgi.identity;filter:='(osgi.identity=org.apache.felix.configadmin)',\
	osgi.identity;filter:='(osgi.identity=org.apache.felix.metatype)',\
	osgi.identity;filter:='(osgi.identity=org.apache.felix.log)',\
	osgi.identity;filter:='(&(osgi.identity=osgi.cmpn)(version>=4.2))',\
	osgi.identity;filter:='(&(osgi.identity=org.apache.felix.scr)(version>=1.6.0))',\
	osgi.identity;filter:='(osgi.identity=myservice)',\
	osgi.identity;filter:='(osgi.identity=org.apache.felix.http.jetty)',\
	osgi.identity;filter:='(osgi.identity=org.apache.felix.http.whiteboard)'

HttpContextRegister.java --- required to provide a bundle sharable HttpContext. If you don't do this the Servlet won't come up due to an inability to share HttpContexts bundles (i.e. the default HttpContext is in a different bundle and isn't shareable -- thus you need to create your own HttpContext)

package foobar;
@Component(provide = { HttpContext.class, HttpContextRegister.class }, properties = {
		"contextId=foo", "context.shared=true" }, immediate = true)
public class HttpContextRegister implements HttpContext {

	@Override
	public boolean handleSecurity(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		return true;
	}

	@Override
	public URL getResource(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getMimeType(String name) {
		// TODO Auto-generated method stub
		return null;
	}

}

MyServlet.java -- Simple example Servlet

package foobar;
@Component(provide = Servlet.class, properties = { "alias=/", "contextId=foo",
		"init.test=sdfdf" })
public class MyServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getWriter().append("Hello World!");
	}

	@Reference(multiple = false, optional = false, dynamic = false)
	public void setHttpContextRegister(HttpContextRegister r) {
	}
}