Search in sources :

Example 6 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) throws UserException {
    try {
        DispatcherInterface instance = DispatcherFactory.getInstance();
        Class c = null;
        if (instance == null) {
            c = Class.forName(className);
        } else {
            c = Class.forName(className, true, instance.getNavajoConfig().getClassloader());
        }
        return (Mappable.class.isAssignableFrom(c));
    } catch (Exception e) {
        throw new UserException(-1, "Could not handle class as either mappable or POJO bean: " + className + ", cause: " + e.getMessage(), e);
    }
}
Also used : Mappable(com.dexels.navajo.script.api.Mappable) DispatcherInterface(com.dexels.navajo.server.DispatcherInterface) UserException(com.dexels.navajo.script.api.UserException) MappingException(com.dexels.navajo.script.api.MappingException) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SystemException(com.dexels.navajo.script.api.SystemException)

Example 7 with UserException

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

the class DomainObjectMapper method setAttributeValue.

public void setAttributeValue(Object value) throws UserException {
    if (attributeName == null) {
        throw new UserException(-1, "Set attribute name before setting its value.");
    }
    try {
        createObject();
        setDomainObjectAttribute(attributeName, value);
    } catch (Exception e) {
        throw new UserException(-1, e.getMessage(), e);
    }
}
Also used : UserException(com.dexels.navajo.script.api.UserException) MappingException(com.dexels.navajo.script.api.MappingException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Example 8 with UserException

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

the class TslCompiler method optimizeExpresssion.

/**
 * VERY NASTY METHOD. IT TRIES ALL KINDS OF TRICKS TO TRY TO AVOID CALLING
 * THE EXPRESSION.EVALUATE() METHOD IN THE GENERATED JAVA.
 *
 * @param ident
 * @param clause
 * @param className
 * @return
 */
public String optimizeExpresssion(int ident, String clause, String className, String objectName) throws UserException {
    boolean exact = false;
    StringBuilder result = new StringBuilder();
    char firstChar = ' ';
    boolean functionCall = false;
    StringBuilder functionNameBuffer = new StringBuilder();
    String functionName = "";
    String call = "";
    // attribute call.
    for (int i = 0; i < clause.length(); i++) {
        char c = clause.charAt(i);
        if (c != ' ' && firstChar == ' ') {
            firstChar = c;
        }
        if (((firstChar > 'a' && firstChar < 'z')) || ((firstChar > 'A') && (firstChar < 'Z'))) {
            functionCall = true;
        }
        if ((functionCall) && (c != '(')) {
            functionNameBuffer.append(c);
        } else if (functionCall && c == '(') {
            functionName = functionNameBuffer.toString();
            functionNameBuffer = new StringBuilder();
        }
        if (c == '$') {
            // New attribute found
            StringBuilder name = new StringBuilder();
            i++;
            c = clause.charAt(i);
            while (c != '(' && i < clause.length() && c != ')') {
                name.append(c);
                i++;
                if (i < clause.length()) {
                    c = clause.charAt(i);
                }
            }
            if (name.toString().contains("..")) {
                // We cannot optimize these yet
                continue;
            }
            i++;
            StringBuilder params = new StringBuilder();
            if (clause.indexOf("(") != -1) {
                // Determine parameters.
                int endOfParams = 1;
                while (endOfParams > 0 && i < clause.length()) {
                    c = clause.charAt(i);
                    if (c == '(') {
                        endOfParams++;
                    } else if (c == ')') {
                        endOfParams--;
                    } else {
                        params.append(c);
                    }
                    i++;
                }
            }
            String expr = "";
            if (functionName.equals("")) {
                expr = (params.toString().length() > 0 ? "$" + name + "(" + params + ")" : "$" + name);
            } else {
                expr = functionName + "(" + (params.toString().length() > 0 ? "$" + name + "(" + params + ")" : "$" + name) + ")";
            }
            if (removeWhiteSpaces(expr).equals(removeWhiteSpaces(clause))) {
                // Let's evaluate this directly.
                exact = true;
                Class expressionContextClass = null;
                try {
                    StringBuilder objectizedParams = new StringBuilder();
                    StringTokenizer allParams = new StringTokenizer(params.toString(), ",");
                    while (allParams.hasMoreElements()) {
                        String param = allParams.nextToken();
                        // Try to evaluate expression (NOTE THAT IF
                        // REFERENCES ARE MADE TO EITHER NAVAJO OR MAPPABLE
                        // OBJECTS THIS WILL FAIL
                        // SINCE THESE OBJECTS ARE NOT KNOWN AT COMPILE
                        // TIME!!!!!!!!!!!!!!1
                        Operand op = Expression.evaluate(param, null);
                        Object v = op.value;
                        if (v instanceof String) {
                            objectizedParams.append("\"" + v + "\"");
                        } else if (v instanceof Integer) {
                            objectizedParams.append("new Integer(" + v + ")");
                        } else if (v instanceof Long) {
                            objectizedParams.append("Long.valueOf(" + v + ")");
                        } else if (v instanceof Float) {
                            objectizedParams.append("new Float(" + v + ")");
                        } else if (v instanceof Boolean) {
                            objectizedParams.append("new Boolean(" + v + ")");
                        } else if (v instanceof Double) {
                            objectizedParams.append("Double.valueOf(" + v + ")");
                        } else if (v == null) {
                            // Null support
                            objectizedParams.append(v);
                        } else {
                            throw new UserException(-1, "Unknown type encountered during compile time: " + v.getClass().getName() + " @clause: " + clause);
                        }
                        if (allParams.hasMoreElements()) {
                            objectizedParams.append(',');
                        }
                    }
                    try {
                        expressionContextClass = Class.forName(className, false, loader);
                    } catch (Exception e) {
                        throw new Exception("Could not find adapter: " + className);
                    }
                    String attrType = MappingUtils.getFieldType(expressionContextClass, name.toString());
                    // Try to locate class:
                    if (!functionName.equals("")) {
                        try {
                            Class.forName("com.dexels.navajo.functions." + functionName, false, loader);
                        } catch (Exception e) {
                            throw new Exception("Could not find Navajo function: " + functionName);
                        }
                    }
                    call = objectName + ".get" + (name.charAt(0) + "").toUpperCase() + name.substring(1) + "(" + objectizedParams.toString() + ")";
                    if (attrType.equals("int")) {
                        call = "new Integer(" + call + ")";
                    } else if (attrType.equals("float") || attrType.equals("double")) {
                        call = "Double.valueOf(" + call + ")";
                    } else if (attrType.equals("boolean")) {
                        call = "new Boolean(" + call + ")";
                    } else if (attrType.equals("long")) {
                        call = "Long.valueOf(" + call + ")";
                    }
                } catch (ClassNotFoundException cnfe) {
                    if (expressionContextClass == null) {
                        throw new UserException(-1, "Error in script: Could not find adapter: " + className + " @clause: " + clause);
                    } else {
                        throw new UserException(-1, "Error in script: Could not locate function: " + functionName + " @ clause: " + clause);
                    }
                } catch (Throwable e) {
                    exact = false;
                }
            }
        }
    }
    // Try to evaluate clause directly (compile time).
    if ((!exact) && !clause.equals("TODAY") && !clause.equals("null") && (clause.indexOf("[") == -1) && (clause.indexOf("$") == -1) && (clause.indexOf("(") == -1) && (clause.indexOf("+") == -1)) {
        try {
            Operand op = Expression.evaluate(clause, null);
            Object v = op.value;
            exact = true;
            if (v instanceof String) {
                call = replaceQuotesValue((String) v);
            } else if (v instanceof Integer) {
                call = "new Integer(" + v + ")";
            } else if (v instanceof Long) {
                call = "Long.valueOf(" + v + ")";
            } else if (v instanceof Float) {
                call = "new Float(" + v + ")";
            } else if (v instanceof Boolean) {
                call = "new Boolean(" + v + ")";
            } else if (v instanceof Double) {
                call = "Double.valueOf(" + v + ")";
            } else
                throw new UserException(-1, "Unknown type encountered during compile time: " + v.getClass().getName() + " @clause: " + clause);
        } catch (NullPointerException | TMLExpressionException ne) {
            exact = false;
        } catch (SystemException se) {
            exact = false;
            if (clause.length() == 0 || clause.charAt(0) != '#') {
                throw new UserException(-1, "Could not compile script, Invalid expression: " + clause);
            }
        } catch (Throwable e) {
            exact = false;
        }
    }
    if (!exact && clause.equals("null")) {
        call = "null";
        exact = true;
    }
    // Use Expression.evaluate() if expression could not be executed in an
    // optimized way.
    result.append(printIdent(ident) + "op = Expression.evaluate(" + replaceQuotes(clause) + ", access.getInDoc(), currentMap, currentInMsg, currentParamMsg, currentSelection, null, getEvaluationParams());\n");
    result.append(printIdent(ident) + "sValue = op.value;\n");
    return result.toString();
}
Also used : Operand(com.dexels.navajo.document.Operand) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) 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) StringTokenizer(java.util.StringTokenizer) SystemException(com.dexels.navajo.script.api.SystemException) UserException(com.dexels.navajo.script.api.UserException)

