Announcement Announcement Module
Collapse
No announcement yet.
ServletToBeanProxy Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ServletToBeanProxy

    I use Acegi's FilterToBeanProxy, and now find myself in need of something similar, only for servlets instead of filters. Because of my particular case, I'm unable to use Spring's DispatcherServlet. Is anyone aware of something like FilterToBeanProxy, only for servlets instead?

    Thanks,
    Andy

  • #2
    I threw together a quick and dirty ServletToBeanProxy. It doesn't have FilterToBeanProxy's 'targetClass' capability, but it does have 'targetBean'. This allows you to define servlets as beans in a Spring bean factory (application context) - providing all the benefits of dependency injection (along with full access to your Spring application context).
    Code:
    public class ServletToBeanProxy implements Servlet
    {
      private Servlet delegate;
    
      protected ApplicationContext getContext(ServletContext servletContext)
      {
        return WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
      }
    
    
      public void init(final ServletConfig servletConfig) throws ServletException
      {
        final String targetBean = servletConfig.getInitParameter("targetBean");
        final ApplicationContext ctx = getContext(servletConfig.getServletContext());
    
        if(targetBean == null || !ctx.containsBean(targetBean)) {
          throw new ServletException("targetBean '" + targetBean + "' not found in context.");
        }
    
        this.delegate = (Servlet)ctx.getBean(targetBean, Servlet.class);
        this.delegate.init(servletConfig);
      }
    
      public ServletConfig getServletConfig()
      {
        return this.delegate.getServletConfig();
      }
    
      public void service(final ServletRequest servletRequest,
                          final ServletResponse servletResponse) throws ServletException,
          IOException
      {
        this.delegate.service(servletRequest, servletResponse);
      }
    
      public String getServletInfo()
      {
        return this.delegate.getServletInfo();
      }
    
      public void destroy()
      {
        if(this.delegate != null) {
          this.delegate.destroy();
          this.delegate = null;
        }
      }
    }
    Pretty straight forward, as you can see. Here is a sample config from my web.xml:
    Code:
      <servlet>
        <servlet-name>MqHttpTunnelServlet</servlet-name>
        <servlet-class>...ServletToBeanProxy</servlet-class>
        <init-param>
          <param-name>targetBean</param-name>
          <param-value>MqHttpTunnelServlet</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>MqHttpTunnelServlet</servlet-name>
        <url-pattern>/services/mq</url-pattern>
      </servlet-mapping>
    In this case, the ServletToBeanProxy will lookup a bean by the name of "MqHttpTunnelServlet" in my Spring WebApplicationContext and proxy all servlet functionality to that bean.

    - Andy

    Comment


    • #3
      Just curious Andy, what's your Servlet use case where DispatcherServlet won't work for you?

      Comment


      • #4
        In my case it was a Servlet from another project that I am not able to directly modify. However, the Servlet contained IoC like methods, so I figured the easiest approach would be to use Spring to configure the 3rd party Servlet and somehow expose this Spring configured servlet to the container via web.xml - thus ServletToBeanProxy.

        Comment


        • #5
          Neat...

          I think there's a lot of value in having your web container grab everything (filters + servlets) from the IoC container (unless of course it directly works with the IoC container like DispatcherServlet).

          Comment

          Working...
          X