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);
}
}
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);
}
}
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();
}
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!");
}
}
}
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);
}
}
Aggregations