From 7b50064579d979d2f3e8901bad32b219c5e1441d 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 Change-Id: I4b383d7987b760c8cbf945ceddac4603c9d4428b --- .../src/org/simantics/scl/osgi/SCLOsgi.java | 1 - .../internal/BundleDocumentationSource.java | 7 ++++- .../scl/osgi/internal/BundleModuleSource.java | 29 ++++++++++++++----- .../scl/osgi/internal/BundleUtils.java | 19 ++++++++++++ .../OsgiJavaReferenceValidatorFactory.java | 25 +++++++++++++--- 5 files changed, 68 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 fc9799de7..23edcccc2 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 @@ -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(); } 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..0f3d9c7c7 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,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"; + } + } + } 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.43.2