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;
}
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;
}
}
Aggregations