1 package org.simantics.scl.reflection.internal.registry;
3 import gnu.trove.map.hash.THashMap;
4 import gnu.trove.procedure.TObjectObjectProcedure;
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;
18 public class BindingRegistry {
20 private static boolean DEBUG_INIT = false;
22 private static THashMap<String, Namespace> namespaces =
23 new THashMap<String, Namespace>();
25 public static TypedValue getValue(String path, String name) throws ValueNotFoundException {
26 Namespace namespace = namespaces.get(path);
28 throw new ValueNotFoundException("Didn't find namespace " + path + ".");
29 return namespace.getValue(name);
32 public static Class<?> getClass(TCon type) throws TypeNotFoundException {
33 Namespace namespace = namespaces.get(type.module);
35 throw new TypeNotFoundException("Didn't find namespace " + type.module + ".");
36 return namespace.getClass(type.name);
40 * Called directly only for testing
42 public static void initialize() {
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);
55 namespaces.forEachEntry(new TObjectObjectProcedure<String, Namespace>() {
58 public boolean execute(String name, Namespace namespace) {
59 System.out.println(name);
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);
74 return Platform.getBundle(contributor.getName());
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);
86 private static void findBindings(
89 IConfigurationElement element,
90 ImportSeq importSeq) {
91 String elementName = element.getName();
92 if(elementName.equals("namespace")) {
94 path = OntologyVersions.getInstance().currentVersion(element.getAttribute("path"));
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_);
105 for(IConfigurationElement childElement : element.getChildren())
106 findBindings(bundle, path, childElement, importSeq);
108 else if(elementName.equals("class")) {
109 String className = element.getAttribute("className");
110 getOrCreateNamespace(path, importSeq)
111 .addClass(new Entry(bundle, className));
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)
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)