cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject cvs commit: xml-cocoon2/src/org/apache/cocoon/servlet CocoonServlet.java
Date Tue, 30 Oct 2001 13:20:28 GMT
cziegeler    01/10/30 05:20:28

  Modified:    src/org/apache/cocoon/components/classloader
                        RepositoryClassLoader.java
               src/org/apache/cocoon/servlet CocoonServlet.java
  Log:
  Started avoiding getRealPath() calls and use getResource() etc. instead.
  We still work in Tomcat 3.2 and 3.3. What about other containers?
  And how can we build the classpath if the war file is not unpacked?
  
  Revision  Changes    Path
  1.7       +8 -1      xml-cocoon2/src/org/apache/cocoon/components/classloader/RepositoryClassLoader.java
  
  Index: RepositoryClassLoader.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/classloader/RepositoryClassLoader.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- RepositoryClassLoader.java	2001/10/23 10:16:04	1.6
  +++ RepositoryClassLoader.java	2001/10/30 13:20:28	1.7
  @@ -27,7 +27,7 @@
    *
    * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  - * @version CVS $Revision: 1.6 $ $Date: 2001/10/23 10:16:04 $
  + * @version CVS $Revision: 1.7 $ $Date: 2001/10/30 13:20:28 $
    */
   public class RepositoryClassLoader extends URLClassLoader implements Loggable {
   
  @@ -114,6 +114,13 @@
             log.error("The repository had a bad URL", mue);
             throw new IOException("Could not add repository");
         }
  +  }
  +
  +  /**
  +   * Add a url to the list of searchable repositories
  +   */
  +  public void addURL(URL url) {
  +      super.addURL(url);
     }
   
     /**
  
  
  
  1.50      +117 -49   xml-cocoon2/src/org/apache/cocoon/servlet/CocoonServlet.java
  
  Index: CocoonServlet.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/servlet/CocoonServlet.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- CocoonServlet.java	2001/10/29 15:31:50	1.49
  +++ CocoonServlet.java	2001/10/30 13:20:28	1.50
  @@ -39,7 +39,7 @@
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
   import java.io.File;
  -import java.io.FileInputStream;
  +import java.io.InputStream;
   import java.io.IOException;
   import java.io.OutputStream;
   import java.lang.reflect.Constructor;
  @@ -58,7 +58,7 @@
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
    * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
  - * @version CVS $Revision: 1.49 $ $Date: 2001/10/29 15:31:50 $
  + * @version CVS $Revision: 1.50 $ $Date: 2001/10/30 13:20:28 $
    */
   
   public class CocoonServlet extends HttpServlet {
  @@ -105,6 +105,18 @@
       private boolean addClassDirs;
   
       /**
  +     * This is the path to the servlet context (or the result
  +     * of calling getRealPath('/') on the ServletContext.
  +     * Note, that this can be null.
  +     */
  +    protected String servletContextPath;
  +
  +    /**
  +     * This is the url to the servlet context directory
  +     */
  +    protected URL servletContextURL;
  +
  +    /**
        * Initialize this <code>CocoonServlet</code> instance.  You will
        * notice that I have broken the init into sub methods to make it
        * easier to maintain (BL).  The context is passed to a couple of
  @@ -125,7 +137,47 @@
           this.servletContext = conf.getServletContext();
           this.appContext.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, new HttpContext(this.servletContext));
   
  +        this.servletContextPath = this.servletContext.getRealPath("/");
  +        // first init the work-directory for the logger.
  +        // this is required if we are running inside a war file!
  +        final String workDirParam = conf.getInitParameter("work-directory");
  +        if ((workDirParam != null) && (!workDirParam.trim().equals(""))) {
  +            if (this.servletContextPath == null) {
  +                this.workDir = new File(workDirParam);
  +            } else {
  +                this.workDir = IOUtils.createFile( new File(servletContextPath) , workDirParam);
  +            }
  +            this.workDir.mkdirs();
  +        } else {
  +            this.workDir = (File) this.servletContext.getAttribute("javax.servlet.context.tempdir");
  +            this.workDir = new File(workDir, "cocoon-files");
  +            this.workDir.mkdirs();
  +        }
  +
           this.initLogger();
  +        String path = this.servletContextPath;
  +        log.debug("getRealPath for /: " + path);
  +        if(path == null) {
  +            // Try to figure out the path of the root from that of WEB-INF
  +            try {
  +                path = this.servletContext.getResource("/WEB-INF").toString();
  +            } catch (java.net.MalformedURLException me) {
  +                throw new ServletException("Unable to get resource 'WEB-INF'.", me);
  +            }
  +            log.debug("getResource for /WEB-INF: " + path);
  +            path = path.substring(0,path.length()-"WEB-INF".length());
  +            log.debug("Path for Root: " + path);
  +        }
  +
  +        try {
  +            if(path.indexOf(':')>1)
  +                this.servletContextURL = new URL(path);
  +            else
  +                this.servletContextURL = (new File(path)).toURL();
  +        } catch (java.net.MalformedURLException me) {
  +            throw new ServletException("Unable to determine servlet context URL.", me);
  +        }
  +        log.debug("URL for Root: " + this.servletContextURL);
   
           this.forceLoadParameter = conf.getInitParameter("load-class");
           if(conf.getInitParameter("load-class") == null) {
  @@ -140,22 +192,26 @@
              log.debug("init-classloader was not set - defaulting to false");
           }
   
  -        String workDirParam = conf.getInitParameter("work-directory");
  +        // add work directory
           if ((workDirParam != null) && (!workDirParam.trim().equals(""))) {
  -            workDir = IOUtils.createFile( new File(this.servletContext.getRealPath("/"))
, workDirParam);
  -            workDir.mkdirs();
  +            log.debug("using work-directory " + this.workDir);
           } else {
  -            workDir = (File) this.servletContext.getAttribute("javax.servlet.context.tempdir");
  -            workDir = new File(workDir, "cocoon-files");
  +            log.debug("work-directory was not set - defaulting to " + this.workDir);
           }
  -
           this.appContext.put(Constants.CONTEXT_WORK_DIR, workDir);
   
  -        String uploadDirParam = conf.getInitParameter("upload-directory");
  +        final String uploadDirParam = conf.getInitParameter("upload-directory");
           if ((uploadDirParam != null) && (!uploadDirParam.trim().equals(""))) {
  -            this.uploadDir = IOUtils.createFile( new File(this.servletContext.getRealPath("/"))
, uploadDirParam);
  +            if (this.servletContextPath == null) {
  +                this.uploadDir = new File(uploadDirParam);
  +            } else {
  +                this.uploadDir = IOUtils.createFile( new File(servletContextPath) , uploadDirParam);
  +            }
  +            this.uploadDir.mkdirs();
  +            log.debug("using upload-directory " + this.uploadDir);
           } else        {
               this.uploadDir = IOUtils.createFile(workDir, "image-dir" + File.separator);
  +            log.debug("upload-directory was not set - defaulting to " + this.uploadDir);
           }
   
           this.appContext.put(Constants.CONTEXT_UPLOAD_DIR, this.uploadDir);
  @@ -163,9 +219,16 @@
   
           String cacheDirParam = conf.getInitParameter("cache-directory");
           if ((cacheDirParam != null) && (!cacheDirParam.trim().equals(""))) {
  -            this.cacheDir = IOUtils.createFile( new File(this.servletContext.getRealPath("/"))
, cacheDirParam);
  +            if (this.servletContextPath == null) {
  +                this.cacheDir = new File(uploadDirParam);
  +            } else {
  +                this.cacheDir = IOUtils.createFile( new File(servletContextPath) , cacheDirParam);
  +            }
  +            this.cacheDir.mkdirs();
  +            log.debug("using cache-directory " + this.cacheDir);
           } else        {
               this.cacheDir = IOUtils.createFile(workDir, "cache-dir" + File.separator);
  +            log.debug("cache-directory was not set - defaulting to " + this.cacheDir);
           }
   
           this.appContext.put(Constants.CONTEXT_CACHE_DIR, this.cacheDir);
  @@ -222,7 +285,7 @@
       /**
        * This builds the important ClassPath used by this Servlet.  It
        * does so in a Servlet Engine neutral way.  It uses the
  -     * <code>ServletContext</code>'s <code>getRealPath</code> method
  +     * <code>ServletContext</code>'s <code>getResource</code> method
        * to get the Servlet 2.2 identified classes and lib directories.
        * It iterates in alphabetical order through every file in the
        * lib directory and adds it to the classpath.
  @@ -239,29 +302,37 @@
        throws ServletException {
           StringBuffer buildClassPath = new StringBuffer();
   
  -        String classDir = this.servletContext.getRealPath("/WEB-INF/classes");
  -        String libDir = this.servletContext.getRealPath("/WEB-INF/lib");
  +        URL classDirURL = null;
  +        URL libDirURL = null;
  +        try {
  +            classDirURL = this.servletContext.getResource("/WEB-INF/classes");
  +        } catch (java.net.MalformedURLException me) {
  +            this.log.warn("Unable to add WEB-INF/classes to the classpath", me);
  +        }
  +        try {
  +            libDirURL = this.servletContext.getResource("/WEB-INF/lib");
  +        } catch (java.net.MalformedURLException me) {
  +            this.log.warn("Unable to add WEB-INF/lib to the classpath", me);
  +        }
   
           File root = null;
  -        if(libDir != null)
  -            root = new File(libDir);
  -        else
  -            root = workDir;
  -
  +        if (libDirURL != null && libDirURL.toExternalForm().startsWith("file:"))
{
  +            root = new File(libDirURL.toExternalForm().substring(5));
  +        }
   
  -        if(classDir != null) {
  -            buildClassPath.append(classDir);
  +        if(classDirURL != null) {
  +            buildClassPath.append(classDirURL.toExternalForm());
   
               if (this.addClassDirs) {
                   try {
  -                    classLoader.addDirectory(new File(classDir));
  +                    classLoader.addURL(classDirURL);
                   } catch (Exception e) {
  -                    log.debug("Could not add directory" + classDir, e);
  +                    log.debug("Could not add directory " + classDirURL, e);
                   }
               }
           }
   
  -        if (root.isDirectory()) {
  +        if (root != null && root.isDirectory()) {
               File[] libraries = root.listFiles();
               Arrays.sort(libraries);
               for (int i = 0; i < libraries.length; i++) {
  @@ -331,13 +402,20 @@
                               }
                            }
                        } else {
  -                         log.debug ("extraClassPath is not absolute pre-pending context
path: " + this.servletContext.getRealPath("/") + s);
  -                         sb.append(this.servletContext.getRealPath("/") + s);
  +                         String path = null;
  +                         if (this.servletContextPath != null) {
  +                             path = this.servletContextPath + s;
  +                             log.debug ("extraClassPath is not absolute pre-pending context
path: " + path);
  +                         } else {
  +                             path = this.workDir.toString() + s;
  +                             log.debug ("extraClassPath is not absolute pre-pending work-directory:
" + path);
  +                         }
  +                         sb.append(path);
                            if (this.addClassDirs) {
                               try {
  -                                classLoader.addDirectory(this.servletContext.getRealPath("/")
+ s);
  +                                classLoader.addDirectory(path);
                               } catch (Exception e) {
  -                                log.debug("Could not add " + this.servletContext.getRealPath("/")
+ s);
  +                                log.debug("Could not add " + path);
                               }
                            }
                        }
  @@ -385,7 +463,14 @@
           logKitManager.setLogger(logger);
           final DefaultContext subcontext = new DefaultContext(this.appContext);
           subcontext.put("servlet-context", this.servletContext);
  -        subcontext.put("context-root", this.servletContext.getRealPath("/"));
  +        if (this.servletContextPath == null) {
  +            File logSCDir = new File(this.workDir, "log");
  +            logSCDir.mkdirs();
  +            this.log.warn("Setting servlet-context for LogKit to " + logSCDir);
  +            subcontext.put("context-root", logSCDir.toString());
  +        } else {
  +            subcontext.put("context-root", this.servletContextPath);
  +        }
   
           try {
               logKitManager.contextualize(subcontext);
  @@ -394,9 +479,9 @@
               //Configure the logkit management
               final String logkitConfig = getInitParameter("logkit-config");
               if (logkitConfig != null) {
  -                final FileInputStream fis = new FileInputStream(this.servletContext.getRealPath("/")
+ logkitConfig);
  +                final InputStream is = this.servletContext.getResourceAsStream(logkitConfig);
                   final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
  -                final Configuration conf = builder.build(fis);
  +                final Configuration conf = builder.build(is);
                   logKitManager.configure(conf);
               }
           } catch (Exception e) {
  @@ -685,26 +770,9 @@
                                          HttpServletResponse res)
       throws Exception {
           HttpEnvironment env;
  -        URL url = null;
  -
  -        String path = this.servletContext.getRealPath("/");
  -        log.debug("getRealPath for /: " + path);
  -        if(path == null) {
  -            // Try to figure out the path of the root from that of WEB-INF
  -            path = this.servletContext.getResource("/WEB-INF").toString();
  -            log.debug("getResource for /WEB-INF: " + path);
  -            path = path.substring(0,path.length()-"WEB-INF".length());
  -            log.debug("Path for Root: " + path);
  -        }
  -
  -        if(path.indexOf(':')>1)
  -            url = new URL(path);
  -        else
  -            url = (new File(path)).toURL();
  -        log.debug("URL for Root: " + url);
   
           env = new HttpEnvironment(uri,
  -                                  url,
  +                                  this.servletContextURL,
                                     req,
                                     res,
                                     this.servletContext,
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org


Mime
View raw message