1 package org.simantics.scl.osgi.internal;
3 import org.osgi.framework.Bundle;
4 import org.osgi.framework.BundleContext;
5 import org.osgi.framework.wiring.BundleWiring;
6 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
7 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
8 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
9 import org.simantics.scl.compiler.internal.codegen.types.RuntimeJavaReferenceValidator;
10 import org.simantics.scl.compiler.types.Type;
12 public class OsgiJavaReferenceValidatorFactory implements JavaReferenceValidatorFactory {
14 private final Bundle bundle;
16 public OsgiJavaReferenceValidatorFactory(Bundle bundle) {
20 private static ClassLoader getClassLoader(Bundle bundle) {
21 if(bundle.getSymbolicName().equals("org.simantics.scl.runtime"))
22 return Type.class.getClassLoader();
24 BundleWiring wiring = bundle.adapt(BundleWiring.class);
26 return wiring.getClassLoader();
27 throw new InternalCompilerError("Cannot get the class loader for bundle " + bundle.getSymbolicName() + ".");
31 private static JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(Bundle bundle) {
34 return (JavaReferenceValidator<Object, Object, Object, Object>)
35 (JavaReferenceValidator<?, ?, ?, ?>)new RuntimeJavaReferenceValidator(getClassLoader(bundle));
38 private static Bundle getBundle(BundleContext bundleContext, String symbolicName) {
40 for (Bundle candidate : bundleContext.getBundles())
41 if (candidate.getSymbolicName().equals(symbolicName))
42 if (result == null || result.getVersion().compareTo(candidate.getVersion()) < 0)
48 public JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String bundleName) {
49 System.out.println("getJavaReferenceValidator(" + bundleName + ")");
50 return getJavaReferenceValidator(getBundle(bundle.getBundleContext(), bundleName));
54 public JavaReferenceValidator<Object, Object, Object, Object> getDefaultJavaReferenceValidator() {
55 return getJavaReferenceValidator(bundle);