Set TransferableGraphFileReader to delete temporary tg files.
Check that MigrationState is disposed where used.
gitlab #857
Change-Id: Ic31cf437c26b4a36f45f16455c6a3376fb8fc361
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) {
}
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);
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);
} catch (Exception e) {
throw new DatabaseException(e);
} finally {
+ state.dispose();
session.getService(XSupport.class).setServiceMode(false, false);
}
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 {
@SuppressWarnings("resource")
StreamingTransferableGraphFileReader r = new StreamingTransferableGraphFileReader(stream, 0);
for(int i=0;i<40000;i++) r.readTG();
+ r.close();
}
}
@SuppressWarnings("resource")
StreamingTransferableGraphFileReader r = new StreamingTransferableGraphFileReader(channel, 0);
for(int i=0;i<40000;i++) r.readTG();
+ r.close();
}
}
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 {
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);
}
};