1 package org.simantics.db.layer0.util;
3 import java.util.Collection;
4 import java.util.HashMap;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.request.BinaryRead;
10 import org.simantics.db.common.request.ObjectsWithType;
11 import org.simantics.db.common.request.ParametrizedPrimitiveRead;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.internal.SimanticsInternal;
14 import org.simantics.db.procedure.Listener;
15 import org.simantics.db.request.Read;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
18 import org.simantics.scl.compiler.module.repository.ImportFailureException;
19 import org.simantics.scl.compiler.module.repository.UpdateListener;
20 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
21 import org.simantics.scl.osgi.SCLOsgi;
22 import org.simantics.scl.runtime.SCLContext;
25 * Finds the runtime environment of a model or other index root.
27 * @author Hannu Niemistö
28 * @author Antti Villberg
30 public class RuntimeEnvironmentRequest2 extends BinaryRead<Resource, Resource, RuntimeEnvironment> {
32 public RuntimeEnvironmentRequest2(Resource parameter, Resource parameter2) {
33 super(parameter, parameter2);
36 protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) {
39 static class UpdateListenerImpl extends UpdateListener {
41 final EnvironmentSpecification environmentSpecification;
42 final Listener<RuntimeEnvironment> callback;
44 UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback) {
45 this.environmentSpecification = environmentSpecification;
46 this.callback = callback;
50 public void notifyAboutUpdate() {
51 if(callback.isDisposed()) {
55 getRuntimeEnvironment(environmentSpecification, callback, this);
59 final public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback, UpdateListenerImpl listener) {
63 SCLContext context = SCLContext.getCurrent();
65 RuntimeEnvironment env;
66 Object graph = context.get("graph");
69 env = SimanticsInternal.getSession().syncRequest(new Read<RuntimeEnvironment>() {
71 public RuntimeEnvironment perform(ReadGraph graph) throws DatabaseException {
73 SCLContext sclContext = SCLContext.getCurrent();
74 Object oldGraph = sclContext.get("graph");
76 sclContext.put("graph", graph);
77 return SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(
78 environmentSpecification,
79 callback.getClass().getClassLoader(), listener);
80 } catch (ImportFailureException e) {
81 throw new DatabaseException(e);
82 } catch (Throwable t) {
83 throw new DatabaseException(t);
85 sclContext.put("graph", oldGraph);
89 } catch (DatabaseException e) {
90 callback.exception(e);
94 env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(
95 environmentSpecification,
96 callback.getClass().getClassLoader(), listener);
97 callback.execute(env);
98 } catch (ImportFailureException e) {
99 callback.exception(new DatabaseException(e));
105 public RuntimeEnvironment perform(ReadGraph graph)
106 throws DatabaseException {
107 final EnvironmentSpecification environmentSpecification = EnvironmentSpecification.of(
109 "StandardLibrary", "",
110 "Simantics/All", "");
111 fillEnvironmentSpecification(environmentSpecification);
113 Layer0 L0 = Layer0.getInstance(graph);
114 Collection<Resource> sclModules = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, L0.SCLModule));
115 for (Resource sclModule : sclModules)
116 environmentSpecification.importModule(graph.getURI(sclModule), "");
118 Resource mainModule = Layer0Utils.getPossibleChild(graph, parameter2, "SCLMain");
119 if(mainModule != null)
120 environmentSpecification.importModule(graph.getURI(mainModule), "");
122 return graph.syncRequest(new ParametrizedPrimitiveRead<EnvironmentSpecification, RuntimeEnvironment>(environmentSpecification) {
123 UpdateListenerImpl sclListener;
125 public void register(ReadGraph graph, Listener<RuntimeEnvironment> procedure) {
127 SCLContext context = SCLContext.getCurrent();
128 Object oldGraph = context.put("graph", graph);
131 if(procedure.isDisposed()) {
132 getRuntimeEnvironment(parameter, procedure, null);
134 sclListener = new UpdateListenerImpl(parameter, procedure);
135 sclListener.notifyAboutUpdate();
139 context.put("graph", oldGraph);
145 public void unregistered() {
146 if(sclListener != null)
147 sclListener.stopListening();
154 public int hashCode() {
155 return 31*getClass().hashCode() + super.hashCode();