Advertisement
Michael-O

HierarchicalWSSpringServlet

Mar 5th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1.  
  2. import java.util.LinkedHashSet;
  3. import java.util.Map;
  4. import java.util.Set;
  5.  
  6. import javax.servlet.ServletConfig;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. import org.springframework.beans.BeanUtils;
  13. import org.springframework.context.ConfigurableApplicationContext;
  14. import org.springframework.core.env.ConfigurableEnvironment;
  15. import org.springframework.web.context.ConfigurableWebApplicationContext;
  16. import org.springframework.web.context.ConfigurableWebEnvironment;
  17. import org.springframework.web.context.WebApplicationContext;
  18. import org.springframework.web.context.support.WebApplicationContextUtils;
  19. import org.springframework.web.context.support.XmlWebApplicationContext;
  20.  
  21. import com.sun.xml.ws.transport.http.servlet.ServletAdapterList;
  22. import com.sun.xml.ws.transport.http.servlet.SpringBinding;
  23. import com.sun.xml.ws.transport.http.servlet.SpringBindingList;
  24. import com.sun.xml.ws.transport.http.servlet.WSServletDelegate;
  25.  
  26. public class HierarchicalWSSpringServlet extends HttpServlet {
  27.  
  28.     private static final long serialVersionUID = 8701641714441040772L;
  29.  
  30.     private WebApplicationContext webApplicationContext;
  31.     private WSServletDelegate delegate;
  32.  
  33.     @Override
  34.     public void init(ServletConfig config) throws ServletException {
  35.         super.init(config);
  36.  
  37.  
  38.         // get the configured adapters from Spring
  39.         WebApplicationContext rootContext = WebApplicationContextUtils
  40.                 .getRequiredWebApplicationContext(getServletContext());
  41.  
  42.         WebApplicationContext wac = null;
  43.  
  44.         ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) BeanUtils
  45.                 .instantiateClass(XmlWebApplicationContext.class);
  46.  
  47.         cwac.setParent(rootContext);
  48.         cwac.setConfigLocation("/WEB-INF/spring/jaxws/*.xml");
  49.  
  50.         cwac.setId("jaxws-context");
  51.         cwac.setServletContext(getServletContext());
  52.         cwac.setServletConfig(config);
  53.         cwac.setNamespace(config.getServletName());
  54.  
  55.         ConfigurableEnvironment env = cwac.getEnvironment();
  56.         if (env instanceof ConfigurableWebEnvironment) {
  57.             ((ConfigurableWebEnvironment) env).initPropertySources(
  58.                     getServletContext(), config);
  59.         }
  60.  
  61.         cwac.refresh();
  62.  
  63.         wac = cwac;
  64.         this.webApplicationContext = wac;
  65.  
  66.         Set<SpringBinding> bindings = new LinkedHashSet<SpringBinding>();
  67.  
  68.         // backward compatibility. recognize all bindings
  69.         Map<String, SpringBindingList> m = wac.getBeansOfType(SpringBindingList.class);
  70.         for (SpringBindingList sbl : m.values())
  71.             bindings.addAll(sbl.getBindings());
  72.  
  73.         bindings.addAll(wac.getBeansOfType(SpringBinding.class).values());
  74.  
  75.         // create adapters
  76.         ServletAdapterList l = new ServletAdapterList(getServletContext());
  77.         for (SpringBinding binding : bindings)
  78.             binding.create(l);
  79.  
  80.         delegate = new WSServletDelegate(l, getServletContext());
  81.     }
  82.  
  83.     @Override
  84.     public void destroy() {
  85.         if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
  86.             ((ConfigurableApplicationContext) this.webApplicationContext).close();
  87.         }
  88.         delegate.destroy();
  89.         delegate = null;
  90.     }
  91.  
  92.     @Override
  93.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  94.             throws ServletException {
  95.         delegate.doPost(request, response, getServletContext());
  96.     }
  97.  
  98.     @Override
  99.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  100.             throws ServletException {
  101.         delegate.doGet(request, response, getServletContext());
  102.     }
  103.  
  104.     @Override
  105.     protected void doPut(HttpServletRequest request, HttpServletResponse response)
  106.             throws ServletException {
  107.         delegate.doPut(request, response, getServletContext());
  108.     }
  109.  
  110.     @Override
  111.     protected void doDelete(HttpServletRequest request, HttpServletResponse response)
  112.             throws ServletException {
  113.         delegate.doDelete(request, response, getServletContext());
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement