users@jersey.java.net

RE: [Jersey] Cannot Instantiate new provider

From: Jonathan Cook - FM&T <Jonathan.Cook2_at_bbc.co.uk>
Date: Fri, 19 Jun 2009 12:23:03 +0100

No Stack trace. I have tried putting some debug info etc. But nothing..
 
And enabling jetty debugging and jersey server debugging.
 
Here is the code for the class.. As I said it works on localhost.
 
That instantiation is definitely the problem. I did some googling and
see a few people have had the same problem but doesn't appear to be an
obvious solution?
 
Thanks
 
package com.bbc.newsi.feeds.sport.webservice.providers;
 
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
 
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
 
import org.apache.log4j.Logger;
 
import com.sun.ws.rest.spi.resource.Singleton;
import com.sun.ws.rest.spi.template.TemplateProcessor;
 
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateException;
 
/**
 * A simple Freemarker template processor.
 *
 * @author Jonathan Cook
 * @version $Revision: #1 $ $Date: 2009/06/10 $ $Author: cookj02 $
 */
@Provider
//_at_Singleton*/
public class FreemarkerTemplateProcessor implements TemplateProcessor {
 private static final Logger log =
Logger.getLogger(FreemarkerTemplateProcessor.class);
 
 private Configuration config;
 
 public FreemarkerTemplateProcessor(){
  try{
   log.debug("instantiating class");
  }catch(Exception ex){
   log.debug("Error instantiating class");
  }
 }
 
 /**
  * Initialise Freemarker configuration
  */
 private synchronized void init() {
  try{
   log.debug("starting initialisation");
   config = new Configuration();
   config.setDirectoryForTemplateLoading(new
File("freemarkertemplates"));
   config.setObjectWrapper(new DefaultObjectWrapper());
   log.debug("finishing initialisation");
  } catch(IOException ex){
   log.error("Error loading template directory", ex);
  }
 }
 
 /**
  * Resolve an abstract template path into a fully qualified
  * concrete template path that identifies a template.
  *
  * @param path - The abstract template path
  * @return java.lang.String - The fully qualified concrete template
path,
  * otherwise null if the abstract template path cannot be resolved.
  */
 public String resolve(String path) {
  if (config == null) {
   init();
  }
  try {
   if (config.getTemplate(path) == null){
    return null;
   }
  } catch (IOException ex) {
    log.error("Error resolving template for path " +path, ex);
   return null;
  }
  return path;
 }
 
 /**
  * Process a template and write the result to an output stream.
  *
  * @param resolvedPath - The resolved path identifying a template
obtained by calling the resolve method.
  * @param object - The model to be passed to the template.
  * @param out - The output stream to write the result of processing the
template.
  *
  * @throws java.io.IOException - if there was an error processing the
template.
  */
 public void writeTo(String resolvedPath, Object object, OutputStream
out)
   throws IOException {
  Map<String, Object> model = new HashMap<String, Object>();
  model.put("resource", object);
  try {
   log.debug(resolvedPath);
   config.getTemplate(resolvedPath).process(model, new
OutputStreamWriter(out));
  } catch (TemplateException ex) {
   throw new WebApplicationException(ex);
  }
 }
}


________________________________

From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
Sent: 19 June 2009 12:20
To: users_at_jersey.dev.java.net
Cc: Jonathan Cook - FM&T
Subject: Re: [Jersey] Cannot Instantiate new provider



On Jun 19, 2009, at 12:51 PM, Jonathan Cook - FM&T wrote:


        Hi,
         
        I have a problem were I have written my own
FreeMarkerTemplateProcessor. Everything works nicely but when I deploy
my application it registers the new provider successfully but then it
complains that it can't instantiate the new class and then goes on to
fail I assume because it can't do the freemarker initialisation.
         
        19-Jun-2009 11:11:58
com.sun.ws.rest.api.core.PackagesResourceConfig init
        INFO: Provider classes found:
          class
com.bbc.newsi.feeds.sport.webservice.providers.FreemarkerTemplateProcess
        or
        19-Jun-2009 11:11:58
com.sun.ws.rest.impl.application.ComponentProviderCache get
        Component
        WARNING: The provider class, class
com.bbc.newsi.feeds.sport.webservice.provider
        s.FreemarkerTemplateProcessor, could not be instantiated


Is any stack trace provided as to why it cannot instantiate?

Can you send the code for the class?



        
        2009-06-19 11:11:58.110::WARN: /football/includepathgenerator:
java.io.IOExcept
        ion: The template name, /include_path_generator.ftl, could not
be resolved to th
        e path of a template
         
        Should I have a default constructor explicitly in the class? Is
there some condition when accessing the application remotely that the
templateprocessor won't get instantiated because as I say it all works
locally? I'm using an embedded instance of jetty as the web server.
        
        


Are your dependencies correct for the remote deployment?

Paul.



         
        I'm using the following annotations at the top of the class:
        @Provider
        @Singleton
         
        Any ideas?
         
        Thanks
        Jonathan