Search in sources :

Example 6 with CompilationException

use of com.dexels.navajo.script.api.CompilationException in project navajo by Dexels.

the class BundleQueueComponent method ensureScriptDependencies.

// ensure that dependencies of the current script are satisfied. If dependencies
// are not satisfied, create them
private void ensureScriptDependencies(String script) {
    String rpcName = script;
    String bareScript = script.substring(script.lastIndexOf('/') + 1);
    if (bareScript.indexOf('_') >= 0) {
        rpcName = script.substring(0, script.lastIndexOf('_'));
    }
    // For now, only entity dependencies are relevant script dependencies.
    // For instance, in the case the server runs in DEVELOP_MODE where
    // entities are lazily loaded, all bundles for super entities also need
    // to be installed
    List<Dependency> dependencies = depanalyzer.getDependencies(rpcName, Dependency.ENTITY_DEPENDENCY);
    for (Dependency dependency : dependencies) {
        String depScript = dependency.getDependee();
        try {
            // do an on demand call to the bundle creator, we only need the script to be
            // compiled, if it wasn't there yet
            bundleCreator.getOnDemandScriptService(depScript, null);
            ensureScriptDependencies(depScript);
        } catch (CompilationException e) {
            logger.info("Failed to compile {} after a change in {}: {}", depScript, script, e);
        }
    }
}
Also used : CompilationException(com.dexels.navajo.script.api.CompilationException) Dependency(com.dexels.navajo.dependency.Dependency)

Example 7 with CompilationException

use of com.dexels.navajo.script.api.CompilationException in project navajo by Dexels.

the class OSGiJavaCompilerImplementation method compile.

@Override
public byte[] compile(String className, InputStream source) throws IOException, CompilationException {
    JavaFileObject javaSource = getJavaSourceFileObject(className, source);
    Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(javaSource);
    final Writer sw = new StringWriter();
    DiagnosticListener<JavaFileObject> compilerOutputListener = jfo -> {
        try {
            sw.write("Compilation problem. Line in .java file: " + jfo.getLineNumber() + ", error: " + jfo.getMessage(Locale.ENGLISH) + "\n");
        } catch (IOException e) {
            logger.error("Compilation problem: ", e);
        }
    };
    StringWriter swe = new StringWriter();
    List<String> options = new ArrayList<>();
    options.add("-nowarn");
    options.add("-target");
    options.add("1.8");
    CompilationTask task = compiler.getTask(swe, customJavaFileManager, compilerOutputListener, options, null, fileObjects);
    boolean success = task.call();
    if (!success) {
        throw new CompilationException(sw.toString() + "\n" + swe.toString());
    }
    CustomJavaFileObject jfo = (CustomJavaFileObject) customJavaFileManager.getJavaFileForInput(StandardLocation.CLASS_OUTPUT, className, Kind.CLASS);
    if (jfo == null) {
        logger.error("Compilation failed: {} \n {}", sw, swe);
        return null;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(jfo.openInputStream(), baos);
    return baos.toByteArray();
}
Also used : Kind(javax.tools.JavaFileObject.Kind) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JavaFileManager(javax.tools.JavaFileManager) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) CustomClassLoader(com.dexels.navajo.compiler.tsl.custom.CustomClassLoader) CompilationTask(javax.tools.JavaCompiler.CompilationTask) Locale(java.util.Locale) Map(java.util.Map) URI(java.net.URI) Hashtable(java.util.Hashtable) CustomClassloaderJavaFileManager(com.dexels.navajo.compiler.tsl.custom.CustomClassloaderJavaFileManager) ServiceRegistration(org.osgi.framework.ServiceRegistration) StandardLocation(javax.tools.StandardLocation) Logger(org.slf4j.Logger) JavaCompiler(javax.tools.JavaCompiler) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) StringWriter(java.io.StringWriter) CustomJavaFileObject(com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject) OSGiJavaCompiler(com.dexels.osgicompiler.OSGiJavaCompiler) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Writer(java.io.Writer) Optional(java.util.Optional) DiagnosticListener(javax.tools.DiagnosticListener) Dictionary(java.util.Dictionary) CompilationException(com.dexels.navajo.script.api.CompilationException) InputStream(java.io.InputStream) CompilationException(com.dexels.navajo.script.api.CompilationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompilationTask(javax.tools.JavaCompiler.CompilationTask) CustomJavaFileObject(com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) CustomJavaFileObject(com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

CompilationException (com.dexels.navajo.script.api.CompilationException)7 IOException (java.io.IOException)4 SkipCompilationException (com.dexels.navajo.mapping.compiler.SkipCompilationException)3 File (java.io.File)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ScriptCompiler (com.dexels.navajo.compiler.ScriptCompiler)1 CustomClassLoader (com.dexels.navajo.compiler.tsl.custom.CustomClassLoader)1 CustomClassloaderJavaFileManager (com.dexels.navajo.compiler.tsl.custom.CustomClassloaderJavaFileManager)1 CustomJavaFileObject (com.dexels.navajo.compiler.tsl.custom.CustomJavaFileObject)1 PackageReportingClassLoader (com.dexels.navajo.compiler.tsl.custom.PackageReportingClassLoader)1 Dependency (com.dexels.navajo.dependency.Dependency)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 KeywordException (com.dexels.navajo.mapping.compiler.meta.KeywordException)1 MetaCompileException (com.dexels.navajo.mapping.compiler.meta.MetaCompileException)1 ParseException (com.dexels.navajo.parser.compiled.ParseException)1 CompiledScriptFactory (com.dexels.navajo.script.api.CompiledScriptFactory)1 MappingException (com.dexels.navajo.script.api.MappingException)1 SystemException (com.dexels.navajo.script.api.SystemException)1 UserException (com.dexels.navajo.script.api.UserException)1