]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.project/src/org/simantics/project/management/PlatformUtil.java
Support ontology install option trueWhenDeployed also during development
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / PlatformUtil.java
index 9dfa67efef7be394081c583f325b09ed63dfea4e..8f93d24ab293d937467e78ca233a0056c5462abe 100644 (file)
@@ -26,8 +26,8 @@ import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Enumeration;
-import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicReference;
@@ -45,22 +45,20 @@ import org.eclipse.equinox.p2.metadata.VersionedId;
 import org.osgi.framework.Bundle;
 import org.simantics.databoard.Bindings;
 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.container.FormatHandler;
 import org.simantics.graph.compiler.CompilationResult;
 import org.simantics.graph.compiler.GraphCompiler;
 import org.simantics.graph.compiler.GraphCompilerPreferences;
 import org.simantics.graph.compiler.ValidationMode;
+import org.simantics.graph.compiler.internal.ltk.FileSource;
+import org.simantics.graph.compiler.internal.ltk.ISource;
+import org.simantics.graph.compiler.internal.ltk.Problem;
 import org.simantics.graph.representation.Extensions;
 import org.simantics.graph.representation.TransferableGraph1;
-import org.simantics.ltk.FileSource;
-import org.simantics.ltk.ISource;
-import org.simantics.ltk.Problem;
+import org.simantics.graph.representation.TransferableGraphFileReader;
 import org.simantics.scl.reflection.OntologyVersions;
-import org.simantics.utils.datastructures.ArrayMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -220,11 +218,23 @@ public class PlatformUtil {
                                LOGGER.error("Extraction to " + fileName + " failed from url " + url, e);
                        }
                } else {
-                       LOGGER.error("Unsupported URL protocol '" + url + "' for FastLZ native library file '" + fileName);
+                       LOGGER.error("Unsupported URL protocol '" + url + "' for reading as file '" + fileName + "'");
                }       
                return null;
        }
 
