Author: angelo.vandersijpt at luminis.eu
Date: Wed Dec 22 21:37:57 2010
New Revision: 538

Log:
AMDATU-180 The Shindig-related servlets now rely on the GuiceInjectorServlet, 
so their startup order is no longer important. Also, the Guice injector now 
pulls its service when the servlet stops.

Modified:
   
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/GuiceInjectorServiceImpl.java
   
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigRegistrationServiceImpl.java

Modified: 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/GuiceInjectorServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/GuiceInjectorServiceImpl.java
   (original)
+++ 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/GuiceInjectorServiceImpl.java
   Wed Dec 22 21:37:57 2010
@@ -81,6 +81,7 @@
 
     // Instance variables
     private boolean m_jmxInitialized;
+    private Component m_serviceComponent;
 
     public ServletConfig getServletConfig() {
         return null;
@@ -101,17 +102,22 @@
         // This registers ourselves as GuiceInjectorServlet. Note that the 
activator explicitly
         // does not register the service with the GuiceInjectorServlet 
interface since we want
         // postpone the availability of the service until the Guice injector 
is initialized.
-        Dictionary<String, String> serviceProps = new Hashtable<String, 
String>();
-        Component component = m_dependencyManager.createComponent();
-        component.setImplementation(this);
-        component.setInterface(GuiceInjectorServlet.class.getName(), 
serviceProps);
-        m_dependencyManager.add(component);
+        m_serviceComponent = m_dependencyManager.createComponent()
+                .setInterface(GuiceInjectorServlet.class.getName(), null)
+                .setImplementation(this)
+                .setCallbacks("_init", "start", "stop", "_destroy");
+        m_dependencyManager.add(m_serviceComponent);
     }
 
     public void service(ServletRequest arg0, ServletResponse arg1) throws 
