]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
WIP: Some old-ish adapter registry initialization improvements 53/2653/2
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 18 Feb 2019 06:50:58 +0000 (08:50 +0200)
committerJani Simomaa <jani.simomaa@semantum.fi>
Mon, 18 Feb 2019 06:51:17 +0000 (06:51 +0000)
gitlab #189

Change-Id: Ibcae41336b9d24cdf34444702b9319ae0c2ce34e

bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/AdapterRegistry2.java

index 0e52b8a417da4ebecf82c27369cf7581171c58e4..578b42a61d885d13cfb06e20e7c31436d00a6af2 100644 (file)
@@ -16,9 +16,10 @@ import java.io.StringReader;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -48,6 +49,9 @@ import org.simantics.db.services.adaption.reflection.StaticMethodAdapter;
 import org.simantics.db.services.adaption.reflection.ThisResource2;
 import org.simantics.scl.reflection.OntologyVersions;
 import org.simantics.utils.FileUtils;
+import org.simantics.utils.threads.ThreadUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
@@ -60,6 +64,8 @@ import org.xml.sax.SAXParseException;
 
 public class AdapterRegistry2 {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(AdapterRegistry2.class);
+
     public static final String ADAPTERS_FILE = "adapters.xml";
 
     public static final String ADAPTERS = "adapters";
@@ -77,8 +83,7 @@ public class AdapterRegistry2 {
     public static final String CONSTRUCTOR = "constructor";
 
     static private AdapterRegistry2 instance = new AdapterRegistry2();
-    Collection<AdapterInstaller> installers = new ArrayList<AdapterInstaller>();
-    Map<AdapterInstaller, String> installerSources = new HashMap<AdapterInstaller, String>();
+    ConcurrentHashMap<AdapterInstaller, String> installerSources = new ConcurrentHashMap<>();
     Collection<Exception> exceptions = new ArrayList<Exception>();
 
     public static AdapterRegistry2 getInstance() {
@@ -86,11 +91,10 @@ public class AdapterRegistry2 {
     }
 
     private void addInstaller(AdapterInstaller installer, String sourceDesc) {
-        installers.add(installer);
         installerSources.put(installer, sourceDesc);
     }
 
-    private void handleException(Exception e, String fileName) {
+    private static void handleException(Exception e, String fileName) {
         System.err.println("At " + fileName);
         e.printStackTrace();
     }
@@ -336,7 +340,7 @@ public class AdapterRegistry2 {
         s.syncRequest(new ReadRequest() {
             @Override
             public void run(ReadGraph g) {
-                for(AdapterInstaller t : installers) {
+                for(AdapterInstaller t : installerSources.keySet()) {
                     try {
                         t.install(g, service);
                     } catch (Exception e) {
@@ -416,7 +420,7 @@ public class AdapterRegistry2 {
     }
 
        public void initialize(BundleContext context) {
-       
+           LOGGER.info("Initializing");
         try {
                
             DocumentBuilderFactory factory =
@@ -433,54 +437,63 @@ public class AdapterRegistry2 {
             }
 
             // TODO Listen bundles (install/uninstall)
+            List<Future<?>> waitFor = new ArrayList<>();
             if (exceptions.isEmpty())
                 for (final Bundle b : context.getBundles()) {
-                    URL file = b.getEntry(ADAPTERS_FILE);
-                    if (file != null) {
-                        String fileName = new Path(b.getLocation()).append(file.getPath()).toString();
-                        try {
-                            DocumentBuilder builder = factory.newDocumentBuilder();
-                            builder.setErrorHandler(new ErrorHandler() {
-
-                                @Override
-                                public void error(SAXParseException exception)
-                                throws SAXException {
-                                    // TODO Put this error somewhere
-                                    System.err.println("Parse error at "
-                                            + b.getSymbolicName() + "/adapters.xml" +
-                                            " line " + exception.getLineNumber() +
-                                            " column " + exception.getColumnNumber() + ":");
-                                    System.err.println(exception.getMessage());
-                                }
-
-                                @Override
-                                public void fatalError(SAXParseException exception)
-                                throws SAXException {
-                                    error(exception);
-                                }
-
-                                @Override
-                                public void warning(SAXParseException exception)
-                                throws SAXException {
-                                    error(exception);
-                                }
-
-                            });
-                            
-                            //System.out.println("bundle=" + b.getSymbolicName());
-                            String text = FileUtils.getContents(file);
-                            text = OntologyVersions.getInstance().currentVersion(text);
-                            StringReader reader = new StringReader( text );
-                            InputSource inputSource = new InputSource( reader );
-                            Document doc = builder.parse( inputSource );
-                            reader.close();                                            
-                            handleAdaptersDocument(loader(b), doc, fileName);
-                        } catch (Exception e) {
-                            handleException(e, fileName);
-
+                    Future<?> submit = ThreadUtils.getNonBlockingWorkExecutor().submit(() -> {
+                        URL file = b.getEntry(ADAPTERS_FILE);
+                        if (file != null) {
+                            String fileName = new Path(b.getLocation()).append(file.getPath()).toString();
+                            try {
+                                DocumentBuilder builder = factory.newDocumentBuilder();
+                                builder.setErrorHandler(new ErrorHandler() {
+
+                                    @Override
+                                    public void error(SAXParseException exception) throws SAXException {
+                                        // TODO Put this error somewhere
+                                        System.err.println("Parse error at " + b.getSymbolicName() + "/adapters.xml"
+                                                + " line " + exception.getLineNumber() + " column "
+                                                + exception.getColumnNumber() + ":");
+                                        System.err.println(exception.getMessage());
+                                    }
+
+                                    @Override
+                                    public void fatalError(SAXParseException exception) throws SAXException {
+                                        error(exception);
+                                    }
+
+                                    @Override
+                                    public void warning(SAXParseException exception) throws SAXException {
+                                        error(exception);
+                                    }
+
+                                });
+
+                                // System.out.println("bundle=" + b.getSymbolicName());
+                                String text = FileUtils.getContents(file);
+                                text = OntologyVersions.getInstance().currentVersion(text);
+                                StringReader reader = new StringReader(text);
+                                InputSource inputSource = new InputSource(reader);
+                                Document doc = builder.parse(inputSource);
+                                reader.close();
+                                handleAdaptersDocument(loader(b), doc, fileName);
+                            } catch (Exception e) {
+                                handleException(e, fileName);
+
+                            }
                         }
-                    }
+                    });
+                    waitFor.add(submit);
+                }
+            // Let's wait in here
+            waitFor.forEach(f -> {
+                try {
+                    f.get();
+                } catch (InterruptedException | ExecutionException e) {
+                    LOGGER.error("Could not wait adapters to load", e);
                 }
+            });
+            LOGGER.info("Adapters installed");
         } catch (Exception e) {
             handleException(e, "(no file name available)");
         }