Search in sources :

Example 6 with Dependency

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

the class ScriptCompiler method compile.

public void compile(File scriptPath, String tenant, boolean hasTenantSpecificFile, boolean forceTenant) throws CompilationException, IOException, SkipCompilationException {
    List<Dependency> dependencies = new ArrayList<>();
    File scriptsMap = new File(navajoIOConfig.getRootPath(), getRelativeScriptPath());
    String script = getRelative(scriptsMap, scriptPath);
    // System.err.println("In ScriptCompiler.script = " + script);
    script = script.substring(0, script.indexOf(getScriptExtension()));
    String scriptName = null;
    String packagePath = null;
    if (script.indexOf('/') >= 0) {
        packagePath = script.substring(0, script.lastIndexOf('/'));
        scriptName = script.substring(script.lastIndexOf('/') + 1);
    } else {
        packagePath = "";
        scriptName = script;
    }
    Set<String> packages = compileScript(scriptPath, scriptName, packagePath, dependencies, tenant, hasTenantSpecificFile, forceTenant);
    // Before generating OSGi stuff, check forceTenant
    if (forceTenant) {
        scriptName += "_" + tenant;
    }
    String scriptString = null;
    if ("".equals(packagePath)) {
        scriptString = script.replaceAll("_", "|");
        if (forceTenant) {
            scriptString = (script + "_" + tenant).replaceAll("_", "|");
        }
    } else {
        scriptString = packagePath + "/" + script.replaceAll("_", "|");
        if (forceTenant) {
            scriptString = packagePath + "/" + (script + "_" + tenant).replaceAll("_", "|");
        }
    }
    Set<String> dependentResources = processDependencies(dependencies);
    generateFactoryClass(scriptName, packagePath, dependentResources);
    generateManifest(scriptString, "1.0.0", packagePath, scriptName, packages);
    String compiledDate = generateDs(packagePath, scriptName, dependencies, dependentResources);
    if (packagePath.startsWith("entity")) {
        generateEntityDs(packagePath, scriptName, compiledDate, dependencies, dependentResources);
    }
}
Also used : ArrayList(java.util.ArrayList) ExtendDependency(com.dexels.navajo.mapping.compiler.meta.ExtendDependency) AdapterFieldDependency(com.dexels.navajo.mapping.compiler.meta.AdapterFieldDependency) IncludeDependency(com.dexels.navajo.mapping.compiler.meta.IncludeDependency) Dependency(com.dexels.navajo.script.api.Dependency) File(java.io.File)

Example 7 with Dependency

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

the class ScriptCompiler method generateEntityDs.

