SCL-compiler should activate installed bundles 52/2152/2
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 10 Sep 2018 07:23:04 +0000 (10:23 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 10 Sep 2018 09:41:18 +0000 (09:41 +0000)
gitlab #115

Conflicts:
bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleModuleSource.java

Change-Id: Ic6c3aa47386e462442285ef79749f27acaf541c9
(cherry picked from commit 7b50064579d979d2f3e8901bad32b219c5e1441d)

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 87b78d6ebf44434c61a252baf8fc339886bc9d1f..a805e6026607c2f31e708651ba67fa982d5d5d90 100644 (file)
@@ -14,6 +14,7 @@ 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;
@@ -80,13 +81,13 @@ public class BundleModuleSource extends EncodedTextualModuleSource implements Up
                 }
                 return digest.digest();
             } catch (NoSuchAlgorithmException e) {
-                LOGGER.error("No SHA1 algorithm available", e);
+                LOGGER.error("No SHA1 algorithm found", e);
                 return new byte[0];
             } finally {
                 stream.close();
             }
         } catch(IOException e) {
-            LOGGER.error("Could not compute digest for {}", this);
+            LOGGER.error("Could not compute digest for {}", getModuleName(), e);
             return new byte[0];
         }
     }
@@ -104,17 +105,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();
+            }
         }
     }
 
@@ -169,7 +180,7 @@ public class BundleModuleSource extends EncodedTextualModuleSource implements Up
                 return;
             Files.write(path, newSourceText.getBytes(Charset.forName("UTF-8")));
         } catch(IOException e) {
-            LOGGER.error("Could not update {} with {}", this, newSourceText, e);
+            LOGGER.error("Could not update module {} in url {} with text {}", getModuleName(), url, newSourceText);
         }
         checkUpdates();
     }
index c16545889425e4ffc0b7fd2d5dbbd6e41f4bab44..8c0bf43dc5421c1f8f3c1a6e9e722d07debb2366 100644 (file)
@@ -48,4 +48,16 @@ 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));
     }