]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java
Yet another fixing commit
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / 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.impl;
13
14 import java.text.DecimalFormat;
15 import java.util.concurrent.TimeUnit;
16
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;
23
24 public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
25         
26         final private static Object NO_RESULT = new Object();
27
28         final private Object key;
29     final private ReadGraphImpl graph;
30         final private AsyncProcedure<Result> procedure;
31         
32     private Object result = NO_RESULT;
33     private Throwable exception = null;
34     
35     public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
36         this.procedure = procedure;
37         this.key = key;
38         this.graph = ReadGraphImpl.newAsync(graph);
39         this.graph.asyncBarrier.inc();
40     }
41     
42     @Override
43     public void execute(AsyncReadGraph graph, Result result) {
44         this.result = result;
45         this.graph.asyncBarrier.dec();
46         try {
47                 if(procedure != null) procedure.execute(graph, result);
48         } catch (Throwable throwable) {
49                 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
50         }
51     }
52
53     @Override
54     public void exception(AsyncReadGraph graph, Throwable t) {
55         this.exception = t;
56         try {
57                 if(procedure != null) procedure.exception(graph, t);
58         } catch (Throwable throwable) {
59                 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
60         } finally {
61         }
62         this.graph.asyncBarrier.dec();
63     }
64     
65     public Result get() throws DatabaseException {
66         
67         graph.asyncBarrier.waitBarrier(key, graph);
68         
69         if(exception != null) {
70                 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
71                 throw new DatabaseException(exception);
72         } else {
73                 return (Result)result;
74         }
75         
76     }
77     
78     public Result getResult() {
79         return (Result)result;
80     }
81     
82     public Throwable getException() {
83         return exception;
84     }
85         
86     @Override
87     public String toString() {
88         return "." + procedure; 
89     }
90     
91 }