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