aspose file tools*
The moose likes Web Services and the fly likes how to read config file when web service starts up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "how to read config file when web service starts up" Watch "how to read config file when web service starts up" New topic
Author

how to read config file when web service starts up

Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104
Hello,

How can a web service read from a config file at its startup? I know in servlets, the code would get called from the servlet's init method.

I thought of writing a handler, but handlers get called for each request and response.

I am using a servlet based web service (not EJB based ws) and JAX-WS.

Thanks for your help.

Ravi


Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
user a listener, ServletContextListener and register it in your web.xml file.


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104
Hello Omar,

I would use the ServletContextListener with a servlet, but is that the recommended way with a (servlet-based) webservice?

Thanks for your response.

Ravi
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
since its a (servlet-based) i would say yes.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
Probably something along the lines of:



Hello.java
javax.annotation.Resource
javax.xml.ws.WebServiceContext
A little bit about Message Context in JAX-WS


"Don't succumb to the false authority of a tool or model. There is no substitute for thinking."
Andy Hunt, Pragmatic Thinking & Learning: Refactor Your Wetware p.41
Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104

Peer, Omar,

Thanks so much. This is perfect. I am so grateful for this information.

-Ravi
Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104
Peer,

I have tried the code:

...
import javax.xml.ws.WebServiceContext;
import javax.annotation.Resource;
import javax.servlet.ServletContext;

@WebService
public class Hello {

...
@WebMethod(exclude = true)
@Resource
public void initializeContext(WebServiceContext wsContext) {
MessageContext mc = wsContext.getMessageContext();
ServletContext context = ((javax.servlet.ServletContext) mc.get(MessageContext.SERVLET_CONTEXT));
String paramValue = context.getInitParameter("ParamName");

...
}
...
}

and I can't call: wsContext.getMessageContext() unless it is called from within an exposed web service method.
From Glassfish, I get this error:

java.lang.IllegalStateException: getMessageContext() can only be called while servicing a request

Do you know of another way to read some parameters (hopefully servlet initialization params from web.xml) from the initializeContext method?

Many thanks for your attention.

Ravi
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
Any chance you can use lazy initialization? Have every web method call the lazy initialization method with the MessageContext but only initialize on the first invocation.
Ravi Danum
Ranch Hand

Joined: Jan 13, 2009
Posts: 104
Thanks, Peer that makes sense.

-Ravi
Bibhu Biswal
Greenhorn

Joined: Dec 16, 2009
Posts: 1
Hello Ravi,

I am new to web service, Could you please show the code snippet.

Bibhu
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to read config file when web service starts up
 
Similar Threads
Rest Feature- No wsdl
JDK and web service, and other tools (questions from dummy)
JAX-RPC vs JAX-WS
How to change the database in Java EE
Performance impact loadProperties