]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/BlockingAsyncProcedure.java
Still working for multiple readers
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / BlockingAsyncProcedure.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.procedure;
13
14 import java.util.concurrent.Semaphore;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.atomic.AtomicBoolean;
17
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;
22
23 public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
24
25         final private Object key;
26     private Result result = null;
27     private Throwable exception = null;
28     final private AsyncProcedure<Result> procedure;
29     final private Semaphore semaphore = new Semaphore(0);
30 //    final private AtomicBoolean latch;
31     
32     public BlockingAsyncProcedure(AsyncProcedure<Result> procedure, Object key) {
33 //      assert(procedure != null);
34         this.key = key;
35         this.procedure = procedure;
36         if(key == null)
37                 System.err.println("asd");
38         //System.err.println("BlockingAsyncProcedure " + key);
39 //        latch = new AtomicBoolean(false);
40     }
41     
42     @Override
43     public void execute(AsyncReadGraph graph, Result result) {
44         this.result = result;
45         semaphore.release();
46 //        if(latch.compareAndSet(false, true)) {
47                 try {
48                         if(procedure != null) procedure.execute(graph, result);
49                 } catch (Throwable throwable) {
50                 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
51                 }
52 //              } finally {
53 ////                    System.err.println("ResultCallWrappedSingleQueryProcedure4 dec " + key);
54 //              }
55 //        } else {
56 //              Logger.defaultLogError("Procedure was called many times (this time is execute)");
57 //        }
58     }
59
60     @Override
61     public void exception(AsyncReadGraph graph, Throwable t) {
62         this.exception = t;
63         semaphore.release();
64 //        if(latch.compareAndSet(false, true)) {
65                 try {
66                         if(procedure != null) procedure.exception(graph, t);
67                 } catch (Throwable throwable) {
68                 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
69                 } finally {
70                 }
71 //        } else {
72 //              Logger.defaultLogError("Procedure was called many times (this time is exception)");
73 //        }
74         
75     }
76     
77     public Result get() throws DatabaseException {
78         
79         try {
80                         boolean success = semaphore.tryAcquire(10, TimeUnit.SECONDS);
81                         if(!success) throw new DatabaseException("Timeout while waiting for async request to complete: " + key);
82                 } catch (InterruptedException e) {
83                         throw new DatabaseException(e);
84                 }
85         
86         if(exception != null) {
87                 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
88                 throw new DatabaseException(exception);
89         } else {
90                 return result;
91         }
92         
93     }
94     
95     public Result getResult() {
96         return result;
97     }
98     
99     public Throwable getException() {
100         return exception;
101     }
102         
103     @Override
104     public String toString() {
105         return "." + procedure; 
106     }
107     
108 }