Support for multiple installable TG files in single bundle 43/1943/4
authorAntti Villberg <antti.villberg@semantum.fi>
Mon, 23 Jul 2018 16:25:19 +0000 (19:25 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 31 Jul 2018 13:47:26 +0000 (13:47 +0000)
gitlab #50

Change-Id: Id3f11ec8fbc4a3276fe7c48e538996542e768312

bundles/org.simantics.project/src/org/simantics/project/management/PlatformUtil.java

index d5c88782a87e42d980773fd8542d8af087327e46..2f94ff967bdcc2c89b82ecb954468ddafac91407 100644 (file)
@@ -26,6 +26,7 @@ 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.Entry;
 import java.util.Objects;
@@ -217,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<>();
@@ -229,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);
@@ -319,15 +332,15 @@ public class PlatformUtil {
                                //.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)
@@ -348,6 +361,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.
         * 
@@ -365,7 +400,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),
@@ -405,7 +440,7 @@ public class PlatformUtil {
                                }
                        };
 
-                       String id = bundle.getSymbolicName();
+                       String id = tgFileId(bundle, url);
 
                        return new GraphBundleEx(
                                        getBundleName(bundle, id),