users@glassfish.java.net

proper use of _at_Resource injection inside Web Services

From: Mark Hansen <mark_at_javector.com>
Date: Thu, 16 Mar 2006 10:27:26 -0500

I'm using an @WebServiceProvider to deploy a web service that uses
Castor (rather than JAXB) to
do the binding of XML to existing Java objects.

To do this, I deploy a Castor mapping.xml file in the WAR. Then, I need
to get it from inside the Provider. Now, I am using injection of
@WebServiceContext to get to ServletContext. But, is there a way that I
can inject the file (mapping.xml) directly into a File instance - more
elegantly?

Here is my code:

  @Resource
  WebServiceContext wscontext;

  // ... snip

  public Source invoke(Source payload) {

   if ( webServiceContext == null ) {
        throw new RuntimeException("WebServiceContext not injected.");
   }
   MessageContext mc = webServiceContext.getMessageContext();
   ServletContext sc = (ServletContext)
mc.get(MessageContext.SERVLET_CONTEXT);
       if ( sc == null ) {
         throw new RuntimeException("ServletContext is null.");
       }
    InputStream castorMappingFile =
        sc.getResourceAsStream("/WEB-INF/castor/mapping.xml");
     if ( castorMappingFile == null ) {
        throw new IOException("Castor mapping file not found.");
     }


I am wonderin if it is possible to do something like this:

@Resource(name="/WEB-INF/castor/mapping.xml")
File castorMappingFile;