]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/WriteStateBase.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / WriteStateBase.java
diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/WriteStateBase.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/WriteStateBase.java
new file mode 100644 (file)
index 0000000..303e42c
--- /dev/null
@@ -0,0 +1,83 @@
+package fi.vtt.simantics.procore.internal;\r
+\r
+import java.util.concurrent.Semaphore;\r
+\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.Procedure;\r
+import org.simantics.db.request.WriteTraits;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ *\r
+ * @param <T> result value type\r
+ */\r
+public class WriteStateBase<T> {\r
+\r
+    final protected WriteTraits request;\r
+    final protected Semaphore notify;\r
+    final protected Procedure<T> procedure;\r
+\r
+    protected T result;\r
+    protected Throwable exception;\r
+\r
+    public WriteStateBase(WriteTraits request, Semaphore notify, Procedure<T> procedure) {\r
+        assert(request != null);\r
+\r
+        this.request = request;\r
+        this.notify = notify;\r
+        this.procedure = procedure;\r
+    }\r
+\r
+    public WriteTraits getRequest() {\r
+        return request;\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    public void setResult(Object result) {\r
+        this.result = (T) result;\r
+    }\r
+\r
+    public void except(Throwable throwable) {\r
+        this.exception = throwable;\r
+    }\r
+\r
+//    public void cancel(CancelTransactionException e) {\r
+//        this.exception = e;\r
+//    }\r
+//\r
+//    public boolean isCanceled() {\r
+//        return exception instanceof CancelTransactionException;\r
+//    }\r
+\r
+    public boolean isExcepted() {\r
+        return exception != null;\r
+    }\r
+\r
+    public void finish() {\r
+\r
+        if(procedure != null) {\r
+\r
+            try {\r
+                // Callback is client code, we have to be prepared for it to throw unexpected exceptions.\r
+                // All we can do here is to log those, can't really pass them anywhere.\r
+                if (procedure != null) {\r
+                    if(isExcepted()) {\r
+                        if(exception instanceof DatabaseException) procedure.exception(exception);\r
+                        else procedure.exception(new DatabaseException(exception));\r
+                    }\r
+                    else procedure.execute(result);\r
+                }\r
+            } catch (Throwable t) {\r
+                Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", t);\r
+                if (SessionImplSocket.DEBUG)\r
+                    t.printStackTrace();\r
+            }\r
+\r
+        }\r
+\r
+        if(notify != null) notify.release();\r
+\r
+    }\r
+\r
+}\r