]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/WriteStateBase.java
Merge "Testing SonarQube with Simantics Platform SDK"
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / WriteStateBase.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import java.util.concurrent.Semaphore;\r
4 \r
5 import org.simantics.db.common.utils.Logger;\r
6 import org.simantics.db.exception.DatabaseException;\r
7 import org.simantics.db.procedure.Procedure;\r
8 import org.simantics.db.request.WriteTraits;\r
9 \r
10 /**\r
11  * @author Tuukka Lehtonen\r
12  *\r
13  * @param <T> result value type\r
14  */\r
15 public class WriteStateBase<T> {\r
16 \r
17     final protected WriteTraits request;\r
18     final protected Semaphore notify;\r
19     final protected Procedure<T> procedure;\r
20 \r
21     protected T result;\r
22     protected Throwable exception;\r
23 \r
24     public WriteStateBase(WriteTraits request, Semaphore notify, Procedure<T> procedure) {\r
25         assert(request != null);\r
26 \r
27         this.request = request;\r
28         this.notify = notify;\r
29         this.procedure = procedure;\r
30     }\r
31 \r
32     public WriteTraits getRequest() {\r
33         return request;\r
34     }\r
35 \r
36     @SuppressWarnings("unchecked")\r
37     public void setResult(Object result) {\r
38         this.result = (T) result;\r
39     }\r
40 \r
41     public void except(Throwable throwable) {\r
42         this.exception = throwable;\r
43     }\r
44 \r
45 //    public void cancel(CancelTransactionException e) {\r
46 //        this.exception = e;\r
47 //    }\r
48 //\r
49 //    public boolean isCanceled() {\r
50 //        return exception instanceof CancelTransactionException;\r
51 //    }\r
52 \r
53     public boolean isExcepted() {\r
54         return exception != null;\r
55     }\r
56 \r
57     public void finish() {\r
58 \r
59         if(procedure != null) {\r
60 \r
61             try {\r
62                 // Callback is client code, we have to be prepared for it to throw unexpected exceptions.\r
63                 // All we can do here is to log those, can't really pass them anywhere.\r
64                 if (procedure != null) {\r
65                     if(isExcepted()) {\r
66                         if(exception instanceof DatabaseException) procedure.exception(exception);\r
67                         else procedure.exception(new DatabaseException(exception));\r
68                     }\r
69                     else procedure.execute(result);\r
70                 }\r
71             } catch (Throwable t) {\r
72                 Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", t);\r
73                 if (SessionImplSocket.DEBUG)\r
74                     t.printStackTrace();\r
75             }\r
76 \r
77         }\r
78 \r
79         if(notify != null) notify.release();\r
80 \r
81     }\r
82 \r
83 }\r