From: jsimomaa Date: Mon, 18 Feb 2019 06:50:58 +0000 (+0200) Subject: WIP: Some old-ish adapter registry initialization improvements X-Git-Tag: v1.43.0~136^2~196 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F53%2F2653%2F2;p=simantics%2Fplatform.git WIP: Some old-ish adapter registry initialization improvements gitlab #189 Change-Id: Ibcae41336b9d24cdf34444702b9319ae0c2ce34e --- diff --git a/bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/AdapterRegistry2.java b/bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/AdapterRegistry2.java index 0e52b8a41..578b42a61 100644 --- a/bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/AdapterRegistry2.java +++ b/bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/AdapterRegistry2.java @@ -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 installers = new ArrayList(); - Map installerSources = new HashMap(); + ConcurrentHashMap installerSources = new ConcurrentHashMap<>(); Collection exceptions = new ArrayList(); 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> 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)"); }