private void generateEntityDs(String packagePath, String script, String compiledDate, List<Dependency> dependencies, Set<String> dependentResources) throws IOException {
    String fullName;
    if (packagePath.equals("")) {
        fullName = script;
    } else {
        fullName = packagePath + "/" + script;
    }
    String entityName = fullName.substring(fullName.indexOf('/') + 1).replaceAll("/", ".");
    String symbolicName = rpcNameFromScriptPath(fullName).replaceAll("/", ".");
    XMLElement xe = new CaseSensitiveXMLElement("scr:component");
    xe.setAttribute("xmlns:scr", "http://www.osgi.org/xmlns/scr/v1.1.0");
    xe.setAttribute("immediate", "true");
    xe.setAttribute("name", "navajo.entities." + symbolicName);
    xe.setAttribute("activate", "activate");
    xe.setAttribute("deactivate", "deactivate");
    xe.setAttribute("enabled", "true");
    XMLElement implementation = new CaseSensitiveXMLElement("implementation");
    xe.addChild(implementation);
    implementation.setAttribute("class", "com.dexels.navajo.enterprise.entity.Entity");
    XMLElement service = new CaseSensitiveXMLElement("service");
    xe.addChild(service);
    XMLElement provide = new CaseSensitiveXMLElement("provide");
    service.addChild(provide);
    provide.setAttribute("interface", "com.dexels.navajo.enterprise.entity.Entity");
    addProperty("entity.name", "String", entityName, xe);
    addProperty("service.name", "String", fullName, xe);
    addProperty("entity.message", "String", script, xe);
    XMLElement refMan = new CaseSensitiveXMLElement("reference");
    refMan.setAttribute("bind", "setEntityManager");
    refMan.setAttribute("unbind", "clearEntityManager");
    refMan.setAttribute("policy", "dynamic");
    refMan.setAttribute("cardinality", "1..1");
    refMan.setAttribute("interface", "com.dexels.navajo.enterprise.entity.EntityManager");
    refMan.setAttribute("name", "EntityManager");
    xe.addChild(refMan);
    XMLElement refScript = new CaseSensitiveXMLElement("reference");
    refScript.setAttribute("cardinality", "1..1");
    refScript.setAttribute("bind", "setCompiledScript");
    refScript.setAttribute("unbind", "clearCompiledScript");
    refScript.setAttribute("interface", "com.dexels.navajo.script.api.CompiledScriptFactory");
    refScript.setAttribute("name", "CompiledScript");
    String target1 = "component.name=" + symbolicName.replace("/", ".");
    String target2 = "navajo.compileddate=" + compiledDate;
    refScript.setAttribute("target", "(&(" + target1 + ")(" + target2 + "))");
    xe.addChild(refScript);
    for (int i = 0; i < dependencies.size(); i++) {
        Dependency d = dependencies.get(i);
        if (d instanceof ExtendDependency) {
            String extendedEntity = d.getId().replaceAll("/", ".");
            XMLElement depref = new CaseSensitiveXMLElement("reference");
            depref.setAttribute("name", "SuperEntity" + i);
            depref.setAttribute("policy", "static");
            depref.setAttribute("cardinality", "1..1");
            depref.setAttribute("interface", "com.dexels.navajo.enterprise.entity.Entity");
            depref.setAttribute("target", "(entity.name=" + extendedEntity + ")");
            depref.setAttribute("bind", "addSuperEntity");
            depref.setAttribute("unbind", "removeSuperEntity");
            xe.addChild(depref);
        }
    }
    for (String resource : dependentResources) {
        XMLElement dep = new CaseSensitiveXMLElement("reference");
        dep.setAttribute("bind", "set" + resource);
        dep.setAttribute("unbind", "clear" + resource);
        dep.setAttribute("policy", "static");
        dep.setAttribute("cardinality", "1..1");
        dep.setAttribute("interface", "javax.sql.DataSource");
        dep.setAttribute("target", "(navajo.resource.name=" + resource + ")");
        xe.addChild(dep);
    }
    PrintWriter w = new PrintWriter(navajoIOConfig.getOutputWriter(navajoIOConfig.getCompiledScriptPath(), packagePath, "entity", ".xml"));
    w.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    xe.write(w);
    w.flush();
    w.close();
}
Also used : ExtendDependency(com.dexels.navajo.mapping.compiler.meta.ExtendDependency) CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) ExtendDependency(com.dexels.navajo.mapping.compiler.meta.ExtendDependency) AdapterFieldDependency(com.dexels.navajo.mapping.compiler.meta.AdapterFieldDependency) IncludeDependency(com.dexels.navajo.mapping.compiler.meta.IncludeDependency) Dependency(com.dexels.navajo.script.api.Dependency) CaseSensitiveXMLElement(com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) PrintWriter(java.io.PrintWriter)

Example 8 with Dependency

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

the class GenericHandler method compileScript.

/**
 * Non-OSGi only
 * @param a
 * @param compilerErrors
 * @return
 * @throws Exception
 */
