{"id":265,"date":"2010-07-19T01:06:00","date_gmt":"2010-07-18T23:06:00","guid":{"rendered":""},"modified":"2023-03-20T13:07:39","modified_gmt":"2023-03-20T12:07:39","slug":"jetty-webapp-osgi-way","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2010\/07\/19\/jetty-webapp-osgi-way\/","title":{"rendered":"Jetty webapp osgi way"},"content":{"rendered":"<p>I will show how to expose simple Jetty OSGi service and use it to register hello world webapp. This app will serve static content from OSGi bundle and implement sample request handler under ServiceMix 4.Code is available here: <a href=\"http:\/\/github.com\/rafalrusin\/jetty-service\">http:\/\/github.com\/rafalrusin\/jetty-service<\/a><br \/>\nAlternative solutions are: using standard OSGi HTTP service <a href=\"http:\/\/www.osgi.org\/javadoc\/r4v42\/org\/osgi\/service\/http\/HttpService.html\">http:\/\/www.osgi.org\/javadoc\/r4v42\/org\/osgi\/service\/http\/HttpService.html<\/a>to register servlet; and wrap existing WAR application using PAX WEB <a href=\"http:\/\/wiki.ops4j.org\/display\/paxweb\/Pax+Web\">http:\/\/wiki.ops4j.org\/display\/paxweb\/Pax+Web<\/a>.I won&#8217;t consider those two, since Jetty itself provides flexible way to handle webapps (including registering servlets). So those two are unnecessary overhead and are less flexible. Anyway those two are usually implemented on top of Jetty.<br \/>\nSo the first thing to do is to create Jetty OSGi service. Basicly it will be a Spring Bean exposed to OSGi. Following snippet does the job:<\/p>\n<pre class=\"hl\"><span class=\"hl kwa\">&lt;bean<\/span> <span class=\"hl kwb\">id<\/span>=<span class=\"hl str\">\"jetty-service\"<\/span> <span class=\"hl kwb\">class<\/span>=<span class=\"hl str\">\"org.apache.jetty.service.JettyServiceImpl\"<\/span> <span class=\"hl kwb\">init-method<\/span>=<span class=\"hl str\">\"init\"<\/span> <span class=\"hl kwb\">destroy-method<\/span>=<span class=\"hl str\">\"destroy\"<\/span><span class=\"hl kwa\">\/&gt;<\/span>\r\n<span class=\"hl kwa\">&lt;osgi:service<\/span> <span class=\"hl kwb\">id<\/span>=<span class=\"hl str\">\"jetty-service-osgi\"<\/span> <span class=\"hl kwb\">ref<\/span>=<span class=\"hl str\">\"jetty-service\"<\/span> <span class=\"hl kwb\">interface<\/span>=<span class=\"hl str\">\"org.apache.jetty.service.api.JettyService\"<\/span> <span class=\"hl kwa\">\/&gt;<\/span><\/pre>\n<p>This will expose jetty-service to OSGi. All other components, which connect to it will wait automaticly until it&#8217;s registered.Exposed interface has following methods:<\/p>\n<pre class=\"hl\"><span class=\"hl kwa\">public<\/span> <span class=\"hl kwc\">Handler<\/span> <span class=\"hl kwd\">registerApp<\/span><span class=\"hl sym\">(<\/span><span class=\"hl kwc\">String<\/span> name<span class=\"hl sym\">,<\/span> <span class=\"hl kwc\">Handler<\/span> handler<span class=\"hl sym\">)<\/span> <span class=\"hl kwa\">throws<\/span> <span class=\"hl kwc\">Exception<\/span><span class=\"hl sym\">;<\/span>\r\n<span class=\"hl kwa\">public<\/span> <span class=\"hl kwb\">void<\/span> <span class=\"hl kwd\">unregisterApp<\/span><span class=\"hl sym\">(<\/span><span class=\"hl kwc\">Handler<\/span> handler<span class=\"hl sym\">)<\/span> <span class=\"hl kwa\">throws<\/span> <span class=\"hl kwc\">Exception<\/span><span class=\"hl sym\">;<\/span><\/pre>\n<p>Those will be invoked to register Hello World application. JettyServiceImpl on the other hand, starts embedded Jetty Server and handles apps registration.<br \/>\npackage org.apache.jetty.service;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\nimport org.apache.jetty.service.api.JettyService;\r\nimport org.mortbay.jetty.Handler;\r\nimport org.mortbay.jetty.Server;\r\nimport org.mortbay.jetty.handler.ContextHandler;\r\nimport org.mortbay.jetty.handler.ContextHandlerCollection;\r\n\r\npublic class JettyServiceImpl implements JettyService {\r\n    private Server server;\r\n    private ContextHandlerCollection rootContext;\r\n\r\n    public void init() throws Exception {\r\n        server = new Server(8080);\r\n        rootContext = new ContextHandlerCollection();\r\n        server.setHandler(rootContext);\r\n        server.start();\r\n    }\r\n\r\n    public void destroy() throws Exception {\r\n        server.stop();\r\n    }\r\n\r\n    public Handler registerApp(String name, Handler handler) throws Exception {\r\n        server.stop();\r\n        ContextHandler h = rootContext.addContext(\"\/\" + name, name);\r\n        h.setHandler(handler);\r\n        server.start();\r\n        return h;\r\n    }\r\n\r\n    public void unregisterApp(Handler handler) throws Exception {\r\n        server.stop();\r\n        rootContext.removeHandler(handler);\r\n        server.start();\r\n    }\r\n}\r\n<\/pre>\n<p>Next step is to implement sample web app. First, we need to connect jetty-service bean to make it visible in our app.<\/p>\n<pre class=\"hl\">  <span class=\"hl kwa\">&lt;osgi:reference<\/span> <span class=\"hl kwb\">id<\/span>=<span class=\"hl str\">\"jetty-service\"<\/span> <span class=\"hl kwb\">interface<\/span>=<span class=\"hl str\">\"org.apache.jetty.service.api.JettyService\"<\/span> <span class=\"hl kwb\">bean-name<\/span>=<span class=\"hl str\">\"jetty-service\"<\/span><span class=\"hl kwa\">\/&gt;<\/span><\/pre>\n<p><span class=\"hl kwa\">&lt;bean<\/span> <span class=\"hl kwb\">class<\/span>=<span class=\"hl str\">&#8220;org.apache.jetty.service.example.HelloWorld&#8221;<\/span> <span class=\"hl kwb\">init-method<\/span>=<span class=\"hl str\">&#8220;init&#8221;<\/span> <span class=\"hl kwb\">destroy-method<\/span>=<span class=\"hl str\">&#8220;destroy&#8221;<\/span><span class=\"hl kwa\">&gt;<\/span><br \/>\n<span class=\"hl kwa\">&lt;property<\/span> <span class=\"hl kwb\">name<\/span>=<span class=\"hl str\">&#8220;jettyService&#8221;<\/span> <span class=\"hl kwb\">ref<\/span>=<span class=\"hl str\">&#8220;jetty-service&#8221;<\/span><span class=\"hl kwa\">\/&gt;<\/span><br \/>\nThen, we need to implement sample app.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage org.apache.jetty.service.example;\r\n\r\nimport java.io.IOException;\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\nimport org.apache.jetty.service.api.JettyService;\r\nimport org.apache.jetty.service.util.BundleResource;\r\nimport org.mortbay.jetty.Handler;\r\nimport org.mortbay.jetty.HttpConnection;\r\nimport org.mortbay.jetty.Request;\r\nimport org.mortbay.jetty.handler.AbstractHandler;\r\nimport org.mortbay.jetty.handler.ContextHandler;\r\nimport org.mortbay.jetty.handler.ContextHandlerCollection;\r\nimport org.mortbay.jetty.handler.ResourceHandler;\r\n\r\npublic class HelloWorld {\r\n    private JettyService jettyService;\r\n    private Handler registered;\r\n\r\n    public void setJettyService(JettyService jettyService) {\r\n        this.jettyService = jettyService;\r\n    }\r\n\r\n    public void init() throws Exception {\r\n        ContextHandlerCollection handler = new ContextHandlerCollection();\r\n\r\n        handler.addContext(\"\/app\", \"app\").setHandler(new AbstractHandler() {\r\n            public void handle(String target, HttpServletRequest request,\r\n                               HttpServletResponse response, int arg3) throws IOException,\r\n                    ServletException {\r\n                response.setContentType(\"text\/html\");\r\n                response.setStatus(HttpServletResponse.SC_OK);\r\n                response.getWriter().println(\r\n                    \"Hello World from Java\"\r\n                    + request.getParameterMap()\r\n                );\r\n\r\n                Request base_request = (request instanceof Request) ? (Request) request : HttpConnection.getCurrentConnection().getRequest();\r\n                base_request.setHandled(true);\r\n            }\r\n        });\r\n\r\n        ResourceHandler resourceHandler = new ResourceHandler();\r\n        resourceHandler.setBaseResource(new BundleResource(getClass().getResource(\"\/static\")));\r\n        ContextHandler contextHandler = handler.addContext(\"\", \"\");\r\n        contextHandler.setHandler(resourceHandler);\r\n\r\n        registered = jettyService.registerApp(\"helloWorld\", handler);\r\n    }\r\n\r\n    public void destroy() throws Exception {\r\n        jettyService.unregisterApp(registered);\r\n    }\r\n}\r\n<\/pre>\n<p>Here, we register sample request handler at &#8216;app&#8217; sub path and serve static content from jar using BundleResource. Last thing is registering app using jettyService under &#8216;helloWorld&#8217; context. So our application will be exposed under <a href=\"http:\/\/localhost:8080\/helloWorld\/\">http:\/\/localhost:8080\/helloWorld\/<\/a> address.<br \/>\nServiceMix has also so called features. This is the way to collect multiple dependencies under a single name. So we have to create features.xml file, like this:<\/p>\n<pre class=\"hl\"><span class=\"hl sym\">&lt;<\/span>features<span class=\"hl sym\">&gt;<\/span>\r\n    <span class=\"hl sym\">&lt;<\/span>feature name<span class=\"hl sym\">=<\/span><span class=\"hl str\">\"jetty-service\"<\/span> version<span class=\"hl sym\">=<\/span><span class=\"hl str\">\"${project.version}\"<\/span><span class=\"hl sym\">&gt;<\/span>\r\n        <span class=\"hl sym\">&lt;<\/span>bundle<span class=\"hl sym\">&gt;<\/span>mvn<span class=\"hl sym\">:<\/span>org<span class=\"hl sym\">.<\/span>apache<span class=\"hl sym\">.<\/span>jetty<span class=\"hl sym\">.<\/span>service<span class=\"hl sym\">\/<\/span>service<span class=\"hl sym\">\/<\/span>$<span class=\"hl sym\">{<\/span>project<span class=\"hl sym\">.<\/span>version<span class=\"hl sym\">}bundle<span class=\"hl sym\">&gt;<\/span> <span class=\"hl sym\">feature<span class=\"hl sym\">&gt;<\/span><\/span><\/span><\/pre>\n<p><span class=\"hl sym\">&lt;<\/span>feature name<span class=\"hl sym\">=<\/span><span class=\"hl str\">&#8220;example-jetty-service-helloworld&#8221;<\/span> version<span class=\"hl sym\">=<\/span><span class=\"hl str\">&#8220;${project.version}&#8221;<\/span><span class=\"hl sym\">&gt;<\/span><br \/>\n<span class=\"hl sym\">&lt;<\/span>feature version<span class=\"hl sym\">=<\/span><span class=\"hl str\">&#8220;${project.version}&#8221;<\/span><span class=\"hl sym\">&gt;<\/span>jetty<span class=\"hl sym\">&#8211;<\/span>service<span class=\"hl sym\">feature<span class=\"hl sym\">&gt;<\/span><br \/>\n<span class=\"hl sym\">&lt;<\/span>bundle<span class=\"hl sym\">&gt;<\/span>mvn<span class=\"hl sym\">:<\/span>org<span class=\"hl sym\">.<\/span>apache<span class=\"hl sym\">.<\/span>jetty<span class=\"hl sym\">.<\/span>service<span class=\"hl sym\">\/<\/span>example<span class=\"hl sym\">&#8211;<\/span>helloworld<span class=\"hl sym\">\/<\/span>$<span class=\"hl sym\">{<\/span>project<span class=\"hl sym\">.<\/span>version<span class=\"hl sym\">}bundle<span class=\"hl sym\">&gt;<\/span><br \/>\n<span class=\"hl sym\">feature<span class=\"hl sym\">&gt;<\/span><br \/>\n<span class=\"hl sym\">features<span class=\"hl sym\">&gt;<\/span><\/span><\/span><\/span><\/span><br \/>\nBasicly, we can provide particular dependencies for our project.<br \/>\nNext, we do &#8216;mvn install&#8217; on our project and run apache-servicemix-4.2.0-fuse-01-00\/bin\/servicemix karaf console. On the console, we need to type following commands:<\/p>\n<pre class=\"hl\">features:addUrl mvn:org.apache.jetty.service\/service-karaf\/0.1.0-SNAPSHOT\/xml\/features\r\nfeatures:install example-jetty-service-helloworld\r\nosgi:list<\/pre>\n[ 230] [Active ] [ ] [Started] [ 60] Unnamed &#8211; org.apache.jetty.service:service:bundle:0.1.0-SNAPSHOT (0.1.0.SNAPSHOT)<br \/>\n[ 231] [Active ] [ ] [Started] [ 60] Unnamed &#8211; org.apache.jetty.service:example-helloworld:bundle:0.1.0-SNAPSHOT (0.1.0.SNAPSHOT)<br \/>\nNow, we can enter <a href=\"http:\/\/localhost:8080\/helloWorld\/\">http:\/\/localhost:8080\/helloWorld\/<\/a> to test our app.<br \/>\nAnd that&#8217;s it. OSGi and ServiceMix 4 features enable easy way to use dynamic modules in web apps. For example, it&#8217;s veryeasy to build simple web framework with loadable components on page (something like mini implementation of Portlets).<\/p>\n<div class=\"blogger-post-footer\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogger.googleusercontent.com\/tracker\/6983141563768732668-3214120969579514837?l=rrusin.blogspot.com\" alt=\"\" width=\"1\" height=\"1\" \/><\/div>\n","protected":false},"excerpt":{"rendered":"I will show how to expose simple Jetty OSGi service and use it to register hello world webapp.&hellip;\n","protected":false},"author":35,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[44],"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/265"}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=265"}],"version-history":[{"count":30,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/265\/revisions"}],"predecessor-version":[{"id":15360,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/265\/revisions\/15360"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}