From: Hannu Niemistö Date: Wed, 10 Oct 2018 06:53:07 +0000 (+0000) Subject: Merge "Use proper environment to resolve SCL types in ontology module" X-Git-Tag: v1.43.0~136^2~338 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=39de724eba3c483631685a7281ee96c17d700c99;hp=258325ccd59cc58a0ec8d26a21353e1470ebd6fa Merge "Use proper environment to resolve SCL types in ontology module" --- diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java index 77af46d55..3feb22e00 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java @@ -18,7 +18,6 @@ import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; -import java.nio.BufferUnderflowException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -135,7 +134,6 @@ import org.simantics.db.exception.NoSingleResultException; import org.simantics.db.exception.ResourceNotFoundException; import org.simantics.db.exception.ServiceException; import org.simantics.db.exception.ValidationException; -import org.simantics.db.impl.BlockingAsyncProcedure; import org.simantics.db.impl.RelationContextImpl; import org.simantics.db.impl.ResourceImpl; import org.simantics.db.impl.internal.RandomAccessValueSupport; @@ -1107,9 +1105,10 @@ public class ReadGraphImpl implements AsyncReadGraph { assert (subject != null); + byte[] bytes = null; try { - byte[] bytes = processor.getValue(this, subject); + bytes = processor.getValue(this, subject); if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject); Serializer serializer = getSerializer(binding); @@ -1119,20 +1118,9 @@ public class ReadGraphImpl implements AsyncReadGraph { throw new DoesNotContainValueException(e); - } catch (IOException e) { - - throw new ServiceException(e); - - } catch (DatabaseException e) { - - throw new ServiceException(e); - - } catch (BufferUnderflowException e) { - // This is sometimes thrown when deserialize fails because wrong format. - // For callers of this method this is just an service exception. - throw new ServiceException(e); - } - + } catch (Throwable t) { + throw new ServiceException("Could not getValue for subject " + debugString(subject) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(bytes), t); + } } @Override @@ -3544,14 +3532,9 @@ public class ReadGraphImpl implements AsyncReadGraph { // else procedure.execute(graph, (T) obj); - } catch (IOException e) { - procedure.exception(graph, e); - } catch (BufferUnderflowException e) { - procedure.exception(graph, e); } catch (Throwable t) { - procedure.exception(graph, t); + procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t)); } - } @Override @@ -3571,6 +3554,24 @@ public class ReadGraphImpl implements AsyncReadGraph { }); } + + private static String safeArrayToString(byte[] a) { + if (a == null) + return "null"; + int iMax = a.length - 1; + if (iMax == -1) + return "[]"; + + StringBuilder b = new StringBuilder(); + b.append('['); + for (int i = 0; i < 100; i++) { // limit to first 100 items + b.append(a[i]); + if (i == iMax) + return b.append(']').toString(); + b.append(", "); + } + return b.append(", ... (" + a.length + ")]").toString(); + } @Override public void forValue(Resource subject, Binding binding, @@ -4265,14 +4266,9 @@ public class ReadGraphImpl implements AsyncReadGraph { else procedure.execute(graph, (T) obj); - } catch (IOException e) { - procedure.exception(graph, e); - } catch (BufferUnderflowException e) { - procedure.exception(graph, e); } catch (Throwable t) { - procedure.exception(graph, t); + procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t)); } - } @Override diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest.java index cde4e0239..9f6b8fe53 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest.java @@ -26,36 +26,36 @@ public class RuntimeEnvironmentRequest extends UnaryRead callback; - - UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener callback) { - this.environmentSpecification = environmentSpecification; - this.callback = callback; - } + + final EnvironmentSpecification environmentSpecification; + final Listener callback; + + UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener callback) { + this.environmentSpecification = environmentSpecification; + this.callback = callback; + } @Override public void notifyAboutUpdate() { - if(callback.isDisposed()) { - stopListening(); - return; - } - getRuntimeEnvironment(environmentSpecification, callback, this); + if(callback.isDisposed()) { + stopListening(); + return; + } + getRuntimeEnvironment(environmentSpecification, callback, this); } - }; + }; public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener callback, UpdateListenerImpl listener) { try { - + SCLContext context = SCLContext.getCurrent(); - + RuntimeEnvironment env; Object graph = context.get("graph"); if(graph == null) @@ -63,7 +63,7 @@ public class RuntimeEnvironmentRequest extends UnaryRead() { @Override public RuntimeEnvironment perform(ReadGraph graph) throws DatabaseException { - + SCLContext sclContext = SCLContext.getCurrent(); Object oldGraph = sclContext.get("graph"); try { @@ -94,7 +94,7 @@ public class RuntimeEnvironmentRequest extends UnaryRead(mainModuleUri) { - - UpdateListenerImpl sclListener; - - @Override - public void register(ReadGraph graph, Listener procedure) { - - SCLContext context = SCLContext.getCurrent(); - Object oldGraph = context.put("graph", graph); - try { - - if(procedure.isDisposed()) { - getRuntimeEnvironment(environmentSpecification, procedure, null); - } else { - sclListener = new UpdateListenerImpl(environmentSpecification, procedure); - sclListener.notifyAboutUpdate(); - } - - } finally { - context.put("graph", oldGraph); - } - - } - - @Override - public void unregistered() { - if(sclListener != null) - sclListener.stopListening(); + + return graph.syncRequest(new ParametrizedPrimitiveRead(mainModuleUri) { + + UpdateListenerImpl sclListener; + + @Override + public void register(ReadGraph graph, Listener procedure) { + + SCLContext context = SCLContext.getCurrent(); + Object oldGraph = context.put("graph", graph); + try { + + if(procedure.isDisposed()) { + getRuntimeEnvironment(environmentSpecification, procedure, null); + } else { + sclListener = new UpdateListenerImpl(environmentSpecification, procedure); + sclListener.notifyAboutUpdate(); + } + + } finally { + context.put("graph", oldGraph); } - - }); + + } + + @Override + public void unregistered() { + if(sclListener != null) + sclListener.stopListening(); + } + + }); } - + @Override public int hashCode() { return 31*getClass().hashCode() + super.hashCode(); diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java index 4bbd48002..ca8bc07cd 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java @@ -1,8 +1,6 @@ package org.simantics.db.layer0.util; import java.util.Collection; -import java.util.HashMap; -import java.util.Map; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -26,13 +24,17 @@ import org.simantics.scl.runtime.SCLContext; * * @author Hannu Niemistö * @author Antti Villberg + * + * Difference between this class and {@code RuntimeEnvironmentRequest} is an additional parameter + * that is typically some component type. All modules under this resource are added to the environment + * in addition to the SCLMain of the root resource. */ public class RuntimeEnvironmentRequest2 extends BinaryRead { - public RuntimeEnvironmentRequest2(Resource parameter, Resource parameter2) { - super(parameter, parameter2); + public RuntimeEnvironmentRequest2(Resource componentType, Resource indexRoot) { + super(componentType, indexRoot); } - + protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) { } @@ -56,7 +58,7 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead callback, UpdateListenerImpl listener) { + public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener callback, UpdateListenerImpl listener) { try { @@ -120,7 +122,9 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead(environmentSpecification) { + UpdateListenerImpl sclListener; + @Override public void register(ReadGraph graph, Listener procedure) { @@ -155,4 +159,4 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead