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