Return-Path: X-Original-To: apmail-camel-users-archive@www.apache.org Delivered-To: apmail-camel-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 594D2F170 for ; Tue, 16 Apr 2013 10:46:26 +0000 (UTC) Received: (qmail 99930 invoked by uid 500); 16 Apr 2013 10:46:26 -0000 Delivered-To: apmail-camel-users-archive@camel.apache.org Received: (qmail 99696 invoked by uid 500); 16 Apr 2013 10:46:25 -0000 Mailing-List: contact users-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@camel.apache.org Delivered-To: mailing list users@camel.apache.org Received: (qmail 99677 invoked by uid 99); 16 Apr 2013 10:46:25 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Apr 2013 10:46:25 +0000 X-ASF-Spam-Status: No, hits=2.2 required=5.0 tests=HTML_MESSAGE,MIME_QP_LONG_LINE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [188.40.30.104] (HELO www221.your-server.de) (188.40.30.104) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Apr 2013 10:46:20 +0000 Received: from [163.166.150.54] (helo=[192.168.6.208]) by www221.your-server.de with esmtpsa (TLSv1:DES-CBC3-SHA:168) (Exim 4.74) (envelope-from ) id 1US3Og-0001p6-8A for users@camel.apache.org; Tue, 16 Apr 2013 12:45:58 +0200 User-Agent: Microsoft-MacOutlook/14.3.2.130206 Date: Tue, 16 Apr 2013 11:45:51 +0100 Subject: Using the Servlet component in a Blueprint container From: Andreas Gies To: Message-ID: Thread-Topic: Using the Servlet component in a Blueprint container Mime-version: 1.0 Content-type: multipart/alternative; boundary="B_3448957558_2267715" X-Authenticated-Sender: andreas@wayofquality.de X-Virus-Scanned: Clear (ClamAV 0.97.6/17019/Tue Apr 16 06:41:21 2013) X-Virus-Checked: Checked by ClamAV on apache.org --B_3448957558_2267715 Content-type: text/plain; charset="ISO-8859-1" Content-transfer-encoding: quoted-printable Hello Community,=20 I apologize for the long post upon list entry, but I thought this might be interesting.=20 I was working on a use case that involves running Camel inside an OSGi Container and for configuration I am using Apache Aries 1.0.0. As i have a HTTP Service already running and need to support HTTP posts, I figured using the servlet component would make sense. This having said, trying to use the configuration found here (http://camel.apache.org/servlet.html) doesn't work out of the box. The main reason seems to be that CamelServlet is a class rather than an interface, so it can't be registered as an OSGi service easily. Googling around indicated that using the attribute "ext:classes" on the blueprint reference should do the trick, but I got errors from Blueprint not being able to create the service proxy. I stepped back and tried to understand what *should* happen and came up wit= h a solution / workaround that I thought was worth sharing. 1. I need to gain access to the CamelServlet by OSGI means. 2. I need to stick a HTTP Registry into that servlet. 3. I need to define an endpoint that wires the Registry and the Servlet together.=20 First, I have defined an Interface CamelServletProvider that has merely one method: public interface CamelServletProvider { public CamelServlet getCamelServlet(); } with an associated implementation: public class CamelServletProviderImpl implements CamelServletProvider { private CamelServlet camelServlet =3D null; public void setCamelServlet(CamelServlet camelServlet) { this.camelServlet =3D camelServlet; } @Override public CamelServlet getCamelServlet() { return camelServlet; } } The idea for this class is, that it lives alongside the CamelServlet and a CamelServletProvider is registered as an OSGi Service like this: Now we have a bundle that exposes a Camel Servlet as HTTP transport which I can use in endpoints to come in another bundle. To make this work I need a HTTP Registry, that I can stick into Blueprint and that reacts to CamelServletProviders coming and going. The DefaultHTTPRegistry in Camel reacts to javax.servlet.Servlet. I have simply cloned the DefaultHTTPRegistry and made it react to CamelServletProviders: public class CamelServletHTTPRegistry implements HttpRegistry { private static final transient Logger LOG =3D LoggerFactory.getLogger( CamelServletHTTPRegistry.class); private static HttpRegistry singleton; private final Set consumers; private final Set providers; public CamelServletHTTPRegistry() { consumers =3D new HashSet(); providers =3D new HashSet(); } /** * Lookup or create a HttpRegistry */ public static synchronized HttpRegistry getSingletonHttpRegistry() { if (singleton =3D=3D null) { singleton =3D new CamelServletHTTPRegistry(); } return singleton; } @Override public void register(HttpConsumer consumer) { LOG.debug("Registering consumer for path {} providers present: {}", consumer.getPath(), providers.size()); consumers.add(consumer); for (CamelServlet provider : providers) { provider.connect(consumer); } } @Override public void unregister(HttpConsumer consumer) { LOG.debug("Unregistering consumer for path {} ", consumer.getPath()); consumers.remove(consumer); for (CamelServlet provider : providers) { provider.disconnect(consumer); } } @SuppressWarnings("rawtypes") public void register(CamelServletProvider provider, Map properties) { LOG.info("Registering provider through OSGi service listener {}", properties); try { CamelServlet camelServlet =3D provider.getCamelServlet(); camelServlet.setServletName((String) properties.get("servlet-name")); register(camelServlet); } catch (ClassCastException cce) { LOG.info("Provider is not a Camel Servlet"); } } public void unregister(CamelServletProvider provider, Map properties) { LOG.info("Deregistering provider through OSGi service listener {}", properties); try { CamelServlet camelServlet =3D provider.getCamelServlet(); unregister((CamelServlet)provider); } catch (ClassCastException cce) { LOG.info("Provider is not a Camel Servlet"); } } @Override public void register(CamelServlet provider) { LOG.debug("Registering CamelServlet with name {} consumers present: {}"= , provider.getServletName(), consumers.size()); providers.add(provider); for (HttpConsumer consumer : consumers) { provider.connect(consumer); } } @Override public void unregister(CamelServlet provider) { providers.remove(provider); } public void setServlets(List servlets) { providers.clear(); for (Servlet servlet : servlets) { if (servlet instanceof CamelServlet) { providers.add((CamelServlet) servlet); } } } } The trick here is in the register / unregister method reacting to the CamelServletProvider interface. Now I was able to stick all of that together in a bundle configuration: If you think this is usefull, I am happy to stick it in the wiki rather tha= n in the user's list. Best regards Andreas Andreas Gies WoQ =AD Way of Quality UG Gesch=E4ftsf=FChrer & CTO eMail: andreas@wayofquality.de Tel DE: +49 151 23470823 Tel UK: +44 755 1381918 Fax: +49 1805 006534 2114 Amtsgericht Landshut: HRB 8352 Ust.-Id.: DE274771254 Haftungsausschluss Diese Email kann vertrauliche und/oder rechtlich gesch=FCtzte Informationen enthalten und ist ausschlie=DFlich f=FCr den/die benannten Adressaten bestimmt. Sollten Sie nicht der beabsichtigte Empf=E4nger sein oder diese Email irrt=FCmlich erhalten haben, ist es Ihnen nicht gestattet diese Mail oder einen Teil davon ohne unsere Erlaubnis zu verbreiten, zu kopieren, unbefugt weiterzuleiten oder zu behalten. Informieren Sie bitte sofort den Absender telefonisch oder per Email und l=F6schen Sie diese Email und alle Kopien aus Ihrem System. Wir haften nicht f=FCr die Unversehrtheit von Emails, nachdem sie unseren Einflussbereich verlassen haben. Disclaimer This email may contain confidential and/or privileged information and is intended solely for the attention and use of the named addressee(s). If you are not the intended recipient, or a person responsible for delivering it t= o the intended recipient, you are not authorized to and must not disclose, copy, distribute, or retain this message or any part of it without our authority. Please contact the sender by call or reply email immediately and destroy all copies and the original message. We are not responsible for the integrity of emails after they have left our sphere of control. --B_3448957558_2267715--