X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Futil%2FRuntimeEnvironmentRequest2.java;h=28d18e50772ece99c914c7069ce73ad89e3bd9e6;hp=a0194612105b2b1eac1c18be28f877e4d494378b;hb=1cd631466bc35e05bc585999b2f325f148cd5629;hpb=8c2e00d0be90fae1b0849210749ec516b8d3d563 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 a01946121..28d18e507 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,166 +1,174 @@ -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; -import org.simantics.db.common.request.BinaryRead; -import org.simantics.db.common.request.ObjectsWithType; -import org.simantics.db.common.request.ParametrizedPrimitiveRead; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.internal.SimanticsInternal; -import org.simantics.db.procedure.Listener; -import org.simantics.db.request.Read; -import org.simantics.layer0.Layer0; -import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification; -import org.simantics.scl.compiler.module.repository.ImportFailureException; -import org.simantics.scl.compiler.module.repository.UpdateListener; -import org.simantics.scl.compiler.runtime.RuntimeEnvironment; -import org.simantics.scl.osgi.SCLOsgi; -import org.simantics.scl.runtime.SCLContext; - -/** - * Finds the runtime environment of a model or other index root. - * - * @author Hannu Niemistö - * @author Antti Villberg - */ -public class RuntimeEnvironmentRequest2 extends BinaryRead { - - public RuntimeEnvironmentRequest2(Resource parameter, Resource parameter2) { - super(parameter, parameter2); - } - - protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) { - } - - static class UpdateListenerImpl implements UpdateListener { - - final EnvironmentSpecification environmentSpecification; - final Listener callback; - - UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener callback) { - this.environmentSpecification = environmentSpecification; - this.callback = callback; - } - - @Override - public void notifyAboutUpdate() { - if(callback.isDisposed()) { - return; - } - getRuntimeEnvironment(environmentSpecification, callback, this); - } - - final 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) - try { - env = SimanticsInternal.getSession().syncRequest(new Read() { - @Override - public RuntimeEnvironment perform(ReadGraph graph) throws DatabaseException { - - SCLContext sclContext = SCLContext.getCurrent(); - Object oldGraph = sclContext.get("graph"); - try { - sclContext.put("graph", graph); - return SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment( - environmentSpecification, - callback.getClass().getClassLoader(), listener); - } catch (ImportFailureException e) { - throw new DatabaseException(e); - } catch (Throwable t) { - throw new DatabaseException(t); - } finally { - sclContext.put("graph", oldGraph); - } - } - }); - } catch (DatabaseException e) { - callback.exception(e); - return; - } - else - env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment( - environmentSpecification, - callback.getClass().getClassLoader(), listener); - callback.execute(env); - } catch (ImportFailureException e) { - callback.exception(new DatabaseException(e)); - } - - } - - }; - - // This is needed to prevent garbage collection from collecting UpdateListenerImpls - // -ModuleRepository only makes a weak reference to the listener - final static Map map = new HashMap<>(); - - @Override - public RuntimeEnvironment perform(ReadGraph graph) - throws DatabaseException { - final EnvironmentSpecification environmentSpecification = EnvironmentSpecification.of( - "Builtin", "", - "Prelude", "", - "Simantics/All", ""); - fillEnvironmentSpecification(environmentSpecification); - - Layer0 L0 = Layer0.getInstance(graph); - Collection sclModules = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, L0.SCLModule)); - for (Resource sclModule : sclModules) - environmentSpecification.importModule(graph.getURI(sclModule), ""); - - Resource mainModule = Layer0Utils.getPossibleChild(graph, parameter2, "SCLMain"); - if(mainModule != null) - environmentSpecification.importModule(graph.getURI(mainModule), ""); - - return graph.syncRequest(new ParametrizedPrimitiveRead(environmentSpecification) { - - @Override - public void register(ReadGraph graph, Listener procedure) { - - SCLContext context = SCLContext.getCurrent(); - Object oldGraph = context.put("graph", graph); - try { - - if(procedure.isDisposed()) { - UpdateListenerImpl.getRuntimeEnvironment(parameter, procedure, null); - } else { - UpdateListenerImpl impl = new UpdateListenerImpl(parameter, procedure); - impl.notifyAboutUpdate(); - map.put(parameter, impl); - } - - } finally { - context.put("graph", oldGraph); - } - - } - - @Override - public void unregistered() { - map.remove(parameter); - } - - }); - } - - @Override - public int hashCode() { - return 31*getClass().hashCode() + super.hashCode(); - } - - public static void flush() { - map.clear(); - } - -} \ No newline at end of file +package org.simantics.db.layer0.util; + +import java.util.Collection; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.BinaryRead; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.ParametrizedPrimitiveRead; +import org.simantics.db.common.utils.CommonDBUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.internal.SimanticsInternal; +import org.simantics.db.procedure.Listener; +import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; +import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification; +import org.simantics.scl.compiler.module.repository.ImportFailureException; +import org.simantics.scl.compiler.module.repository.UpdateListener; +import org.simantics.scl.compiler.runtime.RuntimeEnvironment; +import org.simantics.scl.osgi.SCLOsgi; +import org.simantics.scl.runtime.SCLContext; + +/** + * Finds the runtime environment of a model or other index root. + * + * @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 componentType, Resource indexRoot) { + super(componentType, indexRoot); + } + + protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) { + } + + static class UpdateListenerImpl extends UpdateListener { + + 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); + } + }; + + 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) + try { + env = SimanticsInternal.getSession().syncRequest(new Read() { + @Override + public RuntimeEnvironment perform(ReadGraph graph) throws DatabaseException { + + SCLContext sclContext = SCLContext.getCurrent(); + Object oldGraph = sclContext.get("graph"); + try { + sclContext.put("graph", graph); + return SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment( + environmentSpecification, + callback.getClass().getClassLoader(), listener); + } catch (ImportFailureException e) { + throw new DatabaseException(e); + } catch (Throwable t) { + throw new DatabaseException(t); + } finally { + sclContext.put("graph", oldGraph); + } + } + }); + } catch (DatabaseException e) { + callback.exception(e); + return; + } + else + env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment( + environmentSpecification, + callback.getClass().getClassLoader(), listener); + callback.execute(env); + } catch (ImportFailureException e) { + callback.exception(new DatabaseException(e)); + } + + } + + @Override + public RuntimeEnvironment perform(ReadGraph graph) + throws DatabaseException { + final EnvironmentSpecification environmentSpecification = EnvironmentSpecification.of( + "Builtin", "", + "StandardLibrary", "", + "Simantics/All", ""); + fillEnvironmentSpecification(environmentSpecification); + + Layer0 L0 = Layer0.getInstance(graph); + if (parameter != null) { + Collection sclModules = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, L0.SCLModule)); + for (Resource sclModule : sclModules) { + environmentSpecification.importModule(graph.getURI(sclModule), ""); + } + } else { + // `parameter` is optional and can be null for e.g. procedural user components + } + + Resource mainModule = CommonDBUtils.getPossibleChild(graph, parameter2, "SCLMain"); + if(mainModule != null) { + environmentSpecification.importModule(graph.getURI(mainModule), ""); + for(Resource l : graph.getObjects(parameter2, L0.IsLinkedTo)) { + mainModule = CommonDBUtils.getPossibleChild(graph, l, "SCLMain"); + if(mainModule != null) + environmentSpecification.importModule(graph.getURI(mainModule), ""); + } + } + + return graph.syncRequest(new ParametrizedPrimitiveRead(environmentSpecification) { + + 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(parameter, procedure, null); + } else { + sclListener = new UpdateListenerImpl(parameter, 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(); + } + +}