You can store it as an attribute in the ServletContext
of the application. It will be accessible to any JAX-WS services or JAX-RS resources in the web module. As an added bonus, if your JAX-WS service and JAX-RS resources are in the same module (war), they will share the same instance of the object/document you place there.
For the JAX-RS resource:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.servlet.ServletContext;
@Path("myresource")
public class MyResource {
@Context ServletContext context;
@GET
@Produces("text/plain")
public String getMyResource() {
return context.getAttribute("cachedDocument");
}
}
For the JAX-WS service:
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
@WebService
public class MyService {
@Resource
private WebServiceContext context;
@WebMethod
public String getDocument() {
ServletContext servletContext =
(ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
return servletContext.getAttribute("cachedDocument");
}
}
See also: ServletContext javadocs.