From 5704e9deb877257b2c0c542e185fda124cf3ce37 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Mon, 10 Sep 2018 10:23:04 +0300 Subject: [PATCH] SCL-compiler should activate installed bundles gitlab #115 Conflicts: bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleModuleSource.java Change-Id: Ic6c3aa47386e462442285ef79749f27acaf541c9 (cherry picked from commit 7b50064579d979d2f3e8901bad32b219c5e1441d) --- .../src/org/simantics/scl/osgi/SCLOsgi.java | 1 - .../internal/BundleDocumentationSource.java | 7 +++++- .../scl/osgi/internal/BundleModuleSource.java | 25 +++++++++++++------ .../scl/osgi/internal/BundleUtils.java | 12 +++++++++ .../OsgiJavaReferenceValidatorFactory.java | 25 ++++++++++++++++--- 5 files changed, 57 insertions(+), 13 deletions(-) diff --git a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/SCLOsgi.java b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/SCLOsgi.java index e6bddee7e..2e717f945 100644 --- a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/SCLOsgi.java +++ b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/SCLOsgi.java @@ -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; diff --git a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleDocumentationSource.java b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleDocumentationSource.java index 47db3927f..388029b36 100644 --- a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleDocumentationSource.java +++ b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleDocumentationSource.java @@ -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; } } diff --git a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleModuleSource.java b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleModuleSource.java index 87b78d6eb..a805e6026 100644 --- a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleModuleSource.java +++ b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleModuleSource.java @@ -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(); } diff --git a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleUtils.java b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleUtils.java index c16545889..8c0bf43dc 100644 --- a/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleUtils.java +++ b/bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleUtils.java @@ -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"; + } + } + } 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 index 03524e883..2f3c869b4 100644 --- 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 @@ -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 getJavaReferenceValidator(Bundle bundle) { if(bundle == null) return null; @@ -46,7 +63,7 @@ public class OsgiJavaReferenceValidatorFactory implements JavaReferenceValidator @Override public JavaReferenceValidator getJavaReferenceValidator(String bundleName) { - System.out.println("getJavaReferenceValidator(" + bundleName + ")"); + LOGGER.info("getJavaReferenceValidator(" + bundleName + ")"); return getJavaReferenceValidator(getBundle(bundle.getBundleContext(), bundleName)); } -- 2.47.1