]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Some fixes for FileImportService to throw exceptions forward 23/323/1
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 2 Feb 2017 14:59:38 +0000 (16:59 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Thu, 2 Feb 2017 14:59:38 +0000 (16:59 +0200)
refs #7014

Change-Id: I03350d056b6258e964a990c50973f2597be5080d

bundles/org.simantics.fileimport.ui/META-INF/MANIFEST.MF
bundles/org.simantics.fileimport.ui/src/org/simantics/fileimport/ui/ImportFileHandler.java
bundles/org.simantics.fileimport/META-INF/MANIFEST.MF
bundles/org.simantics.fileimport/scl/Dropins/Core.scl
bundles/org.simantics.fileimport/src/org/simantics/fileimport/Activator.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileImportService.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/FileReferenceFileImport.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/LibraryFolderFileImport.java
bundles/org.simantics.fileimport/src/org/simantics/fileimport/scl/DropinsSCL.java
bundles/org.simantics.spreadsheet.fileimport/src/org/simantics/spreadsheet/fileimport/ExcelFileImport.java

index 94cc4b9d270782b7654d5c19fe365f2602fb87e1..db1100bc370c780c823d029993b6c597e4c36540 100644 (file)
@@ -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
index 3da6858a3f35ef51021632aab00ecfb95d07cb2d..21f8d3c4d8b6dc52767e3b1b9b5b8357df192191 100644 (file)
@@ -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<Throwable>) t -> {
+            LOGGER.error("Could not import file " + fileName, t);
+        }));
     }
 }
\ No newline at end of file
index bbb632778cee2fbd035dbab3e06afaf990d0d12a..a8d8f0752c54d45171227163e5d40f34f0ffc570 100644 (file)
@@ -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
index 7b3c354a35ddd40e73c9302a9a1af7c900b4559e..878326ba1e4ee6c7b42e99cc7f469f17071629f5 100644 (file)
@@ -7,6 +7,10 @@ importJava "org.simantics.fileimport.scl.DropinsSCL" where
     getUploadedFiles :: () -> <Proc> MMap.T String Long
     removeFileForId :: Long -> <Proc> ()
 
+
+importJava "org.simantics.fileimport.FileImportService" where
+    performFileImport :: String -> String -> <Proc> String
+
 getUploadedDropinFiles :: () -> <Proc> [Long]
 getUploadedDropinFiles dummy = do
     files = getUploadedFiles ()
index 9c0f77072c2cdbecb0071f3ac8f9022e36837902..2918674518aade7bed60e1780686ab5c8d18efea 100644 (file)
@@ -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;
+    }
+
 }
index fcbd3cc30c33a04f87993e8307a1fda32b895fc4..ddb9dabae71478a184ef9845af5debd7097327a8 100644 (file)
@@ -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<Throwable> {
+
+        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<Consumer<Throwable>> callback) {
-        if (file.getFileName().toString().equals(DB_FILE))
-            return;
+    public static String performFileImport(Path file, Optional<Consumer<Throwable>> callback) {
+        if (file.getFileName().toString().equals(DB_FILE)) {
+            return null;
+        }
+        String result = "Import failed";
         Optional<IGenericFileImport> serviceOp = findServiceForFileExtension(file);
-        serviceOp.ifPresent(service -> {
+        if (serviceOp.isPresent()) {
+            IGenericFileImport service = serviceOp.get();
             try {
                 Optional<String> 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;
     }
 
     
index 2fea9fde3dd2baac7146293af64b0c640862d513..aa367178125860447af3614a4a01086e3da77ea0 100644 (file)
@@ -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<String, String> ALLOWED_EXTENSIONS = Collections.singletonMap("*.asd", "All files");
+    private static final Map<String, String> ALLOWED_EXTENSIONS = Collections.singletonMap("*", "All files");
     
     @Override
-    public Optional<Resource> perform(Resource parent, Path file) {
-        try {
-            return Optional.of(GraphFileUtil.createFileReference(parent, file));
-        } catch (DatabaseException e) {
-            e.printStackTrace();
-            return Optional.empty();
-        }
+    public Optional<Resource> perform(Resource parent, Path file) throws Exception {
+        return Optional.of(GraphFileUtil.createFileReference(parent, file));
     }
 
     @Override
index 40deb1c148a7c5652ae0569d8e1606793b1d2c85..012c757c214c4a2914703fa6c6341e0ed6d185c8 100644 (file)
@@ -22,19 +22,14 @@ public class LibraryFolderFileImport extends SimanticsResourceFileImport {
     }
 
     @Override
-    public Optional<Resource> perform(Resource parent, Path file) {
+    public Optional<Resource> perform(Resource parent, Path file) throws Exception {
         final String name = file.getFileName().toString();
-        try {
-            return Optional.of(Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
+        return Optional.of(Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
 
-                @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);
+            }
+        }));
     }
 }
index 74d2a0322f45c426e0fffa1434e888641e69632c..50fd584466bedd5849d950b9c778f80b16441fa3 100644 (file)
@@ -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);
         }
     }
 
index 3963f6e7b1b6d0857b8d58d8e68fd67c682d4c47..6719f5913fd6c288a741b6c0359d69d0eb47444b 100644 (file)
@@ -18,8 +18,8 @@ public class ExcelFileImport extends SimanticsResourceFileImport {
     }
     
     @Override
-    public Optional<Resource> perform(Resource parent, Path file) {
-        return Optional.empty();
+    public Optional<Resource> perform(Resource parent, Path file) throws Exception {
+        throw new UnsupportedOperationException("Excel import is not yet supported");
     }
 
     @Override