]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / RuntimeEnvironmentRequest.java
index 6fbe36854ad99bd8934bd843a094be4742496e7e..cde4e02396a48515a31ca777e1265870e8903cc8 100644 (file)
@@ -1,8 +1,5 @@
 package org.simantics.db.layer0.util;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.ParametrizedPrimitiveRead;
@@ -33,7 +30,7 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
     protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) {
     }
 
-    static class UpdateListenerImpl implements UpdateListener {
+    static class UpdateListenerImpl extends UpdateListener {
                
        final EnvironmentSpecification environmentSpecification;
        final Listener<RuntimeEnvironment> callback;
@@ -46,68 +43,64 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
         @Override
         public void notifyAboutUpdate() {
                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);
-                                }
+    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));
-                       }
-
-       }
-        
-    };     
+                        }
+                    });
+                } 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<String, UpdateListenerImpl> map = new HashMap<String, UpdateListenerImpl>(); 
+    }
     
     @Override
     public RuntimeEnvironment perform(ReadGraph graph)
             throws DatabaseException {
         final EnvironmentSpecification environmentSpecification = EnvironmentSpecification.of(
                 "Builtin", "",
-                "Prelude", "",
+                "StandardLibrary", "",
                 "Simantics/All", "");
         fillEnvironmentSpecification(environmentSpecification);
         Resource mainModule = Layer0Utils.getPossibleChild(graph, parameter, "SCLMain");
@@ -120,7 +113,9 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
             mainModuleUri = graph.getURI(parameter) + "/#"; // Add something dummy to the model uri that cannot be in a real URI
         
             return graph.syncRequest(new ParametrizedPrimitiveRead<String, RuntimeEnvironment>(mainModuleUri) {
-               
+                
+                UpdateListenerImpl sclListener;
+                
                @Override
                public void register(ReadGraph graph, Listener<RuntimeEnvironment> procedure) {
 
@@ -129,12 +124,11 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
                        try {
 
                                if(procedure.isDisposed()) {
-                                       UpdateListenerImpl.getRuntimeEnvironment(environmentSpecification, procedure, null);
+                                       getRuntimeEnvironment(environmentSpecification, procedure, null);
                                } else {
-                                       UpdateListenerImpl impl = new UpdateListenerImpl(environmentSpecification, procedure);
-                                       impl.notifyAboutUpdate();
-                                               map.put(parameter, impl);
-                               }
+                                   sclListener = new UpdateListenerImpl(environmentSpecification, procedure);
+                                   sclListener.notifyAboutUpdate();
+                                       }
 
                        } finally {
                                context.put("graph", oldGraph);
@@ -144,7 +138,8 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
                 
                 @Override
                 public void unregistered() {
-                       map.remove(parameter);
+                       if(sclListener != null)
+                           sclListener.stopListening();
                 }
                 
             });
@@ -155,8 +150,4 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
         return 31*getClass().hashCode() + super.hashCode();
     }
 
-    public static void flush() {
-        map.clear();
-    }
-
 }