]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/management/PlatformUtil.java
Improve startup time for fresh or rollback'd session in index writing
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / PlatformUtil.java
index d962ba090d9919cebc6b0f00a2bffefa5e06489e..ebd987e3f2b5238e38ba4ace5ce1944a9eea6e55 100644 (file)
@@ -25,11 +25,16 @@ import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Enumeration;
 import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
+import java.util.stream.Collectors;
 
 import org.eclipse.core.internal.runtime.PlatformActivator;
 import org.eclipse.core.runtime.FileLocator;
@@ -38,13 +43,10 @@ import org.eclipse.equinox.p2.metadata.IVersionedId;
 import org.eclipse.equinox.p2.metadata.Version;
 import org.eclipse.equinox.p2.metadata.VersionedId;
 import org.osgi.framework.Bundle;
-import org.simantics.databoard.adapter.AdaptException;
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.databoard.container.DataContainer;
 import org.simantics.databoard.container.DataContainers;
-import org.simantics.databoard.serialization.SerializationException;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.graph.compiler.CompilationResult;
 import org.simantics.graph.compiler.GraphCompiler;
 import org.simantics.graph.compiler.GraphCompilerPreferences;
@@ -54,6 +56,8 @@ import org.simantics.ltk.FileSource;
 import org.simantics.ltk.ISource;
 import org.simantics.ltk.Problem;
 import org.simantics.scl.reflection.OntologyVersions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This class contains utilities for managing bundles in a active platform. 
@@ -62,7 +66,8 @@ import org.simantics.scl.reflection.OntologyVersions;
 @SuppressWarnings("restriction")
 public class PlatformUtil {
 
-       
+       private static final Logger LOGGER = LoggerFactory.getLogger(PlatformUtil.class);
+
        /**
         * Get all bundles in the platform.
         * 
@@ -204,19 +209,19 @@ public class PlatformUtil {
                                File path = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
                                return path;
                        } catch (UnsupportedEncodingException e) {
-                               Logger.defaultLogError(e);
+                               LOGGER.error("Failed to decode " + url, e);
                        }
                } else if ("jar".equals(url.getProtocol())) {
                        try {
                                File libFile = extractLib(url, fileName);
                                return libFile;
                        } catch (FileNotFoundException e) {
-                               Logger.defaultLogError(e);
+                               LOGGER.error("Extraction to " + fileName + " failed, url not found: " + url, e);
                        } catch (IOException e) {
-                               Logger.defaultLogError(e);
+                               LOGGER.error("Extraction to " + fileName + " failed from url " + url, e);
                        }
                } else {
-                       System.err.println("Unsupported URL protocol '" + url + "' for FastLZ native library file '" + fileName);
+                       LOGGER.error("Unsupported URL protocol '" + url + "' for FastLZ native library file '" + fileName);
                }       
                return null;
        }
@@ -265,7 +270,7 @@ public class PlatformUtil {
                        errorStringBuilder.append(problem.getLocation() + ": " + problem.getDescription() + "\n");
 
                if(errorStringBuilder.length() > 0) {
-                       Logger.defaultLogError(errorStringBuilder.toString());
+                       LOGGER.error(errorStringBuilder.toString());
                } else {
                        DataContainers.writeFile(new File(bundleFile, "graph.tg"), 
                                        new DataContainer("graph", 1, new Variant(TransferableGraph1.BINDING, result.getGraph())));
@@ -296,7 +301,7 @@ public class PlatformUtil {
                                                }
                                        }
                                } catch (Throwable e) {
-                                       Logger.defaultLogError(e);
+                                       LOGGER.error("Failed to compile dynamic ontologies in bundle " + bundle.getSymbolicName(), e);
                                }
                        }
                }
@@ -308,12 +313,30 @@ public class PlatformUtil {
         * @param collection
         * @throws IOException
         */
-       public static void getAllGraphs(Collection<GraphBundle> collection) throws IOException {
-               for (Bundle bundle : getBundles()) {
-                       GraphBundle entry = getGraph(bundle);
-                       if (entry!=null) collection.add(entry);
-               }
-       }
+    public static Collection<GraphBundle> getAllGraphs() throws IOException {
+        CompletableFuture<Object> f = new CompletableFuture<>();
+        Bundle[] bundles = getBundles();
+        Collection<GraphBundle> gbundles = Arrays.stream(bundles).map(t -> { // this could be done in parallel in the future?
+            if (f.isCompletedExceptionally())
+                return null;
+            try {
+                return PlatformUtil.getGraph(t);
+            } catch (IOException e) {
+                if (LOGGER.isDebugEnabled())
+                    LOGGER.debug("Could not get graph {}", t, e);
+                f.completeExceptionally(e);
+                return null;
+            }
+        }).filter(Objects::nonNull).collect(Collectors.toList());
+        if (f.isCompletedExceptionally()) {
+            try {
+                f.get();
+            } catch (ExecutionException | InterruptedException e) {
+                throw (IOException) e.getCause();
+            }
+        }
+        return gbundles;
+    }
 
        /**
         * Get bundle 
@@ -370,24 +393,20 @@ public class PlatformUtil {
                        GraphBundleEx entry = new GraphBundleEx(name, graph, vid, isImmutable);
 //                     System.out.println("getGraph(" + bundle.getSymbolicName() + "): completed in " + (System.nanoTime()-start)*1e-6 + "ms");
                        return entry;
-               } catch (SerializationException e) {
-                       throw new IOException(e);
-               } catch (IOException e) {
-                       throw new IOException("Problem loading graph.tg from bundle " + bundle.getSymbolicName(), e);
-               } catch (RuntimeException e) {
-                       throw new IOException("Problem loading graph.tg from bundle " + bundle.getSymbolicName(), e);
-               } catch (AdaptException e) {
+               } catch (Exception e) {
                        throw new IOException("Problem loading graph.tg from bundle " + bundle.getSymbolicName(), e);
+               } catch (Error e) {
+                       LOGGER.error("Serious problem loading graph.tg from bundle " + bundle.getSymbolicName(), e);
+                       throw e;
                } finally {
                        is.close();
                }
        }
-       
+
        public static class TGInfo {
                public Bundle bundle;
                public URL location;
                public IVersionedId vid;
        }
-       
-}
 
+}
\ No newline at end of file