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> {
25 <<<<<<< Upstream, based on branch 'private/antti_threads' of ssh://villberg@gerrit.simantics.org:29418/simantics/platform.git
27 final private Object key;
28 final private ReadGraphImpl graph;
29 final private AsyncProcedure<Result> procedure;
31 private Result result = null;
32 private Throwable exception = null;
34 public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
35 this.procedure = procedure;
37 this.graph = ReadGraphImpl.newAsync(graph);
38 this.graph.asyncBarrier.inc();
42 public void execute(AsyncReadGraph graph, Result result) {
44 this.graph.asyncBarrier.dec();
46 if(procedure != null) procedure.execute(graph, result);
47 } catch (Throwable throwable) {
48 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
53 public void exception(AsyncReadGraph graph, Throwable t) {
56 if(procedure != null) procedure.exception(graph, t);
57 } catch (Throwable throwable) {
58 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
61 this.graph.asyncBarrier.dec();
64 public Result get() throws DatabaseException {
66 graph.asyncBarrier.waitBarrier(key, graph);
68 if(exception != null) {
69 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
70 throw new DatabaseException(exception);
77 public Result getResult() {
81 final private static Object NO_RESULT = new Object();
83 final private Object key;
84 final private ReadGraphImpl graph;
85 final private AsyncProcedure<Result> procedure;
87 private Object result = NO_RESULT;
88 private Throwable exception = null;
90 public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
91 this.procedure = procedure;
93 this.graph = ReadGraphImpl.newAsync(graph);
94 this.graph.asyncBarrier.inc();
98 public void execute(AsyncReadGraph graph, Result result) {
100 this.graph.asyncBarrier.dec();
102 if(procedure != null) procedure.execute(graph, result);
103 } catch (Throwable throwable) {
104 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
109 public void exception(AsyncReadGraph graph, Throwable t) {
112 if(procedure != null) procedure.exception(graph, t);
113 } catch (Throwable throwable) {
114 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
117 this.graph.asyncBarrier.dec();
120 public Result get() throws DatabaseException {
122 graph.asyncBarrier.waitBarrier(key, graph);
124 if(exception != null) {
125 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
126 throw new DatabaseException(exception);
128 return (Result)result;
133 public Result getResult() {
134 return (Result)result;
135 >>>>>>> 82fa68e Generate parts of db client query code
138 public Throwable getException() {
143 public String toString() {
144 return "." + procedure;