tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject cvs commit: jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/util SystemLogHandler.java
Date Thu, 23 May 2002 06:53:23 GMT
remm        02/05/22 23:53:23

  Modified:    jasper2/src/share/org/apache/jasper/compiler Compiler.java
  Added:       jasper2/src/share/org/apache/jasper/util
                        SystemLogHandler.java
  Log:
  - Add the System.err capture class, as well as the new Ant based compiler.
  - Appears to be working, including compilation error reports, and fixes
    problems with JSTL (at least on JDK 1.3 and 1.4; on JDK 1.2, some compiler
    other than javac will have to be used).
  - Using something other than the Ant default Java compiler is not
    implemented yet.
  - Known issue: will refuse to compile JSPs (even valid ones) if one JSP failed
    to compile before, until it is corrected.
    To fix this, the generated Java file should be moved to a separate
    directory before compiling, as Jasper is unable to compile
    individual files.
  
  Revision  Changes    Path
  1.5       +148 -61   jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java
  
  Index: Compiler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Compiler.java	6 May 2002 04:33:15 -0000	1.4
  +++ Compiler.java	23 May 2002 06:53:23 -0000	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java,v
1.4 2002/05/06 04:33:15 glenn Exp $
  - * $Revision: 1.4 $
  - * $Date: 2002/05/06 04:33:15 $
  + * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java,v
