code_junkie

How can I access the ServletContext from within a JAX-WS web service?

Nov 12th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. @Resource
  2. private WebServiceContext context;
  3.  
  4. ServletContext servletContext =
  5. (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
  6.  
  7. import javax.annotation.Resource;
  8. import javax.jws.WebService;
  9. import javax.servlet.ServletContext;
  10. import javax.xml.ws.WebServiceContext;
  11. import javax.xml.ws.handler.MessageContext;
  12.  
  13. import org.springframework.web.context.WebApplicationContext;
  14. import org.springframework.web.context.support.WebApplicationContextUtils;
  15.  
  16. @WebService
  17. public class MyWebService {
  18.  
  19.  
  20. // boilerplate code begins :(
  21.  
  22. @Resource
  23. private WebServiceContext context;
  24. private WebApplicationContext webApplicationContext = null;
  25.  
  26. /**
  27. * @return
  28. * @throws IllegalStateException
  29. */
  30. private WebApplicationContext getWebApplicationContext()
  31. throws IllegalStateException {
  32. if (webApplicationContext != null)
  33. return webApplicationContext;
  34. ServletContext servletContext =
  35. (ServletContext) context.getMessageContext().get(
  36. MessageContext.SERVLET_CONTEXT);
  37. webApplicationContext =
  38. WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
  39. return webApplicationContext;
  40. }
  41. }
Add Comment
Please, Sign In to add comment