]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/CallWrappedQueryProcedure4.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / procedure / CallWrappedQueryProcedure4.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.AsyncMultiProcedure;
20
21 public class CallWrappedQueryProcedure4<Result> implements AsyncMultiProcedure<Result> {
22
23     final private AsyncMultiProcedure<Result> procedure;
24     final private AsyncBarrier barrier;
25     final private AtomicBoolean latch;
26     
27     public CallWrappedQueryProcedure4(AsyncMultiProcedure<Result> procedure, AsyncBarrier barrier) {
28         this.procedure = procedure;
29         this.barrier = barrier;
30         latch = new AtomicBoolean(false);
31     }
32     
33     @Override
34     public void execute(AsyncReadGraph graph, Result result) {
35         try {
36                 procedure.execute(graph, result);
37         } catch (Throwable t) {
38                 Logger.defaultLogError("AsyncMultiProcedure.execute failed for " + procedure, t);
39         }
40     }
41
42     @Override
43     public void finished(AsyncReadGraph graph) {
44         if(latch.compareAndSet(false, true)) {
45                 try {
46                 procedure.finished(graph);
47                 } catch (Throwable t) {
48                         Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, t);
49                 } finally {
50                 barrier.dec();                  
51                 }
52         } else {
53                 Logger.defaultLogError("Finished or exception was called many times (this time is finished)");
54         }
55     }
56
57     @Override
58     public void exception(AsyncReadGraph graph, Throwable t) {
59         if(latch.compareAndSet(false, true)) {
60                 try {
61                 procedure.exception(graph, t);
62                 } catch (Throwable throwable) {
63                         Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, throwable);
64                 } finally {
65                 barrier.dec();                  
66                 }
67         } else {
68                 Logger.defaultLogError("Finished or exception was called many times (this time is exception)");
69         }
70     }
71         
72 }