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.AsyncMultiProcedure;
21 public class BlockingAsyncMultiProcedure<Result> implements AsyncMultiProcedure<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 AsyncMultiProcedure<Result> procedure;
30 private Object result = NO_RESULT;
31 private Throwable exception = null;
33 public BlockingAsyncMultiProcedure(AsyncBarrierImpl barrier, ReadGraphImpl procedureGraph, AsyncMultiProcedure<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);
52 public void finished(AsyncReadGraph graph) {
54 if(procedure != null) procedure.finished(procedureGraph);
55 } catch (Throwable throwable) {
56 Logger.defaultLogError("AsyncProcedure.finish threw for " + procedure, throwable);
63 public void exception(AsyncReadGraph graph, Throwable t) {
66 if (procedure != null) procedure.exception(procedureGraph, t);
67 } catch (Throwable throwable) {
68 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
74 @SuppressWarnings("unchecked")
75 public Result get() throws DatabaseException {
77 barrier.waitBarrier(key, procedureGraph);
79 if (exception != null) {
80 if (exception instanceof DatabaseException) throw (DatabaseException) exception;
81 throw new DatabaseException(exception);
83 return (Result) result;
88 @SuppressWarnings("unchecked")
89 public Result getResult() {
90 return (Result) result;
93 public Throwable getException() {
98 public String toString() {
99 return "." + procedure;