+       public static Collection<URL> getTGFiles(Bundle b) {
+               Enumeration<URL> enu = b.findEntries("/", "*.tg", false);
+               if (enu == null)
+                       return Collections.emptyList();
+               if (!enu.hasMoreElements())
+                       return Collections.emptyList();
+               ArrayList<URL> result = new ArrayList<>();
+               while (enu.hasMoreElements())
+                       result.add(enu.nextElement());
+               return result;
+       }
+
        public static void compile(Bundle b) throws Exception {
 
                Collection<ISource> sources = new ArrayList<>();
@@ -232,10 +242,10 @@ public class PlatformUtil {
 
                for (Bundle b2 : getBundles()) {
                        if(b.equals(b2)) continue;
-                       URL url = b2.getEntry("graph.tg");
-                       if (url==null) continue;
-                       File graphFile = url2file(FileLocator.resolve(b2.getEntry("/graph.tg")), b2.toString());
-                       dependencies.add(GraphCompiler.read(graphFile));
+                       for (URL url : getTGFiles(b2)) {
+                               File graphFile = url2file(url, b2.toString() + url.toString());
+                               dependencies.add(GraphCompiler.read(graphFile));
+                       }
                }
 
                File bundleFile = FileLocator.getBundleFile(b);
@@ -315,22 +325,18 @@ public class PlatformUtil {
                AtomicReference<IOException> problem = new AtomicReference<>();
 
                Collection<GraphBundle> gbundles = Arrays.stream(getBundles())
-                               // #7806: Due to databoard Binding/Serializer construction process thread-unsafety
-                               // not even the DataContainer.readHeader invocations can run in parallel, most likely
-                               // due to recurring serializer construction for Variant datatypes.
-                               // Therefore, we must disable parallel loading for now.
-                               //.parallel()
+                               .parallel()
                                .map(b -> {
                                        try {
-                                               return problem.get() == null ? getGraph(b) : null;
+                                               return problem.get() == null ? getGraphs(b) : Collections.<GraphBundleEx>emptyList();
                                        } catch (IOException e) {
-                                               if (LOGGER.isDebugEnabled())
+                                               if (LOGGER.isWarnEnabled())
                                                        LOGGER.debug("Could not get graph from bundle {}", b, e);
                                                problem.set(e);
-                                               return null;
+                                               return Collections.<GraphBundleEx>emptyList();
                                        }
                                })
-                               .filter(Objects::nonNull)
+                               .flatMap(Collection::stream)
                                .collect(Collectors.toList());
 
                if (problem.get() != null)
@@ -351,6 +357,28 @@ public class PlatformUtil {
                return getGraph( bundle );
        }
 
+       public static Collection<GraphBundleEx> getGraphs(Bundle bundle) throws IOException {
+               return getTGFiles(bundle).stream()
+                       .map(url -> {
+                               try {
+                                       GraphBundleEx result = tryGetOnDemandGraph(bundle, url);
+                                       return result != null ? result : getCompleteGraph(bundle, url);
+                               } catch (IOException e) {
+                                       if (LOGGER.isWarnEnabled())
+                                               LOGGER.warn("Could not get graph from bundle url {}", url, e);
+                                       return null;
+                               }
+                       })
+                       .filter(Objects::nonNull)
+                       .collect(Collectors.toList());
+       }
+
+       private static String tgFileId(Bundle bundle, URL url) {
+               String urlString = url.toString();
+               String file = urlString.substring(urlString.lastIndexOf("/") + 1);
+               return bundle.getSymbolicName() + "_" + file;
+       }
+
        /**
         * Read the graph in a graph bundle. Graph is read from "graph.tg" file in the root.
         * 
@@ -368,7 +396,7 @@ public class PlatformUtil {
 
        private static GraphBundleEx getCompleteGraph(Bundle bundle, URL url) throws IOException {
                try {
-                       String id = bundle.getSymbolicName();
+                       String id = tgFileId(bundle, url);
                        return new GraphBundleEx(
                                        getBundleName(bundle, id),
                                        readTG(url),
@@ -392,8 +420,10 @@ public class PlatformUtil {
        private static GraphBundleEx tryGetOnDemandGraph(Bundle bundle, URL url) throws IOException {
                try {
                        Integer cachedHash = readCachedHash(url);
-                       if (cachedHash == null)
+                       if (cachedHash == null) {
+                               LOGGER.info("No cached hash for " + bundle);
                                return null;
+                       }
 
                        Supplier<TransferableGraph1> graphSource = () -> {
                                try {
@@ -406,7 +436,7 @@ public class PlatformUtil {
                                }
                        };
 
-                       String id = bundle.getSymbolicName();
+                       String id = tgFileId(bundle, url);
 
                        return new GraphBundleEx(
                                        getBundleName(bundle, id),
@@ -418,40 +448,10 @@ public class PlatformUtil {
                        throw new IOException("Problem loading graph.tg from bundle " + bundle.getSymbolicName(), e);
                }
        }
-       
-       private static FormatHandler<TransferableGraph1> FORMAT_HANDLER = new FormatHandler<TransferableGraph1>() {
-        @Override
-        public Binding getBinding() {
-            return TransferableGraph1.BINDING;
-        }
-        @Override
-        public TransferableGraph1 process(DataContainer container) throws Exception {
-            return (TransferableGraph1) container.content.getValue(TransferableGraph1.BINDING);
-        }
-    };
-
-       @SuppressWarnings("unchecked")
-       private static Map<String, FormatHandler<TransferableGraph1>> handlers = ArrayMap.make(
-                       new String[] {
-                                       "graph:1",
-                                       "sharedLibrary:1"
-                       },
-                       FORMAT_HANDLER,
-                       FORMAT_HANDLER);
-
-       private static TransferableGraph1 readTG(InputStream is) throws Exception {
-               // For an unknown reason this is totally broken when running the TestSCLOsgi
-               // in the SDK Tycho build. It returns incomplete results because the
-               // ReadableByteChannel used by ByteFileReader starts returning 0 unexpectedly.
-//                             try (TransferableGraphFileReader reader = new TransferableGraphFileReader(is)) {
-//                     return reader.readTG();
-//             }
-               return DataContainers.readFile(new DataInputStream(is), handlers);
-       }
 
        private static TransferableGraph1 readTG(URL url) throws Exception {
                try (InputStream is = url.openStream()) {
-                       return readTG(is);
+                       return TransferableGraphFileReader.read(is);
                }
        }
 
@@ -479,7 +479,18 @@ public class PlatformUtil {
 
        private static boolean isImmutable(Bundle bundle) {
                String immutable = (String) bundle.getHeaders().get("Immutable");
-               return immutable != null ? "true".equals(immutable) : true;
+               if(immutable == null)
+                       return true;
+               if("false".equals(immutable))
+                       return false;
+               if("trueWhenDeployed".equals(immutable)) {
+                       String installHint = System.getProperty("installOntologiesAsDeployed");
+                       if("true".equals(installHint))
+                               return true;
+                       else
+                               return false;
+               }
+               return true;
        }
 
        public static class TGInfo {