]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
SCL-compiler should activate installed bundles 51/2151/1
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 10 Sep 2018 07:23:04 +0000 (10:23 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Mon, 10 Sep 2018 07:23:04 +0000 (10:23 +0300)
gitlab #115

Change-Id: I4b383d7987b760c8cbf945ceddac4603c9d4428b

bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/SCLOsgi.java
bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleDocumentationSource.java
bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleModuleSource.java
bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleUtils.java
bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/OsgiJavaReferenceValidatorFactory.java

index e6bddee7e3c9ca9bf4ac140d93db5999573718e4..2e717f945780574f71c3382c33cb3bf157fa55e2 100644 (file)
@@ -2,7 +2,6 @@ package org.simantics.scl.osgi;
 
 import java.util.ArrayList;
 
-import org.eclipse.core.internal.runtime.PlatformActivator;
 import org.simantics.scl.compiler.errors.DoesNotExist;
 import org.simantics.scl.compiler.errors.Failable;
 import org.simantics.scl.compiler.module.Module;
index 47db3927fce3442de4cc54e4807e2d303ac26db7..388029b360337927b2bf3aa91530b2e1868bc4ec 100644 (file)
@@ -7,8 +7,13 @@ import java.nio.charset.Charset;
 import java.util.Arrays;
 
 import org.osgi.framework.Bundle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class BundleDocumentationSource {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(BundleDocumentationSource.class);
+
     public static final Charset UTF8 = Charset.forName("UTF-8");  
     
     public final String documentationName;
@@ -41,7 +46,7 @@ public class BundleDocumentationSource {
                 stream.close();
             }
         } catch(IOException e) {
-            e.printStackTrace();
+            LOGGER.error("Could not get text for {} at {}", documentationName, url);
             return null;
         }
     }
index fc9799de706d6625e401d47dd3320aa7a900cc91..23edcccc2c1a0d1df294b7b92d6dfed978c181b4 100644 (file)
@@ -13,17 +13,22 @@ import java.util.Arrays;
 
 import org.eclipse.core.runtime.FileLocator;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.wiring.BundleWiring;
 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
 import org.simantics.scl.compiler.module.ImportDeclaration;
 import org.simantics.scl.compiler.module.repository.UpdateListener;
 import org.simantics.scl.compiler.source.EncodedTextualModuleSource;
 import org.simantics.scl.compiler.types.Type;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import gnu.trove.set.hash.THashSet;
 
 public class BundleModuleSource extends EncodedTextualModuleSource implements UpdateListener.Observable {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(BundleModuleSource.class);
+
     public static final ImportDeclaration[] DEFAULT_IMPORTS = new ImportDeclaration[] {
         new ImportDeclaration("Builtin", ""),
         new ImportDeclaration("StandardLibrary", "")
@@ -75,13 +80,13 @@ public class BundleModuleSource extends EncodedTextualModuleSource implements Up
                 }
                 return digest.digest();
             } catch (NoSuchAlgorithmException e) {
-                e.printStackTrace();
+                LOGGER.error("No SHA1 algorithm found", e);
                 return new byte[0];
             } finally {
                 stream.close();
             }
         } catch(IOException e) {
-            e.printStackTrace();
+            LOGGER.error("Could not compute digest for {}", getModuleName(), e);
             return new byte[0];
         }
     }
@@ -99,17 +104,27 @@ public class BundleModuleSource extends EncodedTextualModuleSource implements Up
         }
         return url.openStream();
     }
-    
+
     @Override
     public ClassLoader getClassLoader() {
-        if(bundle.getSymbolicName().equals("org.simantics.scl.runtime"))
+        if (bundle.getSymbolicName().equals("org.simantics.scl.runtime"))
             return Type.class.getClassLoader();
         else {
             BundleWiring wiring = bundle.adapt(BundleWiring.class);
-            if(wiring != null)
+            if (wiring == null && bundle.getState() == Bundle.INSTALLED) {
+                try {
+                    bundle.start();
+                } catch (BundleException e) {
+                    LOGGER.error("Could not start bundle {}", bundle.getSymbolicName(), e);
+                }
+                wiring = bundle.adapt(BundleWiring.class);
+            }
+            if (wiring != null) {
                 return wiring.getClassLoader();
-            else
+            } else {
+                LOGGER.error("Couldn't find class loader for bundle {} with state {}", bundle.getSymbolicName(), BundleUtils.resolveBundleState(bundle));
                 return getClass().getClassLoader();
+            }
         }
     }
 
