Search in sources :

Example 1 with UserException

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

the class TslCompiler method propertyNode.

public String propertyNode(int ident, Element n, boolean canBeSubMapped, String className, String objectName) throws UserException, ParseException {
    StringBuilder result = new StringBuilder();
    String propertyName = n.getAttribute("name");
    String direction = n.getAttribute("direction");
    String type = n.getAttribute("type");
    String subtype = n.getAttribute("subtype");
    String lengthStr = n.getAttribute("length");
    int length = ((lengthStr != null && !lengthStr.equals("")) ? Integer.parseInt(lengthStr) : -1);
    String value = n.getAttribute("value");
    String description = n.getAttribute("description");
    String cardinality = n.getAttribute("cardinality");
    String condition = n.getAttribute("condition");
    String key = n.getAttribute("key");
    String reference = n.getAttribute("reference");
    String extendsProp = n.getAttribute("extends");
    String bindProp = n.getAttribute("bind");
    String methodProp = n.getAttribute("method");
    value = (value == null) || (value.equals("")) ? "" : value;
    type = (type == null) ? "" : type;
    subtype = (subtype == null) ? "" : subtype;
    description = (description == null) ? "" : description;
    cardinality = (cardinality == null || cardinality.equals("")) ? "1" : cardinality;
    condition = (condition == null) ? "" : condition;
    methodProp = (methodProp == null) ? "" : methodProp;
    boolean conditionClause = false;
    if (!condition.equals("")) {
        conditionClause = true;
        result.append(printIdent(ident) + "if (Condition.evaluate(" + replaceQuotes(condition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) { \n");
        ident += 2;
    }
    NodeList children = n.getChildNodes();
    boolean hasChildren = false;
    boolean isSelection = false;
    boolean isMapped = false;
    Element mapNode = null;
    StringBuilder optionItems = new StringBuilder();
    int exprCount = countNodes(children, "expression");
    if ("".equals(value)) {
        result.append(printIdent(ident) + "matchingConditions = false;\n");
    }
    Class localContextClass = null;
    for (int i = 0; i < children.getLength(); i++) {
        hasChildren = true;
        // Has condition
        if (children.item(i).getNodeName().equals("expression")) {
            result.append(expressionNode(ident, (Element) children.item(i), --exprCount, className, objectName));
        } else if (children.item(i).getNodeName().equals("option")) {
            isSelection = true;
            String optionCondition = ((Element) children.item(i)).getAttribute("condition");
            String optionName = ((Element) children.item(i)).getAttribute("name");
            String optionValue = ((Element) children.item(i)).getAttribute("value");
            String selectedValue = ((Element) children.item(i)).getAttribute("selected");
            type = "selection";
            String selected = selectedValue;
            if (!(selected.equals("0") || selected.equals("1"))) {
                selected = "Expression.evaluate(" + replaceQuotes(selectedValue) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg, currentSelection, null,getEvaluationParams()).value";
            }
            // Created condition statement if condition is given!
            String conditional = (optionCondition != null && !optionCondition.equals("")) ? "if (Condition.evaluate(" + replaceQuotes(optionCondition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access))\n" : "";
            optionItems.append(conditional + "p.addSelection(NavajoFactory.getInstance().createSelection(access.getOutputDoc(), \"" + optionName + "\", \"" + optionValue + "\", " + selected + "));\n");
        } else if (children.item(i).getNodeName().equals("map")) {
            // property!!!
            if (!canBeSubMapped) {
                throw new ParseException("This property can not be submapped: " + propertyName);
            }
            if (!type.equals("selection")) {
                throw new ParseException("Only selection properties can be submapped: " + propertyName);
            }
            mapNode = (Element) children.item(i);
            isMapped = true;
            isSelection = true;
        } else if (children.item(i) instanceof Element) {
            String tagValue = "<" + n.getNodeName() + " name=\"" + propertyName + "\">";
            throw new ParseException("Illegal child tag <" + children.item(i).getNodeName() + "> in " + tagValue + " (Check your script) ");
        }
    }
    if (!hasChildren || isSelection) {
        if (!isSelection) {
            result.append(printIdent(ident) + "sValue = new StringLiteral(\"" + value + "\");\n");
            result.append(printIdent(ident) + "matchingConditions = true;\n");
        } else {
            result.append(printIdent(ident) + "sValue = new String(\"" + value + "\");\n");
        }
        result.append(printIdent(ident) + "type = \"" + type + "\";\n");
    } else {
        if (!Property.EXPRESSION_LITERAL_PROPERTY.equals(type) && !Property.EXPRESSION_PROPERTY.equals(type)) {
            result.append(printIdent(ident) + "type = (sValue != null) ? MappingUtils.determineNavajoType(sValue) : \"" + type + "\";\n");
        } else {
            result.append(printIdent(ident) + "type = \"" + type + "\";\n");
        }
    }
    result.append(printIdent(ident) + "subtype = \"" + subtype + "\";\n");
    if (n.getNodeName().equals("property")) {
        result.append(printIdent(ident) + "p = MappingUtils.setProperty(false, currentOutMsg, \"" + propertyName + "\", sValue, type, subtype, \"" + direction + "\", \"" + description + "\", " + length + ", access.getOutputDoc(), access.getInDoc(), !matchingConditions);\n");
        if (!"".equals(key)) {
            result.append(printIdent(ident) + "p.setKey(\"" + key + "\");\n");
        }
        if (!"".equals(reference)) {
            result.append(printIdent(ident) + "p.setReference(\"" + reference + "\");\n");
        }
        if (!"".equals(extendsProp)) {
            result.append(printIdent(ident) + "p.setExtends(\"" + extendsProp + "\");\n");
        }
        if (!"".equals(bindProp)) {
            result.append(printIdent(ident) + "p.setBind(\"" + bindProp + "\");\n");
        }
        result.append(printIdent(ident) + "p.setMethod(\"" + methodProp + "\");\n");
    } else {
        // parameter
        result.append(printIdent(ident) + "MappingUtils.setProperty(true, currentParamMsg, \"" + propertyName + "\", sValue, type, subtype, \"" + direction + "\", \"" + description + "\", " + length + ", access.getOutputDoc(), access.getInDoc(), !matchingConditions);\n");
    }
    if (isMapped) {
        try {
            localContextClass = Class.forName(className, false, loader);
        } catch (Exception e) {
            throw new ParseException("Could not find adapter: " + className);
        }
        addDependency("dependentObjects.add( new JavaDependency( -1, \"" + className + "\"));\n", "JAVA" + className);
        if (mapNode == null) {
            throw new IllegalStateException("Unexpected null mapNode");
        }
        String ref = mapNode.getAttribute("ref");
        if (ref != null && !"".equals(ref)) {
            // replace $ for refs.
            ref = ref.replaceAll("\\$", "");
        }
        String filter = mapNode.getAttribute("filter");
        String mappableArrayName = "mappableObject" + (objectCounter++);
        result.append(printIdent(ident + 2) + mappableArrayName + " = " + objectName + ".get" + ((ref.charAt(0) + "").toUpperCase() + ref.substring(1)) + "();\n");
        String mappableArrayDefinition = "Object [] " + mappableArrayName + " = null;\n";
        variableClipboard.add(mappableArrayDefinition);
        result.append(printIdent(ident + 2) + "for (int i" + (ident + 2) + " = 0; i" + (ident + 2) + " < " + mappableArrayName + ".length; i" + (ident + 2) + "++) {\n if (!kill) {\n");
        result.append(printIdent(ident + 4) + "treeNodeStack.push(currentMap);\n");
        result.append(printIdent(ident + 4) + "currentMap = new MappableTreeNode(access, currentMap, " + mappableArrayName + "[i" + (ident + 2) + "], true);\n");
        result.append(printIdent(ident + 4) + "currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
        // If filter is specified, evaluate filter first (31/1/2007)
        if (!filter.equals("")) {
            result.append(printIdent(ident + 4) + "if (Condition.evaluate(" + replaceQuotes(filter) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) {\n");
            ident += 2;
        }
        result.append(printIdent(ident + 4) + "String optionName = \"\";\n");
        result.append(printIdent(ident + 4) + "String optionValue = \"\";\n");
        result.append(printIdent(ident + 4) + "boolean optionSelected = false;\n");
        children = mapNode.getChildNodes();
        String subClassName = MappingUtils.getFieldType(localContextClass, ref);
        String subClassObjectName = "mappableObject" + (objectCounter++);
        result.append(printIdent(ident + 4) + subClassObjectName + " = (" + subClassName + ") currentMap.myObject;\n");
        String objectDefinition = subClassName + " " + subClassObjectName + " = null;\n";
        variableClipboard.add(objectDefinition);
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getNodeName().equals("property")) {
                Element elt = (Element) children.item(i);
                String subPropertyName = elt.getAttribute("name");
                if (!(subPropertyName.equals("name") || subPropertyName.equals("value") || subPropertyName.equals("selected"))) {
                    throw new ParseException("Only 'name' or 'value' named properties expected when submapping a 'selection' property");
                }
                NodeList expressions = elt.getChildNodes();
                int leftOver = countNodes(expressions, "expression");
                for (int j = 0; j < expressions.getLength(); j++) {
                    if ((expressions.item(j) instanceof Element) && expressions.item(j).getNodeName().equals("expression")) {
                        result.append(expressionNode(ident + 4, (Element) expressions.item(j), --leftOver, subClassName, subClassObjectName));
                    }
                }
                if (subPropertyName.equals("name")) {
                    result.append(printIdent(ident + 4) + "optionName = (sValue != null) ? sValue + \"\" : \"\";\n");
                } else if (subPropertyName.equals("value")) {
                    result.append(printIdent(ident + 4) + "optionValue = (sValue != null) ? sValue + \"\" : \"\";\n");
                } else {
                    result.append(printIdent(ident + 4) + "optionSelected = (sValue != null) ? ((Boolean) sValue).booleanValue() : false;\n");
                }
            } else if (children.item(i).getNodeName().equals("debug")) {
                result.append(debugNode(ident, (Element) children.item(i)));
            } else if (children.item(i).getNodeName().equals("param")) {
                result.append(propertyNode(ident, (Element) children.item(i), false, className, objectName));
            } else if (children.item(i) instanceof Element) {
                throw new ParseException("<property> tag expected while sub-mapping a selection property: " + children.item(i).getNodeName());
            }
        }
        result.append(printIdent(ident + 4) + "p.addSelection(NavajoFactory.getInstance().createSelection(access.getOutputDoc(), optionName, optionValue, optionSelected));\n");
        // If filter is specified add closing bracket (31/1/2007)
        if (!filter.equals("")) {
            ident -= 2;
            result.append(printIdent(ident + 4) + "}\n");
        }
        result.append(printIdent(ident + 4) + "currentMap.setEndtime();\ncurrentMap = (MappableTreeNode) treeNodeStack.pop();\n");
        result.append(printIdent(ident + 2) + "}\n} // EOF Array map result to property\n");
    }
    if (isSelection) {
        // Set selection property stuff.
        result.append(optionItems.toString());
    }
    if (n.getNodeName().equals("property")) {
        result.append("p.setCardinality(\"" + cardinality + "\");\n");
    }
    if (conditionClause) {
        ident -= 2;
        result.append(printIdent(ident) + "} // EOF property condition \n");
    }
    return result.toString();
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ParseException(com.dexels.navajo.parser.compiled.ParseException) 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 2 with UserException

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

the class TslCompiler method includeNode.

/**
 * Resolve include nodes in the script: <include
 * script="[name of script to be included]"/>
 *
 * @param scriptPath
 * @param n
 * @param parent
 * @param deps
 * @throws MetaCompileException
 * @throws IOException
 * @throws KeywordException
 * @throws ClassNotFoundException
 * @throws Exception
 */
private final void includeNode(String scriptPath, Node n, Document parent, String tenant, List<Dependency> deps) throws UserException, ClassNotFoundException, KeywordException, IOException, MetaCompileException {
    included++;
    if (included > 1000) {
        throw new UserException(-1, "Too many included scripts!!!");
    }
    String script = ((Element) n).getAttribute("script");
    if (script == null || script.equals("")) {
        throw new UserException(-1, "No script name found in include tag (" + "missing or empty script attribute): " + n);
    }
    // Construct scriptName:
    // First try if applicationGroup specific script exists.
    String fileName = script + "_" + tenant;
    Document includeDoc = null;
    String includeFileName = fetchScriptFileName(scriptPath + "/" + fileName);
    File includedFile = null;
    if (includeFileName != null) {
        includedFile = new File(includeFileName);
        includeDoc = XMLDocumentUtils.createDocument(new FileInputStream(includeFileName), false);
    } else {
        // no tenant specific include found. Try non-tenant include instead.
        fileName = script;
        includeFileName = fetchScriptFileName(scriptPath + "/" + fileName);
        if (includeFileName != null) {
            includedFile = new File(includeFileName);
            if (includeFileName.endsWith(".ns")) {
                // It's an NS3 based script
                NS3ToNSXML nstoxml = new NS3ToNSXML();
                nstoxml.initialize();
                try {
                    String content = nstoxml.read(includeFileName);
                    String tslResult = MapMetaData.getInstance().parse(scriptPath + "/" + fileName + ".ns", nstoxml.parseNavascript(content));
                    includeDoc = XMLDocumentUtils.createDocument(new ByteArrayInputStream(tslResult.getBytes()), false);
                } catch (Exception e) {
                    throw new UserException(e.getLocalizedMessage(), e);
                }
            } else {
                // It's an XML based script
                includeDoc = XMLDocumentUtils.createDocument(new FileInputStream(includedFile), false);
            }
        }
    }
    if (includedFile == null) {
        logger.error("Could not file include file: {}", script);
        throw new UserException("Could not find include file for script: " + script);
    }
    // Add dependency.
    addDependency("dependentObjects.add( new IncludeDependency( Long.valueOf(\"" + IncludeDependency.getFileTimeStamp(includedFile) + "\"), \"" + fileName + "\"));\n", "INCLUDE" + script);
    deps.add(new IncludeDependency(IncludeDependency.getFileTimeStamp(includedFile), fileName, fileName));
    if (includeDoc.getElementsByTagName("tsl").item(0) == null) {
        // Maybe
        // it is
        // navascript??
        String tslResult = MapMetaData.getInstance().parse(scriptPath + "/" + fileName + ".xml");
        includeDoc = XMLDocumentUtils.createDocument(new ByteArrayInputStream(tslResult.getBytes()), false);
    }
    NodeList content = includeDoc.getElementsByTagName("tsl").item(0).getChildNodes();
    Node nextNode = n.getNextSibling();
    while (nextNode != null && !(nextNode instanceof Element)) {
        nextNode = nextNode.getNextSibling();
    }
    if (nextNode == null || !(nextNode instanceof Element)) {
        nextNode = n;
    }
    Node parentNode = nextNode.getParentNode();
    for (int i = 0; i < content.getLength(); i++) {
        Node child = content.item(i);
        Node imported = parent.importNode(child.cloneNode(true), true);
        parentNode.insertBefore(imported, nextNode);
    }
    parentNode.removeChild(n);
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) 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) ByteArrayInputStream(java.io.ByteArrayInputStream) IncludeDependency(com.dexels.navajo.mapping.compiler.meta.IncludeDependency) NS3ToNSXML(com.dexels.navajo.mapping.compiler.navascript.NS3ToNSXML) UserException(com.dexels.navajo.script.api.UserException) File(java.io.File)

