views:

102

answers:

1

I have a webservice running on tomcat (and working). I would like to extract the endpoint URL into a file outside the war, so that it can be changed without needing to redeploy the war. I am running locally with Tomcat 6 standalone on Windows XP.

My thought was to have it in the Context, either as a context param or as an Environment variable.

I wasn't sure which was better, and since I can't get the Context recognized, I have no idea which will work. So my first question, which of the entries below should I be using (if any)?

<Context path="/myapp" docBase="myapp">

<Environment name="webserviceURI" type="java.lang.String" override="true" value="endpointURLredacted"/>

<context-parameter>
<name>uri</name>
<value>endpointURLredacted</value>
</context-parameter>

</Context>

Assuming the Environment line is correct, my code to retrieve is:

InitialContext ic = new InitialContext();
endpoint = (String)ic.lookup("webserviceURI");

However....

I have tried adding this in a context.xml file to the META-INF folder, and I was under the impression that when I deploy the war, to quote an answer from another question on here, "When you deploy the WAR, the context fragment (META-INF/context.xml) is copied to conf/Catalina/[host] directory." Well, that doesn't happen. I don't really want to do it that way, since I want the context to exist completely separately from the war, but it was a test to see if I could even get that far. Fail.

So I tried adding the context xml (renamed to myapp.xml) to that folder (had to create it first) manually, but I still don't think the file is getting read.

When I run the app I get an error like "javax.naming.NameNotFoundException: Name webserviceURI is not bound in this Context".

I have tried printing out the initial context's environment:

Context:     {java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory,java.naming.factory.url.pkgs=org.apache.naming}

So I don't think the context file is getting read, no matter where I put it.

This isn't a servlet (which maybe it should be?) if that matters.

+1  A: 

I deleted the docBase attribute from the context, used the Environment tag, and retrieved the value by using java:comp/env/webserviceURI, and voila! Success.

Maybe that will help someone else!

Michaela