]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / RuntimeEnvironmentRequest2.java
index 75308cc18866f229efcebcd1464ef5ca02d5e1cd..4bbd480020141b5e602c9e7f73fb1b8170410995 100644 (file)
@@ -36,131 +36,123 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead<Resource, Resource, R
     protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) {
     }
 
-    static class UpdateListenerImpl implements UpdateListener {
-          
-     final EnvironmentSpecification environmentSpecification;
-     final Listener<RuntimeEnvironment> callback;
-     
-     UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback) {
-      this.environmentSpecification = environmentSpecification;
-      this.callback = callback;
-     }
+    static class UpdateListenerImpl extends UpdateListener {
+
+        final EnvironmentSpecification environmentSpecification;
+        final Listener<RuntimeEnvironment> callback;
+
+        UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback) {
+            this.environmentSpecification = environmentSpecification;
+            this.callback = callback;
+        }
 
         @Override
         public void notifyAboutUpdate() {
-         if(callback.isDisposed()) {
-          return;
-         }
-         getRuntimeEnvironment(environmentSpecification, callback, this);
+            if(callback.isDisposed()) {
+                stopListening();
+                return;
+            }
+            getRuntimeEnvironment(environmentSpecification, callback, this);
+        }
+    };
+
+    final public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> 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<RuntimeEnvironment>() {
+                        @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));
         }
 
-     final public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> 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<RuntimeEnvironment>() {
-                            @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<EnvironmentSpecification, UpdateListenerImpl> map = new HashMap<>(); 
-    
     @Override
     public RuntimeEnvironment perform(ReadGraph graph)
             throws DatabaseException {
         final EnvironmentSpecification environmentSpecification = EnvironmentSpecification.of(
                 "Builtin", "",
-                "Prelude", "",
+                "StandardLibrary", "",
                 "Simantics/All", "");
         fillEnvironmentSpecification(environmentSpecification);
-        
+
         Layer0 L0 = Layer0.getInstance(graph);
         Collection<Resource> 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, RuntimeEnvironment>(environmentSpecification) {
-         
-         @Override
-         public void register(ReadGraph graph, Listener<RuntimeEnvironment> 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);
-          }
-
-         }
-            
+            UpdateListenerImpl sclListener;
+            @Override
+            public void register(ReadGraph graph, Listener<RuntimeEnvironment> 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() {
-             map.remove(parameter);
+                if(sclListener != null)
+                    sclListener.stopListening();
             }
-            
+
         });
     }
-    
+
     @Override
     public int hashCode() {
         return 31*getClass().hashCode() + super.hashCode();
     }
 
-    public static void flush() {
-        map.clear();
-    }
-
 }
\ No newline at end of file