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.ReadGraphImpl;
18 import org.simantics.db.procedure.AsyncMultiProcedure;
20 public class BlockingAsyncMultiProcedure<Result> implements AsyncMultiProcedure<Result> {
22 private static final Object NO_RESULT = new Object();
24 private final Object key;
25 private final ReadGraphImpl graph;
26 private final AsyncMultiProcedure<Result> procedure;
28 private Object result = NO_RESULT;
29 private Throwable exception = null;
31 public BlockingAsyncMultiProcedure(ReadGraphImpl graph, AsyncMultiProcedure<Result> procedure, Object key) {
32 this.procedure = procedure;
34 this.graph = ReadGraphImpl.newAsync(graph);
35 this.graph.asyncBarrier.inc();
39 public void execute(AsyncReadGraph graph, Result result) {
42 if(procedure != null) procedure.execute(graph, result);
43 } catch (Throwable throwable) {
44 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
49 public void finished(AsyncReadGraph graph) {
50 this.graph.asyncBarrier.dec();
52 if(procedure != null) procedure.finished(graph);
53 } catch (Throwable throwable) {
54 Logger.defaultLogError("AsyncProcedure.finish threw for " + procedure, throwable);
59 public void exception(AsyncReadGraph graph, Throwable t) {
62 if (procedure != null) procedure.exception(graph, t);
63 } catch (Throwable throwable) {
64 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
66 this.graph.asyncBarrier.dec();
70 @SuppressWarnings("unchecked")
71 public Result get() throws DatabaseException {
72 graph.asyncBarrier.waitBarrier(key, graph);
73 if (exception != null) {
74 if (exception instanceof DatabaseException) throw (DatabaseException) exception;
75 throw new DatabaseException(exception);
77 return (Result) result;
81 @SuppressWarnings("unchecked")
82 public Result getResult() {
83 return (Result) result;
86 public Throwable getException() {
91 public String toString() {
92 return "." + procedure;