ServletException, IOException {
     }
     
     public void destroy() {
+        removeService();
+    }
+
+    private void removeService() {
+        m_dependencyManager.remove(m_serviceComponent);
     }
 
     /**

Modified: 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigRegistrationServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigRegistrationServiceImpl.java
     (original)
+++ 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/ShindigRegistrationServiceImpl.java
     Wed Dec 22 21:37:57 2010
@@ -23,10 +23,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.List;
+import java.util.*;
 
 import javax.servlet.Filter;
 import javax.servlet.Servlet;
@@ -85,7 +82,7 @@
 
     // Other instance variables
     private HttpContext m_httpContext;
-    private List<String> m_registeredServletPaths = new ArrayList<String>();
+    private List<Component> m_registeredServletComponents = new 
ArrayList<Component>();
 
     /**
      * The init() method is invoked by the Felix dependency manager.
@@ -105,28 +102,12 @@
         // httpcontext, therefore we register the servlet and not the Guice 
injector service itself.
         // When the servlet is initialized it will register itself as a 
GuiceInjectorServlet service (at this moment
         // the servlet is registered with the GuiceInjectorService interface)
-        registerServlet(GuiceInjectorServiceImpl.SERVLET_ALIAS, 
m_guiceInjectorService);
+        registerInjectorServlet();
 
         // Now register the authentication filter as a service. This filter 
needs the Guice injector to be initialized,
         // which is done when the GuiceInjectorServlet is initialized. So we 
define a service dependency on the
         // GuiceInjectorServlet
-        String baseMatch = "[/\\?]?.*";
-        String regex = ".*("
-            + GADGET_SERVLET_BASE + "[^/]*|"
-            + MAKEREQUEST_BASE + "[^/]*|"
-            + REST_BASE + baseMatch + "|"
-            + GADGETS_REST_BASE + baseMatch + "|"
-            + GADGETS_RPC_BASE + baseMatch + ")";
-        Dictionary<String, String> properties = new Hashtable<String, 
String>();
-        properties.put("pattern", regex);
-        properties.put("service.ranking", "0");
-        properties.put("contextId", this.getResourceId());
-        Component component = m_dependencyManager.createComponent();
-        component.setImplementation(AuthenticationServletFilter.class);
-        component.setInterface(Filter.class.getName(), properties);
-        
component.add(m_dependencyManager.createServiceDependency().setService(GuiceInjectorServlet.class).setRequired(
-            true));
-        m_dependencyManager.add(component);
+        registerAuthenticationFilter();
 
         m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
initialized");
 
@@ -197,6 +178,38 @@
         }
     }
 
+    private void registerInjectorServlet() {
+        Properties servletProperties = new Properties();
+        servletProperties.put("alias", GuiceInjectorServiceImpl.SERVLET_ALIAS);
+        servletProperties.put("contextId", getResourceId());
+
+        Component servletComponent = m_dependencyManager.createComponent()
+                .setImplementation(m_guiceInjectorService)
+                .setInterface(Servlet.class.getName(), servletProperties);
+        m_dependencyManager.add(servletComponent);
+        m_registeredServletComponents.add(servletComponent);
+    }
+
+    private void registerAuthenticationFilter() {
+        String baseMatch = "[/\\?]?.*";
+        String regex = ".*("
+            + GADGET_SERVLET_BASE + "[^/]*|"
+            + MAKEREQUEST_BASE + "[^/]*|"
+            + REST_BASE + baseMatch + "|"
+            + GADGETS_REST_BASE + baseMatch + "|"
+            + GADGETS_RPC_BASE + baseMatch + ")";
+        Dictionary<String, String> properties = new Hashtable<String, 
String>();
+        properties.put("pattern", regex);
+        properties.put("service.ranking", "0");
+        properties.put("contextId", this.getResourceId());
+        Component component = m_dependencyManager.createComponent();
+        component.setImplementation(AuthenticationServletFilter.class);
+        component.setInterface(Filter.class.getName(), properties);
+        
component.add(m_dependencyManager.createServiceDependency().setService(GuiceInjectorServlet.class).setRequired(
+            true));
+        m_dependencyManager.add(component);          // TODO shouldn't we 
remove this filter later on?
+    }
+
     /**
      * This method sets the given {@link HttpService} object and initializes 
all Shindig-specific servlets.
      * Since it can only be invoked when the Guice context is available, it is 
invoked from there
@@ -209,7 +222,7 @@
             registerServlet(GADGET_SERVLET_BASE, new GadgetRenderingServlet());
 
             // Register other servlets
-            Dictionary<String, String> servletProperties = new 
Hashtable<String, String>();
+            Properties servletProperties = new Properties();
             servletProperties.put("init.handlers", 
"org.apache.shindig.social.handlers");
             registerServlet(MAKEREQUEST_BASE, new MakeRequestServlet());
             registerServlet(PROXY_BASE, new ProxyServlet());
@@ -238,10 +251,12 @@
      * This method sets the given {@link HttpService} object and deregisters 
all Shindig-specific servlets.
      */
     private void unregisterResources() {
-        while (m_registeredServletPaths.size() > 0) {
-            m_httpService.unregister(m_registeredServletPaths.get(0));
-            m_registeredServletPaths.remove(0);
+        for (Component servletComponent : m_registeredServletComponents) {
+            m_dependencyManager.remove(servletComponent);
         }
+        m_registeredServletComponents.clear();
+
+        // TODO what about these resources?
         m_httpService.unregister("/gadgets");
         m_httpService.unregister("/features");
         m_httpService.unregister("/shindig");
@@ -266,20 +281,22 @@
      * the
      * whiteboard-style http service.
      */
-    private void registerServlet(String alias, Servlet servlet, 
Dictionary<String, String> servletProperties) {
-        if (servletProperties == null) {
-            servletProperties = new Hashtable<String, String>();
+    private void registerServlet(String alias, Servlet servlet, Properties 
servletProperties) {
+        Properties properties = new Properties();
+        if (servletProperties != null) {
+            properties.putAll(servletProperties);
         }
-        servletProperties.put("alias", alias);
-        servletProperties.put("contextId", getResourceId());
-
-
 
+        properties.put("alias", alias);
+        properties.put("contextId", getResourceId());
 
         Component servletComponent = m_dependencyManager.createComponent()
                 .setImplementation(servlet)
-                .setInterface(Servlet.class.getName(), servletProperties);
+                .setInterface(Servlet.class.getName(), properties)
+                .add(m_dependencyManager.createServiceDependency()
+                    .setService(GuiceInjectorServlet.class)
+                    .setRequired(true));
         m_dependencyManager.add(servletComponent);
-        m_registeredServletPaths.add(alias);
+        m_registeredServletComponents.add(servletComponent);
     }
 }

Reply via email to