tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: deploying war file and "Document base ... does not exist"
Date Tue, 03 Sep 2002 14:47:24 GMT
The only thing not portable is your assumption that the "logs" directory 
exists at the same level as the tempdir.  You may have to create that 
directory instead of just assuming it is there and using it.  Otherwise, it 
is a decent solution, although there are still reasons why getRealPath("/") 
would be useful, but if you can live without it and don't need to write 
anything to the filesystem within that context, then running directly from 
a .war file is just fine.

Jake

At 05:49 AM 9/3/2002 +0000, Daniel Kehoe wrote:
>On Sun, 01 Sep 2002 16:35:16 -0500, "Jacob Kjome" <hoju@visi.com> said:
> > Yes, but now you are constrained by the fact that the app is running
> > directly from myapp.war which means that you have no filesystem access to
> > your webapp.  For instance, the following will return null in your
> > setup...
> > String contextPath = getServletContext().getRealPath("/");
> > This is because there is no "real path" on your system for your webapp
> > since it is running out of an archive, not out of a directory on the
> > filesystem...
>
>Jake,
>
>Thanks, I've gotten around that issue by getting the path to the
>context attribute "javax.servlet.context.tempdir". Seems to work just
>fine for me, both with apps deployed as unarchived wars and those
>deployed through the manager interface as unexploded wars. Here's the
>method I use. It may be helpful to others...
>
>I wonder if this will work on servlet containers that are not Tomcat
>(Resin, etc)?
>
>/**
>  * Utility method returns a path to a log file WITHOUT USING
>  * context.getRealPath("/"). Suitable when an app is deployed
>  * from an unexploded war file.
>  *
>  * @param  context               the ServletContext object
>  * @return                       logFilePath such as
>  <tomcat>/logs/myapp.log
>  * @exception  ServletException  thrown by any error
>  * @exception  IOException       thrown by attempts to access files
>  that don't exist
>  */
>protected static String getLogFilePath(ServletContext context)
>         throws ServletException, IOException {
>         String logFilePath = null;
>         // find out if a configuration parameter defines the path to a 
> logfile:
>         if (context.getInitParameter("com.mycom.myapp.logfilepath") != null
>                 && 
> !"DEFAULT".equals(context.getInitParameter("com.mycom.logfilepath").toUpperCase())) 
> {
>                 logFilePath = 
> context.getInitParameter("com.mycom.myapp.logfilepath");
>         } else {
>                 String logFileName = "myapp.log";
>                 String slash = System.getProperty("file.separator");
>                 // try getting the tempdir value, it looks like
>                 // "C:\<tomcat4>\work\Standalone\localhost\myapp",
>                 // it works better than context.getRealPath("/") because
>                 // context.getRealPath("/") doesn't work well when the
>                 // app is deployed from an unexploded war file
>                 String lengthyPath = ((File) 
> context.getAttribute("javax.servlet.context.tempdir")).getParent();
>                 if (lengthyPath == null) {
>                         String msg = "FATAL: cannot obtain file system 
> reference, cannot set log file";
>                         System.out.println(msg);
>                         context.log(msg);
>                         throw new ServletException(msg);
>                 }
>                 lengthyPath = lengthyPath.substring(0, 
> lengthyPath.lastIndexOf(slash));
>                 lengthyPath = lengthyPath.substring(0, 
> lengthyPath.lastIndexOf(slash));
>                 logFilePath = lengthyPath.substring(0, 
> lengthyPath.lastIndexOf(slash))
>                         + slash + "logs" + slash + logFileName;
>                 // when we're done, the log filepath should look like:
>                 // "C:\<tomcat>\logs\myapp.log"
>         }
>         return logFilePath;
>}
>
>--
>   Daniel
>   kehoe@fortuity.com

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message