Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing FacesContext from Web Service

I'm developing a Web Service which will be called by clients which are written by me. In the web service I need to use application-wide objects which eases the load of application on the system. I have implemented my application-wide objects as shown in this question.

I can use my object in a jsf page with no problem as follows.

MyObject mo = (MyObject) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("MyObjectsName");

But when it comes to use it in a Web Service Request FacesContext.getCurrentInstance() returns null. Is there any way to use the FacesContext in a web service.

like image 973
scriptmonster Avatar asked Apr 21 '10 15:04

scriptmonster


1 Answers

It is null, because web services don't come through the Faces Servlet. And this is the way it should be, because web services have nothing to do with JSF.

In case you are using a jax-ws implementation, you can use:

@Resource
WebServiceContext context;

This will inject the WebServiceContext, by which you can:

ServletContext servletContext = (ServletContext) 
     context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);

And having the ServletContext, you can access your application-scope objects.

like image 106
Bozho Avatar answered Oct 16 '22 15:10

Bozho