X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.osgi%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fosgi%2Finternal%2FBundleModuleSource.java;h=a805e6026607c2f31e708651ba67fa982d5d5d90;hp=1f60de08688f8365c52ed78a55e378dff162d032;hb=5704e9deb877257b2c0c542e185fda124cf3ce37;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 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 1f60de086..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 @@ -1,27 +1,34 @@ package org.simantics.scl.osgi.internal; + +import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; 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 { +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", ""), @@ -36,16 +43,24 @@ public class BundleModuleSource extends EncodedTextualModuleSource { public final URL url; private byte[] digest; - private ArrayList listeners; + private THashSet listeners; public BundleModuleSource(String moduleName, Bundle bundle, URL url) { super(moduleName); this.bundle = bundle; this.url = url; } + + @Override + public void removeListener(UpdateListener listener) { + if(listeners != null) + synchronized(listeners) { + listeners.remove(listener); + } + } @Override - protected ImportDeclaration[] getBuiltinImports(UpdateListener listener) { + public ImportDeclaration[] getBuiltinImports(UpdateListener listener) { if(bundle.getSymbolicName().equals("org.simantics.scl.runtime")) return DEFAULT_IMPORTS_FOR_STANDARD_LIBRARY; else @@ -66,13 +81,13 @@ public class BundleModuleSource extends EncodedTextualModuleSource { } 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]; } } @@ -84,22 +99,33 @@ public class BundleModuleSource extends EncodedTextualModuleSource { digest = computeDigest(); if(listener != null) { if(listeners == null) - listeners = new ArrayList(2); + listeners = new THashSet(4); listeners.add(listener); + listener.addObservable(this); } 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(); + } } } @@ -108,38 +134,53 @@ public class BundleModuleSource extends EncodedTextualModuleSource { byte[] newDigest = computeDigest(); if(!Arrays.equals(digest, newDigest)) { digest = newDigest; - ArrayList oldListeners = listeners; + THashSet oldListeners = listeners; listeners = null; for(UpdateListener listener : oldListeners) listener.notifyAboutUpdate(); } } } - - private Path getPath() throws IOException { - try { - return Paths.get(FileLocator.toFileURL(url).toURI()); - } catch (URISyntaxException e) { - throw new IOException(e); + + /* + * This code is a copy from org.simantics.utils.ui.BundleUtils + */ + public static File resolveWritableBundleFile(URL url) throws IOException { + // This returns file, jar, http etc. - essentially resolves the bundle protocol + URL resolved = FileLocator.resolve(url); + if (resolved.getProtocol().equals("file")) { + return new File(resolved.getPath()); } + return null; } - + + private Path getPath() throws IOException { + File file = resolveWritableBundleFile(url); + return file != null ? file.toPath() : null; + } + @Override public boolean isUpdateable() { try { - return Files.exists(getPath()); + Path path = getPath(); + if(path == null) + return false; + return Files.exists(path); } catch (IOException e) { + LOGGER.debug("Could not check if {} is updateable", this, e); return false; } } - + @Override public void update(String newSourceText) { try { Path path = getPath(); + if(path == null) + 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(); } @@ -151,4 +192,7 @@ public class BundleModuleSource extends EncodedTextualModuleSource { } } + public JavaReferenceValidatorFactory getJavaReferenceValidatorFactory() { + return new OsgiJavaReferenceValidatorFactory(bundle); + } }