001    package railo.runtime.compiler;
002    
003    import java.io.ByteArrayInputStream;
004    import java.io.IOException;
005    import java.io.InputStream;
006    
007    import railo.commons.io.IOUtil;
008    import railo.commons.io.res.Resource;
009    import railo.runtime.PageSource;
010    import railo.runtime.config.ConfigImpl;
011    import railo.runtime.exp.TemplateException;
012    import railo.transformer.bytecode.BytecodeException;
013    import railo.transformer.bytecode.Page;
014    import railo.transformer.cfml.tag.CFMLTransformer;
015    import railo.transformer.library.function.FunctionLib;
016    import railo.transformer.library.tag.TagLib;
017    import railo.transformer.util.AlreadyClassException;
018    
019    
020    
021    /**
022     * CFML Compiler compiles CFML source templates
023     */
024    public final class CFMLCompilerImpl implements CFMLCompiler {
025            
026            private CFMLTransformer cfmlTransformer;
027            
028            
029            /**
030             * Constructor of the compiler
031             * @param config
032             */
033            public CFMLCompilerImpl() {
034                    cfmlTransformer=new CFMLTransformer();
035            }
036            
037            /**
038             * @see railo.runtime.compiler.CFMLCompiler#compile(railo.runtime.config.ConfigImpl, railo.runtime.PageSource, railo.transformer.library.tag.TagLib[], railo.transformer.library.function.FunctionLib[], railo.commons.io.res.Resource, java.lang.String)
039             */
040            public byte[] compile(ConfigImpl config,PageSource source, TagLib[] tld, FunctionLib[] fld, 
041            Resource classRootDir, String className) throws TemplateException, IOException {
042                    //synchronized(source){
043                            //print.out("src:"+source.getDisplayPath());
044                    //print.dumpStack();
045                            Resource classFile=classRootDir.getRealResource(className+".class");
046                            Resource classFileDirectory=classFile.getParentResource();
047                    byte[] barr = null;
048                            Page page = null;
049                            
050                            if(!classFileDirectory.exists()) classFileDirectory.mkdirs(); 
051                            
052                    try {
053                            page = cfmlTransformer.transform(config,source,tld,fld);
054                            barr = page.execute(classFile);
055                                    IOUtil.copy(new ByteArrayInputStream(barr), classFile,true);
056                            return barr;
057                            } 
058                    catch (AlreadyClassException ace) {
059                            InputStream is=null;
060                            try{
061                                    barr=IOUtil.toBytes(is=ace.getInputStream());
062                                    barr=Page.setSourceLastModified(barr,source.getPhyscalFile().lastModified());
063                                    IOUtil.copy(new ByteArrayInputStream(barr), classFile,true);
064                            }
065                            finally {
066                                    IOUtil.closeEL(is);
067                            }
068                            return barr;
069                    }
070                    catch (BytecodeException bce) {
071                            bce.addContext(source, bce.getLineAsInt(), bce.getLineAsInt(),null);
072                            throw bce;
073                            //throw new TemplateException(source,e.getLine(),e.getColumn(),e.getMessage());
074                            }
075                    /*finally {
076                            
077                    }*/
078                    //}
079            }
080    
081        /* *
082         * @return Returns the cfmlTransformer.
083         * /
084        public CFMLTransformer getCfmlTransformer() {
085            return cfmlTransformer;
086        }*/
087    }