I've a tomcat (with JSF) application. I want to know the path of the war of the current application.
I've tried the following code to know where tomcat is
((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("")
However, after I add antiResourceLocking="true"
to the context definition, the previous command is returning a 开发者_如何学Ctemporal directory.
I've been debugging, and I see that in the
((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext())
There is a variable named context with one attribute named docBase, which contains the info that I need. However I can't get this by any way.
The reason of this is: I'm doing an auto-update system, so I need to get the current war in order to apply some patches to it. Therefore I need the current war file path.
Either it is not possible, or no one knows how to resolve this.
Honestly I have to say this is not possible, or only with some very fault-prone hack like this
I presume that the real class of FacesContext.getCurrentInstance().getExternalContext().getContext()
id in this case org.apache.catalina.core.StandardContext
So you may acces docBase
like this
javax.faces.context.ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
if(externalContext instanceof org.apache.catalina.core.StandardContext) {
String path = ((org.apache.catalina.core.StandardContext)externalContext .getContext()).getDocBase();
}
But this this is in my opinion not a solution as you're highly dependend on tomcat's internals. Following issues remain:
* you have no guarantee that it's really an instance of StandardContext
* if this works for a given tomcat version, it may stop working without announcement in future
* in any case it does not work on any other app server
If just worte done this answer as you have discover by debuging this field. In general it's a very bad practice to debug some code you access though an API (here servlet API) and then find a way to access som internals not properly provided by this API, even if it's theoretically possible.
精彩评论