1 package org.simantics.db.common.utils;
3 import java.lang.reflect.InvocationTargetException;
5 import org.simantics.db.RequestProcessor;
6 import org.simantics.db.Resource;
7 import org.simantics.db.common.request.AdaptValue;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.scl.runtime.function.Function;
11 public class Functions {
13 private static DatabaseException findPossibleRootException(Throwable t) {
14 if(t == null) return null;
15 if(t instanceof DatabaseException) return (DatabaseException)t;
16 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
17 return findPossibleRootException(t.getCause());
22 public static <T> T exec(RequestProcessor rp, Resource function, Object ... parameters) throws DatabaseException {
23 Object op = rp.syncRequest(new AdaptValue(function));
25 return (T) ((Function)op).applyArray(parameters);
26 } catch (RuntimeException e) {
27 DatabaseException dte = findPossibleRootException(e);
28 if(dte != null) throw dte;
29 else throw new DatabaseException(e);