]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java
ad9f1643829fe506ac57125edaf2bc724a249d05
[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.ReadGraphImpl;
18 import org.simantics.db.procedure.AsyncProcedure;
19
20 public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
21
22     private static final Object NO_RESULT = new Object();
23
24     private final Object key;
25     private final ReadGraphImpl graph;
26     private final AsyncProcedure<Result> procedure;
27
28     private Object result = NO_RESULT;
29     private Throwable exception = null;
30
31     public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
32         this.procedure = procedure;
33         this.key = key;
34         this.graph = graph;
35         this.graph.asyncBarrier.inc();
36     }
37
38     @Override
39     public void execute(AsyncReadGraph graph, Result result) {
40         this.result = result;
41         this.graph.asyncBarrier.dec();
42         try {
43             if(procedure != null) procedure.execute(graph, result);
44         } catch (Throwable throwable) {
45             Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
46         }
47     }
48
49     @Override
50     public void exception(AsyncReadGraph graph, Throwable t) {
51         this.exception = t;
52         try {
53             if(procedure != null) procedure.exception(graph, t);
54         } catch (Throwable throwable) {
55             Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
56         } finally {
57             this.graph.asyncBarrier.dec();
58         }
59     }
60
61     public void waitBarrier() {
62         graph.asyncBarrier.waitBarrier(key, graph);
63     }
64
65     @SuppressWarnings("unchecked")
66     public Result get() throws DatabaseException {
67
68         graph.asyncBarrier.waitBarrier(key, graph);
69
70         if(exception != null) {
71             if(exception instanceof DatabaseException) throw (DatabaseException)exception;
72             throw new DatabaseException(exception);
73         } else {
74             return (Result)result;
75         }
76
77     }
78
79     public boolean isDone() {
80         return graph.asyncBarrier.get() == 0;
81     }
82
83     @SuppressWarnings("unchecked")
84     public Result getResult() {
85         return (Result)result;
86     }
87
88     public Throwable getException() {
89         return exception;
90     }
91
92     @Override
93     public String toString() {
94         return "." + procedure; 
95     }
96
97 }