1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl;
14 import java.text.DecimalFormat;
15 import java.util.concurrent.TimeUnit;
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.impl.graph.AsyncBarrierImpl;
21 import org.simantics.db.impl.graph.ReadGraphImpl;
22 import org.simantics.db.procedure.AsyncProcedure;
24 public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
26 final private Object key;
27 final private ReadGraphImpl graph;
28 final private AsyncProcedure<Result> procedure;
30 private Result result = null;
31 private Throwable exception = null;
33 public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
34 this.procedure = procedure;
36 this.graph = ReadGraphImpl.newAsync(graph);
37 this.graph.asyncBarrier.inc();
41 public void execute(AsyncReadGraph graph, Result result) {
43 this.graph.asyncBarrier.dec();
45 if(procedure != null) procedure.execute(graph, result);
46 } catch (Throwable throwable) {
47 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
52 public void exception(AsyncReadGraph graph, Throwable t) {
55 if(procedure != null) procedure.exception(graph, t);
56 } catch (Throwable throwable) {
57 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
60 this.graph.asyncBarrier.dec();
63 public Result get() throws DatabaseException {
65 graph.asyncBarrier.waitBarrier(key, graph);
67 if(exception != null) {
68 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
69 throw new DatabaseException(exception);
76 public Result getResult() {
80 public Throwable getException() {
85 public String toString() {
86 return "." + procedure;