Return-Path: X-Original-To: apmail-curator-user-archive@minotaur.apache.org Delivered-To: apmail-curator-user-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DB26C10B75 for ; Sat, 20 Jul 2013 21:27:48 +0000 (UTC) Received: (qmail 41814 invoked by uid 500); 20 Jul 2013 21:27:48 -0000 Delivered-To: apmail-curator-user-archive@curator.apache.org Received: (qmail 41785 invoked by uid 500); 20 Jul 2013 21:27:48 -0000 Mailing-List: contact user-help@curator.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@curator.incubator.apache.org Delivered-To: mailing list user@curator.incubator.apache.org Received: (qmail 41777 invoked by uid 99); 20 Jul 2013 21:27:48 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 20 Jul 2013 21:27:48 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW X-Spam-Check-By: apache.org Received-SPF: error (nike.apache.org: local policy) Received: from [209.85.160.44] (HELO mail-pb0-f44.google.com) (209.85.160.44) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 20 Jul 2013 21:27:41 +0000 Received: by mail-pb0-f44.google.com with SMTP id uo1so5641491pbc.31 for ; Sat, 20 Jul 2013 14:27:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:content-type:message-id:mime-version:subject:date:references :to:in-reply-to:x-mailer:x-gm-message-state; bh=CkQ6aU7jmxfuZIosB9wN9vaEqaTIfI0lGep6UNq9EZ0=; b=orcBuMWOJ7/cvDUCaSp0rvmXZMUoB4yRzUJxxQE5B62d7jQbrO2comfbPTCJkMTfu0 VHS1NAGq3VWuTXW/pGndt+gDZHpWA6yHxn+05ekNt4YqHom9s3JMjF+h8PPIlRtTR64K Fa63R0iAUGcOeBmx7Fn3AO7+nDcRKk63eBna1Xr+hDZlaq4pA3z9lbh55y8kD+vV/jZs 09vwnLls+v9JfvIaXwDZsbtSU79r0v76yH3NEupUK0+EK5yo9FlEq/65p8pwi88A2Kvg /GOHfbqERWkc0cGXJCatXpL/5tB1+IHZwP6Fr2dHEAKLZUX2lSTlySPI0mBJY+Q1rAEm hYqA== X-Received: by 10.68.232.225 with SMTP id tr1mr23310515pbc.143.1374355620021; Sat, 20 Jul 2013 14:27:00 -0700 (PDT) Received: from [192.168.0.179] (c-76-103-249-200.hsd1.ca.comcast.net. [76.103.249.200]) by mx.google.com with ESMTPSA id pv5sm30111385pac.14.2013.07.20.14.26.58 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 20 Jul 2013 14:26:59 -0700 (PDT) From: Jordan Zimmerman Content-Type: multipart/alternative; boundary="Apple-Mail=_77D1755A-6A71-4427-AE96-BEC304C3B7DB" Message-Id: <74BD4ED5-6D18-43E4-B99D-475C5A19F499@jordanzimmerman.com> Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) Subject: Re: NPE when a resource tries to use @Context injected ContextResolver Date: Sat, 20 Jul 2013 14:26:57 -0700 References: To: user@curator.incubator.apache.org In-Reply-To: X-Mailer: Apple Mail (2.1508) X-Gm-Message-State: ALoCoQmDkxoXFuEs7kxoxmy4LoztPTDo/H/1NWXiYLZUL4OG4sywV7ywgOEM3ulb6ETx33fyPnvJ X-Virus-Checked: Checked by ClamAV on apache.org --Apple-Mail=_77D1755A-6A71-4427-AE96-BEC304C3B7DB Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=windows-1252 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 = wrote: > OK - I have a workaround. You need to create a wrapper class that acts = as the context resolver: >=20 > @Provider > public class MyResolver implements = ContextResolver> > { > private final GenericDiscoveryContext context; >=20 > public MyResolver(GenericDiscoveryContext context) > { > this.context =3D context; > } >=20 > @Override > public DiscoveryContext getContext(Class type) > { > return context; > } > } >=20 > In the Singletons, instead of adding discoveryContext, add: >=20 > new MyResolver(discoveryContext) >=20 > In my tests, this fixed the problem. >=20 > -Jordan >=20 > On Jul 19, 2013, at 5:01 PM, xiaochen.chen@barclays.com wrote: >=20 >> Hello, >> =20 >> I am using Curator=92s curator-x-discovery-sever and trying to set it = up with Jersey and Tomcat. >> =20 >> 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 = ) >> =20 >> However, I am getting NPE when the contextResolver tries to = getContext. >> =20 >> The ContextResolver singleton is injected, and it looks like the = @Context injection isn=92t happening. >> =20 >> Can someone please help me to see what is wrong with my code? >> =20 >> Thanks. >> =20 >> Here is my code, the highlighted line is where the exception happens: >> =20 >> Resource >> =20 >> @Path("compass") >> public class CompassResource extends = DiscoveryResource >> { >> public CompassResource(@Context = ContextResolver> resolver) >> { >> super(resolver.getContext(DiscoveryContext.class)); >> } >> } >> =20 >> Application >> =20 >> public class DiscoveryServerApplication extends Application >> { >> @Override >> public Set> getClasses() { >> Set> s =3D new HashSet>(); >> //s.add(MyResource.class); >> s.add(CompassResource.class); >> return s; >> } >> =20 >> @Override >> public Set getSingletons() { >> final Set singletons =3D Sets.newHashSet(); >> =20 >> // inject ServiceDiscovery singleton >> CuratorFrameworkFactory.Builder builder =3D = CuratorFrameworkFactory.builder() >> = .connectString("localhost:2181").sessionTimeoutMs(300000).connectionTimeou= tMs(30000) >> .retryPolicy(new ExponentialBackoffRetry(1000,3)); >> CuratorFramework client =3D builder.build(); >> client.start(); >> =20 >> ServiceDiscovery serviceDiscovery =3D = ServiceDiscoveryBuilder.builder(ServicePayload.class) >> .client(client).basePath("/").build(); >> singletons.add(serviceDiscovery); >> =20 >> // inject DiscoveryContext singleton >> GenericDiscoveryContext discoveryContext =3D = new GenericDiscoveryContext >> (serviceDiscovery, new = RandomStrategy(), 500, ServicePayload.class); >> singletons.add(discoveryContext); >> =20 >> // inject JsonServiceInstanceMarshaller singleton >> singletons.add(new = JsonServiceInstanceMarshaller(discoveryContext)); >> =20 >> // inject JsonServiceInstancesMarshaller singleton >> singletons.add(new = JsonServiceInstancesMarshaller(discoveryContext)); >> =20 >> // inject JsonServiceNamesMarshaller >> singletons.add(new JsonServiceNamesMarshaller()); >> =20 >> return singletons; >> } >> } >> web.xml >> =20 >> >> Curator Discovery Server >> = org.glassfish.jersey.servlet.ServletContainer >> >> javax.ws.rs.Application >> = com.lb.eqr.discovery.server.DiscoveryServerApplication >> >> 1 >> >> >> Curator Discovery Server >> /* >> >> =20 >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >> =20 >> Here is the exception: >> =20 >> root cause >> java.lang.NullPointerException >> = com.lb.eqr.discovery.server.CompassResource.(CompassResource.java:23= ) >> sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native = Method) >> = sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAcc= essorImpl.java:39) >> = sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstr= uctorAccessorImpl.java:27) >> = java.lang.reflect.Constructor.newInstance(Constructor.java:513) >> = org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionH= elper.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(RequestSco= pe.java:157) >> = org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2203) >> = org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.ja= va:579) >> = org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.ja= va:566) >> = org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.jav= a:172) >> = org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.ge= tInstance(MethodHandler.java:185) >> = org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply= (PushMethodHandlerRouter.java:105) >> = org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingSt= age.java:118) >> = org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingSt= age.java:121) >> = org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingSt= age.java:121) >> = org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingSt= age.java:121) >> = org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingSt= age.java:121) >> = org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingSta= ge.java:102) >> = org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingSta= ge.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.j= ava:946) >> = org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323) >> = org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.jav= a:372) >> = org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.jav= a:335) >> = org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.jav= a:218) >> =20 >> =20 >> _______________________________________________ >>=20 >> 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. >>=20 >> 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. >>=20 >> _______________________________________________ >>=20 >=20 --Apple-Mail=_77D1755A-6A71-4427-AE96-BEC304C3B7DB Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=windows-1252 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 <jordan@jordanzimmerman.com&= gt; 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>>
{<= /div>
    private final = GenericDiscoveryContext<ServicePayload> = context;

    public = MyResolver(GenericDiscoveryContext<ServicePayload> = context)
    {
        = this.context =3D 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=92s 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  )


= = --Apple-Mail=_77D1755A-6A71-4427-AE96-BEC304C3B7DB--