Search in sources :

Example 1 with CompilationException

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

the class BundleCreatorComponent method compileAndCreateBundle.

private void compileAndCreateBundle(String script, File scriptPath, final String scriptTenant, boolean hasTenantSpecificFile, boolean forceTenant, boolean keepIntermediate, List<String> success, List<String> skipped, List<String> failures) throws CompilationException {
    String myScript = script;
    if (forceTenant) {
        myScript = script + "_" + scriptTenant;
    }
    try {
        getScriptCompiler(scriptPath).compile(scriptPath, scriptTenant, hasTenantSpecificFile, forceTenant);
        if (getScriptCompiler(scriptPath).scriptNeedsCompilation()) {
            javaCompiler.compileJava(myScript);
        }
        javaCompiler.compileJava(myScript + "Factory");
        createBundleJar(myScript, scriptPath, scriptTenant, keepIntermediate, hasTenantSpecificFile);
        success.add(myScript);
    } catch (SkipCompilationException e) {
        logger.debug("Script fragment: {} ", script);
        skipped.add(script);
        return;
    } catch (Exception e) {
        failures.add(script);
        throw new CompilationException("Error compiling script", e);
    }
    if (forceTenant) {
        logger.info("Finished compiling and bundling {} for {}", script, scriptTenant);
    } else {
        logger.info("Finished compiling and bundling {}", script);
    }
}
Also used : SkipCompilationException(com.dexels.navajo.mapping.compiler.SkipCompilationException) CompilationException(com.dexels.navajo.script.api.CompilationException) SkipCompilationException(com.dexels.navajo.mapping.compiler.SkipCompilationException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SkipCompilationException(com.dexels.navajo.mapping.compiler.SkipCompilationException) CompilationException(com.dexels.navajo.script.api.CompilationException)

Example 2 with CompilationException

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

the class ScalaCompiler method compileScript.

@Override
protected Set<String> compileScript(File scriptFile, String script, String packagePath, List<Dependency> dependencies, String tenant, boolean hasTenantSpecificFile, boolean forceTenant) throws CompilationException {
    final Set<String> packages = new HashSet<>();
    for (String pkg : standardPackages) {
        packages.add(pkg);
    }
    PackageReportingClassLoader prc = new PackageReportingClassLoader(navajoScriptClassLoader);
    prc.addPackageListener(name -> {
        if (whitelist.contains(name)) {
            packages.add(name);
        }
    });
    File targetDir = new File(navajoIOConfig.getCompiledScriptPath(), packagePath + File.separator + script);
    targetDir.mkdirs();
    Settings settings = new Settings();
    settings.outputDirs().setSingleOutput(targetDir.getAbsolutePath());
    ConsoleReporter reporter = new ConsoleReporter(settings);
    ReflectGlobal g = new ReflectGlobal(settings, reporter, prc);
    Global.Run compiler = g.new Run();
    ListBuffer<String> files = new ListBuffer<>();
    String file = scriptFile.getAbsolutePath();
    addScalaCommon(files);
    files.$plus$eq(file);
    try {
        compiler.compile(files.toList());
    } catch (Exception e) {
        logger.error("Exception on getting scala code! {}", e);
    }
    logger.debug("finished compiling scala for the following files: {}", compiler.compiledFiles());
    return packages;
}
Also used : ConsoleReporter(scala.tools.nsc.reporters.ConsoleReporter) ListBuffer(scala.collection.mutable.ListBuffer) PackageReportingClassLoader(com.dexels.navajo.compiler.tsl.custom.PackageReportingClassLoader) Global(scala.tools.nsc.Global) ReflectGlobal(scala.tools.reflect.ReflectGlobal) IOException(java.io.IOException) CompilationException(com.dexels.navajo.script.api.CompilationException) File(java.io.File) Settings(scala.tools.nsc.Settings) ReflectGlobal(scala.tools.reflect.ReflectGlobal) HashSet(java.util.HashSet)

Example 3 with CompilationException

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

the class TslCompiler method compileToJava.

/**
 * Only used by OSGi now
 *
 * @param script
 * @param input
 * @param output
 * @param packagePath
 *            The package in the Java file. OSGi doesn't handle default
 *            packages well, so default package will be translated to
 *            'defaultPackage'
 * @param scriptPackagePath
 *            the path of the scriptFile from the scriptRoot
 * @param classLoader
 * @param navajoIOConfig
 * @param forceTenant TODO
 * @return
 * @throws Exception
 */
public String compileToJava(String script, String input, String output, String packagePath, String scriptPackagePath, ClassLoader classLoader, NavajoIOConfig navajoIOConfig, List<Dependency> deps, String tenant, boolean hasTenantSpecificScript, boolean forceTenant) throws CompilationException, SkipCompilationException {
    String tenantScript = script;
    if (forceTenant) {
        tenantScript = script + "_" + tenant;
    }
    String javaFile = output + "/" + tenantScript + ".java";
    TslCompiler tslCompiler = new TslCompiler(classLoader, navajoIOConfig);
    try {
        String bareScript;
        if (script.indexOf('/') >= 0) {
            bareScript = script.substring(script.lastIndexOf('/') + 1, script.length());
        } else {
            bareScript = script;
        }
        tslCompiler.compileScript(bareScript, input, output, scriptPackagePath, navajoIOConfig.getOutputWriter(output, packagePath, tenantScript, ".java"), deps, tenant, hasTenantSpecificScript, forceTenant);
        return javaFile;
    } catch (SkipCompilationException ex) {
        throw new SkipCompilationException("Skip compilation exception, script: " + script + " does not need compiling");
    } catch (Throwable ex) {
        logger.error("Error compiling script: " + script, ex);
        // Isn't this what 'finally' is for?
        File f = new File(javaFile);
        if (f.exists()) {
            f.delete();
        }
        if (ex instanceof Exception) {
            throw new CompilationException("Other compilation exception:", ex);
        }
        return null;
    }
}
Also used : CompilationException(com.dexels.navajo.script.api.CompilationException) File(java.io.File) UserException(com.dexels.navajo.script.api.UserException) TransformerException(javax.xml.transform.TransformerException) MappingException(com.dexels.navajo.script.api.MappingException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) ParseException(com.dexels.navajo.parser.compiled.ParseException) KeywordException(com.dexels.navajo.mapping.compiler.meta.KeywordException) MetaCompileException(com.dexels.navajo.mapping.compiler.meta.MetaCompileException) IOException(java.io.IOException) SystemException(com.dexels.navajo.script.api.SystemException) CompilationException(com.dexels.navajo.script.api.CompilationException)

Example 4 with CompilationException

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

the class BundleCreatorComponent method createBundleNoLocking.

private void createBundleNoLocking(String rpcName, List<String> failures, List<String> success, List<String> skipped, boolean force, boolean keepIntermediate) throws CompilationException {
    boolean matchedScript = false;
    removeOldCompiledScriptFiles(rpcName);
    for (ScriptCompiler compiler : compilers.values()) {
        System.err.println("Checking compiler " + compiler + " to compile service " + rpcName);
        File scriptFolder = new File(navajoIOConfig.getRootPath(), compiler.getRelativeScriptPath());
        File f = new File(scriptFolder, rpcName + compiler.getScriptExtension());
        // Look for other tenant-specific files
        AbstractFileFilter fileFilter = new WildcardFileFilter(FilenameUtils.getBaseName(rpcName) + "_*" + compiler.getScriptExtension());
        File dir = new File(scriptFolder, FilenameUtils.getPath(rpcName));
        Collection<File> files = Collections.<File>emptySet();
        if (dir.exists()) {
            files = FileUtils.listFiles(dir, fileFilter, null);
        }
        Map<String, File> tenantSpecificFiles = new HashMap<>();
        Collection<String> tenantsToIgnore = new ArrayList<>();
        for (File ascript : files) {
            matchedScript = true;
            String pathRelative = getRelative(scriptFolder, ascript);
            String[] splitted = pathRelative.split("\\.");
            String tenantScriptName = splitted[0].replace('\\', '/');
            tenantSpecificFiles.put(tenantScriptName, ascript);
            // Get the tenant out of the name and put it in the tenantsToIgnore collection
            tenantsToIgnore.add(tenantScriptName.split("_")[1]);
        }
        if (f.exists()) {
            matchedScript = true;
            createBundleForScript(rpcName, rpcName, f, tenantsToIgnore, true, failures, success, skipped, keepIntermediate);
        }
        for (Map.Entry<String, File> entry : tenantSpecificFiles.entrySet()) {
            createBundleForScript(entry.getKey(), rpcName, entry.getValue(), tenantsToIgnore, false, failures, success, skipped, keepIntermediate);
        }
        if (matchedScript) {
            break;
        }
    }
    if (!matchedScript) {
        throw new CompilationException("Unable to find script for " + rpcName);
    }
}
Also used : SkipCompilationException(com.dexels.navajo.mapping.compiler.SkipCompilationException) CompilationException(com.dexels.navajo.script.api.CompilationException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) ScriptCompiler(com.dexels.navajo.compiler.ScriptCompiler) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) AbstractFileFilter(org.apache.commons.io.filefilter.AbstractFileFilter)

