]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncMultiProcedure.java
Merge "Trying to wait for procedures"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / BlockingAsyncMultiProcedure.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.AsyncMultiProcedure;
20
21 public class BlockingAsyncMultiProcedure<Result> implements AsyncMultiProcedure<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 AsyncMultiProcedure<Result> procedure;
29
30     private Object result = NO_RESULT;
31     private Throwable exception = null;
32
33     public BlockingAsyncMultiProcedure(AsyncBarrierImpl barrier, ReadGraphImpl procedureGraph, AsyncMultiProcedure<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         }
49     }
50
51     @Override
52     public void finished(AsyncReadGraph graph) {
53         try {
54             if(procedure != null) procedure.finished(procedureGraph);
55         } catch (Throwable throwable) {
56             Logger.defaultLogError("AsyncProcedure.finish threw for " + procedure, throwable);
57         } finally {
58             barrier.dec();
59         }
60     }
61
62     @Override
63     public void exception(AsyncReadGraph graph, Throwable t) {
64         this.exception = t;
65         try {
66             if (procedure != null) procedure.exception(procedureGraph, t);
67         } catch (Throwable throwable) {
68             Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
69         } finally {
70             barrier.dec();
71         }
72     }
73
74     @SuppressWarnings("unchecked")
75     public Result get() throws DatabaseException {
76         
77         barrier.waitBarrier(key, procedureGraph);
78         
79         if (exception != null) {
80             if (exception instanceof DatabaseException) throw (DatabaseException) exception;
81             throw new DatabaseException(exception);
82         } else {
83             return (Result) result;
84         }
85         
86     }
87
88     @SuppressWarnings("unchecked")
89     public Result getResult() {
90         return (Result) result;
91     }
92
93     public Throwable getException() {
94         return exception;
95     }
96
97     @Override
98     public String toString() {
99         return "." + procedure; 
100     }
101
102 }