users@jersey.java.net

_at_Resource ServletContext injection problem

From: Keith Sheppard <Keith.Sheppard_at_jax.org>
Date: Mon, 4 May 2009 11:06:36 -0400

Hi All,

When I annotate my ServletContext with @Resource, I end up with a null
reference. I'm assuming that I'm somehow misusing the @Resource annotation,
but after some searching I haven't been able to figure out how what I am
doing is wrong. Note that all of my restful annotations work as expected.
Does anyone know why this doesn't work?

Thanks!
Keith


Here is my system configuration:
Mac OS-X Leopard (Intel)
Java 1.5.0_16
Jersey 1.0.3
Tomcat 6.0.16


Here is the class with the problem:
==============
/*
 * Copyright (c) 2008 The Jackson Laboratory
 *
 * This software was developed by Gary Churchill's Lab at The Jackson
 * Laboratory (see http://research.jax.org/faculty/churchill).
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software. If not, see <http://www.gnu.org/licenses/>.
 */

package org.jax.pubarray.restful;

import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import com.sun.jersey.spi.resource.Singleton;

/**
 * A JAX-RS implementation of a table administration resource
 * @author <A HREF="mailto:keith.sheppard_at_jax.org">Keith Sheppard</A>
 */
@Singleton
@Path("/")
public class TableAdministrationResource
{
    private static final Logger LOG = Logger.getLogger(
            TableAdministrationResource.class.getName());
    
    @Resource
    private ServletContext servletContext;
    
    /**
     *
     */
    public TableAdministrationResource()
    {
    }
    
    /**
     * For uploading a candidate design file
     * NOTE: MIME type is HTML instead of text/plain for reasons documented
     * in the javadoc for GWT's FormPanel class
     * @param designFileFormat
     * the design file
     * @param designFileName
     * the file name
     * @param designFile
     * the data stream to convert
     * @return
     * if everything works we return a {_at_link Status#OK} with the
given
     * file name. if we see a problem with the input we return a
     * {_at_link Status#BAD_REQUEST}
     */
    @Path("/candidate-design")
    @POST
    @Consumes("multipart/form-data")
    @Produces("text/html")
    public Response uploadDesignFile(
            @FormParam("designFileFormat") String designFileFormat,
            @FormParam("designFileName") String designFileName,
            @FormParam("designFile") InputStream designFile)
    {
        try
        {
            LOG.info("The servlet context is: " + this.servletContext);
            return Response.ok(designFileName +
this.servletContext).build();
        }
        catch(Exception ex)
        {
            LOG.log(Level.SEVERE,
                    "failed to upload design file",
                    ex);
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    
    /**
     * For uploading a candidate data file
     * NOTE: MIME type is HTML instead of text/plain for reasons documented
     * in the javadoc for GWT's FormPanel class
     * @param dataFileFormat
     * the format to use
     * @param dataFileName
     * the name of the file being uploaded
     * @param dataFile
     * the stream of data from the file
     * @return
     * if everything works we return a {_at_link Status#OK} with the
given
     * file name. if we see a problem with the input we return a
     * {_at_link Status#BAD_REQUEST}
     */
    @Path("/candidate-data")
    @POST
    @Consumes("multipart/form-data")
    @Produces("text/html")
    public Response uploadDataFile(
            @FormParam("dataFileFormat") String dataFileFormat,
            @FormParam("dataFileName") String dataFileName,
            @FormParam("dataFile") InputStream dataFile)
    {
        try
        {
            return Response.ok(dataFileName).build();
        }
        catch(Exception ex)
        {
            LOG.log(Level.SEVERE,
                    "failed to upload data file",
                    ex);
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    
    /**
     * For uploading a candidate annotation file
     * NOTE: MIME type is HTML instead of text/plain for reasons documented
     * in the javadoc for GWT's FormPanel class
     * @param annotationFileFormat
     * the format to use
     * @param annotationFileName
     * the file name to use
     * @param annotationFile
     * the annotation file
     * @return
     * if everything works we return a {_at_link Status#OK} with the
given
     * file name. if we see a problem with the input we return a
     * {_at_link Status#BAD_REQUEST}
     */
    @Path("/candidate-annotations")
    @POST
    @Consumes("multipart/form-data")
    @Produces("text/html")
    public Response uploadAnnotationFile(
            @FormParam("annotationFileFormat") String annotationFileFormat,
            @FormParam("annotationFileName") String annotationFileName,
            @FormParam("annotationFile") InputStream annotationFile)
    {
        try
        {
            return Response.ok(annotationFileName).build();
        }
        catch(Exception ex)
        {
            LOG.log(Level.SEVERE,
                    "failed to upload annotation file",
                    ex);
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
}
==============


Here is catalina.out:
==============
kss_at_CATSKILL:~/bin/apache-tomcat/logs/> cat catalina.out
May 4, 2009 10:52:16 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal
performance in production environments was not found on the
java.library.path:
.:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
May 4, 2009 10:52:16 AM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
May 4, 2009 10:52:16 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 408 ms
May 4, 2009 10:52:17 AM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
May 4, 2009 10:52:17 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.16
May 4, 2009 10:52:17 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive pub-array-gwt-server-1.0.war
May 4, 2009 10:52:17 AM org.apache.catalina.loader.WebappClassLoader
validateJarFile
INFO:
validateJarFile(/Users/kss/bin/apache-tomcat/webapps/pub-array-gwt-server-1.
0/WEB-INF/lib/servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3,
section 9.7.2. Offending class: javax/servlet/Servlet.class
May 4, 2009 10:52:17 AM com.sun.jersey.api.core.ClasspathResourceConfig init
INFO: Scanning for root resource and provider classes in the paths:
  /Users/kss/bin/apache-tomcat/webapps/pub-array-gwt-server-1.0/WEB-INF/lib
  
/Users/kss/bin/apache-tomcat/webapps/pub-array-gwt-server-1.0/WEB-INF/classe
s
May 4, 2009 10:52:18 AM com.sun.jersey.api.core.ClasspathResourceConfig init
INFO: Root resource classes found:
  class org.jax.pubarray.restful.TableAdministrationResource
May 4, 2009 10:52:18 AM com.sun.jersey.api.core.ClasspathResourceConfig init
INFO: Provider classes found:
May 4, 2009 10:52:19 AM org.apache.catalina.core.StandardContext
addApplicationListener
INFO: The listener "listeners.ContextListener" is already configured for
this context. The duplicate definition has been ignored.
May 4, 2009 10:52:19 AM org.apache.catalina.core.StandardContext
addApplicationListener
INFO: The listener "listeners.SessionListener" is already configured for
this context. The duplicate definition has been ignored.
May 4, 2009 10:52:19 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
May 4, 2009 10:52:19 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
May 4, 2009 10:52:19 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/17 config=null
May 4, 2009 10:52:19 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2753 ms
May 4, 2009 10:53:46 AM org.jax.pubarray.restful.TableAdministrationResource
uploadDesignFile
INFO: The servlet context is: null
kss_at_CATSKILL:~/bin/apache-tomcat/logs/>
==============


-- 
Keith Sheppard
Scientific Software Engineer
The Jackson Lab
(207) 288-6897
JAX Mission:
³We discover the genetic basis for preventing, treating and curing human
disease, and we enable research and education for the global biomedical
community.²