use of org.apache.sling.scripting.api.ScriptNameAware in project sling by apache.
the class RhinoJavaScriptEngine method eval.
public Object eval(Reader scriptReader, ScriptContext scriptContext) throws ScriptException {
String scriptName = getScriptName(scriptReader);
Reader reader = wrapReaderIfEspScript(scriptReader, scriptName);
if (!(scriptReader instanceof ScriptNameAware)) {
if (NO_SCRIPT_NAME.equals(scriptName)) {
String script = (String) scriptContext.getBindings(ScriptContext.ENGINE_SCOPE).get(ScriptEngine.FILENAME);
if (script != null) {
for (String extension : getFactory().getExtensions()) {
if (script.endsWith(extension)) {
scriptName = script;
reader = new ScriptNameAwareReader(reader, scriptName);
break;
}
}
}
}
}
return compile(reader).eval(scriptContext);
}
use of org.apache.sling.scripting.api.ScriptNameAware in project sling by apache.
the class SightlyScriptEngine method internalCompile.
private SightlyCompiledScript internalCompile(final Reader script, ScriptContext scriptContext) throws ScriptException {
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(((SightlyScriptEngineFactory) getFactory()).getClassLoader());
try {
String sName = NO_SCRIPT;
if (script instanceof ScriptNameAware) {
sName = ((ScriptNameAware) script).getScriptName();
}
if (sName.equals(NO_SCRIPT)) {
sName = getScriptName(scriptContext);
}
final String scriptName = sName;
CompilationUnit compilationUnit = new CompilationUnit() {
@Override
public String getScriptName() {
return scriptName;
}
@Override
public Reader getScriptReader() {
return script;
}
};
JavaClassBackendCompiler javaClassBackendCompiler = new JavaClassBackendCompiler();
GlobalShadowCheckBackendCompiler shadowCheckBackendCompiler = null;
if (scriptContext != null) {
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
Set<String> globals = bindings.keySet();
shadowCheckBackendCompiler = new GlobalShadowCheckBackendCompiler(javaClassBackendCompiler, globals);
}
CompilationResult result = shadowCheckBackendCompiler == null ? sightlyCompiler.compile(compilationUnit, javaClassBackendCompiler) : sightlyCompiler.compile(compilationUnit, shadowCheckBackendCompiler);
if (result.getWarnings().size() > 0) {
for (CompilerMessage warning : result.getWarnings()) {
LOGGER.warn("Script {} {}:{}: {}", new Object[] { warning.getScriptName(), warning.getLine(), warning.getColumn(), warning.getMessage() });
}
}
if (result.getErrors().size() > 0) {
CompilerMessage error = result.getErrors().get(0);
throw new ScriptException(error.getMessage(), error.getScriptName(), error.getLine(), error.getColumn());
}
SourceIdentifier sourceIdentifier = new SourceIdentifier(configuration, scriptName);
String javaSourceCode = javaClassBackendCompiler.build(sourceIdentifier);
Object renderUnit = javaCompilerService.compileSource(sourceIdentifier, javaSourceCode);
if (renderUnit instanceof RenderUnit) {
return new SightlyCompiledScript(this, (RenderUnit) renderUnit);
} else {
throw new SightlyException("Expected a RenderUnit.");
}
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
Aggregations