Hi again,

Good idea. I've integrated your changes proposition to SVN.

Best regards,
Jerome  

> -----Message d'origine-----
> De : Valdis Rigdon [mailto:[EMAIL PROTECTED] 
> Envoyé : mercredi 10 janvier 2007 21:41
> À : [email protected]
> Objet : [Patch] ServerServlet refactor for overriding 
> Application creation
> 
> This is is a patch for a simple refactor of ServerServlet, 
> breaking up 
> getServer() into three methods -- (1) the original 
> getServer(), (2) one 
> to create the HttpServerHelper, and (3) one to create the 
> Application.  
> No functionality has been changed; in my application I need 
> to override 
> the mechanism for creating an Application to load it from a Spring 
> ApplicationContext as opposed to using reflection.  This 
> patch gives me 
> that extension point easier than trying to override the entire 
> getServer() method.
> 
> I've attached the patch to this email, as well as pasted it below.
> 
> 
> Index: 
> module/com.noelios.restlet.ext.servlet_2.4/src/com/noelios/res
tlet/ext/servlet/ServerServlet.java
> ===================================================================
> --- 
> module/com.noelios.restlet.ext.servlet_2.4/src/com/noelios/res
tlet/ext/servlet/ServerServlet.java    
> (revision 1394)
> +++ 
> module/com.noelios.restlet.ext.servlet_2.4/src/com/noelios/res
tlet/ext/servlet/ServerServlet.java    
> (working copy)
> @@ -21,6 +21,8 @@
>  import java.io.IOException;
>  import java.util.List;
>  
> +import java.lang.reflect.InvocationTargetException;
> +
>  import javax.servlet.ServletException;
>  import javax.servlet.http.HttpServlet;
>  import javax.servlet.http.HttpServletRequest;
> @@ -156,74 +158,20 @@
>                          serverAttributeName);
>  
>                  if (result == null) {
> -                    // Try to instantiate a new target application
> -                    // First, find the application class name
> -                    String applicationClassName = getInitParameter(
> -                            NAME_APPLICATION_CLASS, null);
> -                    if (applicationClassName != null) {
> +                    result = createServer(request);
> +                    if (result != null) {
> +                        // Starts the target Restlet
>                          try {
> -                            // Load the application class using the 
> given class
> -                            // name
> -                            Class targetClass = Class
> -                                    .forName(applicationClassName);
> -
> -                            // First, let's locate the 
> closest component
> -                            Component component = new Component();
> -                            Server server = new 
> Server(component.getContext(),
> -                                    (List<Protocol>) null, request
> -                                            .getLocalAddr(), request
> -                                            .getLocalPort(), 
> component);
> -                            result = new HttpServerHelper(server);
> -                            getServletContext().setAttribute(
> -                                    NAME_SERVER_ATTRIBUTE, result);
> -
> -                            if (component != null) {
> -                                // Create a new instance of the 
> application
> -                                // class
> -                                Application application = 
> (Application) 
> targetClass
> -                                        
> .getConstructor(Context.class)
> -                                        
> .newInstance(component.getContext());
> -
> -                                // Set the Servlet context
> -                                application
> -                                        .setContext(new 
> ServletContextAdapter(
> -                                                this, application, 
> component
> -                                                        
> .getContext()));
> -
> -                                // Attach the application
> -                                String uriPattern = 
> request.getContextPath()
> -                                        + request.getServletPath();
> -                                
> component.getDefaultHost().attach(uriPattern,
> -                                        application);
> -
> -                                // Starts the target Restlet
> -                                result.start();
> -                            } else {
> -                                log("[Noelios Restlet Engine] - The 
> Restlet component couldn't be instantiated.");
> -                            }
> -                        } catch (ClassNotFoundException e) {
> -                            log(
> -                                    "[Noelios Restlet Engine] - The 
> ServerServlet couldn't find the target class. Please check that your 
> classpath includes "
> -                                            + 
> applicationClassName, e);
> -                        } catch (InstantiationException e) {
> -                            log(
> -                                    "[Noelios Restlet Engine] - The 
> ServerServlet couldn't instantiate the target class. Please 
> check this 
> class has an empty constructor "
> -                                            + 
> applicationClassName, e);
> -                        } catch (IllegalAccessException e) {
> -                            log(
> -                                    "[Noelios Restlet Engine] - The 
> ServerServlet couldn't instantiate the target class. Please 
> check that 
> you have to proper access rights to "
> -                                            + 
> applicationClassName, e);
> -                        } catch (Exception e) {
> -                            log(
> -                                    "[Noelios Restlet Engine] - The 
> ServerServlet couldn't start the target Restlet.",
> -                                    e);
> +                            result.start();
>                          }
> +                        catch (Exception e) {
> +                            log("[Noelios Restlet Engine] - The 
> ServerServlet couldn't start the target Restlet.",e);    
> +                        }
>                      } else {
>                          log("[Noelios Restlet Engine] - The 
> ServerServlet couldn't find the target class name. Please set the 
> initialization parameter called "
>                                  + NAME_APPLICATION_CLASS);
>                      }
>                  }
> -
>                  this.helper = result;
>              }
>          }
> @@ -232,6 +180,93 @@
>      }
>  
>      /**
> +     * Creates the associated HTTP server handling calls.
> +     *
> +     * @return The new HTTP server handling calls. 
> +     *
> +     * @see #getServer()
> +     */
> +    protected HttpServerHelper 
> createServer(HttpServletRequest request) {
> +        HttpServerHelper result = null;
> +        
> +        Component component = new Component();
> +        Application application = 
> createApplication(component.getContext());
> +        if (application != null) {
> +            // First, let's locate the closest component
> +            Server server = new Server(component.getContext(),
> +                    (List<Protocol>) null, request
> +                            .getLocalAddr(), request
> +                            .getLocalPort(), component);
> +            result = new HttpServerHelper(server);
> +            getServletContext().setAttribute(
> +                    NAME_SERVER_ATTRIBUTE, result);
> +
> +            // Set the Servlet context
> +            application
> +                    .setContext(new ServletContextAdapter(
> +                            this, application, component
> +                                    .getContext()));
> +
> +            // Attach the application
> +            String uriPattern = request.getContextPath()
> +                    + request.getServletPath();
> +            component.getDefaultHost().attach(uriPattern,
> +                    application);
> +            
> +        }
> +        return result;
> +    }
> +    
> +    /**
> +     *  Creates the single Application used by this Servlet.
> +     *
> +     * @param ctx       the Context for the Application 
> +     *
> +     * @return  the newly created Application or null if 
> unable to create
> +     */
> +    protected Application createApplication(Context ctx) {
> +        // Try to instantiate a new target application
> +        // First, find the application class name
> +        String applicationClassName = getInitParameter(
> +                NAME_APPLICATION_CLASS, null);        
> +        // Load the application class using the given class
> +        // name
> +        if (applicationClassName != null) {
> +            try {
> +                Class targetClass = Class
> +                        .forName(applicationClassName);
> +
> +                // Create a new instance of the application
> +                // class
> +                return (Application) targetClass
> +                        .getConstructor(Context.class)
> +                        .newInstance(ctx);
> +            } catch (ClassNotFoundException e) {
> +                log(
> +                        "[Noelios Restlet Engine] - The 
> ServerServlet 
> couldn't find the target class. Please check that your 
> classpath includes "
> +                                + applicationClassName, e);
> +            } catch (InstantiationException e) {
> +                log(
> +                        "[Noelios Restlet Engine] - The 
> ServerServlet 
> couldn't instantiate the target class. Please check this class has an 
> empty constructor "
> +                                + applicationClassName, e);
> +            } catch (IllegalAccessException e) {
> +                log(
> +                        "[Noelios Restlet Engine] - The 
> ServerServlet 
> couldn't instantiate the target class. Please check that you have to 
> proper access rights to "
> +                                + applicationClassName, e);
> +            }  catch (NoSuchMethodException e) {
> +                log(
> +                        "[Noelios Restlet Engine] - The 
> ServerServlet 
> couldn't invoke the constructor of the target class. Please 
> check this 
> class has a constructor with a single parameter of Context "
> +                                + applicationClassName, e);
> +            } catch (InvocationTargetException e) {
> +                log(
> +                        "[Noelios Restlet Engine] - The 
> ServerServlet 
> couldn't instantiate the target class. An exception was thrown while 
> creating "
> +                                + applicationClassName, e);
> +            }
> +        }
> +        return null;
> +    }
> +
> +    /**
>       * Returns the value of a given initialization parameter, first 
> from the
>       * Servlet configuration, then from the Web Application context.
>       * 
> 
> 

Reply via email to