1 package fi.vtt.simantics.procore.internal;
3 import java.util.concurrent.Semaphore;
5 import org.simantics.db.common.utils.Logger;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.procedure.Procedure;
8 import org.simantics.db.request.WriteTraits;
11 * @author Tuukka Lehtonen
13 * @param <T> result value type
15 public class WriteStateBase<T> {
17 final protected WriteTraits request;
18 final protected Semaphore notify;
19 final protected Procedure<T> procedure;
22 protected Throwable exception;
24 public WriteStateBase(WriteTraits request, Semaphore notify, Procedure<T> procedure) {
25 assert(request != null);
27 this.request = request;
29 this.procedure = procedure;
32 public WriteTraits getRequest() {
36 @SuppressWarnings("unchecked")
37 public void setResult(Object result) {
38 this.result = (T) result;
41 public void except(Throwable throwable) {
42 this.exception = throwable;
45 // public void cancel(CancelTransactionException e) {
46 // this.exception = e;
49 // public boolean isCanceled() {
50 // return exception instanceof CancelTransactionException;
53 public boolean isExcepted() {
54 return exception != null;
57 public void finish() {
59 if(procedure != null) {
62 // Callback is client code, we have to be prepared for it to throw unexpected exceptions.
63 // All we can do here is to log those, can't really pass them anywhere.
64 if (procedure != null) {
66 if(exception instanceof DatabaseException) procedure.exception(exception);
67 else procedure.exception(new DatabaseException(exception));
69 else procedure.execute(result);
71 } catch (Throwable t) {
72 Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", t);
73 if (SessionImplSocket.DEBUG)
79 if(notify != null) notify.release();