]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedSingleQueryProcedure4.java
a6717c9d9ec991c48fd8150eca4de5b17e9f78ec
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / procedure / ResultCallWrappedSingleQueryProcedure4.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.procedure;
13
14 import java.util.concurrent.atomic.AtomicBoolean;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.common.utils.Logger;
18 import org.simantics.db.impl.graph.AsyncBarrierImpl;
19 import org.simantics.db.procedure.AsyncProcedure;
20
21 public class ResultCallWrappedSingleQueryProcedure4<Result> implements AsyncProcedure<Result> {
22
23         final private Object key;
24     private Result result = null;
25     private Throwable exception = null;
26     final private AsyncProcedure<Result> procedure;
27     final private AtomicBoolean latch;
28     
29     public ResultCallWrappedSingleQueryProcedure4(AsyncProcedure<Result> procedure, Object key) {
30         assert(procedure != null);
31         this.key = key;
32         this.procedure = procedure;
33         latch = new AtomicBoolean(false);
34     }
35     
36     @Override
37     public void execute(AsyncReadGraph graph, Result result) {
38         this.result = result;
39         if(latch.compareAndSet(false, true)) {
40                 try {
41                         procedure.execute(graph, result);
42                 } catch (Throwable throwable) {
43                 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
44                 } finally {
45 //                      System.err.println("ResultCallWrappedSingleQueryProcedure4 dec " + key);
46                 }
47         } else {
48                 Logger.defaultLogError("Procedure was called many times (this time is execute)");
49         }
50     }
51
52     @Override
53     public void exception(AsyncReadGraph graph, Throwable t) {
54         this.exception = t;
55         if(latch.compareAndSet(false, true)) {
56                 try {
57                         procedure.exception(graph, t);
58                 } catch (Throwable throwable) {
59                 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
60                 } finally {
61                 }
62         } else {
63                 Logger.defaultLogError("Procedure was called many times (this time is exception)");
64         }
65         
66     }
67     
68     public Result getResult() {
69         return result;
70     }
71     
72     public Throwable getException() {
73         return exception;
74     }
75         
76     @Override
77     public String toString() {
78         return "." + procedure; 
79     }
80     
81 }