]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedQueryProcedure4.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / procedure / ResultCallWrappedQueryProcedure4.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.impl.procedure;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.concurrent.atomic.AtomicBoolean;\r
16 \r
17 import org.simantics.db.AsyncReadGraph;\r
18 import org.simantics.db.common.utils.Logger;\r
19 import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier;\r
20 import org.simantics.db.procedure.AsyncMultiProcedure;\r
21 \r
22 public class ResultCallWrappedQueryProcedure4<Result> implements AsyncMultiProcedure<Result> {\r
23 \r
24     final private ArrayList<Result> result;\r
25     private Throwable exception = null;\r
26     final private AsyncMultiProcedure<Result> procedure;\r
27     final private AtomicBoolean latch;\r
28     \r
29     public ResultCallWrappedQueryProcedure4(AsyncMultiProcedure<Result> procedure) {\r
30         this.procedure = procedure;\r
31         latch = new AtomicBoolean(false);\r
32         result = new ArrayList<Result>();\r
33     }\r
34     \r
35     @Override\r
36     public void execute(AsyncReadGraph graph, Result result) {\r
37         try {\r
38             synchronized(this.result) {\r
39                 this.result.add(result);\r
40             }\r
41                 procedure.execute(graph, result);\r
42         } catch (Throwable t) {\r
43                 Logger.defaultLogError("AsyncMultiProcedure.execute failed for " + procedure, t);\r
44         }\r
45     }\r
46     \r
47     @Override\r
48     public void finished(AsyncReadGraph graph) {\r
49         if(latch.compareAndSet(false, true)) {\r
50                 try {\r
51                 procedure.finished(graph);\r
52                 } catch (Throwable t) {\r
53                         Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, t);\r
54                 } finally {\r
55                 }\r
56         } else {\r
57                 Logger.defaultLogError("Finished or exception was called many times (this time is finished)");\r
58         }\r
59     }\r
60 \r
61     @Override\r
62     public void exception(AsyncReadGraph graph, Throwable t) {\r
63         if(latch.compareAndSet(false, true)) {\r
64                 try {\r
65                 this.exception = t;\r
66                 procedure.exception(graph, t);\r
67                 } catch (Throwable throwable) {\r
68                         Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, throwable);\r
69                 } finally {\r
70                 }\r
71         } else {\r
72                 Logger.defaultLogError("Finished or exception was called many times (this time is exception)");\r
73         }\r
74     }\r
75     \r
76     public ArrayList<Result> getResult() {\r
77         return result;\r
78     }\r
79     \r
80     public Throwable getException() {\r
81         return exception;\r
82     }\r
83 \r
84     @Override\r
85     public String toString() {\r
86         return "ResultCallWrappedQueryProcedure4[" + procedure + "]"; \r
87     }\r
88     \r
89 }\r