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