]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Functions.java
ListUtils.create(g,elements) creates a list without element inverses
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / Functions.java
1 package org.simantics.db.common.utils;
2
3 import java.lang.reflect.InvocationTargetException;
4
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;
10
11 public class Functions {
12         
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());
18                 }
19                 return null;
20         }
21         
22         public static <T> T exec(RequestProcessor rp, Resource function, Object ... parameters) throws DatabaseException {
23         Object op = rp.syncRequest(new AdaptValue(function));
24         try {
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);
30         }
31         }
32         
33 }