Example 5 with CompilationException

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

the class BundleCreatorComponent method getCompiledScript.

@SuppressWarnings("unchecked")
private CompiledScriptInterface getCompiledScript(String rpcName, String tenant) throws CompilationException {
    String scriptName = rpcName.replaceAll("/", ".");
    String realTenant = "default";
    if (tenant != null) {
        realTenant = tenant;
    }
    if (scriptsMap.containsKey(rpcName)) {
        Map<String, CompiledScriptFactory> myScripts = scriptsMap.get(rpcName);
        CompiledScriptFactory csf = myScripts.get(realTenant);
        if (csf != null) {
            try {
                return csf.getCompiledScript();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                logger.error("Exception on retrieving cached CompiledScriptFactory for {} {} - going to try non-cached one", rpcName, realTenant);
            }
        }
    }
    String filter = "(&(navajo.scriptName=" + scriptName + ") (|(navajo.tenant=" + tenant + ") (navajo.tenant=default)))";
    ServiceReference<CompiledScriptFactory>[] servicereferences;
    try {
        servicereferences = (ServiceReference<CompiledScriptFactory>[]) bundleContext.getServiceReferences(CompiledScriptFactory.class.getName(), filter);
        if (servicereferences != null) {
            // First try to find one that matches our tenant
            for (ServiceReference<CompiledScriptFactory> srinstance : servicereferences) {
                if (srinstance.getProperty("navajo.tenant").equals(tenant)) {
                    CompiledScriptFactory csf = bundleContext.getService(srinstance);
                    if (csf == null) {
                        logger.warn("Script with filter: {} found, but could not be resolved.", filter);
                        return null;
                    }
                    updateCachedCompiledScript(rpcName, realTenant, csf);
                    return csf.getCompiledScript();
                }
            }
            // if that fails, simply return first one (probably "default")
            if (servicereferences.length > 0) {
                ServiceReference<CompiledScriptFactory> srinstance = servicereferences[0];
                CompiledScriptFactory csf = bundleContext.getService(srinstance);
                if (csf == null) {
                    logger.warn("Script with filter: {} found, but could not be resolved.", filter);
                    return null;
                }
                updateCachedCompiledScript(rpcName, realTenant, csf);
                return csf.getCompiledScript();
            }
        }
    } catch (InvalidSyntaxException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new CompilationException("Error resolving script service for: " + rpcName, e);
    }
    return null;
}
Also used : SkipCompilationException(com.dexels.navajo.mapping.compiler.SkipCompilationException) CompilationException(com.dexels.navajo.script.api.CompilationException) ServiceReference(org.osgi.framework.ServiceReference) CompiledScriptFactory(com.dexels.navajo.script.api.CompiledScriptFactory) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

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