On Sun, 01 Sep 2002 16:35:16 -0500, "Jacob Kjome" <[EMAIL PROTECTED]> 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
  [EMAIL PROTECTED]

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to