]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedSingleQueryProcedure45.java
Add possibility to bookkeep barrier tracing with env var
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / procedure / ResultCallWrappedSingleQueryProcedure45.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.query.QueryProcessor.AsyncBarrier;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.db.procedure.Procedure;
21
22 public class ResultCallWrappedSingleQueryProcedure45<Result> implements AsyncProcedure<Result> {
23
24     private Result result = null;
25     private Throwable exception = null;
26     final private Procedure<Result> procedure;
27     final private AsyncBarrier barrier;
28     final private AtomicBoolean latch;
29     
30     public ResultCallWrappedSingleQueryProcedure45(Procedure<Result> procedure, AsyncBarrier barrier) {
31         assert(procedure != null);
32         this.procedure = procedure;
33         this.barrier = barrier;
34         latch = new AtomicBoolean(false);
35     }
36     
37     @Override
38     public void execute(AsyncReadGraph graph, Result result) {
39         this.result = result;
40         if(latch.compareAndSet(false, true)) {
41                 try {
42                         procedure.execute(result);
43                 } catch (Throwable throwable) {
44                 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
45                 } finally {
46                         barrier.dec();
47                 }
48         } else {
49                 Logger.defaultLogError("Procedure was called many times (this time is execute)");
50         }
51     }
52
53     @Override
54     public void exception(AsyncReadGraph graph, Throwable t) {
55         this.exception = t;
56         if(latch.compareAndSet(false, true)) {
57                 try {
58                         procedure.exception(t);
59                 } catch (Throwable throwable) {
60                 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
61                 } finally {
62                         barrier.dec();
63                 }
64         } else {
65                 Logger.defaultLogError("Procedure was called many times (this time is exception)");
66         }
67         
68     }
69     
70     public Result getResult() {
71         return result;
72     }
73     
74     public Throwable getException() {
75         return exception;
76     }
77         
78 }