You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@curator.apache.org by xi...@barclays.com on 2013/07/20 02:01:03 UTC

NPE when a resource tries to use @Context injected ContextResolver

Hello,

I am using Curator's curator-x-discovery-sever and trying to set it up with Jersey and Tomcat.

I created my resource class, injected the class. Also injected those singletons mentioned.
(following this page http://curator.incubator.apache.org/curator-x-discovery-server/index.html )

However, I am getting NPE when the contextResolver tries to getContext.

The ContextResolver singleton is injected, and it looks like the @Context injection isn't happening.

Can someone please help me to see what is wrong with my code?

Thanks.

Here is my code, the highlighted line is where the exception happens:

Resource

@Path("compass")
public class CompassResource extends DiscoveryResource<ServicePayload>
{
    public CompassResource(@Context ContextResolver<DiscoveryContext<ServicePayload>> resolver)
    {
        super(resolver.getContext(DiscoveryContext.class));
    }
}

Application

public class DiscoveryServerApplication extends Application
{
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        //s.add(MyResource.class);
        s.add(CompassResource.class);
        return s;
    }

    @Override
    public Set<Object> getSingletons() {
        final Set<Object> singletons = Sets.newHashSet();

        // inject ServiceDiscovery singleton
        CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
                .connectString("localhost:2181").sessionTimeoutMs(300000).connectionTimeoutMs(30000)
                .retryPolicy(new ExponentialBackoffRetry(1000,3));
        CuratorFramework client = builder.build();
        client.start();

        ServiceDiscovery<ServicePayload> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServicePayload.class)
                .client(client).basePath("/").build();
        singletons.add(serviceDiscovery);

        // inject DiscoveryContext singleton
        GenericDiscoveryContext<ServicePayload> discoveryContext = new GenericDiscoveryContext<ServicePayload>
                (serviceDiscovery, new RandomStrategy<ServicePayload>(), 500, ServicePayload.class);
        singletons.add(discoveryContext);

        // inject JsonServiceInstanceMarshaller singleton
        singletons.add(new JsonServiceInstanceMarshaller(discoveryContext));

        // inject JsonServiceInstancesMarshaller singleton
        singletons.add(new JsonServiceInstancesMarshaller(discoveryContext));

        // inject JsonServiceNamesMarshaller
        singletons.add(new JsonServiceNamesMarshaller());

        return singletons;
    }
}
web.xml

    <servlet>
        <servlet-name>Curator Discovery Server</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.lb.eqr.discovery.server.DiscoveryServerApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Curator Discovery Server</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

===========

Here is the exception:

root cause
java.lang.NullPointerException
        com.lb.eqr.discovery.server.CompassResource.<init>(CompassResource.java:23)
        sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1091)
        org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:244)
        org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:319)
        org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:448)
        org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:157)
        org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2203)
        org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:579)
        org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:566)
        org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
        org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
        org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:105)
        org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
        org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
        org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
        org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
        org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
        org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:102)
        org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:62)
        org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
        org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:215)
        org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
        org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
        org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
        org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)



_______________________________________________

This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.

For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.

_______________________________________________

Re: NPE when a resource tries to use @Context injected ContextResolver

Posted by Xiaochen Chen <xi...@gmail.com>.
Couldn't agree more! That totally makes sense.

Though I'm surprised this hasn't been fixed …

Thanks Jordan. I'll test it out. And confirm later.

On Jul 20, 2013, at 5:26 PM, Jordan Zimmerman <jo...@jordanzimmerman.com> wrote:

