Yes, this is nice. Thanks to all who took the time to post.
-S From: osgi-dev-boun...@mail.osgi.org [mailto:osgi-dev-boun...@mail.osgi.org] On Behalf Of Dennis Geurts Sent: Thursday, January 08, 2009 4:43 PM To: OSGi Developer Mail List Subject: Re: [osgi-dev] Servlet design pattern I usually do it like this: 1) make Activator extend the DependencyActivatorBase (from the Dependency manager (from apache felix)) you'll be required to implement (a.o.) an init() method; The following would be a good start: public void init(BundleContext context, DependencyManager manager) throws Exception { Service svc = createService() .setImplementation(MainServlet.class) .add(createServiceDependency() .setService(HttpService.class) .setRequired(true)) .add(createServiceDependency() .setService(EventAdmin.class) .setRequired(true)); svc.start(); } the code above sets in motion quite a lot: * The dependency manager takes care of automatic instantiation of the "MainServlet". This is only done when both the HttpService and (e.g.) the EventAdmin service can be obtained from the OSGi framework. * It then tries to invoke init() on the MainServlet instance (if available) * It then injects the above mentioned OSGi services into the corresponding fields. e.g: public class MainServlet extends HttpServlet { //dependency managed private HttpService m_server; //dependency managed private EventAdmin m_admin; //dependency managed private BundleContext m_context; } would suffice. * It then tries to invoke start() on the MainServlet instance (if available) Notice that both m_server and m_admin are now available to the MainsServlet code ! Since this is an excellent moment to register the servlet, I propose to do that here: private void start() throws ServletException, NamespaceException { m_server.registerServlet("/main", this, null, null); } * At any time in the future, a required dependency might disappear from the framework. Then this servlet is obviously (your decision) not able to perform its functionality anymore. It then needs to be unregistered. The dependency manager can again do that for you: just before the service it depends on is deregistered we get 'notified': the dep.man will try to invoke: stop() e.g. private void stop() { m_server.unregister("/main"); } would do the trick for you. As soon as the services this servlet depends on have all reappeared, the dep. man. will again call "start()" so the servlet gets registered again. 2) well, there's no "2" ;-) your doGet() method can now make use of the injected OSGi services without any checks. Does the above provide you with a pattern you feel comfortable with ? Cheers, Dennis On 08-01-09 22:10, "BJ Hargrave" <hargr...@us.ibm.com> wrote: Well you have to construct your Servlet objects to register them with the HttpService. Why not pass the BundleContext to the Servlet object during construction or shortly thereafter but before you register the Servlet object. -- No trees were killed in the sending of this message. However, a large number of electrons were terribly disturbed! ------------------------------------------------- Dennis Geurts software development luminis K.v.k Centraal Gelderland: 09 16 28 87 ------------------------------------------------- +31 6 12078236 dennis.geu...@luminis.nl www.luminis.nl -------------------------------------------------
_______________________________________________ OSGi Developer Mail List osgi-dev@mail.osgi.org https://mail.osgi.org/mailman/listinfo/osgi-dev