Example 3 with UserException

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

the class TslCompiler method mapNode.

public String mapNode(int ident, Element n, List<Dependency> deps, String tenant) throws ParseException, ClassNotFoundException, KeywordException, UserException, IOException, MetaCompileException, MappingException {
    StringBuilder result = new StringBuilder();
    String object = n.getAttribute("object");
    String condition = n.getAttribute("condition");
    // If name, is specified it could be an AsyncMap.
    String name = n.getAttribute("name");
    boolean asyncMap = false;
    condition = (condition == null) ? "" : condition;
    boolean conditionClause = false;
    if (!condition.equals("")) {
        conditionClause = true;
        result.append(printIdent(ident) + "if (Condition.evaluate(" + replaceQuotes(condition) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg,access)) { \n");
        ident += 2;
    }
    String className = object;
    if (className.equals("")) {
        throw new ParseException("Error in reading Map xml - found map with empty object! Line " + n.getAttribute("linenr") + ":" + n.getAttribute("startoffset"));
    }
    if (contextClass != null) {
        contextClassStack.push(contextClass);
    }
    contextClass = Class.forName(className, false, loader);
    addDependency("dependentObjects.add( new JavaDependency( -1, \"" + className + "\"));\n", "JAVA" + className);
    if (!name.equals("")) {
        // We have a potential async mappable object.
        if (contextClass.getSuperclass().getName().equals("com.dexels.navajo.mapping.AsyncMappable")) {
            asyncMap = true;
        } else {
            asyncMap = false;
        }
    }
    if (asyncMap) {
        String aoName = "ao" + asyncMapCounter;
        String headerName = "h" + asyncMapCounter;
        String asyncMapFinishedName = "asyncMapFinished" + asyncMapCounter;
        String callbackRefName = "callbackRef" + asyncMapCounter;
        String interruptTypeName = "interruptType" + asyncMapCounter;
        String asyncMapName = "asyncMap" + asyncMapCounter;
        String asyncStatusName = "asyncStatus" + asyncMapCounter;
        String resumeAsyncName = "resumeAsync" + asyncMapCounter;
        asyncMapCounter++;
        variableClipboard.add("boolean " + asyncMapName + ";\n");
        variableClipboard.add("Header " + headerName + ";\n");
        variableClipboard.add("String " + callbackRefName + ";\n");
        variableClipboard.add(className + " " + aoName + ";\n");
        variableClipboard.add("boolean " + asyncMapFinishedName + ";\n");
        variableClipboard.add("boolean " + resumeAsyncName + ";\n");
        variableClipboard.add("String " + asyncStatusName + ";\n");
        variableClipboard.add("String " + interruptTypeName + ";\n");
        result.append(printIdent(ident) + asyncMapName + " = true;\n");
        result.append(printIdent(ident) + headerName + " = access.getInDoc().getHeader();\n");
        result.append(printIdent(ident) + callbackRefName + " = " + headerName + ".getCallBackPointer(\"" + name + "\");\n");
        result.append(printIdent(ident) + aoName + " = null;\n");
        result.append(printIdent(ident) + asyncMapFinishedName + " = false;\n");
        result.append(printIdent(ident) + resumeAsyncName + " = false;\n");
        result.append(printIdent(ident) + asyncStatusName + " = \"request\";\n\n");
        result.append(printIdent(ident) + "if (" + callbackRefName + " != null) {\n");
        ident += 2;
        result.append(printIdent(ident) + aoName + " = (" + className + ") DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().getInstance(" + callbackRefName + ");\n");
        result.append(printIdent(ident) + interruptTypeName + " = " + headerName + ".getCallBackInterupt(\"" + name + "\");\n");
        result.append(printIdent(ident) + " if (" + aoName + " == null) {\n " + "  throw new UserException( -1, \"Asynchronous object reference instantiation error: no sych instance (perhaps cleaned up?)\");\n}\n");
        result.append(printIdent(ident) + "if (" + interruptTypeName + ".equals(\"kill\")) { // Kill thread upon client request.\n" + "   " + aoName + ".stop();\n" + "   DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().removeInstance(" + callbackRefName + ");\n" + "   return;\n" + "} else if ( " + aoName + ".isKilled() ) " + "{ " + "     DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().removeInstance(" + callbackRefName + ");\n" + "     throw new UserException(-1, " + aoName + ".getException().getMessage()," + aoName + ".getException());\n" + "} else if (" + interruptTypeName + ".equals(\"interrupt\")) {\n" + "   " + aoName + ".interrupt();\n " + "   return;\n" + "} else if (" + interruptTypeName + ".equals(\"resume\")) { " + "  " + aoName + ".resume();\n" + "return;\n" + "}\n");
        ident -= 2;
        result.append(printIdent(ident) + "} else { // New instance!\n");
        result.append(printIdent(ident) + aoName + " = (" + className + ") classLoader.getClass(\"" + object + "\").newInstance();\n" + "  // Call load method for async map in advance:\n" + "  " + aoName + ".load(access);\n" + "  " + callbackRefName + " = DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().addInstance( " + aoName + ", access );\n" + "}\n");
        result.append(printIdent(ident) + "treeNodeStack.push(currentMap);\n");
        result.append(printIdent(ident) + "currentMap = new MappableTreeNode(access, currentMap, " + aoName + ", false);\n");
        result.append(printIdent(ident) + "currentMap.name = \"" + name + "\";\n");
        result.append(printIdent(ident) + "currentMap.ref = " + callbackRefName + ";\n");
        result.append(printIdent(ident) + aoName + ".afterReload(\"" + name + "\", " + callbackRefName + ");\n");
        result.append(printIdent(ident) + "try {\n");
        ident += 2;
        result.append(printIdent(ident) + asyncMapFinishedName + " = " + aoName + ".isFinished(access.getOutputDoc(), access);\n");
        NodeList response = n.getElementsByTagName("response");
        boolean hasResponseNode = false;
        if (response.getLength() > 0) {
            hasResponseNode = true;
        }
        NodeList running = n.getElementsByTagName("running");
        boolean hasRunningNode = false;
        if (running.getLength() > 0) {
            hasRunningNode = true;
        }
        NodeList request = n.getElementsByTagName("request");
        boolean whileRunning = ((Element) response.item(0)).getAttribute("while_running").equals("true");
        result.append(printIdent(ident) + "if (" + asyncMapFinishedName + " || (" + aoName + ".isActivated() && " + hasResponseNode + " && " + whileRunning + ")) {\n");
        result.append(printIdent(ident) + "  " + asyncStatusName + " = \"response\";\n");
        result.append(printIdent(ident) + "  " + aoName + ".beforeResponse(access);\n");
        result.append(printIdent(ident) + "  if (" + aoName + ".isActivated() && " + whileRunning + ") {\n");
        result.append(printIdent(ident) + "     " + resumeAsyncName + " = true;\n");
        result.append(printIdent(ident) + "  }\n");
        result.append(printIdent(ident) + "} else if (!" + aoName + ".isActivated()) {\n");
        result.append(printIdent(ident) + "  " + asyncStatusName + " = \"request\";\n");
        result.append(printIdent(ident) + "} else if (" + hasRunningNode + ") {\n");
        result.append(printIdent(ident) + "  " + asyncStatusName + " = \"running\";\n");
        result.append(printIdent(ident) + "  " + resumeAsyncName + " = true;\n");
        result.append(printIdent(ident) + "}\n");
        NodeList children = null;
        result.append(printIdent(ident) + "if (" + asyncStatusName + ".equals(\"response\")) {\n");
        children = response.item(0).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element) {
                result.append(compile(ident + 2, children.item(i), className, aoName, deps, tenant));
            }
        }
        result.append(printIdent(ident) + "} else if (" + asyncStatusName + ".equals(\"request\")) {\n");
        children = request.item(0).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element) {
                result.append(compile(ident + 2, children.item(i), className, aoName, deps, tenant));
            }
        }
        result.append(printIdent(ident) + "} else if (" + asyncStatusName + ".equals(\"running\")) {\n");
        children = running.item(0).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element) {
                result.append(compile(ident + 2, children.item(i), className, aoName, deps, tenant));
            }
        }
        result.append(printIdent(ident) + "}\n");
        result.append(printIdent(ident) + "if ((currentMap.myObject != null)) {\n");
        result.append(printIdent(ident + 2) + "if (!" + asyncMapFinishedName + ") {\n");
        result.append(printIdent(ident + 4) + "if (" + resumeAsyncName + ") { " + aoName + ".afterResponse(); } else { " + aoName + ".afterRequest(); " + aoName + ".runThread(); }\n");
        result.append(printIdent(ident + 2) + "} else {\n");
        result.append(printIdent(ident + 4) + "MappingUtils.callStoreMethod(currentMap.myObject);\n");
        result.append(printIdent(ident + 4) + "DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().removeInstance(currentMap.ref);\n");
        result.append(printIdent(ident + 2) + "}\n");
        result.append(printIdent(ident) + "}\n");
        result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
        result.append(printIdent(ident) + " MappingUtils.callKillOrStoreMethod(currentMap.myObject, e" + ident + ");\n");
        result.append(printIdent(ident) + " DispatcherFactory.getInstance().getNavajoConfig().getAsyncStore().removeInstance(currentMap.ref);\n");
        result.append(printIdent(ident) + "  throw e" + ident + ";\n");
        result.append(printIdent(ident) + "}\n");
    } else {
        result.append(printIdent(ident) + "treeNodeStack.push(currentMap);\n");
        result.append(printIdent(ident) + "currentMap = new MappableTreeNode(access, currentMap, classLoader.getClass(\"" + object + "\").newInstance(), false);\n");
        result.append("currentMap.setNavajoLineNr(" + n.getAttribute("linenr") + ");\n");
        n.getAttribute("navajoScript");
        String objectName = "mappableObject" + (objectCounter++);
        result.append(printIdent(ident) + objectName + " = (" + className + ") currentMap.myObject;\n");
        boolean objectMappable = false;
        try {
            objectMappable = MappingUtils.isObjectMappable(className);
        } catch (UserException e) {
            objectMappable = MappingUtils.isObjectMappable(className, loader);
        }
        if (objectMappable) {
            result.append(printIdent(ident) + objectName + ".load(access);\n");
        }
        String objectDefinition = className + " " + objectName + " = null;\n";
        variableClipboard.add(objectDefinition);
        result.append(printIdent(ident) + "try {\n");
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            result.append(compile(ident + 2, children.item(i), className, objectName, deps, tenant));
        }
        result.append(printIdent(ident) + "} catch (Exception e" + ident + ") {\n");
        result.append(printIdent(ident) + "MappingUtils.callKillOrStoreMethod( " + objectName + ", e" + ident + ");\n");
        result.append(printIdent(ident) + "  throw e" + ident + ";\n");
        result.append(printIdent(ident) + "}\n");
        result.append(printIdent(ident) + "MappingUtils.callStoreMethod(" + objectName + ");\n");
        result.append(printIdent(ident) + "currentMap.setEndtime();\ncurrentMap = (MappableTreeNode) treeNodeStack.pop();\n");
    }
    if (conditionClause) {
        ident -= 2;
        result.append(printIdent(ident) + "} // EOF map condition \n");
    }
    if (!contextClassStack.isEmpty()) {
        contextClass = contextClassStack.pop();
    } else {
        contextClass = null;
    }
    return result.toString();
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ParseException(com.dexels.navajo.parser.compiled.ParseException) UserException(com.dexels.navajo.script.api.UserException)