> BTW - how many thousands of man years have been lost to erasure?!! What a horrible mistake that decision was.
> 
> -JZ
> 
> On Jul 20, 2013, at 2:24 PM, Jordan Zimmerman <jo...@jordanzimmerman.com> wrote:
> 
>> OK - I have a workaround. You need to create a wrapper class that acts as the context resolver:
>> 
>> @Provider
>> public class MyResolver implements ContextResolver<DiscoveryContext<ServicePayload>>
>> {
>>     private final GenericDiscoveryContext<ServicePayload> context;
>> 
>>     public MyResolver(GenericDiscoveryContext<ServicePayload> context)
>>     {
>>         this.context = context;
>>     }
>> 
>>     @Override
>>     public DiscoveryContext<ServicePayload> getContext(Class<?> type)
>>     {
>>         return context;
>>     }
>> }
>> 
>> In the Singletons, instead of adding discoveryContext, add:
>> 
>> 	new MyResolver(discoveryContext)
>> 
>> In my tests, this fixed the problem.
>> 
>> -Jordan
>> 
>> On Jul 19, 2013, at 5:01 PM, xiaochen.chen@barclays.com wrote:
>> 
>>> Hello,
>>>  
>>> I am using Curator’s curator-x-discovery-sever and trying to set it up with Jersey and Tomcat.
>>>  
>>> I created my resource class, injected the class. Also injected those singletons mentioned.
>>> (following this page http://curator.incubator.apache.org/curator-x-discovery-server/index.html )
>>>  
>>> However, I am getting NPE when the contextResolver tries to getContext.
>>>  
>>> The ContextResolver singleton is injected, and it looks like the @Context injection isn’t happening.
>>>  
>>> Can someone please help me to see what is wrong with my code?
>>>  
>>> Thanks.
>>>  
>>> Here is my code, the highlighted line is where the exception happens:
>>>  
>>> Resource
>>>  
>>> @Path("compass")
>>> public class CompassResource extends DiscoveryResource<ServicePayload>
>>> {
>>>     public CompassResource(@Context ContextResolver<DiscoveryContext<ServicePayload>> resolver)
>>>     {
>>>         super(resolver.getContext(DiscoveryContext.class));
>>>     }
>>> }
>>>  
>>> Application
>>>  
>>> public class DiscoveryServerApplication extends Application
>>> {
>>>     @Override
>>>     public Set<Class<?>> getClasses() {
>>>         Set<Class<?>> s = new HashSet<Class<?>>();
>>>         //s.add(MyResource.class);
>>>         s.add(CompassResource.class);
>>>         return s;
>>>     }
>>>  
>>>     @Override
>>>     public Set<Object> getSingletons() {
>>>         final Set<Object> singletons = Sets.newHashSet();
>>>  
>>>         // inject ServiceDiscovery singleton
>>>         CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
>>>                 .connectString("localhost:2181").sessionTimeoutMs(300000).connectionTimeoutMs(30000)
>>>                 .retryPolicy(new ExponentialBackoffRetry(1000,3));
>>>         CuratorFramework client = builder.build();
>>>         client.start();
>>>  
>>>         ServiceDiscovery<ServicePayload> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServicePayload.class)
>>>                 .client(client).basePath("/").build();
>>>         singletons.add(serviceDiscovery);
>>>  
>>>         // inject DiscoveryContext singleton
>>>         GenericDiscoveryContext<ServicePayload> discoveryContext = new GenericDiscoveryContext<ServicePayload>
>>>                 (serviceDiscovery, new RandomStrategy<ServicePayload>(), 500, ServicePayload.class);
>>>         singletons.add(discoveryContext);
>>>  
>>>         // inject JsonServiceInstanceMarshaller singleton
>>>         singletons.add(new JsonServiceInstanceMarshaller(discoveryContext));
>>>  
>>>         // inject JsonServiceInstancesMarshaller singleton
>>>         singletons.add(new JsonServiceInstancesMarshaller(discoveryContext));
>>>  
>>>         // inject JsonServiceNamesMarshaller
>>>         singletons.add(new JsonServiceNamesMarshaller());
>>>  
>>>         return singletons;
>>>     }
>>> }
>>> web.xml
>>>  
>>>     <servlet>
>>>         <servlet-name>Curator Discovery Server</servlet-name>
>>>         <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
>>>         <init-param>
>>>             <param-name>javax.ws.rs.Application</param-name>
>>>             <param-value>com.lb.eqr.discovery.server.DiscoveryServerApplication</param-value>
>>>         </init-param>
>>>         <load-on-startup>1</load-on-startup>
>>>     </servlet>
>>>     <servlet-mapping>
>>>         <servlet-name>Curator Discovery Server</servlet-name>
>>>         <url-pattern>/*</url-pattern>
>>>     </servlet-mapping>
>>>  
>>> ===========
>>>  
>>> Here is the exception:
>>>  
>>> root cause
>>> java.lang.NullPointerException
>>>         com.lb.eqr.discovery.server.CompassResource.<init>(CompassResource.java:23)
>>>         sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>>>         sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
>>>         sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
>>>         java.lang.reflect.Constructor.newInstance(Constructor.java:513)
>>>         org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1091)
>>>         org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:244)
>>>         org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:319)
>>>         org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:448)
>>>         org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:157)
>>>         org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2203)
>>>         org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:579)
>>>         org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:566)
>>>         org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
>>>         org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
>>>         org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:105)
>>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
>>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>>>         org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:102)
>>>         org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:62)
>>>         org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
>>>         org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:215)
>>>         org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
>>>         org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
>>>         org.glassfish.jersey.internal.Errors.process(Errors.java:315)
>>>         org.glassfish.jersey.internal.Errors.process(Errors.java:297)
>>>         org.glassfish.jersey.internal.Errors.process(Errors.java:267)
>>>         org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
>>>         org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
>>>         org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
>>>         org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
>>>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
>>>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
>>>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
>>>  
>>>  
>>> _______________________________________________
>>> 
>>> This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.
>>> 
>>> For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.
>>> 
>>> _______________________________________________
>>> 
>> 
> 


Re: NPE when a resource tries to use @Context injected ContextResolver

Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
BTW - how many thousands of man years have been lost to erasure?!! What a horrible mistake that decision was.

-JZ

On Jul 20, 2013, at 2:24 PM, Jordan Zimmerman <jo...@jordanzimmerman.com> wrote:

> OK - I have a workaround. You need to create a wrapper class that acts as the context resolver:
> 
> @Provider
> public class MyResolver implements ContextResolver<DiscoveryContext<ServicePayload>>
> {
>     private final GenericDiscoveryContext<ServicePayload> context;
> 
>     public MyResolver(GenericDiscoveryContext<ServicePayload> context)
>     {
>         this.context = context;
>     }
> 
>     @Override
>     public DiscoveryContext<ServicePayload> getContext(Class<?> type)
>     {
>         return context;
>     }
> }
> 
> In the Singletons, instead of adding discoveryContext, add:
> 
> 	new MyResolver(discoveryContext)
> 
> In my tests, this fixed the problem.
> 
> -Jordan
> 
> On Jul 19, 2013, at 5:01 PM, xiaochen.chen@barclays.com wrote:
> 
>> Hello,
>>  
>> I am using Curator’s curator-x-discovery-sever and trying to set it up with Jersey and Tomcat.
>>  
>> I created my resource class, injected the class. Also injected those singletons mentioned.
>> (following this page http://curator.incubator.apache.org/curator-x-discovery-server/index.html )
>>  
>> However, I am getting NPE when the contextResolver tries to getContext.
>>  
>> The ContextResolver singleton is injected, and it looks like the @Context injection isn’t happening.
>>  
>> Can someone please help me to see what is wrong with my code?
>>  
>> Thanks.
>>  
>> Here is my code, the highlighted line is where the exception happens:
>>  
>> Resource
>>  
>> @Path("compass")
>> public class CompassResource extends DiscoveryResource<ServicePayload>
>> {
>>     public CompassResource(@Context ContextResolver<DiscoveryContext<ServicePayload>> resolver)
>>     {
>>         super(resolver.getContext(DiscoveryContext.class));
>>     }
>> }
>>  
>> Application
>>  
>> public class DiscoveryServerApplication extends Application
>> {
>>     @Override
>>     public Set<Class<?>> getClasses() {
>>         Set<Class<?>> s = new HashSet<Class<?>>();
>>         //s.add(MyResource.class);
>>         s.add(CompassResource.class);
>>         return s;
>>     }
>>  
>>     @Override
>>     public Set<Object> getSingletons() {
>>         final Set<Object> singletons = Sets.newHashSet();
>>  
>>         // inject ServiceDiscovery singleton
>>         CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
>>                 .connectString("localhost:2181").sessionTimeoutMs(300000).connectionTimeoutMs(30000)
>>                 .retryPolicy(new ExponentialBackoffRetry(1000,3));
>>         CuratorFramework client = builder.build();
>>         client.start();
>>  
>>         ServiceDiscovery<ServicePayload> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServicePayload.class)
>>                 .client(client).basePath("/").build();
>>         singletons.add(serviceDiscovery);
>>  
>>         // inject DiscoveryContext singleton
>>         GenericDiscoveryContext<ServicePayload> discoveryContext = new GenericDiscoveryContext<ServicePayload>
>>                 (serviceDiscovery, new RandomStrategy<ServicePayload>(), 500, ServicePayload.class);
>>         singletons.add(discoveryContext);
>>  
>>         // inject JsonServiceInstanceMarshaller singleton
>>         singletons.add(new JsonServiceInstanceMarshaller(discoveryContext));
>>  
>>         // inject JsonServiceInstancesMarshaller singleton
>>         singletons.add(new JsonServiceInstancesMarshaller(discoveryContext));
>>  
>>         // inject JsonServiceNamesMarshaller
>>         singletons.add(new JsonServiceNamesMarshaller());
>>  
>>         return singletons;
>>     }
>> }
>> web.xml
>>  
>>     <servlet>
>>         <servlet-name>Curator Discovery Server</servlet-name>
>>         <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
>>         <init-param>
>>             <param-name>javax.ws.rs.Application</param-name>
>>             <param-value>com.lb.eqr.discovery.server.DiscoveryServerApplication</param-value>
>>         </init-param>
>>         <load-on-startup>1</load-on-startup>
>>     </servlet>
>>     <servlet-mapping>
>>         <servlet-name>Curator Discovery Server</servlet-name>
>>         <url-pattern>/*</url-pattern>
>>     </servlet-mapping>
>>  
>> ===========
>>  
>> Here is the exception:
>>  
>> root cause
>> java.lang.NullPointerException
>>         com.lb.eqr.discovery.server.CompassResource.<init>(CompassResource.java:23)
>>         sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>>         sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
>>         sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
>>         java.lang.reflect.Constructor.newInstance(Constructor.java:513)
>>         org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1091)
>>         org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:244)
>>         org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:319)
>>         org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:448)
>>         org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:157)
>>         org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2203)
>>         org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:579)
>>         org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:566)
>>         org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
>>         org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
>>         org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:105)
>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>>         org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:102)
>>         org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:62)
>>         org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
>>         org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:215)
>>         org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
>>         org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
>>         org.glassfish.jersey.internal.Errors.process(Errors.java:315)
>>         org.glassfish.jersey.internal.Errors.process(Errors.java:297)
>>         org.glassfish.jersey.internal.Errors.process(Errors.java:267)
>>         org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
>>         org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
>>         org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
>>         org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
>>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
>>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
>>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
>>  
>>  
>> _______________________________________________
>> 
>> This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.
>> 
>> For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.
>> 
>> _______________________________________________
>> 
> 


Re: NPE when a resource tries to use @Context injected ContextResolver

Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
OK - I have a workaround. You need to create a wrapper class that acts as the context resolver:

@Provider
public class MyResolver implements ContextResolver<DiscoveryContext<ServicePayload>>
{
    private final GenericDiscoveryContext<ServicePayload> context;

    public MyResolver(GenericDiscoveryContext<ServicePayload> context)
    {
        this.context = context;
    }

    @Override
    public DiscoveryContext<ServicePayload> getContext(Class<?> type)
    {
        return context;
    }
}

In the Singletons, instead of adding discoveryContext, add:

	new MyResolver(discoveryContext)

In my tests, this fixed the problem.

-Jordan

On Jul 19, 2013, at 5:01 PM, xiaochen.chen@barclays.com wrote:

> Hello,
>  
> I am using Curator’s curator-x-discovery-sever and trying to set it up with Jersey and Tomcat.
>  
> I created my resource class, injected the class. Also injected those singletons mentioned.
> (following this page http://curator.incubator.apache.org/curator-x-discovery-server/index.html )
>  
> However, I am getting NPE when the contextResolver tries to getContext.
>  
> The ContextResolver singleton is injected, and it looks like the @Context injection isn’t happening.
>  
> Can someone please help me to see what is wrong with my code?
>  
> Thanks.
>  
> Here is my code, the highlighted line is where the exception happens:
>  
> Resource
>  
> @Path("compass")
> public class CompassResource extends DiscoveryResource<ServicePayload>
> {
>     public CompassResource(@Context ContextResolver<DiscoveryContext<ServicePayload>> resolver)
>     {
>         super(resolver.getContext(DiscoveryContext.class));
>     }
> }
>  
> Application
>  
> public class DiscoveryServerApplication extends Application
> {
>     @Override
>     public Set<Class<?>> getClasses() {
>         Set<Class<?>> s = new HashSet<Class<?>>();
>         //s.add(MyResource.class);
>         s.add(CompassResource.class);
>         return s;
>     }
>  
>     @Override
>     public Set<Object> getSingletons() {
>         final Set<Object> singletons = Sets.newHashSet();
>  
>         // inject ServiceDiscovery singleton
>         CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
>                 .connectString("localhost:2181").sessionTimeoutMs(300000).connectionTimeoutMs(30000)
>                 .retryPolicy(new ExponentialBackoffRetry(1000,3));
>         CuratorFramework client = builder.build();
>         client.start();
>  
>         ServiceDiscovery<ServicePayload> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServicePayload.class)
>                 .client(client).basePath("/").build();
>         singletons.add(serviceDiscovery);
>  
>         // inject DiscoveryContext singleton
>         GenericDiscoveryContext<ServicePayload> discoveryContext = new GenericDiscoveryContext<ServicePayload>
>                 (serviceDiscovery, new RandomStrategy<ServicePayload>(), 500, ServicePayload.class);
>         singletons.add(discoveryContext);
>  
>         // inject JsonServiceInstanceMarshaller singleton
>         singletons.add(new JsonServiceInstanceMarshaller(discoveryContext));
>  
>         // inject JsonServiceInstancesMarshaller singleton
>         singletons.add(new JsonServiceInstancesMarshaller(discoveryContext));
>  
>         // inject JsonServiceNamesMarshaller
>         singletons.add(new JsonServiceNamesMarshaller());
>  
>         return singletons;
>     }
> }
> web.xml
>  
>     <servlet>
>         <servlet-name>Curator Discovery Server</servlet-name>
>         <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
>         <init-param>
>             <param-name>javax.ws.rs.Application</param-name>
>             <param-value>com.lb.eqr.discovery.server.DiscoveryServerApplication</param-value>
>         </init-param>
>         <load-on-startup>1</load-on-startup>
>     </servlet>
>     <servlet-mapping>
>         <servlet-name>Curator Discovery Server</servlet-name>
>         <url-pattern>/*</url-pattern>
>     </servlet-mapping>
>  
> ===========
>  
> Here is the exception:
>  
> root cause
> java.lang.NullPointerException
>         com.lb.eqr.discovery.server.CompassResource.<init>(CompassResource.java:23)
>         sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>         sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
>         sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
>         java.lang.reflect.Constructor.newInstance(Constructor.java:513)
>         org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1091)
>         org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:244)
>         org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:319)
>         org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:448)
>         org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:157)
>         org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2203)
>         org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:579)
>         org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:566)
>         org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
>         org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
>         org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:105)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>         org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:102)
>         org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:62)
>         org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
>         org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:215)
>         org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
>         org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
>         org.glassfish.jersey.internal.Errors.process(Errors.java:315)
>         org.glassfish.jersey.internal.Errors.process(Errors.java:297)
>         org.glassfish.jersey.internal.Errors.process(Errors.java:267)
>         org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
>         org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
>         org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
>         org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
>  
>  
> _______________________________________________
> 
> This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.
> 
> For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.
> 
> _______________________________________________
> 


Re: NPE when a resource tries to use @Context injected ContextResolver

Posted by Jordan Zimmerman <jo...@jordanzimmerman.com>.
There appears to be a bug in Jersey. I found this on the net:

	https://java.net/projects/jersey/lists/users/archive/2009-04/message/498

In debugging the problem, internally Jersey uses a map and the key from the map to the context resolver is the wrong type. I'll see if I can figure out a workaround.

-JZ

On Jul 19, 2013, at 5:01 PM, xiaochen.chen@barclays.com wrote:

> Hello,
>  
> I am using Curator’s curator-x-discovery-sever and trying to set it up with Jersey and Tomcat.
>  
> I created my resource class, injected the class. Also injected those singletons mentioned.
> (following this page http://curator.incubator.apache.org/curator-x-discovery-server/index.html )
>  
> However, I am getting NPE when the contextResolver tries to getContext.
>  
> The ContextResolver singleton is injected, and it looks like the @Context injection isn’t happening.
>  
> Can someone please help me to see what is wrong with my code?
>  
> Thanks.
>  
> Here is my code, the highlighted line is where the exception happens:
>  
> Resource
>  
> @Path("compass")
> public class CompassResource extends DiscoveryResource<ServicePayload>
> {
>     public CompassResource(@Context ContextResolver<DiscoveryContext<ServicePayload>> resolver)
>     {
>         super(resolver.getContext(DiscoveryContext.class));
>     }
> }
>  
> Application
>  
> public class DiscoveryServerApplication extends Application
> {
>     @Override
>     public Set<Class<?>> getClasses() {
>         Set<Class<?>> s = new HashSet<Class<?>>();
>         //s.add(MyResource.class);
>         s.add(CompassResource.class);
>         return s;
>     }
>  
>     @Override
>     public Set<Object> getSingletons() {
>         final Set<Object> singletons = Sets.newHashSet();
>  
>         // inject ServiceDiscovery singleton
>         CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
>                 .connectString("localhost:2181").sessionTimeoutMs(300000).connectionTimeoutMs(30000)
>                 .retryPolicy(new ExponentialBackoffRetry(1000,3));
>         CuratorFramework client = builder.build();
>         client.start();
>  
>         ServiceDiscovery<ServicePayload> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServicePayload.class)
>                 .client(client).basePath("/").build();
>         singletons.add(serviceDiscovery);
>  
>         // inject DiscoveryContext singleton
>         GenericDiscoveryContext<ServicePayload> discoveryContext = new GenericDiscoveryContext<ServicePayload>
>                 (serviceDiscovery, new RandomStrategy<ServicePayload>(), 500, ServicePayload.class);
>         singletons.add(discoveryContext);
>  
>         // inject JsonServiceInstanceMarshaller singleton
>         singletons.add(new JsonServiceInstanceMarshaller(discoveryContext));
>  
>         // inject JsonServiceInstancesMarshaller singleton
>         singletons.add(new JsonServiceInstancesMarshaller(discoveryContext));
>  
>         // inject JsonServiceNamesMarshaller
>         singletons.add(new JsonServiceNamesMarshaller());
>  
>         return singletons;
>     }
> }
> web.xml
>  
>     <servlet>
>         <servlet-name>Curator Discovery Server</servlet-name>
>         <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
>         <init-param>
>             <param-name>javax.ws.rs.Application</param-name>
>             <param-value>com.lb.eqr.discovery.server.DiscoveryServerApplication</param-value>
>         </init-param>
>         <load-on-startup>1</load-on-startup>
>     </servlet>
>     <servlet-mapping>
>         <servlet-name>Curator Discovery Server</servlet-name>
>         <url-pattern>/*</url-pattern>
>     </servlet-mapping>
>  
> ===========
>  
> Here is the exception:
>  
> root cause
> java.lang.NullPointerException
>         com.lb.eqr.discovery.server.CompassResource.<init>(CompassResource.java:23)
>         sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>         sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
>         sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
>         java.lang.reflect.Constructor.newInstance(Constructor.java:513)
>         org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1091)
>         org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:244)
>         org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:319)
>         org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:448)
>         org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:157)
>         org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2203)
>         org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:579)
>         org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:566)
>         org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
>         org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
>         org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:105)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>         org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
>         org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:102)
>         org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:62)
>         org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
>         org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:215)
>         org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
>         org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
>         org.glassfish.jersey.internal.Errors.process(Errors.java:315)
>         org.glassfish.jersey.internal.Errors.process(Errors.java:297)
>         org.glassfish.jersey.internal.Errors.process(Errors.java:267)
>         org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
>         org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
>         org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
>         org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
>         org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
>  
>  
> _______________________________________________
> 
> This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.
> 
> For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.
> 
> _______________________________________________
>