X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2FBlockingAsyncMultiProcedure.java;fp=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2FBlockingAsyncMultiProcedure.java;h=cc50ea9b8fcc4447f209f55fd370c4b477380a89;hb=0d9b90834ce56b292c00b1a39850ed842c3e4d42;hp=0000000000000000000000000000000000000000;hpb=e5db6157fd8722c946613d4e46d7aaf6bfa92609;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncMultiProcedure.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncMultiProcedure.java new file mode 100644 index 000000000..cc50ea9b8 --- /dev/null +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncMultiProcedure.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (c) 2018 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.db.impl; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.procedure.AsyncMultiProcedure; + +public class BlockingAsyncMultiProcedure implements AsyncMultiProcedure { + + private static final Object NO_RESULT = new Object(); + + private final Object key; + private final ReadGraphImpl graph; + private final AsyncMultiProcedure procedure; + + private Object result = NO_RESULT; + private Throwable exception = null; + + public BlockingAsyncMultiProcedure(ReadGraphImpl graph, AsyncMultiProcedure procedure, Object key) { + this.procedure = procedure; + this.key = key; + this.graph = ReadGraphImpl.newAsync(graph); + this.graph.asyncBarrier.inc(); + } + + @Override + public void execute(AsyncReadGraph graph, Result result) { + this.result = result; + try { + if(procedure != null) procedure.execute(graph, result); + } catch (Throwable throwable) { + Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable); + } + } + + @Override + public void finished(AsyncReadGraph graph) { + this.graph.asyncBarrier.dec(); + try { + if(procedure != null) procedure.finished(graph); + } catch (Throwable throwable) { + Logger.defaultLogError("AsyncProcedure.finish threw for " + procedure, throwable); + } + } + + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + this.exception = t; + try { + if (procedure != null) procedure.exception(graph, t); + } catch (Throwable throwable) { + Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable); + } finally { + this.graph.asyncBarrier.dec(); + } + } + + @SuppressWarnings("unchecked") + public Result get() throws DatabaseException { + graph.asyncBarrier.waitBarrier(key, graph); + if (exception != null) { + if (exception instanceof DatabaseException) throw (DatabaseException) exception; + throw new DatabaseException(exception); + } else { + return (Result) result; + } + } + + @SuppressWarnings("unchecked") + public Result getResult() { + return (Result) result; + } + + public Throwable getException() { + return exception; + } + + @Override + public String toString() { + return "." + procedure; + } + +}