1 /*******************************************************************************
2 * Copyright (c) 2018 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl;
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;
21 public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
23 private static final Object NO_RESULT = new Object();
25 private final Object key;
26 private final AsyncBarrierImpl barrier;
27 private final ReadGraphImpl procedureGraph;
28 private final AsyncProcedure<Result> procedure;
30 private Object result = NO_RESULT;
31 private Throwable exception = null;
33 public BlockingAsyncProcedure(AsyncBarrierImpl barrier, ReadGraphImpl procedureGraph, AsyncProcedure<Result> procedure, Object key) {
34 this.procedure = procedure;
36 this.barrier = barrier;
38 this.procedureGraph = procedureGraph;
42 public void execute(AsyncReadGraph graph_, Result result) {
45 if(procedure != null) procedure.execute(procedureGraph, result);
46 } catch (Throwable throwable) {
47 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
54 public void exception(AsyncReadGraph graph_, Throwable t) {
57 if(procedure != null) procedure.exception(procedureGraph, t);
58 } catch (Throwable throwable) {
59 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
65 public void waitBarrier() {
66 barrier.waitBarrier(key, procedureGraph);
69 @SuppressWarnings("unchecked")
70 public Result get() throws DatabaseException {
72 barrier.waitBarrier(key, procedureGraph);
74 if(exception != null) {
75 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
76 throw new DatabaseException(exception);
78 return (Result)result;
83 public boolean isDone() {
84 return barrier.get() == 0;
87 @SuppressWarnings("unchecked")
88 public Result getResult() {
89 return (Result)result;
92 public Throwable getException() {
97 public String toString() {
98 return "." + procedure;