From fcaaf23487d5aacf4fb2482e61ddbc55112ca17b Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Thu, 2 Feb 2017 16:59:38 +0200 Subject: [PATCH] Some fixes for FileImportService to throw exceptions forward refs #7014 Change-Id: I03350d056b6258e964a990c50973f2597be5080d --- .../META-INF/MANIFEST.MF | 3 +- .../fileimport/ui/ImportFileHandler.java | 10 +++- .../META-INF/MANIFEST.MF | 3 +- .../scl/Dropins/Core.scl | 4 ++ .../org/simantics/fileimport/Activator.java | 13 ++++- .../fileimport/FileImportService.java | 56 ++++++++++++++++--- .../fileimport/FileReferenceFileImport.java | 12 +--- .../fileimport/LibraryFolderFileImport.java | 19 +++---- .../simantics/fileimport/scl/DropinsSCL.java | 9 ++- .../fileimport/ExcelFileImport.java | 4 +- 10 files changed, 97 insertions(+), 36 deletions(-) diff --git a/bundles/org.simantics.fileimport.ui/META-INF/MANIFEST.MF b/bundles/org.simantics.fileimport.ui/META-INF/MANIFEST.MF index 94cc4b9d2..db1100bc3 100644 --- a/bundles/org.simantics.fileimport.ui/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.fileimport.ui/META-INF/MANIFEST.MF @@ -9,7 +9,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.e4.ui.model.workbench;bundle-version="1.1.100.v20150407-1430", org.eclipse.e4.core.di, org.eclipse.e4.ui.services, - org.simantics.fileimport;bundle-version="1.0.0" + org.simantics.fileimport;bundle-version="1.0.0", + org.slf4j.api Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Import-Package: javax.inject;version="1.0.0" Bundle-ActivationPolicy: lazy diff --git a/bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java b/bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java index 3da6858a3..21f8d3c4d 100644 --- a/bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java +++ b/bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java @@ -4,6 +4,7 @@ package org.simantics.fileimport.ui; import java.nio.file.Paths; import java.util.Map; import java.util.Optional; +import java.util.function.Consumer; import javax.inject.Named; @@ -14,9 +15,13 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; import org.simantics.fileimport.FileImportService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ImportFileHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(ImportFileHandler.class); + @CanExecute public boolean canExecute() { return !FileImportService.supportedExtensionsWithFilters().isEmpty(); @@ -44,6 +49,9 @@ public class ImportFileHandler { final String fileName = dialog.open(); if (fileName == null) return; - FileImportService.performFileImport(Paths.get(fileName), Optional.empty()); + + FileImportService.performFileImport(Paths.get(fileName), Optional.of((Consumer) t -> { + LOGGER.error("Could not import file " + fileName, t); + })); } } \ No newline at end of file diff --git a/bundles/org.simantics.fileimport/META-INF/MANIFEST.MF b/bundles/org.simantics.fileimport/META-INF/MANIFEST.MF index bbb632778..a8d8f0752 100644 --- a/bundles/org.simantics.fileimport/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.fileimport/META-INF/MANIFEST.MF @@ -10,7 +10,8 @@ Require-Bundle: org.eclipse.core.runtime, org.simantics, org.simantics.graphfile;bundle-version="0.1.0", org.simantics.graphfile.ontology;bundle-version="0.1.0", - org.simantics.modeling;bundle-version="1.1.1" + org.simantics.modeling;bundle-version="1.1.1", + org.slf4j.api Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Bundle-ActivationPolicy: lazy Export-Package: org.simantics.fileimport diff --git a/bundles/org.simantics.fileimport/scl/Dropins/Core.scl b/bundles/org.simantics.fileimport/scl/Dropins/Core.scl index 7b3c354a3..878326ba1 100644 --- a/bundles/org.simantics.fileimport/scl/Dropins/Core.scl +++ b/bundles/org.simantics.fileimport/scl/Dropins/Core.scl @@ -7,6 +7,10 @@ importJava "org.simantics.fileimport.scl.DropinsSCL" where getUploadedFiles :: () -> MMap.T String Long removeFileForId :: Long -> () + +importJava "org.simantics.fileimport.FileImportService" where + performFileImport :: String -> String -> String + getUploadedDropinFiles :: () -> [Long] getUploadedDropinFiles dummy = do files = getUploadedFiles () diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/Activator.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/Activator.java index 9c0f77072..291867451 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/Activator.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/Activator.java @@ -13,8 +13,9 @@ import org.simantics.fileimport.dropins.FileImportDropins; public class Activator implements BundleActivator { - private static BundleContext context; + private static BundleContext context; + private static Path modelsFolder = null; private static Path dropinsFolder = null; static BundleContext getContext() { @@ -48,4 +49,14 @@ public class Activator implements BundleActivator { return dropinsFolder; } + public static Path getModelsFolder() throws IOException { + if (modelsFolder == null) { + IPath state = Platform.getStateLocation(context.getBundle()); + modelsFolder = Paths.get(state.append("models").toOSString()); + if (!Files.exists(modelsFolder)) + Files.createDirectories(modelsFolder); + } + return modelsFolder; + } + } diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java index fcbd3cc30..ddb9dabae 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java @@ -17,7 +17,11 @@ import java.util.function.Consumer; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; +import org.simantics.databoard.util.Base64; import org.simantics.fileimport.dropins.FileImportDropins; +import org.simantics.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Utility class for Simantics File import functions @@ -27,6 +31,8 @@ import org.simantics.fileimport.dropins.FileImportDropins; */ public class FileImportService { + private static final Logger LOGGER = LoggerFactory.getLogger(FileImportService.class); + private FileImportService() {} public static final String DB_FILE = ".simanticsdb"; @@ -37,7 +43,7 @@ public class FileImportService { serviceReferences = Activator.getContext().getAllServiceReferences(IGenericFileImport.class.getName(), null); } catch (InvalidSyntaxException e) { - e.printStackTrace(); + LOGGER.error("Could not get service references for IGenericFileImport!", e); } if (serviceReferences.length == 0) return Collections.emptyList(); @@ -63,6 +69,33 @@ public class FileImportService { return extensionsWithFilters; } + + private static class ConsumerHolder implements Consumer { + + private Throwable throwable; + + @Override + public void accept(Throwable t) { + throwable = t; + } + + public Throwable getThrowable() { + return throwable; + } + + } + + public static String performFileImport(String base64, String name) throws Throwable { + byte[] bytes = Base64.decode(base64); + Path file = Activator.getModelsFolder().resolve(name); + Files.write(file, bytes); + + ConsumerHolder holder = new ConsumerHolder(); + String result = performFileImport(file, Optional.of(holder)); + if (holder.getThrowable() != null) + throw holder.getThrowable(); + return result; + } /** * Method that performs the import of the given file. This method is called when e.g. {@link FileImportDropins} watcher detects {@link java.nio.file.StandardWatchEventKinds.ENTRY_CREATE} operation @@ -70,22 +103,31 @@ public class FileImportService { * @param file Path file to be imported * @param callback Optional callback which can be used to catch Throwables thrown in the import process */ - public static void performFileImport(Path file, Optional> callback) { - if (file.getFileName().toString().equals(DB_FILE)) - return; + public static String performFileImport(Path file, Optional> callback) { + if (file.getFileName().toString().equals(DB_FILE)) { + return null; + } + String result = "Import failed"; Optional serviceOp = findServiceForFileExtension(file); - serviceOp.ifPresent(service -> { + if (serviceOp.isPresent()) { + IGenericFileImport service = serviceOp.get(); try { Optional resource = service.perform(file); saveResourceForPath(file, resource); + result = resource.get(); } catch (Throwable t) { if (callback.isPresent()) { callback.get().accept(t); } else { - t.printStackTrace(); + LOGGER.error("Could not import file " + file, t); } } - }); + } else { + LOGGER.warn("Could not find service for importing file " + file); + if (callback.isPresent()) + callback.get().accept(new Exception("Could not find IGenericFileImport service for file " + file)); + } + return result; } diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileReferenceFileImport.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileReferenceFileImport.java index 2fea9fde3..aa3671781 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileReferenceFileImport.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileReferenceFileImport.java @@ -6,21 +6,15 @@ import java.util.Map; import java.util.Optional; import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; import org.simantics.graphfile.util.GraphFileUtil; public class FileReferenceFileImport extends SimanticsResourceFileImport { - private static final Map ALLOWED_EXTENSIONS = Collections.singletonMap("*.asd", "All files"); + private static final Map ALLOWED_EXTENSIONS = Collections.singletonMap("*", "All files"); @Override - public Optional perform(Resource parent, Path file) { - try { - return Optional.of(GraphFileUtil.createFileReference(parent, file)); - } catch (DatabaseException e) { - e.printStackTrace(); - return Optional.empty(); - } + public Optional perform(Resource parent, Path file) throws Exception { + return Optional.of(GraphFileUtil.createFileReference(parent, file)); } @Override diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/LibraryFolderFileImport.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/LibraryFolderFileImport.java index 40deb1c14..012c757c2 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/LibraryFolderFileImport.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/LibraryFolderFileImport.java @@ -22,19 +22,14 @@ public class LibraryFolderFileImport extends SimanticsResourceFileImport { } @Override - public Optional perform(Resource parent, Path file) { + public Optional perform(Resource parent, Path file) throws Exception { final String name = file.getFileName().toString(); - try { - return Optional.of(Simantics.getSession().syncRequest(new WriteResultRequest() { + return Optional.of(Simantics.getSession().syncRequest(new WriteResultRequest() { - @Override - public Resource perform(WriteGraph graph) throws DatabaseException { - return ModelingUtils.createLibrary(graph, parent, name); - } - })); - } catch (DatabaseException e) { - e.printStackTrace(); - return Optional.empty(); - } + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + return ModelingUtils.createLibrary(graph, parent, name); + } + })); } } diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java index 74d2a0322..50fd58446 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java @@ -21,6 +21,8 @@ import org.simantics.fileimport.FileImportService; import org.simantics.fileimport.dropins.FileImportDropins; import org.simantics.layer0.Layer0; import org.simantics.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * SCL interface for Simantics File Import Functionality @@ -30,6 +32,8 @@ import org.simantics.utils.FileUtils; * */ public class DropinsSCL { + + private static final Logger LOGGER = LoggerFactory.getLogger(DropinsSCL.class); public static void watchDropinsFolder() { FileImportDropins.watchDropinsFolder(); @@ -42,8 +46,9 @@ public class DropinsSCL { public static void uploadToDropinsBase64(String base64, String fileName) { // ensure that watcher is awake FileImportDropins.watchDropinsFolder(); + Path rootFolder = null; try { - Path rootFolder = Activator.getDropinsFolder(); + rootFolder = Activator.getDropinsFolder(); Path newFile = rootFolder.resolve(fileName); if (Files.exists(newFile)) { newFile = findFreshFileName(rootFolder, fileName); @@ -51,7 +56,7 @@ public class DropinsSCL { byte[] bytes = Base64.decode(base64); FileUtils.writeFile(newFile.toFile(), bytes); } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("Could not upload base64 to file " + (rootFolder != null ? rootFolder.resolve(fileName).toAbsolutePath() : ""), e); } } diff --git a/bundles/org.simantics.spreadsheet.fileimport/src/org/simantics/spreadsheet/fileimport/ExcelFileImport.java b/bundles/org.simantics.spreadsheet.fileimport/src/org/simantics/spreadsheet/fileimport/ExcelFileImport.java index 3963f6e7b..6719f5913 100644 --- a/bundles/org.simantics.spreadsheet.fileimport/src/org/simantics/spreadsheet/fileimport/ExcelFileImport.java +++ b/bundles/org.simantics.spreadsheet.fileimport/src/org/simantics/spreadsheet/fileimport/ExcelFileImport.java @@ -18,8 +18,8 @@ public class ExcelFileImport extends SimanticsResourceFileImport { } @Override - public Optional perform(Resource parent, Path file) { - return Optional.empty(); + public Optional perform(Resource parent, Path file) throws Exception { + throw new UnsupportedOperationException("Excel import is not yet supported"); } @Override -- 2.47.0