jbx
jbx

Reputation: 22148

How do I inject @WebServiceRef resources through Glassfish JNDI?

My web application is a consumer of some internal web services provided by the organisation. I have seen a couple of examples where a web service reference is injected using something like:

@WebServiceRef(name="services/MyService")
MyService myService; 

But I have no idea how to define the services/MyService JNDI object in Glassfish v3 such that it gets injected. I wish to define the webservice client class through the Glassfish administration and also specify the endpoint URL through the admin console. This way URLs are not hardcoded and easily managed.

Eventually I will also need to specify the username and password for HTTP authentication too in the same way apart from the URL. How do I go about it?

Upvotes: 2

Views: 5339

Answers (1)

Joel Barnum
Joel Barnum

Reputation: 131

I don't know of a way to define a "global" JNDI name, but you can write a service-ref element in web.xml that will work:

<service-ref>
  <service-ref-name>services/MyService</service-ref-name>
  <service-interface>service.MyEndpointService</service-interface>
  <wsdl-file>http://example.com/MyWsdl</wsdl-file>
</service-ref>

You can then use the annotated field you mentioned.

For the username and password, once you get a proxy from the service, you can cast it to BindingProvider and then set properties:

MySEI proxy = myService.getMyEndpointPort();
BindingProvider bp = (BindingProvider)proxy;
Map<String, Object> rc = bp.getRequestContext();
rc.put(BindingProvider.USERNAME_PROPERTY, "myuser");
rc.put(BindingProvider.PASSWORD_PROPERTY, "mypass");

Upvotes: 2

Related Questions