]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedQueryProcedure4.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / procedure / ResultCallWrappedQueryProcedure4.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.ArrayList;
15 import java.util.concurrent.atomic.AtomicBoolean;
16
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier;
20 import org.simantics.db.procedure.AsyncMultiProcedure;
21
22 public class ResultCallWrappedQueryProcedure4<Result> implements AsyncMultiProcedure<Result> {
23
24     final private ArrayList<Result> result;
25     private Throwable exception = null;
26     final private AsyncMultiProcedure<Result> procedure;
27     final private AtomicBoolean latch;
28     
29     public ResultCallWrappedQueryProcedure4(AsyncMultiProcedure<Result> procedure) {
30         this.procedure = procedure;
31         latch = new AtomicBoolean(false);
32         result = new ArrayList<Result>();
33     }
34     
35     @Override
36     public void execute(AsyncReadGraph graph, Result result) {
37         try {
38             synchronized(this.result) {
39                 this.result.add(result);
40             }
41                 procedure.execute(graph, result);
42         } catch (Throwable t) {
43                 Logger.defaultLogError("AsyncMultiProcedure.execute failed for " + procedure, t);
44         }
45     }
46     
47     @Override
48     public void finished(AsyncReadGraph graph) {
49         if(latch.compareAndSet(false, true)) {
50                 try {
51                 procedure.finished(graph);
52                 } catch (Throwable t) {
53                         Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, t);
54                 } finally {
55                 }
56         } else {
57                 Logger.defaultLogError("Finished or exception was called many times (this time is finished)");
58         }
59     }
60
61     @Override
62     public void exception(AsyncReadGraph graph, Throwable t) {
63         if(latch.compareAndSet(false, true)) {
64                 try {
65                 this.exception = t;
66                 procedure.exception(graph, t);
67                 } catch (Throwable throwable) {
68                         Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, throwable);
69                 } finally {
70                 }
71         } else {
72                 Logger.defaultLogError("Finished or exception was called many times (this time is exception)");
73         }
74     }
75     
76     public ArrayList<Result> getResult() {
77         return result;
78     }
79     
80     public Throwable getException() {
81         return exception;
82     }
83
84     @Override
85     public String toString() {
86         return "ResultCallWrappedQueryProcedure4[" + procedure + "]"; 
87     }
88     
89 }