X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.fileimport%2Fsrc%2Forg%2Fsimantics%2Ffileimport%2FFileImportService.java;h=ddb9dabae71478a184ef9845af5debd7097327a8;hb=fcaaf23487d5aacf4fb2482e61ddbc55112ca17b;hp=fcbd3cc30c33a04f87993e8307a1fda32b895fc4;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git 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; }