@@ -163,7 +178,7 @@ public class BundleModuleSource extends EncodedTextualModuleSource implements Up
                 return;
             Files.write(path, newSourceText.getBytes(Charset.forName("UTF-8")));
         } catch(IOException e) {
-            e.printStackTrace();
+            LOGGER.error("Could not update module {} in url {} with text {}", getModuleName(), url, newSourceText);
         }
         checkUpdates();
     }
index c16545889425e4ffc0b7fd2d5dbbd6e41f4bab44..0f3d9c7c7e7f45ac8a2ce5f8bf5bda266fa05993 100644 (file)
@@ -48,4 +48,23 @@ public class BundleUtils {
                }
        }
 
+    public static String resolveBundleState(Bundle bundle) {
+        switch (bundle.getState()) {
+        case Bundle.UNINSTALLED:
+            return "UNINSTALLED";
+        case Bundle.INSTALLED:
+            return "INSTALLED";
+        case Bundle.RESOLVED:
+            return "RESOLVED";
+        case Bundle.STARTING:
+            return "STARTING";
+        case Bundle.STOPPING:
+            return "STOPPING";
+        case Bundle.ACTIVE:
+            return "ACTIVE";
+        default:
+            return "UNKNOWN";
+        }
+    }
+
 }
index 03524e8831bd5e5bfe42aadcc763a107ed5ea548..2f3c869b478aa631bb652b07341ab66a8abd5dbe 100644 (file)
@@ -2,15 +2,21 @@ package org.simantics.scl.osgi.internal;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
 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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+@SuppressWarnings("restriction")
 public class OsgiJavaReferenceValidatorFactory implements JavaReferenceValidatorFactory {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(OsgiJavaReferenceValidatorFactory.class);
+
     private final Bundle bundle;
 
     public OsgiJavaReferenceValidatorFactory(Bundle bundle) {
@@ -18,16 +24,27 @@ public class OsgiJavaReferenceValidatorFactory implements JavaReferenceValidator
     }
 
     private static ClassLoader getClassLoader(Bundle bundle) {
-        if(bundle.getSymbolicName().equals("org.simantics.scl.runtime"))
+        if (bundle.getSymbolicName().equals("org.simantics.scl.runtime"))
             return Type.class.getClassLoader();
         else {
             BundleWiring wiring = bundle.adapt(BundleWiring.class);
-            if(wiring != null)
+            if (wiring == null && bundle.getState() == Bundle.INSTALLED) {
+                LOGGER.info("Starting bundle {} with state {}", bundle.getSymbolicName(), BundleUtils.resolveBundleState(bundle));
+                try {
+                    bundle.start();
+                } catch (BundleException e) {
+                    throw new InternalCompilerError("Couldn't activate bundle " + bundle.getSymbolicName() + ". Bundle state is " + BundleUtils.resolveBundleState(bundle));
+                }
+                wiring = bundle.adapt(BundleWiring.class);
+            }
+            if (wiring != null)
                 return wiring.getClassLoader();
-            throw new InternalCompilerError("Cannot get the class loader for bundle " + bundle.getSymbolicName() + ".");
+            else
+                throw new InternalCompilerError("Cannot get the class loader for bundle " + bundle.getSymbolicName() + ". Bundle state is " + BundleUtils.resolveBundleState(bundle));
         }
     }
 
+    @SuppressWarnings("unchecked")
     private static JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(Bundle bundle) {
         if(bundle == null)
             return null;
@@ -46,7 +63,7 @@ public class OsgiJavaReferenceValidatorFactory implements JavaReferenceValidator
     
     @Override
     public JavaReferenceValidator<Object, Object, Object, Object> getJavaReferenceValidator(String bundleName) {
-        System.out.println("getJavaReferenceValidator(" + bundleName + ")");
+        LOGGER.info("getJavaReferenceValidator(" + bundleName + ")");
         return getJavaReferenceValidator(getBundle(bundle.getBundleContext(), bundleName));
     }