Search in sources :

Example 1 with AlreadyClassException

use of lucee.transformer.util.AlreadyClassException in project Lucee by lucee.

the class CFMLCompilerImpl method readEncrypted.

private byte[] readEncrypted(AlreadyClassException ace) throws IOException {
    String str = System.getenv("PUBLIC_KEY");
    if (str == null)
        str = System.getProperty("PUBLIC_KEY");
    if (str == null)
        throw new RuntimeException("to decrypt encrypted bytecode, you need to set PUBLIC_KEY as system property or or enviroment variable");
    byte[] bytes = IOUtil.toBytes(ace.getInputStream(), true);
    try {
        PublicKey publicKey = RSA.toPublicKey(str);
        // first 2 bytes are just a mask to detect encrypted code, so we need to set offset 2
        bytes = RSA.decrypt(bytes, publicKey, 2);
    } catch (IOException ioe) {
        throw ioe;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return bytes;
}
Also used : PublicKey(java.security.PublicKey) IOException(java.io.IOException) TransformerException(lucee.transformer.TransformerException) AlreadyClassException(lucee.transformer.util.AlreadyClassException) IOException(java.io.IOException) TemplateException(lucee.runtime.exp.TemplateException)

Example 2 with AlreadyClassException

use of lucee.transformer.util.AlreadyClassException in project Lucee by lucee.

the class CFMLCompilerImpl method _compile.

/*private byte[] _compiless(ConfigImpl config,PageSource ps,SourceCode sc,String className, TagLib[] tld, FunctionLib[] fld, 
			Resource classRootDir,TransfomerSettings settings) throws TemplateException {
		Factory factory = BytecodeFactory.getInstance(config);
		
		Page page=null;
		
		TagLib[][] _tlibs=new TagLib[][]{null,new TagLib[0]};
		_tlibs[CFMLTransformer.TAG_LIB_GLOBAL]=tld;
		// reset page tlds
		if(_tlibs[CFMLTransformer.TAG_LIB_PAGE].length>0) {
			_tlibs[CFMLTransformer.TAG_LIB_PAGE]=new TagLib[0];
		}
		
		CFMLScriptTransformer scriptTransformer = new CFMLScriptTransformer();
		scriptTransformer.transform(
				BytecodeFactory.getInstance(config)
				, page
				, new EvaluatorPool()
				, _tlibs, fld
				, null
				, config.getCoreTagLib(ps.getDialect()).getScriptTags()
				, sc
				, settings);
		
		//CFMLExprTransformer extr=new CFMLExprTransformer();
		//extr.transform(factory, page, ep, tld, fld, scriptTags, cfml, settings)
		
		return null;
	}*/
private Result _compile(ConfigImpl config, PageSource ps, SourceCode sc, String className, TagLib[] tld, FunctionLib[] fld, Resource classRootDir, boolean returnValue, boolean ignoreScopes) throws TemplateException, IOException {
    if (className == null)
        className = ps.getClassName();
    Result result = null;
    // byte[] barr = null;
    Page page = null;
    Factory factory = BytecodeFactory.getInstance(config);
    try {
        page = sc == null ? cfmlTransformer.transform(factory, config, ps, tld, fld, returnValue, ignoreScopes) : cfmlTransformer.transform(factory, config, sc, tld, fld, System.currentTimeMillis(), sc.getDialect() == CFMLEngine.DIALECT_CFML && config.getDotNotationUpperCase(), returnValue, ignoreScopes);
        page.setSplitIfNecessary(false);
        try {
            result = new Result(page, page.execute(className));
        } catch (RuntimeException re) {
            String msg = StringUtil.emptyIfNull(re.getMessage());
            if (StringUtil.indexOfIgnoreCase(msg, "Method code too large!") != -1) {
                page = sc == null ? cfmlTransformer.transform(factory, config, ps, tld, fld, returnValue, ignoreScopes) : cfmlTransformer.transform(factory, config, sc, tld, fld, System.currentTimeMillis(), sc.getDialect() == CFMLEngine.DIALECT_CFML && config.getDotNotationUpperCase(), returnValue, ignoreScopes);
                page.setSplitIfNecessary(true);
                result = new Result(page, page.execute(className));
            } else
                throw re;
        } catch (ClassFormatError cfe) {
            String msg = StringUtil.emptyIfNull(cfe.getMessage());
            if (StringUtil.indexOfIgnoreCase(msg, "Invalid method Code length") != -1) {
                page = ps != null ? cfmlTransformer.transform(factory, config, ps, tld, fld, returnValue, ignoreScopes) : cfmlTransformer.transform(factory, config, sc, tld, fld, System.currentTimeMillis(), sc.getDialect() == CFMLEngine.DIALECT_CFML && config.getDotNotationUpperCase(), returnValue, ignoreScopes);
                page.setSplitIfNecessary(true);
                result = new Result(page, page.execute(className));
            } else
                throw cfe;
        }
        // store
        if (classRootDir != null) {
            Resource classFile = classRootDir.getRealResource(page.getClassName() + ".class");
            Resource classFileDirectory = classFile.getParentResource();
            if (!classFileDirectory.exists())
                classFileDirectory.mkdirs();
            IOUtil.copy(new ByteArrayInputStream(result.barr), classFile, true);
        }
        return result;
    } catch (AlreadyClassException ace) {
        byte[] bytes = ace.getEncrypted() ? readEncrypted(ace) : readPlain(ace);
        result = new Result(null, bytes);
        String displayPath = ps != null ? "[" + ps.getDisplayPath() + "] " : "";
        String srcName = ASMUtil.getClassName(result.barr);
        int dialect = sc == null ? ps.getDialect() : sc.getDialect();
        // source is cfm and target cfc
        if (dialect == CFMLEngine.DIALECT_CFML && endsWith(srcName, Constants.getCFMLTemplateExtensions(), dialect) && className.endsWith("_" + Constants.getCFMLComponentExtension() + (dialect == CFMLEngine.DIALECT_CFML ? Constants.CFML_CLASS_SUFFIX : Constants.LUCEE_CLASS_SUFFIX))) {
            throw new TemplateException("source file " + displayPath + "contains the bytecode for a regular cfm template not for a component");
        }
        // source is cfc and target cfm
        if (dialect == CFMLEngine.DIALECT_CFML && srcName.endsWith("_" + Constants.getCFMLComponentExtension() + (dialect == CFMLEngine.DIALECT_CFML ? Constants.CFML_CLASS_SUFFIX : Constants.LUCEE_CLASS_SUFFIX)) && endsWith(className, Constants.getCFMLTemplateExtensions(), dialect))
            throw new TemplateException("source file " + displayPath + "contains a component not a regular cfm template");
        // rename class name when needed
        if (!srcName.equals(className))
            result = new Result(result.page, ClassRenamer.rename(result.barr, className));
        // store
        if (classRootDir != null) {
            Resource classFile = classRootDir.getRealResource(className + ".class");
            Resource classFileDirectory = classFile.getParentResource();
            if (!classFileDirectory.exists())
                classFileDirectory.mkdirs();
            result = new Result(result.page, Page.setSourceLastModified(result.barr, ps != null ? ps.getPhyscalFile().lastModified() : System.currentTimeMillis()));
            IOUtil.copy(new ByteArrayInputStream(result.barr), classFile, true);
        }
        return result;
    } catch (TransformerException bce) {
        Position pos = bce.getPosition();
        int line = pos == null ? -1 : pos.line;
        int col = pos == null ? -1 : pos.column;
        if (ps != null)
            bce.addContext(ps, line, col, null);
        throw bce;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TemplateException(lucee.runtime.exp.TemplateException) Position(lucee.transformer.Position) Resource(lucee.commons.io.res.Resource) BytecodeFactory(lucee.transformer.bytecode.BytecodeFactory) Factory(lucee.transformer.Factory) Page(lucee.transformer.bytecode.Page) AlreadyClassException(lucee.transformer.util.AlreadyClassException) TransformerException(lucee.transformer.TransformerException)

Aggregations

TemplateException (lucee.runtime.exp.TemplateException)2 TransformerException (lucee.transformer.TransformerException)2 AlreadyClassException (lucee.transformer.util.AlreadyClassException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 PublicKey (java.security.PublicKey)1 Resource (lucee.commons.io.res.Resource)1 Factory (lucee.transformer.Factory)1 Position (lucee.transformer.Position)1 BytecodeFactory (lucee.transformer.bytecode.BytecodeFactory)1 Page (lucee.transformer.bytecode.Page)1