Revert "Prime SCL BindingRegistry to shave ~0.5s from startup"
[simantics/platform.git] / bundles / org.simantics.scl.reflection / src / org / simantics / scl / reflection / internal / registry / BindingRegistry.java
1 package org.simantics.scl.reflection.internal.registry;
2
3 import gnu.trove.map.hash.THashMap;
4 import gnu.trove.procedure.TObjectObjectProcedure;
5
6 import org.eclipse.core.runtime.IConfigurationElement;
7 import org.eclipse.core.runtime.IContributor;
8 import org.eclipse.core.runtime.Platform;
9 import org.eclipse.core.runtime.spi.RegistryContributor;
10 import org.osgi.framework.Bundle;
11 import org.simantics.scl.compiler.types.TCon;
12 import org.simantics.scl.reflection.OntologyVersions;
13 import org.simantics.scl.reflection.TypeNotFoundException;
14 import org.simantics.scl.reflection.TypedValue;
15 import org.simantics.scl.reflection.ValueNotFoundException;
16 import org.simantics.scl.reflection.internal.Activator;
17
18 public class BindingRegistry {
19
20     private static boolean DEBUG_INIT = false;
21
22     private static THashMap<String, Namespace> namespaces =
23             new THashMap<String, Namespace>();
24         
25     public static TypedValue getValue(String path, String name) throws ValueNotFoundException {
26         Namespace namespace = namespaces.get(path);
27         if(namespace == null)
28             throw new ValueNotFoundException("Didn't find namespace " + path + ".");
29         return namespace.getValue(name);
30     }
31     
32     public static Class<?> getClass(TCon type) throws TypeNotFoundException {
33         Namespace namespace = namespaces.get(type.module);
34         if(namespace == null)
35             throw new TypeNotFoundException("Didn't find namespace " + type.module + ".");
36         return namespace.getClass(type.name);
37     }   
38
39     /*
40      * Called directly only for testing 
41      */
42     public static void initialize() {
43         namespaces.clear();
44         IConfigurationElement[] elements = 
45                 Platform.getExtensionRegistry().getConfigurationElementsFor("org.simantics.scl.reflection.binding");        
46         for(IConfigurationElement element : elements) {
47             Bundle bundle = getBundle(element);
48             findBindings(bundle, null, element, null);
49         }
50     }
51     
52     static {
53         initialize();
54         if (DEBUG_INIT) {
55             namespaces.forEachEntry(new TObjectObjectProcedure<String, Namespace>() {
56
57                 @Override
58                 public boolean execute(String name, Namespace namespace) {
59                     System.out.println(name);
60                     namespace.print();
61                     return true;
62                 }
63             });
64         }
65     }
66     
67     private static Bundle getBundle(IConfigurationElement element) {
68         IContributor contributor = element.getContributor();
69         if(contributor instanceof RegistryContributor) {
70             long id = Long.parseLong(((RegistryContributor) contributor).getActualId());
71             return Activator.getInstance().getContext().getBundle(id);
72         }
73         else
74             return Platform.getBundle(contributor.getName());
75     }
76
77     private static Namespace getOrCreateNamespace(String path, ImportSeq importSeq) {
78         Namespace namespace = namespaces.get(path);
79         if(namespace == null) {
80             namespace = new Namespace(path, importSeq);
81             namespaces.put(path, namespace);
82         }
83         return namespace;
84     }
85     
86     private static void findBindings(
87             Bundle bundle,
88             String path,
89             IConfigurationElement element,
90             ImportSeq importSeq) {
91         String elementName = element.getName();        
92         if(elementName.equals("namespace")) {
93             if(path == null)
94                 path = OntologyVersions.getInstance().currentVersion(element.getAttribute("path"));
95             else
96                 path = OntologyVersions.getInstance().currentVersion(path + "/" + element.getAttribute("path"));
97             if(path.endsWith("/"))
98                 path = path.substring(0, path.length()-1);
99             for(IConfigurationElement childElement : element.getChildren())
100                 if(childElement.getName().equals("import")) {
101                     String path_ = OntologyVersions.getInstance().currentVersion(childElement.getAttribute("path"));
102                     String localName = childElement.getAttribute("localName");
103                     importSeq = new ImportSeq(importSeq, localName, path_);
104                 }
105             for(IConfigurationElement childElement : element.getChildren())
106                 findBindings(bundle, path, childElement, importSeq);
107         }
108         else if(elementName.equals("class")) {
109             String className = element.getAttribute("className");
110             getOrCreateNamespace(path, importSeq)
111                 .addClass(new Entry(bundle, className));
112         }
113         else if(elementName.equals("externalClass")) {
114             String className = element.getAttribute("className");
115             String alternativeName = element.getAttribute("alternativeName");
116             getOrCreateNamespace(path, importSeq).addExternalClass(
117                     new ExternalClass(bundle, className, alternativeName)
118                     );
119         }
120         else if(elementName.equals("externalMethod")) {
121             String className = element.getAttribute("className");
122             String methodName = element.getAttribute("methodName");
123             String alternativeName = element.getAttribute("alternativeName");
124             getOrCreateNamespace(path, importSeq).addExternalMethod(
125                     new ExternalMethod(bundle, className, methodName, alternativeName)
126                     );
127         }
128     } 
129 }