public CompiledScript compileScript(Access a, StringBuilder compilerErrors) throws Exception {
    NavajoConfigInterface properties = DispatcherFactory.getInstance().getNavajoConfig();
    List<Dependency> deps = new ArrayList<>();
    String scriptPath = properties.getScriptPath();
    Object[] all = getScriptPathServiceNameAndScriptFile(a, a.rpcName, a.betaUser);
    if (all == null) {
        throw new FileNotFoundException("No script found for: " + a.rpcName);
    }
    String pathPrefix = (String) all[0];
    String serviceName = (String) all[1];
    File scriptFile = (File) all[2];
    // String sourceFileName = (String) all[3];
    File sourceFile = (File) all[4];
    String className = (String) all[5];
    if (properties.isCompileScripts()) {
        if (scriptFile != null && scriptFile.exists()) {
            if (checkScriptRecompile(scriptFile, sourceFile) || hasDirtyDepedencies(a, className)) {
                synchronized (mutex1) {
                    if (checkScriptRecompile(scriptFile, sourceFile) || hasDirtyDepedencies(a, className)) {
                        com.dexels.navajo.mapping.compiler.TslCompiler tslCompiler = new com.dexels.navajo.mapping.compiler.TslCompiler(properties.getClassloader());
                        try {
                            final String tenant = tenantConfig.getInstanceGroup();
                            tslCompiler.compileScript(serviceName, scriptPath, properties.getCompiledScriptPath(), pathPrefix, properties.getOutputWriter(properties.getCompiledScriptPath(), pathPrefix, serviceName, ".java"), deps, tenant, tenantConfig.hasTenantScriptFile(serviceName, tenant, null), false);
                        } catch (SystemException ex) {
                            Files.deleteIfExists(sourceFile.toPath());
                            AuditLog.log(AuditLog.AUDIT_MESSAGE_SCRIPTCOMPILER, ex.getMessage(), Level.SEVERE, a.accessID);
                            throw ex;
                        }
                    }
                }
            }
        // end of sync block.
        // Java recompile.
        // TODO check if this removal is ok
        // compilerErrors.append(recompileJava(a, sourceFileName, sourceFile, className, targetFile,serviceName));
        } else {
            // Maybe the jave file exists in the script path.
            if (!sourceFile.exists()) {
                // There is no java file present.
                AuditLog.log(AuditLog.AUDIT_MESSAGE_SCRIPTCOMPILER, "SCRIPT FILE DOES NOT EXISTS, I WILL TRY TO LOAD THE CLASS FILE ANYWAY....", Level.WARNING, a.accessID);
            } else {
                // Compile java file using normal java compiler.
                if (sourceFile.getName().endsWith("java")) {
                    logger.error("Separate java scripts not supported!");
                }
            }
        }
    }
    return getCompiledScript(a, className, sourceFile, serviceName);
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Dependency(com.dexels.navajo.script.api.Dependency) SystemException(com.dexels.navajo.script.api.SystemException) File(java.io.File)

Example 9 with Dependency

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

the class CompiledTestScript4 method getDependentObjects.

@Override
public ArrayList<Dependency> getDependentObjects() {
    ArrayList<Dependency> deps = new ArrayList<Dependency>();
    deps.add(new AdapterFieldDependency(-1, "com.dexels.navajo.server.resource.ResourceTestAdapter", "whatever", "'id5'"));
    // Deliberately create circular dependency
    deps.add(new AdapterFieldDependency(-1, "com.dexels.navajo.adapter.NavajoMap", GenericDependentResource.SERVICE_DEPENDENCY, "'CompiledTestScript3'"));
    return deps;
}
Also used : AdapterFieldDependency(com.dexels.navajo.mapping.compiler.meta.AdapterFieldDependency) ArrayList(java.util.ArrayList) AdapterFieldDependency(com.dexels.navajo.mapping.compiler.meta.AdapterFieldDependency) Dependency(com.dexels.navajo.script.api.Dependency)

Example 10 with Dependency

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

the class CompiledTestScript3 method getDependentObjects.

@Override
public ArrayList<Dependency> getDependentObjects() {
    ArrayList<Dependency> deps = new ArrayList<Dependency>();
    deps.add(new AdapterFieldDependency(-1, "com.dexels.navajo.server.resource.ResourceTestAdapter", "whatever", "'id1'"));
    deps.add(new AdapterFieldDependency(-1, "com.dexels.navajo.adapter.NavajoMap", GenericDependentResource.SERVICE_DEPENDENCY, "'CompiledTestScript4'"));
    return deps;
}
Also used : AdapterFieldDependency(com.dexels.navajo.mapping.compiler.meta.AdapterFieldDependency) ArrayList(java.util.ArrayList) AdapterFieldDependency(com.dexels.navajo.mapping.compiler.meta.AdapterFieldDependency) Dependency(com.dexels.navajo.script.api.Dependency)

Aggregations

Dependency (com.dexels.navajo.script.api.Dependency)10 AdapterFieldDependency (com.dexels.navajo.mapping.compiler.meta.AdapterFieldDependency)9 ArrayList (java.util.ArrayList)6 ExtendDependency (com.dexels.navajo.mapping.compiler.meta.ExtendDependency)4 IncludeDependency (com.dexels.navajo.mapping.compiler.meta.IncludeDependency)4 ExpressionValueDependency (com.dexels.navajo.mapping.compiler.meta.ExpressionValueDependency)2 SystemException (com.dexels.navajo.script.api.SystemException)2 File (java.io.File)2 Operand (com.dexels.navajo.document.Operand)1 CaseSensitiveXMLElement (com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement)1 XMLElement (com.dexels.navajo.document.nanoimpl.XMLElement)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 DependentResource (com.dexels.navajo.mapping.DependentResource)1 GenericMultipleDependentResource (com.dexels.navajo.mapping.GenericMultipleDependentResource)1 HasDependentResources (com.dexels.navajo.mapping.HasDependentResources)1 JavaDependency (com.dexels.navajo.mapping.compiler.meta.JavaDependency)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 CompilationException (com.dexels.navajo.script.api.CompilationException)1