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.common.procedure;
14 import java.util.concurrent.Semaphore;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.atomic.AtomicBoolean;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.common.utils.Logger;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.procedure.AsyncProcedure;
23 public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
25 final private Object key;
26 private Result result = null;
27 private Throwable exception = null;
28 final private AsyncReadGraph graph;
29 final private AsyncProcedure<Result> procedure;
30 final private Semaphore semaphore = new Semaphore(0);
31 // final private AtomicBoolean latch;
33 public BlockingAsyncProcedure(AsyncReadGraph graph, AsyncProcedure<Result> procedure, Object key) {
34 // assert(procedure != null);
37 this.procedure = procedure;
39 System.err.println("asd");
40 //System.err.println("BlockingAsyncProcedure " + key);
41 // latch = new AtomicBoolean(false);
45 public void execute(AsyncReadGraph graph, Result result) {
48 // if(latch.compareAndSet(false, true)) {
50 if(procedure != null) procedure.execute(graph, result);
51 } catch (Throwable throwable) {
52 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
55 //// System.err.println("ResultCallWrappedSingleQueryProcedure4 dec " + key);
58 // Logger.defaultLogError("Procedure was called many times (this time is execute)");
63 public void exception(AsyncReadGraph graph, Throwable t) {
66 // if(latch.compareAndSet(false, true)) {
68 if(procedure != null) procedure.exception(graph, t);
69 } catch (Throwable throwable) {
70 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
74 // Logger.defaultLogError("Procedure was called many times (this time is exception)");
79 private void waitFor() throws DatabaseException {
81 boolean success = false;
82 success = semaphore.tryAcquire();
87 if(graph.performPending()) {
89 success = semaphore.tryAcquire();
91 // Nothing to do - just wait
93 success = semaphore.tryAcquire(10, TimeUnit.SECONDS);
94 if(!success) throw new DatabaseException("Timeout while waiting for async request to complete: " + key);
95 } catch (InterruptedException e) {
96 throw new DatabaseException(e);
104 public Result get() throws DatabaseException {
108 if(exception != null) {
109 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
110 throw new DatabaseException(exception);
117 public Result getResult() {
121 public Throwable getException() {
126 public String toString() {
127 return "." + procedure;