Search in sources :

Example 1 with SourceIdentifier

use of org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier in project sling by apache.

the class SightlyJavaCompilerService method getUseObjectAndRecompileIfNeeded.

private Object getUseObjectAndRecompileIfNeeded(Resource pojoResource) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    SourceIdentifier sourceIdentifier = new SourceIdentifier(sightlyEngineConfiguration, pojoResource.getPath());
    long sourceLastModifiedDateFromCache = resourceBackedPojoChangeMonitor.getLastModifiedDateForJavaUseObject(pojoResource.getPath());
    long classLastModifiedDate = classLoaderWriter.getLastModified("/" + sourceIdentifier.getFullyQualifiedClassName().replaceAll("\\.", "/") + ".class");
    if (sourceLastModifiedDateFromCache == 0) {
        // first access; let's check the real last modified date of the source
        long sourceLastModifiedDate = pojoResource.getResourceMetadata().getModificationTime();
        resourceBackedPojoChangeMonitor.recordLastModifiedTimestamp(pojoResource.getPath(), sourceLastModifiedDate);
        if (classLastModifiedDate < 0 || sourceLastModifiedDate > classLastModifiedDate) {
            return compileSource(sourceIdentifier, IOUtils.toString(pojoResource.adaptTo(InputStream.class), "UTF-8"));
        } else {
            return classLoaderWriter.getClassLoader().loadClass(sourceIdentifier.getFullyQualifiedClassName()).newInstance();
        }
    } else {
        if (sourceLastModifiedDateFromCache > classLastModifiedDate) {
            return compileSource(sourceIdentifier, IOUtils.toString(pojoResource.adaptTo(InputStream.class), "UTF-8"));
        } else {
            return classLoaderWriter.getClassLoader().loadClass(sourceIdentifier.getFullyQualifiedClassName()).newInstance();
        }
    }
}
Also used : SourceIdentifier(org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier)

Example 2 with SourceIdentifier

use of org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier 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);
    }
}
Also used : CompilationUnit(org.apache.sling.scripting.sightly.compiler.CompilationUnit) ScriptNameAware(org.apache.sling.scripting.api.ScriptNameAware) CompilerMessage(org.apache.sling.scripting.sightly.compiler.CompilerMessage) JavaClassBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler) RenderUnit(org.apache.sling.scripting.sightly.java.compiler.RenderUnit) GlobalShadowCheckBackendCompiler(org.apache.sling.scripting.sightly.java.compiler.GlobalShadowCheckBackendCompiler) SourceIdentifier(org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) ScriptException(javax.script.ScriptException) SightlyException(org.apache.sling.scripting.sightly.SightlyException) CompilationResult(org.apache.sling.scripting.sightly.compiler.CompilationResult)

Aggregations

SourceIdentifier (org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier)2 Bindings (javax.script.Bindings)1 ScriptException (javax.script.ScriptException)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1 ScriptNameAware (org.apache.sling.scripting.api.ScriptNameAware)1 SightlyException (org.apache.sling.scripting.sightly.SightlyException)1 CompilationResult (org.apache.sling.scripting.sightly.compiler.CompilationResult)1 CompilationUnit (org.apache.sling.scripting.sightly.compiler.CompilationUnit)1 CompilerMessage (org.apache.sling.scripting.sightly.compiler.CompilerMessage)1 GlobalShadowCheckBackendCompiler (org.apache.sling.scripting.sightly.java.compiler.GlobalShadowCheckBackendCompiler)1 JavaClassBackendCompiler (org.apache.sling.scripting.sightly.java.compiler.JavaClassBackendCompiler)1 RenderUnit (org.apache.sling.scripting.sightly.java.compiler.RenderUnit)1