]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java
Truncate size of the binary file
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / BlockingAsyncProcedure.java
1 /*******************************************************************************
2  * Copyright (c) 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl;
13
14 import org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.common.utils.Logger;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.AsyncBarrierImpl;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20
21 public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
22
23     private static final Object NO_RESULT = new Object();
24
25     private final Object key;
26     private final AsyncBarrierImpl barrier;
27     private final ReadGraphImpl procedureGraph;
28     private final AsyncProcedure<Result> procedure;
29
30     private Object result = NO_RESULT;
31     private Throwable exception = null;
32
33     public BlockingAsyncProcedure(AsyncBarrierImpl barrier, ReadGraphImpl procedureGraph, AsyncProcedure<Result> procedure, Object key) {
34         this.procedure = procedure;
35         this.key = key;
36         this.barrier = barrier;
37         this.barrier.inc();
38         this.procedureGraph = procedureGraph;
39     }
40
41     @Override
42     public void execute(AsyncReadGraph graph_, Result result) {
43         this.result = result;
44         try {
45             if(procedure != null) procedure.execute(procedureGraph, result);
46         } catch (Throwable throwable) {
47             Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
48         } finally {
49             barrier.dec();
50         }
51     }
52
53     @Override
54     public void exception(AsyncReadGraph graph_, Throwable t) {
55         this.exception = t;
56         try {
57             if(procedure != null) procedure.exception(procedureGraph, t);
58         } catch (Throwable throwable) {
59             Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
60         } finally {
61             barrier.dec();
62         }
63     }
64
65     public void waitBarrier() {
66         barrier.waitBarrier(key, procedureGraph);
67     }
68
69     @SuppressWarnings("unchecked")
70     public Result get() throws DatabaseException {
71
72         barrier.waitBarrier(key, procedureGraph);
73
74         if(exception != null) {
75             if(exception instanceof DatabaseException) throw (DatabaseException)exception;
76             throw new DatabaseException(exception);
77         } else {
78             return (Result)result;
79         }
80
81     }
82
83     public boolean isDone() {
84         return barrier.get() == 0;
85     }
86
87     @SuppressWarnings("unchecked")
88     public Result getResult() {
89         return (Result)result;
90     }
91
92     public Throwable getException() {
93         return exception;
94     }
95
96     @Override
97     public String toString() {
98         return "." + procedure; 
99     }
100
101 }