Ah ! Had the same issue when building the "everything is a resource"
prototype: I solved this by having the activate method initialize all
pending servlets. This works as the ServletContext is a static mandatory
reference and those are bound BEFORE the component gets activated.

That is, as soon as the ComponentContext field is set by the activate
method, the servlet binder method can safely assume that the
ServletContext field is also set and initialize new servlets.

On the other hand, the Servlet unbinder always destroy the undbound
servlet. In addition, the deactivate method unbinds all servlets still
bound when the component is actually deactivated.

See
http://svn.apache.org/repos/asf/incubator/sling/whiteboard/fmeschbe/resource/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/SlingServletResolver.java

Regards
Felix


Am Dienstag, den 08.01.2008, 16:27 +0000 schrieb [EMAIL PROTECTED]:
> Author: cziegeler
> Date: Tue Jan  8 08:27:11 2008
> New Revision: 610055
> 
> URL: http://svn.apache.org/viewvc?rev=610055&view=rev
> Log:
> Quick fix to always register servlets: if servlets and the servlet context 
> are registered before activate is called, the servlets were either never 
> registered or an npe occured during registration.
> 
> Modified:
>     
> incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/SlingServletResolver.java
> 
> Modified: 
> incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/SlingServletResolver.java
> URL: 
> http://svn.apache.org/viewvc/incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/SlingServletResolver.java?rev=610055&r1=610054&r2=610055&view=diff
> ==============================================================================
> --- 
> incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/SlingServletResolver.java
>  (original)
> +++ 
> incubator/sling/trunk/sling/servlet-resolver/src/main/java/org/apache/sling/servlet/resolver/SlingServletResolver.java
>  Tue Jan  8 08:27:11 2008
> @@ -18,9 +18,13 @@
>   */
>  package org.apache.sling.servlet.resolver;
>  
> -import static org.apache.sling.api.SlingConstants.*;
> -import static org.apache.sling.core.CoreConstants.*;
> -import static org.apache.sling.servlet.resolver.ServletResolverConstants.*;
> +import static org.apache.sling.api.SlingConstants.ERROR_MESSAGE;
> +import static org.apache.sling.api.SlingConstants.ERROR_REQUEST_URI;
> +import static org.apache.sling.api.SlingConstants.ERROR_SERVLET_NAME;
> +import static org.apache.sling.api.SlingConstants.ERROR_STATUS;
> +import static org.apache.sling.core.CoreConstants.SLING_CURRENT_SERVLET_NAME;
> +import static 
> org.apache.sling.servlet.resolver.ServletResolverConstants.DEFAULT_SERVLET_NAME;
> +import static 
> org.apache.sling.servlet.resolver.ServletResolverConstants.SLING_RESOURCE_TYPES;
>  
>  import java.io.IOException;
>  import java.util.ArrayList;
> @@ -339,6 +343,16 @@
>                  }
>              }
>          }
> +        List<ServiceReference> refs = null;
> +        synchronized (this) {
> +            if ( this.servletContext != null ) {
> +                refs = this.pendingServlets;
> +                this.pendingServlets = new ArrayList<ServiceReference>();
> +            }
> +        }
> +        if ( refs != null ) {
> +            this.createAllServlets(this.servletContext, refs);
> +        }
>      }
>  
>      protected synchronized void bindServlet(ServiceReference reference) {
> @@ -366,8 +380,12 @@
>  
>              if (this.servletContext == null) {
>  
> -                refs = pendingServlets;
> -                pendingServlets = new ArrayList<ServiceReference>();
> +                if ( this.context != null ) {
> +                    refs = pendingServlets;
> +                    pendingServlets = new ArrayList<ServiceReference>();
> +                } else {
> +                    refs = null;
> +                }
>                  destroy = false;
>  
>              } else {
> @@ -384,7 +402,9 @@
>              destroyAllServlets(refs);
>          }
>  
> -        createAllServlets(this.servletContext, refs);
> +        if ( refs != null ) {
> +            createAllServlets(this.servletContext, refs);
> +        }
>      }
>  
>      protected void unbindServletContext(ServletContext oldServletContext) {
> 
> 

Reply via email to