1.5 2002/05/23 06:53:23 remm Exp $
  + * $Revision: 1.5 $
  + * $Date: 2002/05/23 06:53:23 $
    *
    * ====================================================================
    * 
  @@ -63,44 +63,93 @@
   import java.util.*;
   import java.io.*;
   import javax.servlet.jsp.tagext.TagInfo;
  +
   import org.xml.sax.Attributes;
  +
  +import org.apache.tools.ant.BuildEvent;
  +import org.apache.tools.ant.BuildException;
  +import org.apache.tools.ant.BuildListener;
  +import org.apache.tools.ant.Project;
  +import org.apache.tools.ant.taskdefs.Javac;
  +import org.apache.tools.ant.types.Path;
  +
   import org.apache.jasper.JspCompilationContext;
   import org.apache.jasper.Constants;
   import org.apache.jasper.JasperException;
   import org.apache.jasper.logging.Logger;
  +import org.apache.jasper.util.SystemLogHandler;
   
   /**
  - * If you want to customize JSP compilation aspects, this class is
  - * something you should take a look at. 
  - * 
  - * Hope is that people can just extend Compiler and override things
  - * like isOutDated() but inherit things like compile(). This might
  - * change. 
  + * Main JSP compiler class. This class uses Ant for compiling.
    *
    * @author Anil K. Vijendran
    * @author Mandar Raje
    * @author Pierre Delisle
    * @author Kin-man Chung
  + * @author Remy Maucherat
    */
   public class Compiler {
   
  -    protected JavaCompiler javac;
  +
  +    // ----------------------------------------------------------------- Static
  +
  +
  +    protected static Project project;
  +    protected static Javac javac;
  +    protected static Path path;
  +    protected static Path srcPath;
  +
  +    protected static CompilerBuildListener listener;
  +
  +    static {
  +
  +        System.setErr(new SystemLogHandler(System.err));
  +
  +        // Initializing project
  +        project = new Project();
  +        project.init();
  +
  +        // Initializing javac task
  +        javac = (Javac) project.createTask("javac");
  +
  +        // Initializing paths
  +        path = new Path(project);
  +        srcPath = new Path(project);
  +
  +        // Initializing listener
  +        listener = new CompilerBuildListener();
  +        project.addBuildListener(listener);
  +
  +    }
  +
  +
  +    // ----------------------------------------------------- Instance Variables
  +
  +
       protected Mangler mangler;
       protected JspCompilationContext ctxt;
   
       private ErrorDispatcher errDispatcher;
       private PageInfo pageInfo;
   
  +
  +    // ------------------------------------------------------------ Constructor
  +
  +
       public Compiler(JspCompilationContext ctxt) {
           this.ctxt = ctxt;
   	this.errDispatcher = new ErrorDispatcher();
       }
  -    
  +
  +
  +    // --------------------------------------------------------- Public Methods
  +
  +
       /** 
        * Compile the jsp file from the current engine context
        */
       public void compile()
  -	    throws FileNotFoundException, JasperException, Exception {
  +        throws FileNotFoundException, JasperException, Exception {
   
   	// Setup page info area
   	pageInfo = new PageInfo(new BeanRepository(ctxt.getClassLoader()));
  @@ -158,57 +207,42 @@
   
           String classpath = ctxt.getClassPath(); 
   
  -        // I'm nuking
  -        //          System.getProperty("jsp.class.path", ".") 
  -        // business. If anyone badly needs this we can talk. -akv
  -
           String sep = System.getProperty("path.separator");
  -        String[] argv = new String[] {
  -            "-encoding",
  -            javaEncoding,
  -            "-classpath",
  -            System.getProperty("java.class.path") + sep + classpath,
  -            "-d",
  -            ctxt.getOutputDir(),
  -            javaFileName
  -        };
  -
  -        StringBuffer b = new StringBuffer();
  -        for(int i = 0; i < argv.length; i++) {
  -            b.append(argv[i]);
  -            b.append(" ");
  -        }
   
  +        String errorReport = null;
  +        boolean success = true;
   
  -        Constants.message("jsp.message.compiling_with",
  -                          new Object[] { b.toString() },
  -                          Logger.DEBUG);
  +        // Call the actual Java compiler
  +        synchronized (project) {
  +
  +            path.setPath(System.getProperty("java.class.path") + sep
  +                         + classpath);
  +            srcPath.setPath(ctxt.getOutputDir());
  +
  +            /*
  +             * Configure the compiler object
  +             */
  +            javac.setEncoding(javaEncoding);
  +            javac.setClasspath(path);
  +            if (ctxt.getJavacOutputDir() != null) {
  +                javac.setDestdir(new File(ctxt.getJavacOutputDir()));
  +            }
  +            javac.setDebug(ctxt.getOptions().getClassDebugInfo());
  +            javac.setSrcdir(srcPath);
  +
  +            listener.clear();
   
  -        /*
  -         * 256 chosen randomly. The default is 32 if you don't pass
  -         * anything to the constructor which will be less. 
  -         */
  -        ByteArrayOutputStream out = new ByteArrayOutputStream (256);
  -
  -        // if no compiler was set we can kick out now
  -        if (javac == null) {
  -            return;
  -        }
  -
  -        /*
  -         * Configure the compiler object
  -         */
  -        javac.setEncoding(javaEncoding);
  -        javac.setClasspath(System.getProperty("java.class.path") + sep
  -			   + classpath);
  -        javac.setOutputDir(ctxt.getJavacOutputDir());
  -        javac.setMsgOutput(out);
  -        javac.setClassDebugInfo(ctxt.getOptions().getClassDebugInfo());
  -
  -        /*
  -         * Execute the compiler
  -         */
  -        boolean success = javac.compile(javaFileName);
  +            SystemLogHandler.setThread();
  +
  +            try {
  +                javac.execute();
  +            } catch (BuildException e) {
  +                success = false;
  +            }
  +
  +            errorReport = SystemLogHandler.unsetThread();
  +
  +        }
   
           if (!ctxt.keepGenerated()) {
               File javaFile = new File(javaFileName);
  @@ -216,10 +250,12 @@
           }
       
           if (!success) {
  -            errDispatcher.javacError(out.toString(), javaFileName, pageNodes);
  +            errDispatcher.javacError(errorReport, javaFileName, pageNodes);
           }
  +
       }
   
  +
       /**
        * This is a protected method intended to be overridden by 
        * subclasses of Compiler. This is used by the compile method
  @@ -228,14 +264,15 @@
       public boolean isOutDated() {
   	return true;
       }
  +
       
       /**
        * Set java compiler info
        */
       public void setJavaCompiler(JavaCompiler javac) {
  -        this.javac = javac;
       }
   
  +
       /**
        * Set Mangler which will be used as part of compile().
        */
  @@ -244,6 +281,7 @@
           ctxt.setServletJavaFileName(mangler.getJavaFileName());
       }
   
  +
       /**
        * Gets the error dispatcher.
        */
  @@ -251,6 +289,7 @@
   	return errDispatcher;
       }
   
  +
       /**
        * Gets the info about the page under compilation
        */
  @@ -258,10 +297,12 @@
   	return pageInfo;
       }
   
  +
       public JspCompilationContext getCompilationContext() {
   	return ctxt;
       }
   
  +
       /**
        * Change the encoding for the reader if specified.
        */
  @@ -299,6 +340,7 @@
   	return null;
       }
   
  +
       /**
        * Remove generated files
        */
  @@ -322,4 +364,49 @@
               //Remove as much as possible, ignore possible exceptions
           }
       }
  +
  +
  +    // -------------------------------------- CompilerBuildListener Inner Class
  +
  +
  +    protected static class CompilerBuildListener
  +        implements BuildListener {
  +
  +        protected StringBuffer report = null;
  +
  +        public String getReport() {
  +            return report.toString();
  +        }
  +
  +        public void clear() {
  +            report = new StringBuffer();
  +        }
  +
  +        public void buildStarted(BuildEvent event) {
  +        }
  +
  +        public void buildFinished(BuildEvent event) {
  +        }
  +
  +        public void targetStarted(BuildEvent event) {
  +        }
  +
  +        public void targetFinished(BuildEvent event) {
  +        }
  +
  +        public void taskStarted(BuildEvent event) {
  +        }
  +
  +        public void taskFinished(BuildEvent event) {
  +        }
  +
  +        public void messageLogged(BuildEvent event) {
  +            String line = event.getMessage();
  +            report.append(line);
  +            report.append("\n");
  +        }
  +
  +    }
  +
  +
   }
  
  
  
  1.1                  jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/util/SystemLogHandler.java
  
  Index: SystemLogHandler.java
  ===================================================================
  /*
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */ 
  package org.apache.jasper.util;
  
  import java.io.ByteArrayOutputStream;
  import java.io.Writer;
  import java.io.PrintStream;
  import java.io.PrintWriter;
  import java.io.FileWriter;
  import java.io.File;
  import java.io.OutputStreamWriter;
  import java.io.IOException;
  import java.io.StringWriter;
  
  import java.util.Hashtable;
  
  
  /**
   * This helper class may be used to do sophisticated redirection of 
   * System.out and System.err.
   * 
   * @author Remy Maucherat
   */
  public class SystemLogHandler extends PrintStream {
  
  
      // ----------------------------------------------------------- Constructors
  
  
      /**
       * Construct the handler to capture the output of the given steam.
       */
      public SystemLogHandler(PrintStream wrapped) {
          super(wrapped);
          out = wrapped;
      }
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * Wrapped PrintStream.
       */
      protected PrintStream out = null;
  
  
      /**
       * Thread <-> PrintStream associations.
       */
      protected static Hashtable streams = new Hashtable();
  
  
      /**
       * Thread <-> ByteArrayOutputStream associations.
       */
      protected static Hashtable data = new Hashtable();
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Start capturing thread's output.
       */
      public static void setThread() {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          PrintStream ps = new PrintStream(baos);
          data.put(Thread.currentThread(), baos);
          streams.put(Thread.currentThread(), ps);
      }
  
  
      /**
       * Stop capturing thread's output and return captured data as a String.
       */
      public static String unsetThread() {
          ByteArrayOutputStream baos = 
              (ByteArrayOutputStream) data.get(Thread.currentThread());
          if (baos == null) {
              return null;
          }
          streams.remove(Thread.currentThread());
          data.remove(Thread.currentThread());
          return baos.toString();
      }
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * Find PrintStream to which the output must be written to.
       */
      protected PrintStream findStream() {
          PrintStream ps = (PrintStream) streams.get(Thread.currentThread());
          if (ps == null) {
              ps = out;
          }
          return ps;
      }
  
  
      // ---------------------------------------------------- PrintStream Methods
  
  
      public void flush() {
          findStream().flush();
      }
  
      public void close() {
          findStream().close();
      }
  
      public boolean checkError() {
          return findStream().checkError();
      }
  
      protected void setError() {
          //findStream().setError();
      }
  
      public void write(int b) {
          findStream().write(b);
      }
  
      public void write(byte[] b)
          throws IOException {
          findStream().write(b);
      }
  
      public void write(byte[] buf, int off, int len) {
          findStream().write(buf, off, len);
      }
  
      public void print(boolean b) {
          findStream().print(b);
      }
  
      public void print(char c) {
          findStream().print(c);
      }
  
      public void print(int i) {
          findStream().print(i);
      }
  
      public void print(long l) {
          findStream().print(l);
      }
  
      public void print(float f) {
          findStream().print(f);
      }
  
      public void print(double d) {
          findStream().print(d);
      }
  
      public void print(char[] s) {
          findStream().print(s);
      }
  
      public void print(String s) {
          findStream().print(s);
      }
  
      public void print(Object obj) {
          findStream().print(obj);
      }
  
      public void println() {
          findStream().println();
      }
  
      public void println(boolean x) {
          findStream().println(x);
      }
  
      public void println(char x) {
          findStream().println(x);
      }
  
      public void println(int x) {
          findStream().println(x);
      }
  
      public void println(long x) {
          findStream().println(x);
      }
  
      public void println(float x) {
          findStream().println(x);
      }
  
      public void println(double x) {
          findStream().println(x);
      }
  
      public void println(char[] x) {
          findStream().println(x);
      }
  
      public void println(String x) {
          findStream().println(x);
      }
  
      public void println(Object x) {
          findStream().println(x);
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:tomcat-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-dev-help@jakarta.apache.org>


Mime
View raw message