Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using the StandardContext.getDocBase()

 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there.

I'm trying to use the StandardContext in the org.apache.catalina.core.* package to retreive the docBase from the context. I have experience using the java.naming.Context and the java.naming.InitialContext classes, but can't seem to get the StandardContext to work. I've included the catalina 5.5.9 jar in my classpath and things seem to compile correctly, but I get runtime errors.

In all honesty, I can't even get this to work:

StandardContext tomcatContext = new StandardContext();

When I load the page I get this error:

ERROR - Servlet.service() for servlet action threw exception java.lang.NoClassDefFoundError: org/apache/tomcat/util/http/mapper/Mapper at org.apache.catalina.core.StandardContext.<init>(StandardContext.java:377

Has anyone tried to do this before? All I really want to be able to do is StandardContext.getDocBase(). That's all.

Thanks for your time!
bryan
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The missing class (Mapper) is in CATALINA_HOME/server/lib/tomcat-util.jar
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh. Well that solved that problem. (So why didn't the compiler catch that that class wasn't available?)

Unfortunately I'm still not quite there yet. I have this so far:



This is actually returning "null" right now. Which makes sense because if I just created a new context, the docBase will be null for it. So my question is, how do I get the current context (perhaps the javax.naming.Context?) to populate my new StandardContext? Everytime I try to do something like this:



i receive a class cast exception. any ideas?

thanks for your help so far!
bryan
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check what's being returned with
System.out.println( initialContext.lookup("java:comp/env").getClass() );
and go from there.
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that class is this:

org.apache.naming.NamingContext

So it seems like I need to get from an org.apache.catalina.core.StandardContext to an org.apache.naming.NamingContext. Only I have no idea how to do this. I can't find any methods in either of those classes to attempt such a thing when looking through the API Docs.

What's also weird is that this compiles just fine:

tomcatContext = (StandardContext) initialContext.lookup("java:comp/env");

however, it throws a classCastException at runtime.

Any ideas on how to get my NamingContext to a StandardContext?

Thank you!
bryan
 
Joseph Marques
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well of course

tomcatContext = (StandardContext) initialContext.lookup("java:comp/env");

compiles just fine. The lookup returns an object, and you're down-casting to some other type. A compiler does not konw what subtypes of objects were put into the context, and it can't possible know what the string "java:comp/env" will be mapped to in a target environment anyway.

Then, at runtime, the "java:comp/env" is mapped to one type of context object, but you're trying to cast it into another - ClassCastException.

My two cents,
-joe
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough.

So how DO I get a NamingContext to a StandardContext?
 
Roel De Nijs
Sheriff
Posts: 10054
127
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a really good question, because i need this also.

How could you get the StandardContext-object where you can call getters like getDocBase, findMimeMapping,...
 
bryan nelson
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I still haven't figured this out either. There has to be a way...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want your webapps to have access to objects residing outside of it's own context, you have to set the privileged attribute to "true" in your Context entry.

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

I haven't done what you're trying to do so I can't help you beyond that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic