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 static Object NO_RESULT = new Object();
28 final private Object key;
29 final private ReadGraphImpl graph;
30 final private AsyncProcedure<Result> procedure;
32 private Object result = NO_RESULT;
33 private Throwable exception = null;
35 public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
36 this.procedure = procedure;
38 this.graph = ReadGraphImpl.newAsync(graph);
39 this.graph.asyncBarrier.inc();
43 public void execute(AsyncReadGraph graph, Result result) {
45 this.graph.asyncBarrier.dec();
47 if(procedure != null) procedure.execute(graph, result);
48 } catch (Throwable throwable) {
49 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
54 public void exception(AsyncReadGraph graph, Throwable t) {
57 if(procedure != null) procedure.exception(graph, t);
58 } catch (Throwable throwable) {
59 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
62 this.graph.asyncBarrier.dec();
65 public Result get() throws DatabaseException {
67 graph.asyncBarrier.waitBarrier(key, graph);
69 if(exception != null) {
70 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
71 throw new DatabaseException(exception);
73 return (Result)result;
78 public Result getResult() {
79 return (Result)result;
82 public Throwable getException() {
87 public String toString() {
88 return "." + procedure;