]> gerrit.simantics Code Review - simantics/fmil.git/blobdiff - org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java
Fixed unsafe handling of temporary directory deletion.
[simantics/fmil.git] / org.simantics.fmil.core / src / org / simantics / fmil / core / FMIL.java
index fa09a71c4e2536af375ba8b4b0c65cf6a6839b89..f4c7ca4f2fbbf7d044be10c09a2b4f896b31fe0b 100644 (file)
@@ -6,8 +6,10 @@ import java.io.RandomAccessFile;
 import java.net.URL;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
+import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -129,15 +131,16 @@ public class FMIL {
        }
        
        public boolean subscribe(String name) throws FMILException {
-               // Safety check
-               int vr = variableMap.get(name);
-               if(vr == 0) return false;
-               if(!subscriptionSet.add(name)) return false;
-               subscribedNames.add(name);
-               System.err.println("subscribed : " + name + " => " + subscribedNames.size());
-               subscription.add(vr);
-               subscribe(new int[] { vr });
-               return true;
+               synchronized(syncObject) {
+                       // Safety check
+                       int vr = variableMap.get(name);
+                       if(vr == 0) return false;
+                       if(!subscriptionSet.add(name)) return false;
+                       subscribedNames.add(name);
+                       subscription.add(vr);
+                       subscribe(new int[] { vr });
+                       return true;
+               }
        }
 
     public FMIL() {
@@ -191,21 +194,23 @@ public class FMIL {
                 fmuN = 0;
             }
 
-            File tempDir = new File(fmuDir);
-            if(tempDir.isDirectory()) {
+            java.nio.file.Path tempDir = Paths.get(fmuDir);
+            if(Files.exists(tempDir) && Files.isDirectory(tempDir)) {
                 try {
-                    FileUtils.deleteAll(tempDir);
+                       FileUtils.emptyDirectory(tempDir);
                 } catch (IOException e) {
-                    throw new FMILException("Could not create temp folder for fmu");
+                    throw new FMILException("Could not delete existing files from temp folder for fmu " + path, e);
                 }
-                tempDir.mkdir();
             } else {
-                tempDir.mkdir();
+                try {
+                       Files.createDirectory(tempDir);
+                } catch (IOException e) {
+                    throw new FMILException("Could not create temp folder for fmu " + path, e);
+                }
             }
 
-
             try {
-                String tmpPath = tempDir.getAbsolutePath();
+                String tmpPath = tempDir.toString();
                 if(!tmpPath.endsWith("\\") && !tmpPath.endsWith("/"))
                     tmpPath = tmpPath + "/";
                 id = loadFMUFile_(path, tmpPath);
@@ -495,10 +500,11 @@ public class FMIL {
         synchronized(syncObject) {
 
             try {
-
+               
                double[] results = new double[subscription.size()];
-                return getSubscribedResults_(getModelIDNew(), results);
-
+               Arrays.fill(results, Double.NaN);
+               
+               return getSubscribedResults_(getModelIDNew(), results);
             } catch (UnsatisfiedLinkError err) {
                 throw new FMILException(UNSATISFIED_LINK);
             } catch (Exception e) {