X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.services%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fservices%2Fadaption%2FAdapterRegistry2.java;h=0db80b1b4f5cb48044709875cf3b821aabca4278;hp=1eb00704409ab15b5f3291af22afc76874b54026;hb=68e686fde705a331fe371bf1a0fbd07ae21be26e;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 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 1eb007044..0db80b1b4 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; @@ -33,8 +34,8 @@ import org.simantics.db.Session; import org.simantics.db.adaption.Adapter; import org.simantics.db.adaption.AdapterInstaller; import org.simantics.db.adaption.AdaptionService; -import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; import org.simantics.db.services.adaption.reflection.AdaptingDynamicAdapter2; import org.simantics.db.services.adaption.reflection.AtMostOneRelatedResource2; import org.simantics.db.services.adaption.reflection.ConstantAdapter; @@ -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,20 +91,16 @@ public class AdapterRegistry2 { } private void addInstaller(AdapterInstaller installer, String sourceDesc) { - installers.add(installer); installerSources.put(installer, sourceDesc); } - private void handleException(Exception e, String fileName) { - System.err.println("At " + fileName); - e.printStackTrace(); + private static void handleException(Exception e, String fileName) { + LOGGER.error("At {}", fileName, e); } private void handleException(Exception e, AdapterInstaller installer) { String desc = installerSources.get(installer); - if (desc != null) - System.err.println("At " + desc); - e.printStackTrace(); + LOGGER.error("At {}, installer {}", desc, installer, e); } private void handleAdaptersDocument(Loader b, Document doc, String fileName) { @@ -223,7 +224,6 @@ public class AdapterRegistry2 { }, fileName); } catch (Exception e) { - e.printStackTrace(); handleException(e, fileName); } } @@ -333,16 +333,17 @@ public class AdapterRegistry2 { } public void updateAdaptionService(Session s, final AdaptionService service) throws DatabaseException { - s.syncRequest(new ReadRequest() { + s.syncRequest(new Read() { @Override - public void run(ReadGraph g) { - for(AdapterInstaller t : installers) { + public Object perform(ReadGraph g) { + for(AdapterInstaller t : installerSources.keySet()) { try { t.install(g, service); } catch (Exception e) { AdapterRegistry2.this.handleException(e, t); } } + return null; } }); } @@ -354,7 +355,7 @@ public class AdapterRegistry2 { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - if(schemaURL != null) { + if(schemaURL != null && validateAgainstSchema()) { factory.setValidating(true); factory.setAttribute( @@ -410,70 +411,86 @@ public class AdapterRegistry2 { } } - - public void initialize(BundleContext context) { - + + private boolean validateAgainstSchema() { + return Platform.inDevelopmentMode(); + } + + public void initialize(BundleContext context) { + LOGGER.info("Initializing"); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setValidating(true); - factory.setAttribute( - "http://java.sun.com/xml/jaxp/properties/schemaLanguage", - "http://www.w3.org/2001/XMLSchema"); - factory.setAttribute( - "http://java.sun.com/xml/jaxp/properties/schemaSource", - context.getBundle().getResource("adapters.xsd").toString()); + + if (validateAgainstSchema()) { + factory.setValidating(true); + factory.setAttribute( + "http://java.sun.com/xml/jaxp/properties/schemaLanguage", + "http://www.w3.org/2001/XMLSchema"); + factory.setAttribute( + "http://java.sun.com/xml/jaxp/properties/schemaSource", + context.getBundle().getResource("adapters.xsd").toString()); + } // 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)"); }