Example 9 with UserException

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

the class AsyncMappable method log.

private final void log() {
    if (DispatcherFactory.getInstance().getNavajoConfig().getStatisticsRunner() != null && !logged) {
        Access a = AsyncStore.getInstance().getAccessObject(this.pointer);
        if (a != null) {
            // determine total time.
            a.setFinished();
            UserException ue = null;
            if (isKilled()) {
                ue = new UserException(-1, "Killed by client");
                if (caught != null) {
                    a.setException(caught);
                } else {
                    a.setException(ue);
                }
            }
            logged = true;
            DispatcherFactory.getInstance().getNavajoConfig().getStatisticsRunner().addAccess(a, this);
        } else {
            AuditLog.log(AuditLog.AUDIT_MESSAGE_ASYNC_RUNNER, "Warning: could not log async access due to missing access object!");
        }
    }
}
Also used : Access(com.dexels.navajo.script.api.Access) UserException(com.dexels.navajo.script.api.UserException)

Example 10 with UserException

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

the class CompiledScript method run.

/*
     * (non-Javadoc)
     * 
     * @see
     * com.dexels.navajo.script.api.CompiledScriptInterface#run(com.dexels.navajo
     * .api.Access)
     */
@Override
public final void run(Access access) throws Exception {
    myAccess = access;
    long start = System.currentTimeMillis();
    try {
        dumpRequest();
        setValidations();
        currentParamMsg = access.getInDoc().getMessage("__parms__");
        ConditionData[] conditions = getValidationRules(access);
        boolean conditionsFailed = false;
        if (conditions != null && conditions.length > 0) {
            Navajo outMessage = access.getOutputDoc();
            Message[] failed = checkValidationRules(conditions, access.getInDoc(), outMessage, access);
            if (failed != null) {
                conditionsFailed = true;
                access.setExitCode(Access.EXIT_VALIDATION_ERR);
                Message msg = NavajoFactory.getInstance().createMessage(outMessage, "ConditionErrors");
                outMessage.addMessage(msg);
                msg.setType(Message.MSG_TYPE_ARRAY);
                for (int i = 0; i < failed.length; i++) {
                    msg.addMessage(failed[i]);
                }
            }
        }
        if (!conditionsFailed) {
            try {
                execute(access);
                access.setExitCode(Access.EXIT_OK);
            } catch (com.dexels.navajo.mapping.BreakEvent be) {
                access.setExitCode(Access.EXIT_BREAK);
                throw be;
            } catch (UserException e) {
                access.setExitCode(Access.EXIT_USEREXCEPTION);
                throw e;
            } catch (ConditionErrorException e) {
                access.setExitCode(Access.EXIT_VALIDATION_ERR);
                throw e;
            } catch (Exception e) {
                access.setExitCode(Access.EXIT_EXCEPTION);
                throw e;
            } finally {
                finalBlock(access);
            }
        }
    } finally {
        dumpResponse();
        // Release acquired locks.
        for (Lock l : acquiredLocks) {
            try {
                l.unlock();
            } catch (Throwable t) {
            }
        }
        acquiredLocks.clear();
        access.processingTime = (int) (System.currentTimeMillis() - start);
    }
}
Also used : ConditionData(com.dexels.navajo.server.ConditionData) Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SystemException(com.dexels.navajo.script.api.SystemException) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) CompilationException(com.dexels.navajo.script.api.CompilationException) Lock(java.util.concurrent.locks.Lock) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) UserException(com.dexels.navajo.script.api.UserException)

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