costin      01/06/12 08:00:28

  Modified:    jasper34/generator/org/apache/jasper34/core Compiler.java
  Log:
  Changed Compiler so people can use it direclty, without extending it.
  
  Split the compile() method in 2 - jsp2java and javac, to allow finer control.
  
  The compiler is just doing the compilation - jsp to java, java to class
  ( later jsp to class ). Deciding to recompile ( isOutDated), etc is part
  of the liaison's job.
  
  ( note some other changes - use ContainerLiaison for container callbacks,
  Constants no longer have callbacks but only constants )
  
  Also added some common code that used to be duplicated in each liaison
  ( jspservlet, jspc, jspinterceptor ).
  
  Revision  Changes    Path
  1.4       +146 -153  
jakarta-tomcat-jasper/jasper34/generator/org/apache/jasper34/core/Compiler.java
  
  Index: Compiler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper34/generator/org/apache/jasper34/core/Compiler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Compiler.java     2001/06/07 07:02:25     1.3
  +++ Compiler.java     2001/06/12 15:00:26     1.4
  @@ -69,6 +69,7 @@
   import org.apache.jasper34.generator.*;
   import org.apache.jasper34.runtime.JasperException;
   import org.apache.jasper34.javacompiler.*;
  +import org.apache.jasper34.jsptree.*;
   
   import org.apache.tomcat.util.log.*;
   
  @@ -83,68 +84,38 @@
    * 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. 
  - *
    * @author Anil K. Vijendran
    * @author Mandar Raje
    */
   public class Compiler {
  -    protected JavaCompiler javac;
  -    protected Mangler mangler;
  -    protected JspCompilationContext ctxt;
  -    protected ContainerLiaison liaison;
  -
  -    public Compiler(ContainerLiaison liaison, JspCompilationContext ctxt) {
  -        this.ctxt = ctxt;
  -     this.liaison=liaison;
  -    }
  -    public Compiler(JspCompilationContext ctxt) {
  -        this.ctxt = ctxt;
  -     this.liaison=null;
  -    }
  +    protected ContainerLiaison containerL;
       
  +    /**
  +     */
  +    public Compiler(ContainerLiaison liaison) {
  +     this.containerL=liaison;
  +    }
  +
  +    // -------------------- Conversion methods --------------------
  +
       /** 
        * Compile the jsp file from the current engine context
        *
        * @return true if the class file was outdated the jsp file
        *         was recomp iled. 
        */
  -    public boolean compile()
  +    public boolean jsp2java(JspPageInfo pageInfo)
           throws FileNotFoundException, JasperException, Exception 
       {
  -        String pkgName = mangler.getPackageName();
  -        String classFileName = mangler.getClassFileName();
  +     // assert pageInfo has valid mangler, jspFile
   
  -        ctxt.setServletPackageName(pkgName);
  -        Constants.message("jsp.message.package_name_is",
  -                          new Object[] { (pkgName==null)?
  -                                          "[default package]":pkgName },
  -                          Log.DEBUG);
  -        Constants.message("jsp.message.class_file_name_is",
  -                          new Object[] { classFileName },
  -                          Log.DEBUG);
  +        String javaFileName = pageInfo.getMangler().getJavaFileName();
  +        //pageInfo.setServletJavaFileName(javaFileName);
   
  -     if (!isOutDated())
  -            return false;
  -
  -     // Hack to avoid readign the class file every time -
  -     // getClassName() is an _expensive_ operation, and it's needed only
  -     // if isOutDated() return true. 
  -        String javaFileName = mangler.getJavaFileName();
  -        ctxt.setServletJavaFileName(javaFileName);
  -
  -        Constants.message("jsp.message.java_file_name_is",
  +        containerL.message("jsp.message.java_file_name_is",
                             new Object[] { javaFileName },
                             Log.DEBUG);
   
  -     String className = mangler.getClassName();
  -        ctxt.setServletClassName(className);
  -        Constants.message("jsp.message.class_name_is",
  -                          new Object[] { className },
  -                          Log.DEBUG);
  -
           
           
           // Need the encoding specified in the JSP 'page' directive for
  @@ -166,19 +137,14 @@
        // taken out once we have a more efficient method.
   
        if (true) {
  -         JspReader tmpReader = JspReader.createJspReader(
  -                                                         ctxt.getJspFile(),
  -                                                         ctxt,
  -                                                         jspEncoding);
  +         JspReader tmpReader = JspReader.
  +             createJspReader(pageInfo.getJspFile(), containerL,jspEncoding);
            String newEncode = changeEncodingIfNecessary(tmpReader);
            if (newEncode != null) jspEncoding = newEncode;
        }
   
  -        JspReader reader = JspReader.createJspReader(
  -            ctxt.getJspFile(),
  -            ctxt,
  -            jspEncoding
  -        );
  +        JspReader reader = JspReader.
  +         createJspReader(pageInfo.getJspFile(),containerL,jspEncoding);
   
        OutputStreamWriter osw; 
        try {
  @@ -187,7 +153,7 @@
        } catch (java.io.UnsupportedEncodingException ex) {
            // Try to get the java encoding from the "javaEncoding"
            // init parameter for JspServlet.
  -         javaEncoding = ctxt.getOptions().getJavaEncoding();
  +         javaEncoding = containerL.getOptions().getJavaEncoding();
            if (javaEncoding != null) {
                try {
                    osw = new OutputStreamWriter(
  @@ -195,7 +161,7 @@
                } catch (java.io.UnsupportedEncodingException ex2) {
                    // no luck :-(
                    throw new JasperException(
  -                     Constants.getString("jsp.error.invalid.javaEncoding",
  +                     containerL.getString("jsp.error.invalid.javaEncoding",
                                            new Object[] { 
                                                "UTF8", 
                                                javaEncoding,
  @@ -203,97 +169,125 @@
                }
            } else {
                throw new JasperException(
  -                 Constants.getString("jsp.error.needAlternateJavaEncoding",
  +                 containerL.getString("jsp.error.needAlternateJavaEncoding",
                                        new Object[] { "UTF8" }));              
            }
        }
  +
  +     pageInfo.setJavaEncoding( javaEncoding );
  +     
        ServletWriter writer = new ServletWriter(new PrintWriter(osw));
   
  -        ctxt.setReader(reader);
  -        ctxt.setWriter(writer);
  +     //        ctxt.setReader(reader);
  +     //        ctxt.setWriter(writer);
   
  -        ParseEventListener listener = new JspParseEventListener(ctxt);
  +        ParseEventListener listener =
  +         new JspParseEventListener(containerL,
  +                                   reader,
  +                                   writer,
  +                                   pageInfo);
           
  -        Parser p = new Parser(reader, listener);
  +        Parser p = new Parser(containerL,reader, listener);
           listener.beginPageProcessing();
           p.parse();
           listener.endPageProcessing();
           writer.close();
  -
  -        String classpath = ctxt.getClassPath(); 
  +     
  +     return true;
  +    }
   
  -        // I'm nuking
  -        //          System.getProperty("jsp.class.path", ".") 
  -        // business. If anyone badly needs this we can talk. -akv
  -
  -        // I'm adding tc_path_add because it solves a real problem
  -        // and nobody has yet to come up with a better alternative.
  -        // Note: this is in two places.  Search for tc_path_add below.
  -        // If you have one, please let me know.  -Sam Ruby
  +    
  +    /** 
  +     * Compile the jsp file from the current engine context
  +     *
  +     * @return true if the class file was outdated the jsp file
  +     *         was recomp iled. 
  +     */
  +    public boolean compile(JspPageInfo pageInfo, JavaCompiler javac)
  +        throws FileNotFoundException, JasperException, Exception 
  +    {
  +     jsp2java( pageInfo );
  +     return javac( pageInfo, javac );
  +    }
   
  -        String sep = System.getProperty("path.separator");
  -        String[] argv = new String[] 
  -        {
  -            "-encoding",
  -            javaEncoding,
  -            "-classpath",
  -         System.getProperty("java.class.path")+ sep + classpath + sep +
  -                System.getProperty("tc_path_add") + sep + ctxt.getOutputDir(),
  -            "-d", ctxt.getOutputDir(),
  -            javaFileName
  -        };
  +    public void prepareCompiler( JavaCompiler javac, Options options,
  +                              JspPageInfo pageInfo)
  +    {
  +     String sep = System.getProperty("path.separator");
  +     String classpath = containerL.getClassPath(); 
  +        javac.setEncoding(pageInfo.getJavaEncoding());
  +        javac.setClasspath( System.getProperty("java.class.path")+ sep + 
  +                            System.getProperty("tc_path_add") + sep +
  +                            classpath + sep + containerL.getOutputDir());
  +        javac.setOutputDir(containerL.getOutputDir());
  +        javac.setClassDebugInfo(pageInfo.getOptions().getClassDebugInfo());
  +
  +     String compilerPath = options.getJspCompilerPath();
  +        if (compilerPath != null)
  +            javac.setCompilerPath(compilerPath);
   
  -        StringBuffer b = new StringBuffer();
  -        for(int i = 0; i < argv.length; i++) {
  -            b.append(argv[i]);
  -            b.append(" ");
  -        }
  +    }
  +    
  +    public boolean javac(JspPageInfo pageInfo, String javacName)
  +        throws FileNotFoundException, JasperException, Exception 
  +    {
  +     JavaCompiler javaC=null;
  +     try {
  +         Class jspCompilerPlugin=Class.forName(javacName);
  +         javaC=JavaCompiler.createJavaCompiler( containerL,
  +                                                jspCompilerPlugin );
  +     } catch( Exception ex ) {
  +         throw ex;
  +     }
  +     return javac( pageInfo, javaC );
  +    }
   
  -        Constants.message("jsp.message.compiling_with",
  -                          new Object[] { b.toString() },
  -                          Log.DEBUG);
   
  -        /**
  -         * 256 chosen randomly. The default is 32 if you don't pass
  -         * anything to the constructor which will be less. 
  -         */
  +    public boolean javac(JspPageInfo pageInfo, JavaCompiler javac)
  +        throws FileNotFoundException, JasperException, Exception 
  +    {
  +     prepareCompiler( javac, pageInfo.getOptions(), pageInfo);
           ByteArrayOutputStream out = new ByteArrayOutputStream (256);
  -
  -        // if no compiler was set we can kick out now
  -
  -        if (javac == null) {
  -            return true;
  -        }
   
  +     prepareCompiler( javac, pageInfo.getOptions(), pageInfo );
  +     javac.setMsgOutput(out);
           /**
  -         * Configure the compiler object
  -         * See comment above: re tc_path_add
  -         */
  -        javac.setEncoding(javaEncoding);
  -        javac.setClasspath( System.getProperty("java.class.path")+ sep + 
  -                            System.getProperty("tc_path_add") + sep +
  -                            classpath + sep + ctxt.getOutputDir());
  -        javac.setOutputDir(ctxt.getOutputDir());
  -        javac.setMsgOutput(out);
  -        javac.setClassDebugInfo(ctxt.getOptions().getClassDebugInfo());
  -
  -        /**
            * Execute the compiler
            */
  -        boolean status = javac.compile(javaFileName);
  +        boolean status = javac.compile(pageInfo.getMangler().getJavaFileName());
   
  -        if (!ctxt.keepGenerated()) {
  -            File javaFile = new File(javaFileName);
  +        if (!containerL.getOptions().getKeepGenerated()) {
  +            File javaFile = new File(pageInfo.getMangler().getJavaFileName());
               javaFile.delete();
           }
       
           if (status == false) {
               String msg = out.toString ();
  -            throw new 
JasperException(Constants.getString("jsp.error.unable.compile")
  +            throw new 
JasperException(containerL.getString("jsp.error.unable.compile")
                                         + msg);
           }
  +
  +        String classFile = containerL.getOutputDir() + File.separatorChar;
   
  -        String classFile = ctxt.getOutputDir() + File.separatorChar;
  +        String pkgName = pageInfo.getMangler().getPackageName();
  +        containerL.message("jsp.message.package_name_is",
  +                          new Object[] { (pkgName==null)?
  +                                          "[default package]":pkgName },
  +                        Log.DEBUG);
  +
  +     String className = pageInfo.getMangler().getClassName();
  +        //pageInfo.setServletClassName(className);
  +        containerL.message("jsp.message.class_name_is",
  +                          new Object[] { className },
  +                          Log.DEBUG);
  +
  +     String classFileName = pageInfo.getMangler().getClassFileName();
  +        containerL.message("jsp.message.class_file_name_is",
  +                          new Object[] { classFileName },
  +                          Log.DEBUG);
  +
  +
  +
           if (pkgName != null && !pkgName.equals(""))
               classFile = classFile + pkgName.replace('.', File.separatorChar) + 
                   File.separatorChar;
  @@ -305,7 +299,7 @@
               if (myClassFileObject.exists())
                   myClassFileObject.delete();
               if (classFileObject.renameTo(myClassFileObject) == false)
  -                throw new 
JasperException(Constants.getString("jsp.error.unable.rename",
  +                throw new 
JasperException(containerL.getString("jsp.error.unable.rename",
                                                                 new Object[] { 
                                                                     classFileObject, 
                                                                     myClassFileObject
  @@ -315,44 +309,12 @@
           return true;
       }
   
  -    public void computeServletClassName() {
  -     // Hack to avoid readign the class file every time -
  -     // getClassName() is an _expensive_ operation, and it's needed only
  -     // if isOutDated() return true. 
  -     String className = mangler.getClassName();
  -        ctxt.setServletClassName(className);
  -        Constants.message("jsp.message.class_name_is",
  -                          new Object[] { className },
  -                          Log.DEBUG);
  -    }
  -    
  -    /**
  -     * This is a protected method intended to be overridden by 
  -     * subclasses of Compiler. This is used by the compile method
  -     * to do all the compilation. 
  -     */
  -    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().
  -     */
  -    public void setMangler(Mangler mangler) {
  -        this.mangler = mangler;
  -    }
   
  +    // XXX move to parser
       /**
        * Change the encoding for the reader if specified.
        */
  -    public String changeEncodingIfNecessary(JspReader tmpReader)
  +    private String changeEncodingIfNecessary(JspReader tmpReader)
        throws ParseException
       {
   
  @@ -390,11 +352,11 @@
       /**
        * Remove generated files
        */
  -    public void removeGeneratedFiles()
  +    public void removeGeneratedFiles(JspPageInfo pageInfo )
       {
        try{
            // XXX Should we delete the generated .java file too?
  -         String classFileName = mangler.getClassFileName();
  +         String classFileName = pageInfo.getMangler().getClassFileName();
            if(classFileName != null){
                File classFile = new File(classFileName);
                classFile.delete();
  @@ -402,6 +364,37 @@
        }catch(Exception e){
        }
       }
  +
  +    // For debug mostly
  +    public String getJavacCommand(JspPageInfo pageInfo) {
  +     String classpath = containerL.getClassPath();
  +     
  +        String sep = System.getProperty("path.separator");
  +        String[] argv = new String[] 
  +        {
  +            "-encoding",
  +            pageInfo.getJavaEncoding(),
  +            "-classpath",
  +         System.getProperty("java.class.path")+ sep + classpath + sep +
  +         System.getProperty("tc_path_add") + sep +
  +         containerL.getOutputDir(),
  +            "-d", containerL.getOutputDir(),
  +            pageInfo.getServletJavaFileName()
  +        };
  +
  +        StringBuffer b = new StringBuffer();
  +        for(int i = 0; i < argv.length; i++) {
  +            b.append(argv[i]);
  +            b.append(" ");
  +        }
  +
  +        containerL.message("jsp.message.compiling_with",
  +                          new Object[] { b.toString() },
  +                          Log.DEBUG);
  +     return b.toString();
  +    }
  +
  +
   }
   
   
  
  
  

Reply via email to