Example 4 with UserException

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

the class MappingUtils method isObjectMappable.

public static final boolean isObjectMappable(String className, ClassLoader cls) throws UserException {
    try {
        Class c = Class.forName(className, true, cls);
        Class mappable = Class.forName("com.dexels.navajo.script.api.Mappable", true, cls);
        return (mappable.isAssignableFrom(c));
    } catch (Throwable e) {
        throw new UserException(-1, "Could not handle class as either mappable or POJO bean: " + className + ", cause: " + e.getMessage(), e);
    }
}
Also used : UserException(com.dexels.navajo.script.api.UserException)

Example 5 with UserException

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

the class MappingUtils method getAttributeObject.

private static final Object getAttributeObject(MappableTreeNode o, String name, Object[] arguments) throws UserException, MappingException {
    Object result = null;
    String methodName = "";
    try {
        java.lang.reflect.Method m = o.getMethodReference(name, arguments);
        result = m.invoke(o.myObject, arguments);
    } catch (IllegalAccessException iae) {
        throw new MappingException(methodName + " illegally accessed in mappable class: " + o.myObject.getClass().getName());
    } catch (InvocationTargetException ite) {
        Throwable t = ite.getTargetException();
        if (t instanceof UserException) {
            throw (UserException) t;
        } else {
            throw new MappingException("Error getting attribute: " + name + " of object: " + o, ite);
        }
    }
    return result;
}
Also used : UserException(com.dexels.navajo.script.api.UserException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) MappingException(com.dexels.navajo.script.api.MappingException)

Aggregations

UserException (com.dexels.navajo.script.api.UserException)113 MappableException (com.dexels.navajo.script.api.MappableException)54 IOException (java.io.IOException)33 NavajoException (com.dexels.navajo.document.NavajoException)25 Message (com.dexels.navajo.document.Message)22 SQLException (java.sql.SQLException)19 SystemException (com.dexels.navajo.script.api.SystemException)18 Binary (com.dexels.navajo.document.types.Binary)14 ConditionErrorException (com.dexels.navajo.server.ConditionErrorException)13 Property (com.dexels.navajo.document.Property)12 ArrayList (java.util.ArrayList)12 Navajo (com.dexels.navajo.document.Navajo)11 AuthorizationException (com.dexels.navajo.script.api.AuthorizationException)11 ResultSet (java.sql.ResultSet)11 MappingException (com.dexels.navajo.script.api.MappingException)10 ResultSetMetaData (java.sql.ResultSetMetaData)9 Element (org.w3c.dom.Element)8 CompilationException (com.dexels.navajo.script.api.CompilationException)7 File (java.io.File)7 NodeList (org.w3c.dom.NodeList)7