FileDocCategorySizeDatePackage
AntJavaCompiler.javaAPI DocGlassfish v2 API12632Tue May 15 17:49:40 BST 2007org.apache.jasper.compiler

AntJavaCompiler

public class AntJavaCompiler extends Object implements JavaCompiler
Java compiler through ant
author
Kin-man Chung

Fields Summary
private JasperAntLogger
logger
private org.apache.tools.ant.taskdefs.Javac
javac
private org.apache.tools.ant.Project
project
private org.apache.jasper.JspCompilationContext
ctxt
private org.apache.jasper.Options
options
private ErrorDispatcher
errDispatcher
private String
javaFileName
private String
javaEncoding
private boolean
nolog
private StringBuffer
info
private com.sun.org.apache.commons.logging.Log
log
private static ExecutorService
threadPool
private static ThreadFactory
threadFactory
private static final String
JAVAC_THREAD_PREFIX
private static String
lineSeparator
Constructors Summary
Methods Summary
public JavacErrorDetail[]compile(java.lang.String className, Node.Nodes pageNodes)


        // Start capturing the System.err output for this thread
        SystemLogHandler.setThread();

        javac.setEncoding(javaEncoding);
        // Initialize sourcepath
        Path srcPath = new Path(project);
        srcPath.setLocation(options.getScratchDir());
        javac.setSrcdir(srcPath);
        info.append("    srcDir=" + srcPath + "\n" );
        info.append("    work dir=" + options.getScratchDir() + "\n");

        // Build includes path
        PatternSet.NameEntry includes = javac.createInclude();
        includes.setName(ctxt.getJavaPath());
        info.append("    include="+ ctxt.getJavaPath() + "\n" );

        BuildException be = null;
        StringBuffer errorReport = new StringBuffer();
        String errorCapture = null;
        if (ctxt.getOptions().getFork()) {
            try {
                javac.execute();
            } catch (BuildException e) {
                be = e;
                log.error( "Javac exception ", e);
                log.error( "Env: " + info.toString());
            }
            errorReport.append(logger.getReport());
            // Stop capturing the System.err output for this thread
            errorCapture = SystemLogHandler.unsetThread();
        } else {
            errorReport.append(logger.getReport());
            errorCapture = SystemLogHandler.unsetThread();

            // Capture the current thread
            if (errorCapture != null) {
                errorReport.append(lineSeparator);
                errorReport.append(errorCapture);
            }

            JavacObj javacObj = new JavacObj(javac);
            synchronized(javacObj) {
                threadPool.execute(javacObj);
                // Wait for the thread to complete
                try {
                    javacObj.wait();
                } catch (InterruptedException e) {
                    ;
                }
            }
            be = javacObj.getException();
            if (be != null) {
                log.error( "Javac exception ", be);
                log.error( "Env: " + info.toString());
            }
            errorReport.append(logger.getReport());
            errorCapture = javacObj.getErrorCapture();
        }

        if (errorCapture != null) {
            errorReport.append(lineSeparator);
            errorReport.append(errorCapture);
        }

        JavacErrorDetail[] javacErrors = null;
        if (be != null) {
            try {
                String errorReportString = errorReport.toString();
                javacErrors = ErrorDispatcher.parseJavacMessage(
                        pageNodes, errorReportString, javaFileName);
            } catch (IOException ex) {
                throw new JasperException(ex);
            }
        }
        return javacErrors;
    
public voiddoJavaFile(boolean keep)

        if (!keep) {
            File javaFile = new File(javaFileName);
            javaFile.delete();
        }
    
public longgetClassLastModified()

        File classFile = new File(ctxt.getClassFileName());
        return classFile.lastModified();
    
public java.io.WritergetJavaWriter(java.lang.String javaFileName, java.lang.String javaEncoding)


        this.javaFileName = javaFileName;
        info.append("Compile: javaFileName=" + javaFileName + "\n" );

        this.javaEncoding = javaEncoding;
    
        Writer writer = null;
        try {
            writer = new OutputStreamWriter(
                        new FileOutputStream(javaFileName), javaEncoding);
        } catch (UnsupportedEncodingException ex) {
            errDispatcher.jspError("jsp.error.needAlternateJavaEncoding",
                                   javaEncoding);
        } catch (IOException ex) {
            errDispatcher.jspError("jsp.error.unableToCreateOutputWriter",
                                   javaFileName, ex);
        }
        return writer;
    
private org.apache.tools.ant.ProjectgetProject()



       

        if( project!=null ) return project;

        // Initializing project
        project = new Project();
        logger = new JasperAntLogger();
        logger.setOutputPrintStream(System.out);
        logger.setErrorPrintStream(System.err);
        logger.setMessageOutputLevel(Project.MSG_INFO);
        project.addBuildListener( logger);
        if (System.getProperty("catalina.home") != null) {
            project.setBasedir( System.getProperty("catalina.home"));
        }

        if( options.getCompiler() != null ) {
            if( log.isDebugEnabled() )
                log.debug("Compiler " + options.getCompiler() );
            project.setProperty("build.compiler", options.getCompiler() );
        }
        project.init();
        return project;
    
public voidinit(org.apache.jasper.JspCompilationContext ctxt, ErrorDispatcher errDispatcher, boolean suppressLogging)


        this.ctxt = ctxt;
        this.errDispatcher = errDispatcher;
        options = ctxt.getOptions();
        log = suppressLogging?
            new com.sun.org.apache.commons.logging.impl.NoOpLog():
            com.sun.org.apache.commons.logging.LogFactory.getLog(
                AntJavaCompiler.class);
        getProject();
        javac = (Javac) project.createTask("javac");
        javac.setFork(options.getFork());
        // Set the Java compiler to use
        if (options.getCompiler() != null) {
            javac.setCompiler(options.getCompiler());
        }
        startThreadPool();
    
public voidsaveClassFile(java.lang.String className, java.lang.String classFileName)

        // class files are alwyas saved.
    
public voidsetClassPath(java.util.List cpath)

        Path path = new Path(project);
        for (File file: cpath) {
            path.setLocation(file);
            info.append("    cp=" + file + "\n");
        }
        javac.setClasspath(path);
    
public voidsetDebug(boolean debug)

        javac.setDebug(debug);
        javac.setOptimize(!debug);
    
public voidsetExtdirs(java.lang.String exts)

        Path extdirs = new Path(project);
        extdirs.setPath(exts);
        javac.setExtdirs(extdirs);
        info.append("    extdirs=" + exts+ "\n");
    
public voidsetSourceVM(java.lang.String sourceVM)

        javac.setSource(sourceVM);
        info.append("   compilerSourceVM=" + sourceVM + "\n");
    
public voidsetTargetVM(java.lang.String targetVM)

        javac.setTarget(targetVM);
        info.append("   compilerTargetVM=" + targetVM + "\n");

    
public static voidshutdownThreadPool()

        if (threadPool != null) {
            threadPool.shutdown();
        }
    
public static voidstartThreadPool()

        if (threadPool == null) {
            threadPool = Executors.newCachedThreadPool(threadFactory);
        }