]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/AdapterRegistry2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.services / src / org / simantics / db / services / adaption / AdapterRegistry2.java
index 43d6ab7eed76b69bea42ea21faa6532300c62955..1eb00704409ab15b5f3291af22afc76874b54026 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.services.adaption;\r
-\r
-import java.io.File;\r
-import java.io.StringReader;\r
-import java.net.URL;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import javax.xml.parsers.DocumentBuilder;\r
-import javax.xml.parsers.DocumentBuilderFactory;\r
-\r
-import org.eclipse.core.runtime.Path;\r
-import org.eclipse.core.runtime.Platform;\r
-import org.osgi.framework.Bundle;\r
-import org.osgi.framework.BundleContext;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.adaption.Adapter;\r
-import org.simantics.db.adaption.AdapterInstaller;\r
-import org.simantics.db.adaption.AdaptionService;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.services.adaption.reflection.AdaptingDynamicAdapter2;\r
-import org.simantics.db.services.adaption.reflection.AtMostOneRelatedResource2;\r
-import org.simantics.db.services.adaption.reflection.ConstantAdapter;\r
-import org.simantics.db.services.adaption.reflection.GraphObject2;\r
-import org.simantics.db.services.adaption.reflection.IDynamicAdapter2;\r
-import org.simantics.db.services.adaption.reflection.OrderedSetResources2;\r
-import org.simantics.db.services.adaption.reflection.ReflectionAdapter2;\r
-import org.simantics.db.services.adaption.reflection.RelatedResources2;\r
-import org.simantics.db.services.adaption.reflection.SingleRelatedResource2;\r
-import org.simantics.db.services.adaption.reflection.StaticMethodAdapter;\r
-import org.simantics.db.services.adaption.reflection.ThisResource2;\r
-import org.simantics.scl.reflection.OntologyVersions;\r
-import org.simantics.utils.FileUtils;\r
-import org.w3c.dom.DOMException;\r
-import org.w3c.dom.Document;\r
-import org.w3c.dom.NamedNodeMap;\r
-import org.w3c.dom.Node;\r
-import org.w3c.dom.NodeList;\r
-import org.xml.sax.ErrorHandler;\r
-import org.xml.sax.InputSource;\r
-import org.xml.sax.SAXException;\r
-import org.xml.sax.SAXParseException;\r
-\r
-public class AdapterRegistry2 {\r
-\r
-    public static final String ADAPTERS_FILE = "adapters.xml";\r
-\r
-    public static final String ADAPTERS = "adapters";\r
-    public static final String ADAPTER = "adapter";\r
-    public static final String TARGET = "target";\r
-    public static final String BASE_TYPE = "baseType";\r
-    public static final String TYPE = "type";\r
-    public static final String RESOURCE = "resource";\r
-    public static final String URI = "uri";\r
-    public static final String INTERFACE = "interface";\r
-    public static final String CLASS = "class";\r
-    public static final String ADAPTER_CLASS = "adapterClass";\r
-    public static final String CONTEXT_CLASS = "contextClass";\r
-    public static final String INSTALLER = "installer";\r
-    public static final String CONSTRUCTOR = "constructor";\r
-\r
-    static private AdapterRegistry2 instance = new AdapterRegistry2();\r
-    Collection<AdapterInstaller> installers = new ArrayList<AdapterInstaller>();\r
-    Map<AdapterInstaller, String> installerSources = new HashMap<AdapterInstaller, String>();\r
-    Collection<Exception> exceptions = new ArrayList<Exception>();\r
-\r
-    public static AdapterRegistry2 getInstance() {\r
-        return instance;\r
-    }\r
-\r
-    private void addInstaller(AdapterInstaller installer, String sourceDesc) {\r
-        installers.add(installer);\r
-        installerSources.put(installer, sourceDesc);\r
-    }\r
-\r
-    private void handleException(Exception e, String fileName) {\r
-        System.err.println("At " + fileName);\r
-        e.printStackTrace();\r
-    }\r
-\r
-    private void handleException(Exception e, AdapterInstaller installer) {\r
-        String desc = installerSources.get(installer);\r
-        if (desc != null)\r
-            System.err.println("At " + desc);\r
-        e.printStackTrace();\r
-    }\r
-\r
-    private void handleAdaptersDocument(Loader b, Document doc, String fileName) {\r
-        try {\r
-            Node node = doc.getDocumentElement();\r
-            if(node.getNodeName().equals(ADAPTERS)) {\r
-                NodeList nodeList = node.getChildNodes();\r
-                for(int i=0;i<nodeList.getLength();++i) {\r
-                    Node n = nodeList.item(i);\r
-                    if(n.getNodeName().equals(TARGET))\r
-                        handleTarget(b, n, fileName);\r
-                    else if(n.getNodeName().equals(INSTALLER))\r
-                        handleInstaller(b, n, fileName);\r
-                }\r
-            }\r
-        } catch (Exception e) {\r
-            handleException(e, fileName);\r
-        }\r
-    }\r
-\r
-    private void handleTarget(Loader b, Node node, String fileName) {\r
-        try {\r
-            Class<?> interface_ =\r
-                b.loadClass(node.getAttributes().getNamedItem("interface")\r
-                        .getNodeValue());\r
-            NodeList nodeList = node.getChildNodes();\r
-            for(int i=0;i<nodeList.getLength();++i) {\r
-                Node n = nodeList.item(i);\r
-                String nodeName = n.getNodeName();\r
-                if(nodeName.equals(BASE_TYPE))\r
-                    handleBaseType(b, interface_, n, fileName);\r
-                else if(nodeName.equals(TYPE))\r
-                    handleType(b, interface_, n, fileName);\r
-                else if(nodeName.equals(ADAPTER))\r
-                    handleAdapter(b, interface_, n, fileName);\r
-                else if(nodeName.equals(RESOURCE))\r
-                    handleResource(b, interface_, n, fileName);\r
-            }\r
-        } catch (Exception e) {\r
-            handleException(e, fileName);\r
-        }\r
-    }\r
-\r
-    private void handleInstaller(Loader b, Node node, String fileName) {\r
-        try {\r
-            AdapterInstaller installer =\r
-                ((Class<?>)b.loadClass(node.getAttributes().getNamedItem("class").getNodeValue()))\r
-                .asSubclass(AdapterInstaller.class).newInstance();\r
-            addInstaller(installer, fileName);\r
-        } catch (Exception e) {\r
-            handleException(e, fileName);\r
-        }\r
-    }\r
-\r
-    private <T> void handleResource(final Loader b, final Class<T> interface_, final Node node, String fileName) {\r
-        try {\r
-            NamedNodeMap attr = node.getAttributes();\r
-            final String uri = attr.getNamedItem(URI).getNodeValue();\r
-            final String className = attr.getNamedItem(CLASS).getNodeValue();\r
-            Node constructorNode = attr.getNamedItem(CONSTRUCTOR);\r
-            final String constructor = constructorNode == null ? null : constructorNode.getNodeValue();\r
-//            System.out.println("AdapterRegistry2.handleResource: " + b + " " + uri + " " + interface_);\r
-            addInstaller(\r
-\r
-                    new AdapterInstaller() {\r
-\r
-                        @Override\r
-                        public void install(ReadGraph g, AdaptionService service) throws Exception {\r
-                            Class<? extends T> clazz = b.loadClass(className).asSubclass(interface_);\r
-                            List<IDynamicAdapter2> parameters = readParameters(g, node, b);\r
-                            IDynamicAdapter2[] parameterArray = \r
-                                parameters.toArray(new IDynamicAdapter2[parameters.size()]);\r
-                            Resource r = g.getResource(uri);\r
-                            service.addInstanceAdapter(\r
-                                    r,\r
-                                    interface_,\r
-                                    constructor == null \r
-                                    ? new ReflectionAdapter2<T>(clazz, parameterArray)\r
-                                    : new StaticMethodAdapter<T>(clazz, constructor, parameterArray));\r
-                        }\r
-\r
-                    }, fileName);\r
-        } catch (Exception e) {\r
-            handleException(e, fileName);\r
-        }\r
-    }\r
-\r
-    private <T> void handleType(final Loader b, final Class<T> interface_, final Node node, String fileName) {\r
-        try {\r
-            final NamedNodeMap attr = node.getAttributes();\r
-            final String uri = attr.getNamedItem(URI).getNodeValue();\r
-            Node constructorNode = attr.getNamedItem(CONSTRUCTOR);\r
-            final String constructor = constructorNode == null ? null : constructorNode.getNodeValue();\r
-            //System.out.println("AdapterRegistry2.handleType: " + b + " " + uri + " " + interface_);\r
-            addInstaller(\r
-                    new AdapterInstaller() {\r
-\r
-                        @Override\r
-                        public void install(ReadGraph g, AdaptionService service) throws Exception {\r
-                               try {\r
-                            Class<? extends T> clazz =\r
-                                ((Class<?>)b.loadClass(attr.getNamedItem(CLASS).getNodeValue()))\r
-                                .asSubclass(interface_);\r
-                            List<IDynamicAdapter2> parameters = readParameters(g, node, b);\r
-                            IDynamicAdapter2[] parameterArray = \r
-                                parameters.toArray(new IDynamicAdapter2[parameters.size()]);\r
-                            service.addAdapter(\r
-                                    g.getResource(uri),\r
-                                    interface_,\r
-                                    constructor == null \r
-                                    ? new ReflectionAdapter2<T>(clazz, parameterArray)\r
-                                    : new StaticMethodAdapter<T>(clazz, constructor, parameterArray));\r
-                               } catch(Error t) {\r
-                                       System.err.println("Failed to adapt "+interface_.getName());\r
-                                       throw t;\r
-                               } catch(RuntimeException t) {\r
-                                       System.err.println("Failed to adapt "+interface_.getName());\r
-                                       throw t;\r
-                               }\r
-                        }\r
-\r
-                    }, fileName);\r
-        } catch (Exception e) {\r
-            e.printStackTrace();\r
-            handleException(e, fileName);\r
-        }\r
-    }\r
-\r
-    private List<IDynamicAdapter2> readParameters(ReadGraph g, Node node, Loader b) throws DatabaseException, DOMException, ClassNotFoundException {\r
-        NodeList nodeList = node.getChildNodes();\r
-        ArrayList<IDynamicAdapter2> parameters = new ArrayList<IDynamicAdapter2>();\r
-        for(int i=0;i<nodeList.getLength();++i) {\r
-            Node n = nodeList.item(i);\r
-            if(n.getNodeType() == Node.ELEMENT_NODE) {\r
-                NamedNodeMap attr = n.getAttributes();\r
-                IDynamicAdapter2 da = null;\r
-                if(n.getNodeName().equals("this"))\r
-                    da = new ThisResource2();\r
-                else if(n.getNodeName().equals("graph"))\r
-                    da = new GraphObject2();\r
-                else if(n.getNodeName().equals("bundle")) {\r
-                    String bundleId = null;\r
-                    Node fc = n.getFirstChild();\r
-                    if (fc != null)\r
-                        bundleId = fc.getNodeValue();\r
-                    if (bundleId == null) {\r
-                        da = new ConstantAdapter(Bundle.class, b.getBundle());\r
-                    } else {\r
-                        Bundle ob = Platform.getBundle(bundleId);\r
-                        if (ob != null) {\r
-                            da = new ConstantAdapter(Bundle.class, ob);\r
-                        } else {\r
-                            throw new DOMException(DOMException.NOT_FOUND_ERR, "bundle '" + bundleId + "' not found");\r
-                        }\r
-                    }\r
-                } else if(n.getNodeName().equals("related"))\r
-                    da = new RelatedResources2(\r
-                            g.getResource(attr.getNamedItem("uri").getNodeValue()));\r
-                else if(n.getNodeName().equals("orderedSet"))\r
-                    da = new OrderedSetResources2(\r
-                            g.getResource(attr.getNamedItem("uri").getNodeValue()));\r
-                else if(n.getNodeName().equals("single"))\r
-                    da = new SingleRelatedResource2(\r
-                            g.getResource(attr.getNamedItem("uri").getNodeValue()));\r
-                else if(n.getNodeName().equals("atMostOne"))\r
-                    da = new AtMostOneRelatedResource2(\r
-                            g.getResource(attr.getNamedItem("uri").getNodeValue()));\r
-                else if(n.getNodeName().equals("string"))\r
-                    da = new ConstantAdapter(String.class, n.getFirstChild().getNodeValue());\r
-                {\r
-                    Node toNode = attr.getNamedItem("to");\r
-                    if(toNode != null) {\r
-                        String to = toNode.getNodeValue();\r
-                        da = new AdaptingDynamicAdapter2(da, b.loadClass(to));\r
-                    }\r
-                }\r
-                parameters.add(da);\r
-            }\r
-        }\r
-        return parameters;\r
-    }\r
-\r
-    private <T> void handleAdapter(final Loader b, final Class<T> interface_, Node node, String fileName) {\r
-        try {\r
-            NamedNodeMap attr = node.getAttributes();\r
-            final String uri = attr.getNamedItem(URI).getNodeValue();\r
-            final String clazz = attr.getNamedItem(ADAPTER_CLASS).getNodeValue();\r
-\r
-            Node contextNode = attr.getNamedItem(CONTEXT_CLASS);\r
-            final Class<?> contextClass = contextNode != null ? b.loadClass(contextNode.getNodeValue()) : Resource.class;\r
-            \r
-            //System.out.println("AdapterRegistry2.handleAdapter: " + b + " " + uri + " " + interface_ + ", class=" + clazz);\r
-            addInstaller(\r
-                    new AdapterInstaller() {\r
-\r
-                        @SuppressWarnings("unchecked")\r
-                        @Override\r
-                        public void install(ReadGraph g, AdaptionService service) throws Exception {\r
-                            service.addAdapter(\r
-                                    g.getResource(uri),\r
-                                    interface_,\r
-                                    contextClass,\r
-                                    ((Class<?>)b.loadClass(clazz))\r
-                                    .asSubclass(Adapter.class).newInstance());\r
-                        }\r
-\r
-                    }, fileName);\r
-        } catch (Exception e) {\r
-            handleException(e, fileName);\r
-        }\r
-    }\r
-\r
-    private <T> void handleBaseType(Loader b, final Class<T> interface_, Node node, String fileName) {\r
-        try {\r
-            NamedNodeMap attr = node.getAttributes();\r
-            final String uri = attr.getNamedItem(URI).getNodeValue();\r
-            addInstaller(\r
-                    new AdapterInstaller() {\r
-\r
-                        @Override\r
-                        public void install(ReadGraph g, AdaptionService service) throws Exception {\r
-                            service.declareAdapter(\r
-                                    g.getResource(uri),\r
-                                    interface_);\r
-                        }\r
-\r
-                    }, fileName);\r
-        } catch (Exception e) {\r
-            handleException(e, fileName);\r
-        }\r
-    }\r
-\r
-    public void updateAdaptionService(Session s, final AdaptionService service) throws DatabaseException {\r
-        s.syncRequest(new ReadRequest() {\r
-            @Override\r
-            public void run(ReadGraph g) {\r
-                for(AdapterInstaller t : installers) {\r
-                    try {\r
-                        t.install(g, service);\r
-                    } catch (Exception e) {\r
-                        AdapterRegistry2.this.handleException(e, t);\r
-                    }\r
-                }\r
-            }\r
-        });\r
-    }\r
-\r
-    public void initialize(ClassLoader b, String schemaURL, File[] files) {\r
-\r
-        try {\r
-               \r
-            DocumentBuilderFactory factory =\r
-                DocumentBuilderFactory.newInstance();\r
-            \r
-            if(schemaURL != null) {\r
-            \r
-                   factory.setValidating(true);\r
-                   factory.setAttribute(\r
-                           "http://java.sun.com/xml/jaxp/properties/schemaLanguage",\r
-                   "http://www.w3.org/2001/XMLSchema");\r
-                   factory.setAttribute(\r
-                           "http://java.sun.com/xml/jaxp/properties/schemaSource", schemaURL);\r
-                   \r
-            }\r
-\r
-            // TODO Listen bundles (install/uninstall)\r
-            if (exceptions.isEmpty())\r
-                for (final File f : files) {\r
-//                        String fileName = new Path(b.getLocation()).append(file.getPath()).toString();\r
-                        try {\r
-                            DocumentBuilder builder = factory.newDocumentBuilder();\r
-                            builder.setErrorHandler(new ErrorHandler() {\r
-\r
-                                @Override\r
-                                public void error(SAXParseException exception)\r
-                                throws SAXException {\r
-                                    // TODO Put this error somewhere\r
-                                    System.err.println("Parse error at " + f.getAbsolutePath() + \r
-//                                            + b.getSymbolicName() + "/adapters.xml" +\r
-                                            " line " + exception.getLineNumber() +\r
-                                            " column " + exception.getColumnNumber() + ":");\r
-                                    System.err.println(exception.getMessage());\r
-                                }\r
-\r
-                                @Override\r
-                                public void fatalError(SAXParseException exception)\r
-                                throws SAXException {\r
-                                    error(exception);\r
-                                }\r
-\r
-                                @Override\r
-                                public void warning(SAXParseException exception)\r
-                                throws SAXException {\r
-                                    error(exception);\r
-                                }\r
-\r
-                            });\r
-                            //System.out.println("bundle=" + b.getSymbolicName());\r
-                            Document doc = builder.parse(f);\r
-                            handleAdaptersDocument(loader(b), doc, f.getAbsolutePath());\r
-                        } catch (Exception e) {\r
-                            handleException(e, f.getAbsolutePath());\r
-\r
-                        }\r
-                    }\r
-        } catch (Exception e) {\r
-            handleException(e, "(no file name available)");\r
-        }\r
-       \r
-    }\r
-    \r
-    public void initialize(BundleContext context) {\r
-       \r
-        try {\r
-               \r
-            DocumentBuilderFactory factory =\r
-                DocumentBuilderFactory.newInstance();\r
-            factory.setValidating(true);\r
-            factory.setAttribute(\r
-                    "http://java.sun.com/xml/jaxp/properties/schemaLanguage",\r
-            "http://www.w3.org/2001/XMLSchema");\r
-            factory.setAttribute(\r
-                    "http://java.sun.com/xml/jaxp/properties/schemaSource",\r
-                    context.getBundle().getResource("adapters.xsd").toString());\r
-\r
-            // TODO Listen bundles (install/uninstall)\r
-            if (exceptions.isEmpty())\r
-                for (final Bundle b : context.getBundles()) {\r
-                    URL file = b.getEntry(ADAPTERS_FILE);\r
-                    if (file != null) {\r
-                        String fileName = new Path(b.getLocation()).append(file.getPath()).toString();\r
-                        try {\r
-                            DocumentBuilder builder = factory.newDocumentBuilder();\r
-                            builder.setErrorHandler(new ErrorHandler() {\r
-\r
-                                @Override\r
-                                public void error(SAXParseException exception)\r
-                                throws SAXException {\r
-                                    // TODO Put this error somewhere\r
-                                    System.err.println("Parse error at "\r
-                                            + b.getSymbolicName() + "/adapters.xml" +\r
-                                            " line " + exception.getLineNumber() +\r
-                                            " column " + exception.getColumnNumber() + ":");\r
-                                    System.err.println(exception.getMessage());\r
-                                }\r
-\r
-                                @Override\r
-                                public void fatalError(SAXParseException exception)\r
-                                throws SAXException {\r
-                                    error(exception);\r
-                                }\r
-\r
-                                @Override\r
-                                public void warning(SAXParseException exception)\r
-                                throws SAXException {\r
-                                    error(exception);\r
-                                }\r
-\r
-                            });\r
-                            \r
-                            //System.out.println("bundle=" + b.getSymbolicName());\r
-                            String text = FileUtils.getContents(file);\r
-                            text = OntologyVersions.getInstance().currentVersion(text);\r
-                            StringReader reader = new StringReader( text );\r
-                            InputSource inputSource = new InputSource( reader );\r
-                            Document doc = builder.parse( inputSource );\r
-                            reader.close();                                            \r
-                            handleAdaptersDocument(loader(b), doc, fileName);\r
-                        } catch (Exception e) {\r
-                            handleException(e, fileName);\r
-\r
-                        }\r
-                    }\r
-                }\r
-        } catch (Exception e) {\r
-            handleException(e, "(no file name available)");\r
-        }\r
-    }\r
-\r
-       interface Loader {\r
-               Class<?> loadClass(String name) throws ClassNotFoundException ;\r
-               Bundle getBundle();\r
-       }\r
-\r
-       private Loader loader(final Bundle b) {\r
-               return new Loader() {\r
-\r
-                       @Override\r
-                       public Class<?> loadClass(String name) throws ClassNotFoundException {\r
-                               return b.loadClass(name);\r
-                       }\r
-                       \r
-                       @Override\r
-                       public Bundle getBundle() {\r
-                               return b;\r
-                       }\r
-                       \r
-               };\r
-       }\r
-\r
-       private Loader loader(final ClassLoader b) {\r
-               return new Loader() {\r
-\r
-                       @Override\r
-                       public Class<?> loadClass(String name) throws ClassNotFoundException {\r
-                               return b.loadClass(name);\r
-                       }\r
-\r
-                       @Override\r
-                       public Bundle getBundle() {\r
-                               return null;\r
-                       }\r
-                       \r
-               };\r
-       }\r
-       \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.services.adaption;
+
+import java.io.File;
+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 javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Platform;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+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.services.adaption.reflection.AdaptingDynamicAdapter2;
+import org.simantics.db.services.adaption.reflection.AtMostOneRelatedResource2;
+import org.simantics.db.services.adaption.reflection.ConstantAdapter;
+import org.simantics.db.services.adaption.reflection.GraphObject2;
+import org.simantics.db.services.adaption.reflection.IDynamicAdapter2;
+import org.simantics.db.services.adaption.reflection.OrderedSetResources2;
+import org.simantics.db.services.adaption.reflection.ReflectionAdapter2;
+import org.simantics.db.services.adaption.reflection.RelatedResources2;
+import org.simantics.db.services.adaption.reflection.SingleRelatedResource2;
+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.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+public class AdapterRegistry2 {
+
+    public static final String ADAPTERS_FILE = "adapters.xml";
+
+    public static final String ADAPTERS = "adapters";
+    public static final String ADAPTER = "adapter";
+    public static final String TARGET = "target";
+    public static final String BASE_TYPE = "baseType";
+    public static final String TYPE = "type";
+    public static final String RESOURCE = "resource";
+    public static final String URI = "uri";
+    public static final String INTERFACE = "interface";
+    public static final String CLASS = "class";
+    public static final String ADAPTER_CLASS = "adapterClass";
+    public static final String CONTEXT_CLASS = "contextClass";
+    public static final String INSTALLER = "installer";
+    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>();
+    Collection<Exception> exceptions = new ArrayList<Exception>();
+
+    public static AdapterRegistry2 getInstance() {
+        return instance;
+    }
+
+    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 void handleException(Exception e, AdapterInstaller installer) {
+        String desc = installerSources.get(installer);
+        if (desc != null)
+            System.err.println("At " + desc);
+        e.printStackTrace();
+    }
+
+    private void handleAdaptersDocument(Loader b, Document doc, String fileName) {
+        try {
+            Node node = doc.getDocumentElement();
+            if(node.getNodeName().equals(ADAPTERS)) {
+                NodeList nodeList = node.getChildNodes();
+                for(int i=0;i<nodeList.getLength();++i) {
+                    Node n = nodeList.item(i);
+                    if(n.getNodeName().equals(TARGET))
+                        handleTarget(b, n, fileName);
+                    else if(n.getNodeName().equals(INSTALLER))
+                        handleInstaller(b, n, fileName);
+                }
+            }
+        } catch (Exception e) {
+            handleException(e, fileName);
+        }
+    }
+
+    private void handleTarget(Loader b, Node node, String fileName) {
+        try {
+            Class<?> interface_ =
+                b.loadClass(node.getAttributes().getNamedItem("interface")
+                        .getNodeValue());
+            NodeList nodeList = node.getChildNodes();
+            for(int i=0;i<nodeList.getLength();++i) {
+                Node n = nodeList.item(i);
+                String nodeName = n.getNodeName();
+                if(nodeName.equals(BASE_TYPE))
+                    handleBaseType(b, interface_, n, fileName);
+                else if(nodeName.equals(TYPE))
+                    handleType(b, interface_, n, fileName);
+                else if(nodeName.equals(ADAPTER))
+                    handleAdapter(b, interface_, n, fileName);
+                else if(nodeName.equals(RESOURCE))
+                    handleResource(b, interface_, n, fileName);
+            }
+        } catch (Exception e) {
+            handleException(e, fileName);
+        }
+    }
+
+    private void handleInstaller(Loader b, Node node, String fileName) {
+        try {
+            AdapterInstaller installer =
+                ((Class<?>)b.loadClass(node.getAttributes().getNamedItem("class").getNodeValue()))
+                .asSubclass(AdapterInstaller.class).newInstance();
+            addInstaller(installer, fileName);
+        } catch (Exception e) {
+            handleException(e, fileName);
+        }
+    }
+
+    private <T> void handleResource(final Loader b, final Class<T> interface_, final Node node, String fileName) {
+        try {
+            NamedNodeMap attr = node.getAttributes();
+            final String uri = attr.getNamedItem(URI).getNodeValue();
+            final String className = attr.getNamedItem(CLASS).getNodeValue();
+            Node constructorNode = attr.getNamedItem(CONSTRUCTOR);
+            final String constructor = constructorNode == null ? null : constructorNode.getNodeValue();
+//            System.out.println("AdapterRegistry2.handleResource: " + b + " " + uri + " " + interface_);
+            addInstaller(
+
+                    new AdapterInstaller() {
+
+                        @Override
+                        public void install(ReadGraph g, AdaptionService service) throws Exception {
+                            Class<? extends T> clazz = b.loadClass(className).asSubclass(interface_);
+                            List<IDynamicAdapter2> parameters = readParameters(g, node, b);
+                            IDynamicAdapter2[] parameterArray = 
+                                parameters.toArray(new IDynamicAdapter2[parameters.size()]);
+                            Resource r = g.getResource(uri);
+                            service.addInstanceAdapter(
+                                    r,
+                                    interface_,
+                                    constructor == null 
+                                    ? new ReflectionAdapter2<T>(clazz, parameterArray)
+                                    : new StaticMethodAdapter<T>(clazz, constructor, parameterArray));
+                        }
+
+                    }, fileName);
+        } catch (Exception e) {
+            handleException(e, fileName);
+        }
+    }
+
+    private <T> void handleType(final Loader b, final Class<T> interface_, final Node node, String fileName) {
+        try {
+            final NamedNodeMap attr = node.getAttributes();
+            final String uri = attr.getNamedItem(URI).getNodeValue();
+            Node constructorNode = attr.getNamedItem(CONSTRUCTOR);
+            final String constructor = constructorNode == null ? null : constructorNode.getNodeValue();
+            //System.out.println("AdapterRegistry2.handleType: " + b + " " + uri + " " + interface_);
+            addInstaller(
+                    new AdapterInstaller() {
+
+                        @Override
+                        public void install(ReadGraph g, AdaptionService service) throws Exception {
+                               try {
+                            Class<? extends T> clazz =
+                                ((Class<?>)b.loadClass(attr.getNamedItem(CLASS).getNodeValue()))
+                                .asSubclass(interface_);
+                            List<IDynamicAdapter2> parameters = readParameters(g, node, b);
+                            IDynamicAdapter2[] parameterArray = 
+                                parameters.toArray(new IDynamicAdapter2[parameters.size()]);
+                            service.addAdapter(
+                                    g.getResource(uri),
+                                    interface_,
+                                    constructor == null 
+                                    ? new ReflectionAdapter2<T>(clazz, parameterArray)
+                                    : new StaticMethodAdapter<T>(clazz, constructor, parameterArray));
+                               } catch(Error t) {
+                                       System.err.println("Failed to adapt "+interface_.getName());
+                                       throw t;
+                               } catch(RuntimeException t) {
+                                       System.err.println("Failed to adapt "+interface_.getName());
+                                       throw t;
+                               }
+                        }
+
+                    }, fileName);
+        } catch (Exception e) {
+            e.printStackTrace();
+            handleException(e, fileName);
+        }
+    }
+
+    private List<IDynamicAdapter2> readParameters(ReadGraph g, Node node, Loader b) throws DatabaseException, DOMException, ClassNotFoundException {
+        NodeList nodeList = node.getChildNodes();
+        ArrayList<IDynamicAdapter2> parameters = new ArrayList<IDynamicAdapter2>();
+        for(int i=0;i<nodeList.getLength();++i) {
+            Node n = nodeList.item(i);
+            if(n.getNodeType() == Node.ELEMENT_NODE) {
+                NamedNodeMap attr = n.getAttributes();
+                IDynamicAdapter2 da = null;
+                if(n.getNodeName().equals("this"))
+                    da = new ThisResource2();
+                else if(n.getNodeName().equals("graph"))
+                    da = new GraphObject2();
+                else if(n.getNodeName().equals("bundle")) {
+                    String bundleId = null;
+                    Node fc = n.getFirstChild();
+                    if (fc != null)
+                        bundleId = fc.getNodeValue();
+                    if (bundleId == null) {
+                        da = new ConstantAdapter(Bundle.class, b.getBundle());
+                    } else {
+                        Bundle ob = Platform.getBundle(bundleId);
+                        if (ob != null) {
+                            da = new ConstantAdapter(Bundle.class, ob);
+                        } else {
+                            throw new DOMException(DOMException.NOT_FOUND_ERR, "bundle '" + bundleId + "' not found");
+                        }
+                    }
+                } else if(n.getNodeName().equals("related"))
+                    da = new RelatedResources2(
+                            g.getResource(attr.getNamedItem("uri").getNodeValue()));
+                else if(n.getNodeName().equals("orderedSet"))
+                    da = new OrderedSetResources2(
+                            g.getResource(attr.getNamedItem("uri").getNodeValue()));
+                else if(n.getNodeName().equals("single"))
+                    da = new SingleRelatedResource2(
+                            g.getResource(attr.getNamedItem("uri").getNodeValue()));
+                else if(n.getNodeName().equals("atMostOne"))
+                    da = new AtMostOneRelatedResource2(
+                            g.getResource(attr.getNamedItem("uri").getNodeValue()));
+                else if(n.getNodeName().equals("string"))
+                    da = new ConstantAdapter(String.class, n.getFirstChild().getNodeValue());
+                {
+                    Node toNode = attr.getNamedItem("to");
+                    if(toNode != null) {
+                        String to = toNode.getNodeValue();
+                        da = new AdaptingDynamicAdapter2(da, b.loadClass(to));
+                    }
+                }
+                parameters.add(da);
+            }
+        }
+        return parameters;
+    }
+
+    private <T> void handleAdapter(final Loader b, final Class<T> interface_, Node node, String fileName) {
+        try {
+            NamedNodeMap attr = node.getAttributes();
+            final String uri = attr.getNamedItem(URI).getNodeValue();
+            final String clazz = attr.getNamedItem(ADAPTER_CLASS).getNodeValue();
+
+            Node contextNode = attr.getNamedItem(CONTEXT_CLASS);
+            final Class<?> contextClass = contextNode != null ? b.loadClass(contextNode.getNodeValue()) : Resource.class;
+            
+            //System.out.println("AdapterRegistry2.handleAdapter: " + b + " " + uri + " " + interface_ + ", class=" + clazz);
+            addInstaller(
+                    new AdapterInstaller() {
+
+                        @SuppressWarnings("unchecked")
+                        @Override
+                        public void install(ReadGraph g, AdaptionService service) throws Exception {
+                            service.addAdapter(
+                                    g.getResource(uri),
+                                    interface_,
+                                    contextClass,
+                                    ((Class<?>)b.loadClass(clazz))
+                                    .asSubclass(Adapter.class).newInstance());
+                        }
+
+                    }, fileName);
+        } catch (Exception e) {
+            handleException(e, fileName);
+        }
+    }
+
+    private <T> void handleBaseType(Loader b, final Class<T> interface_, Node node, String fileName) {
+        try {
+            NamedNodeMap attr = node.getAttributes();
+            final String uri = attr.getNamedItem(URI).getNodeValue();
+            addInstaller(
+                    new AdapterInstaller() {
+
+                        @Override
+                        public void install(ReadGraph g, AdaptionService service) throws Exception {
+                            service.declareAdapter(
+                                    g.getResource(uri),
+                                    interface_);
+                        }
+
+                    }, fileName);
+        } catch (Exception e) {
+            handleException(e, fileName);
+        }
+    }
+
+    public void updateAdaptionService(Session s, final AdaptionService service) throws DatabaseException {
+        s.syncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph g) {
+                for(AdapterInstaller t : installers) {
+                    try {
+                        t.install(g, service);
+                    } catch (Exception e) {
+                        AdapterRegistry2.this.handleException(e, t);
+                    }
+                }
+            }
+        });
+    }
+
+    public void initialize(ClassLoader b, String schemaURL, File[] files) {
+
+        try {
+               
+            DocumentBuilderFactory factory =
+                DocumentBuilderFactory.newInstance();
+            
+            if(schemaURL != null) {
+            
+                   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", schemaURL);
+                   
+            }
+
+            // TODO Listen bundles (install/uninstall)
+            if (exceptions.isEmpty())
+                for (final File f : files) {
+//                        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 " + f.getAbsolutePath() + 
+//                                            + 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());
+                            Document doc = builder.parse(f);
+                            handleAdaptersDocument(loader(b), doc, f.getAbsolutePath());
+                        } catch (Exception e) {
+                            handleException(e, f.getAbsolutePath());
+
+                        }
+                    }
+        } catch (Exception e) {
+            handleException(e, "(no file name available)");
+        }
+       
+    }
+    
+    public void initialize(BundleContext context) {
+       
+        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());
+
+            // TODO Listen bundles (install/uninstall)
+            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);
+
+                        }
+                    }
+                }
+        } catch (Exception e) {
+            handleException(e, "(no file name available)");
+        }
+    }
+
+       interface Loader {
+               Class<?> loadClass(String name) throws ClassNotFoundException ;
+               Bundle getBundle();
+       }
+
+       private Loader loader(final Bundle b) {
+               return new Loader() {
+
+                       @Override
+                       public Class<?> loadClass(String name) throws ClassNotFoundException {
+                               return b.loadClass(name);
+                       }
+                       
+                       @Override
+                       public Bundle getBundle() {
+                               return b;
+                       }
+                       
+               };
+       }
+
+       private Loader loader(final ClassLoader b) {
+               return new Loader() {
+
+                       @Override
+                       public Class<?> loadClass(String name) throws ClassNotFoundException {
+                               return b.loadClass(name);
+                       }
+
+                       @Override
+                       public Bundle getBundle() {
+                               return null;
+                       }
+                       
+               };
+       }
+       
+}