]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/OsgiJavaReferenceValidatorFactory.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.scl.osgi / src / org / simantics / scl / osgi / internal / OsgiJavaReferenceValidatorFactory.java
1 package org.simantics.scl.osgi.internal;
2
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;
11
12 public class OsgiJavaReferenceValidatorFactory implements JavaReferenceValidatorFactory {
13
14     private final Bundle bundle;
15
16     public OsgiJavaReferenceValidatorFactory(Bundle bundle) {
17         this.bundle = bundle;
18     }
19
20     private static ClassLoader getClassLoader(Bundle bundle) {
21         if(bundle.getSymbolicName().equals("org.simantics.scl.runtime"))
22             return Type.class.getClassLoader();
23         else {
24             BundleWiring wiring = bundle.adapt(BundleWiring.class);
25             if(wiring != null)
26                 return wiring.getClassLoader();
27             throw new InternalCompilerError("Cannot get the class loader for bundle " + bundle.getSymbolicName() + ".");
28         }
29     }
30
31     private static JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(Bundle bundle) {
32         if(bundle == null)
33             return null;
34         return (JavaReferenceValidator<Object, Object, Object, Object>)
35                 (JavaReferenceValidator<?, ?, ?, ?>)new RuntimeJavaReferenceValidator(getClassLoader(bundle));
36     }
37
38     private static Bundle getBundle(BundleContext bundleContext, String symbolicName) {
39         Bundle result = null;
40         for (Bundle candidate : bundleContext.getBundles())
41             if (candidate.getSymbolicName().equals(symbolicName))
42                 if (result == null || result.getVersion().compareTo(candidate.getVersion()) < 0)
43                     result = candidate;
44         return result;
45     }
46     
47     @Override
48     public JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String bundleName) {
49         System.out.println("getJavaReferenceValidator(" + bundleName + ")");
50         return getJavaReferenceValidator(getBundle(bundle.getBundleContext(), bundleName));
51     }
52
53     @Override
54     public JavaReferenceValidator<Object, Object, Object, Object> getDefaultJavaReferenceValidator() {
55         return getJavaReferenceValidator(bundle);
56     }
57
58 }