]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/OsgiJavaReferenceValidatorFactory.java
Added module header feature to SCL language.
[simantics/platform.git] / bundles / org.simantics.scl.osgi / src / org / simantics / scl / osgi / internal / OsgiJavaReferenceValidatorFactory.java
diff --git a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/OsgiJavaReferenceValidatorFactory.java b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/OsgiJavaReferenceValidatorFactory.java
new file mode 100644 (file)
index 0000000..03524e8
--- /dev/null
@@ -0,0 +1,58 @@
+package org.simantics.scl.osgi.internal;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.wiring.BundleWiring;
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
+import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
+import org.simantics.scl.compiler.internal.codegen.types.RuntimeJavaReferenceValidator;
+import org.simantics.scl.compiler.types.Type;
+
+public class OsgiJavaReferenceValidatorFactory implements JavaReferenceValidatorFactory {
+
+    private final Bundle bundle;
+
+    public OsgiJavaReferenceValidatorFactory(Bundle bundle) {
+        this.bundle = bundle;
+    }
+
+    private static ClassLoader getClassLoader(Bundle bundle) {
+        if(bundle.getSymbolicName().equals("org.simantics.scl.runtime"))
+            return Type.class.getClassLoader();
+        else {
+            BundleWiring wiring = bundle.adapt(BundleWiring.class);
+            if(wiring != null)
+                return wiring.getClassLoader();
+            throw new InternalCompilerError("Cannot get the class loader for bundle " + bundle.getSymbolicName() + ".");
+        }
+    }
+
+    private static JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(Bundle bundle) {
+        if(bundle == null)
+            return null;
+        return (JavaReferenceValidator<Object, Object, Object, Object>)
+                (JavaReferenceValidator<?, ?, ?, ?>)new RuntimeJavaReferenceValidator(getClassLoader(bundle));
+    }
+
+    private static Bundle getBundle(BundleContext bundleContext, String symbolicName) {
+        Bundle result = null;
+        for (Bundle candidate : bundleContext.getBundles())
+            if (candidate.getSymbolicName().equals(symbolicName))
+                if (result == null || result.getVersion().compareTo(candidate.getVersion()) < 0)
+                    result = candidate;
+        return result;
+    }
+    
+    @Override
+    public JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String bundleName) {
+        System.out.println("getJavaReferenceValidator(" + bundleName + ")");
+        return getJavaReferenceValidator(getBundle(bundle.getBundleContext(), bundleName));
+    }
+
+    @Override
+    public JavaReferenceValidator<Object, Object, Object, Object> getDefaultJavaReferenceValidator() {
+        return getJavaReferenceValidator(bundle);
+    }
+
+}