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