]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java
b751e8f70d5771f8bd0994e58dbaa1d890b53b93
[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 <<<<<<< Upstream, based on branch 'private/antti_threads' of ssh://villberg@gerrit.simantics.org:29418/simantics/platform.git
26
27         final private Object key;
28     final private ReadGraphImpl graph;
29         final private AsyncProcedure<Result> procedure;
30         
31     private Result result = null;
32     private Throwable exception = null;
33     
34     public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
35         this.procedure = procedure;
36         this.key = key;
37         this.graph = ReadGraphImpl.newAsync(graph);
38         this.graph.asyncBarrier.inc();
39     }
40     
41     @Override
42     public void execute(AsyncReadGraph graph, Result result) {
43         this.result = result;
44         this.graph.asyncBarrier.dec();
45         try {
46                 if(procedure != null) procedure.execute(graph, result);
47         } catch (Throwable throwable) {
48                 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
49         }
50     }
51
52     @Override
53     public void exception(AsyncReadGraph graph, Throwable t) {
54         this.exception = t;
55         try {
56                 if(procedure != null) procedure.exception(graph, t);
57         } catch (Throwable throwable) {
58                 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
59         } finally {
60         }
61         this.graph.asyncBarrier.dec();
62     }
63     
64     public Result get() throws DatabaseException {
65         
66         graph.asyncBarrier.waitBarrier(key, graph);
67         
68         if(exception != null) {
69                 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
70                 throw new DatabaseException(exception);
71         } else {
72                 return result;
73         }
74         
75     }
76     
77     public Result getResult() {
78         return result;
79 =======
80         
81         final private static Object NO_RESULT = new Object();
82
83         final private Object key;
84     final private ReadGraphImpl graph;
85         final private AsyncProcedure<Result> procedure;
86         
87     private Object result = NO_RESULT;
88     private Throwable exception = null;
89     
90     public BlockingAsyncProcedure(ReadGraphImpl graph, AsyncProcedure<Result> procedure, Object key) {
91         this.procedure = procedure;
92         this.key = key;
93         this.graph = ReadGraphImpl.newAsync(graph);
94         this.graph.asyncBarrier.inc();
95     }
96     
97     @Override
98     public void execute(AsyncReadGraph graph, Result result) {
99         this.result = result;
100         this.graph.asyncBarrier.dec();
101         try {
102                 if(procedure != null) procedure.execute(graph, result);
103         } catch (Throwable throwable) {
104                 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
105         }
106     }
107
108     @Override
109     public void exception(AsyncReadGraph graph, Throwable t) {
110         this.exception = t;
111         try {
112                 if(procedure != null) procedure.exception(graph, t);
113         } catch (Throwable throwable) {
114                 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
115         } finally {
116         }
117         this.graph.asyncBarrier.dec();
118     }
119     
120     public Result get() throws DatabaseException {
121         
122         graph.asyncBarrier.waitBarrier(key, graph);
123         
124         if(exception != null) {
125                 if(exception instanceof DatabaseException) throw (DatabaseException)exception;
126                 throw new DatabaseException(exception);
127         } else {
128                 return (Result)result;
129         }
130         
131     }
132     
133     public Result getResult() {
134         return (Result)result;
135 >>>>>>> 82fa68e Generate parts of db client query code
136     }
137     
138     public Throwable getException() {
139         return exception;
140     }
141         
142     @Override
143     public String toString() {
144         return "." + procedure; 
145     }
146     
147 }