]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Importing TG files creates copies of files without deleting them. 17/4917/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 16 Aug 2022 12:45:33 +0000 (15:45 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 16 Aug 2022 12:45:33 +0000 (15:45 +0300)
Set TransferableGraphFileReader to delete temporary tg files.
Check that MigrationState is disposed where used.

gitlab #857

Change-Id: Ic31cf437c26b4a36f45f16455c6a3376fb8fc361

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationStateImpl.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java
bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphFileReader.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java

index fb8f8c9e7aef04f1e93f1f087ed5c297fa9879f7..3e531e368e512761cca1c4d8691d6d01c55cabc0 100644 (file)
@@ -161,8 +161,16 @@ public class MigrationStateImpl implements MigrationState {
             final boolean updateDependencies = MigrationUtils.getProperty(this, MigrationStateKeys.UPDATE_DEPENDENCIES, Boolean.TRUE);
 
             File temporaryTg = exportCurrentTgAsTemporaryFile(session, monitor);
-            if (temporaryTg != null)
-                setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg));
+            if (temporaryTg != null) {
+                TransferableGraphSource tgs = (TransferableGraphSource)properties.get(MigrationStateKeys.CURRENT_TGS);
+                if (tgs != null)
+                    try {
+                        tgs.close();
+                    } catch (IOException e) {
+                       Logger.defaultLogError(e);
+                    }
+                setProperty(MigrationStateKeys.CURRENT_TGS, initializeTransferableGraphSource(temporaryTg, true));
+            }
 
             TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);
             if (tgs != null) {
@@ -253,8 +261,11 @@ public class MigrationStateImpl implements MigrationState {
        }
 
        private TransferableGraphSource initializeTransferableGraphSource(File dataContainer) throws DatabaseException {
+               return initializeTransferableGraphSource(dataContainer, false);
+       }
+       private TransferableGraphSource initializeTransferableGraphSource(File dataContainer, boolean deleteOnClose) throws DatabaseException {
                try {
-                       StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer);
+                       StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(dataContainer, deleteOnClose);
                        TransferableGraphSource tgs = reader.readTG();
                        setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader);
                        setProperty(MigrationStateKeys.CURRENT_TGS, tgs);
index e1421df90a89054f23d0a4625665f90ccf8be527..7cd73366061033c5374b71a70212065428c6363d 100644 (file)
@@ -358,11 +358,13 @@ public class MigrationUtils {
        
         Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);
         if(roots.size() == 1) {
+               MigrationState state = newState();
             try {
                 TGTransferableGraphSource tgSource = new TGTransferableGraphSource(tg);
                 SharedOntologyImportAdvisor advisor = new SharedOntologyImportAdvisor(published);
 
-                MigrationState state = newState();
+                
+                
                 state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
                 state.setProperty(MigrationStateKeys.CURRENT_TGS, tgSource);
                 state.setProperty(MigrationStateKeys.SESSION, session);
@@ -383,6 +385,7 @@ public class MigrationUtils {
                        } catch (Exception e) {
                            throw new DatabaseException(e);
             } finally {
+               state.dispose();
                 session.getService(XSupport.class).setServiceMode(false, false);
             }
             
index 8c82c23bc69bb8a8f37ff6855e577ea050a2cb16..eb6294e3e98cece34c737406ba4ce2caa84128a2 100644 (file)
@@ -36,14 +36,24 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
        
        final private static int SIZE = 1<<18;
        
+       private boolean deleteOnClose = false;
+       private File file;
+       
        public StreamingTransferableGraphFileReader(File file) throws Exception {
+               this(file,false);
+       }
+       
+       public StreamingTransferableGraphFileReader(File file, boolean deleteOnClose) throws Exception {
                super(file, SIZE);
                if(init) {
                        init=false;
                        @SuppressWarnings("resource")
                        StreamingTransferableGraphFileReader r = new StreamingTransferableGraphFileReader(file, 0);
                        for(int i=0;i<40000;i++) r.readTG();
+                       r.close();
                }
+               this.file = file;
+               this.deleteOnClose = deleteOnClose;
        }
        
        public StreamingTransferableGraphFileReader(InputStream stream) throws Exception {
@@ -53,6 +63,7 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
                        @SuppressWarnings("resource")
                        StreamingTransferableGraphFileReader r = new StreamingTransferableGraphFileReader(stream, 0);
                        for(int i=0;i<40000;i++) r.readTG();
+                       r.close();
                }
        }
 
@@ -63,6 +74,7 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
                        @SuppressWarnings("resource")
                        StreamingTransferableGraphFileReader r = new StreamingTransferableGraphFileReader(channel, 0);
                        for(int i=0;i<40000;i++) r.readTG();
+                       r.close();
                }
        }
 
@@ -77,6 +89,14 @@ final public class StreamingTransferableGraphFileReader extends ByteFileReader {
        public StreamingTransferableGraphFileReader(File file, int size) throws IOException {
                super(file, size);
        }
+       
+       @Override
+       public void close() throws IOException {
+               super.close();
+               if (deleteOnClose && file != null && file.exists()) {
+                       file.delete();
+               }
+       }
 
        class FileTransferableGraphSource implements TransferableGraphSource {
 
index e7707dd407a8d4978aa8c54716ba5f1fe6604363..91660a4925667396d8b5240ad56951f50ec5d74b 100644 (file)
@@ -68,29 +68,32 @@ public class ModelImporter {
                 state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile);
                 state.setProperty(MigrationStateKeys.SESSION, session);
                 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
-
-                if (includeDependencies) {
-                    try {
-                        ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state);
-                        if (libraryDependenciesBean != null) {
-                            for (ModelDependency dependency : libraryDependenciesBean.dependencies) {
-                                Resource existing = session.sync(new PossibleResource(dependency.uri));
-                                boolean isExternalEntity = existing != null && session.syncRequest(new IsExternalEntity(existing));
-                                if (existing == null || isExternalEntity) {
-                                    MigrationUtils.importSharedOntology(session, dependency.tg, false);
-                                }
-                            }
-                        }
-                    } catch (AdaptException e) {
-                        Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e));
-                    }
+                try {
+                       if (includeDependencies) {
+                           try {
+                               ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state);
+                               if (libraryDependenciesBean != null) {
+                                   for (ModelDependency dependency : libraryDependenciesBean.dependencies) {
+                                       Resource existing = session.sync(new PossibleResource(dependency.uri));
+                                       boolean isExternalEntity = existing != null && session.syncRequest(new IsExternalEntity(existing));
+                                       if (existing == null || isExternalEntity) {
+                                           MigrationUtils.importSharedOntology(session, dependency.tg, false);
+                                       }
+                                   }
+                               }
+                           } catch (AdaptException e) {
+                               Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e));
+                           }
+                       }
+       
+                       MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
+       
+                       Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
+                       ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
+                       return new MigratedImportResult(resultRoots, result);
+                } finally {
+                   state.dispose();
                 }
-
-                MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
-
-                Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
-                ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
-                return new MigratedImportResult(resultRoots, result);
             }
         };