users@jersey.java.net

Re: [Jersey] Cannot Instantiate new provider

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 19 Jun 2009 13:58:04 +0200

On Jun 19, 2009, at 1:42 PM, Jonathan Cook - FM&T wrote:

> Hi,
>
> It is a windows environment. Our deployment is manual. We copy over
> some
> folders/files. We don't have jersey installed as such the jars are
> just
> bundled with our release.
>
> We are using the same version of Jersey in both environments and it is
> older than 1.0.2. Why does it get to that line of code below?
>

Because there is an error when Jersey attempts to instantiate an
instance of FreemarkerTemplateProcessor class. But we do not know what
it is because the version of Jersey you are using does not log it.

Can you point me to the information you found when searching?

Paul.

> Thanks
> Jonathan
>
> -----Original Message-----
> From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
> Sent: 19 June 2009 12:35
> To: Jonathan Cook - FM&T
> Cc: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] Cannot Instantiate new provider
>
> Hi,
>
> What is your remote environment? how are you deploying? do you have a
> version of Jersey installed in that remote environment?
>
> The error:
>
> WARNING: The provider class, class
> com.bbc.newsi.feeds.sport.webservice.provider
> s.FreemarkerTemplateProcessor, could not be instantiated
>
> is from Jersey 1.0.2.
>
> For 1.0.3 and above the error will be different, as per the code:
>
> LOGGER.log(Level.SEVERE,
> "The provider class, " + c +
> ", could not be instantiated. Processing will
> continue but the class will not be utilized", ex);
>
> Are the versions of Jersey you are using for local and remote
> deployment
> different?
>
> Paul.
>
> On Jun 19, 2009, at 1:23 PM, Jonathan Cook - FM&T wrote:
>
>> 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
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>