]> gerrit.simantics Code Review - simantics/platform.git/blob
7f2b54559ed35f0ecb7c077fe97c6131a8ddca82
[simantics/platform.git] /
1 package org.simantics.modeling;
2
3 import java.util.ArrayList;
4
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.common.request.ParametrizedPrimitiveRead;
7 import org.simantics.db.procedure.Listener;
8 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
9 import org.simantics.scl.compiler.module.repository.ImportFailureException;
10 import org.simantics.scl.compiler.module.repository.UpdateListener;
11 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
12 import org.simantics.scl.osgi.SCLOsgi;
13 import org.simantics.scl.runtime.SCLContext;
14
15 public class ComponentTypeScriptRuntimeEnvironmentRequest extends ParametrizedPrimitiveRead<EnvironmentSpecification, RuntimeEnvironment> {
16
17     // This array list is only needed to keep strong references to update listeners preventing their garbage collection.
18     ArrayList<UpdateListener> listeners;
19     
20     public ComponentTypeScriptRuntimeEnvironmentRequest(EnvironmentSpecification parameter) {
21         super(parameter);
22     }
23
24     @Override
25     public void register(ReadGraph graph, Listener<RuntimeEnvironment> procedure) {
26         UpdateListener listener = null;
27         if(!procedure.isDisposed()) {
28             listener = new UpdateListener() {
29                 @Override
30                 public void notifyAboutUpdate() {
31                     createRuntimeEnvironment(graph, procedure, this);
32                 }
33             };
34             if(listeners == null)
35                 listeners = new ArrayList<UpdateListener>(2);
36             listeners.add(listener);
37         }
38         createRuntimeEnvironment(graph, procedure, listener);
39     }
40     
41     private void createRuntimeEnvironment(ReadGraph graph, Listener<RuntimeEnvironment> procedure, UpdateListener listener) {
42         SCLContext context = SCLContext.getCurrent();
43         Object oldGraph = context.put("graph", graph);
44         try {
45             procedure.execute(SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(parameter, ComponentTypeScriptRuntimeEnvironmentRequest.class.getClassLoader(), listener));
46         }
47         catch (ImportFailureException e) {
48             procedure.exception(e);
49         }
50         finally {
51             context.put("graph", oldGraph);
52         }
53     }
54
55 }