diff options
author | Thomas Watson | 2015-05-29 15:47:45 +0000 |
---|---|---|
committer | Thomas Watson | 2015-05-29 18:57:58 +0000 |
commit | 89ba02d5324b563b284d881f9dd6fb619bd33bcc (patch) | |
tree | cf793d8d6d1fc0056d3de956de962aac81546f8b | |
parent | 456cd343ff6bf630e9f1571bc51d5ab26b20eeaf (diff) | |
download | rt.equinox.framework-89ba02d5324b563b284d881f9dd6fb619bd33bcc.tar.gz rt.equinox.framework-89ba02d5324b563b284d881f9dd6fb619bd33bcc.tar.xz rt.equinox.framework-89ba02d5324b563b284d881f9dd6fb619bd33bcc.zip |
Bug 468817 - Implementation of BundleFileWrapper.getResourceURL does notI20150529-2000
allow custom BundleEntry to be used
Change-Id: I42d611638e47933d2da4437539a80da0fccbb671
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
4 files changed, 126 insertions, 28 deletions
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/wrapper.hooks.a/wrapper/hooks/a/TestHookConfigurator.java b/bundles/org.eclipse.osgi.tests/bundles_src/wrapper.hooks.a/wrapper/hooks/a/TestHookConfigurator.java index 0ea6d109d..80459addb 100644..100755 --- a/bundles/org.eclipse.osgi.tests/bundles_src/wrapper.hooks.a/wrapper/hooks/a/TestHookConfigurator.java +++ b/bundles/org.eclipse.osgi.tests/bundles_src/wrapper.hooks.a/wrapper/hooks/a/TestHookConfigurator.java @@ -10,19 +10,67 @@ *******************************************************************************/ package wrapper.hooks.a; -import java.net.URL; -import java.util.Collection; +import java.io.*; +import java.net.*; import org.eclipse.osgi.container.Module; import org.eclipse.osgi.internal.hookregistry.*; -import org.eclipse.osgi.service.urlconversion.URLConverter; import org.eclipse.osgi.storage.BundleInfo.Generation; -import org.eclipse.osgi.storage.bundlefile.BundleFile; -import org.eclipse.osgi.storage.bundlefile.BundleFileWrapper; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; +import org.eclipse.osgi.storage.bundlefile.*; public class TestHookConfigurator implements HookConfigurator { public void addHooks(HookRegistry hookRegistry) { + + BundleFileWrapperFactoryHook modifyContent = new BundleFileWrapperFactoryHook() { + + @Override + public BundleFileWrapper wrapBundleFile(BundleFile bundleFile, Generation generation, boolean base) { + return new BundleFileWrapper(bundleFile) { + + @Override + public BundleEntry getEntry(String path) { + final BundleEntry original = super.getEntry(path); + final byte[] content = "CUSTOM_CONTENT".getBytes(); + if ("data/resource1".equals(path)) { + return new BundleEntry() { + + @Override + public long getTime() { + return original.getTime(); + } + + @Override + public long getSize() { + return content.length; + } + + @Override + public String getName() { + return original.getName(); + } + + @Override + public URL getLocalURL() { + return original.getLocalURL(); + } + + @SuppressWarnings("unused") + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(content); + } + + @Override + public URL getFileURL() { + return original.getFileURL(); + } + }; + } + return original; + } + + }; + } + }; BundleFileWrapperFactoryHook noop = new BundleFileWrapperFactoryHook() { @Override public BundleFileWrapper wrapBundleFile(BundleFile bundleFile, Generation generation, boolean base) { @@ -34,6 +82,9 @@ public class TestHookConfigurator implements HookConfigurator { // add no-op before the getResourceURL override hookRegistry.addBundleFileWrapperFactoryHook(noop); + // add a hook that modifies content + hookRegistry.addBundleFileWrapperFactoryHook(modifyContent); + hookRegistry.addBundleFileWrapperFactoryHook(new BundleFileWrapperFactoryHook() { @Override @@ -41,22 +92,28 @@ public class TestHookConfigurator implements HookConfigurator { return new BundleFileWrapper(bundleFile) { @Override public URL getResourceURL(String path, Module hostModule, int index) { - URL url = super.getResourceURL(path, hostModule, index); - if (url != null) { - try { - return converter(hostModule, url); - } catch (Exception e) { - e.printStackTrace(); - } - } - return null; + // just making sure the wrapper getResourceURL is never called + throw new RuntimeException("Should not be called"); } - private URL converter(Module module, URL url) throws Exception { - // This is just test code, don't get the URLConverter this way in real code - BundleContext systemContext = module.getContainer().getModule(0).getBundle().getBundleContext(); - Collection<ServiceReference<URLConverter>> converters = systemContext.getServiceReferences(URLConverter.class, "(protocol=" + url.getProtocol() + ")"); - return systemContext.getService(converters.iterator().next()).resolve(url); + @Override + protected URL createResourceURL(BundleEntry bundleEntry, Module hostModule, int index, String path) { + final URL url = super.createResourceURL(bundleEntry, hostModule, index, path); + if (url == null) { + return null; + } + try { + return new URL("custom", "custom", 0, path, new URLStreamHandler() { + + @Override + protected URLConnection openConnection(URL u) throws IOException { + // TODO Auto-generated method stub + return url.openConnection(); + } + }); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } } }; diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/BundleFileWrapperFactoryHookTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/BundleFileWrapperFactoryHookTests.java index 4af7ed3d1..cb791c238 100644 --- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/BundleFileWrapperFactoryHookTests.java +++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/BundleFileWrapperFactoryHookTests.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.osgi.tests.hooks.framework; -import java.io.File; +import java.io.*; import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -59,7 +59,28 @@ public class BundleFileWrapperFactoryHookTests extends AbstractFrameworkHookTest initAndStartFramework(); Bundle b = installBundle(); - URL url = b.getResource("data/resource1"); - assertTrue("Wrong protocol used: " + url, "jar".equals(url.getProtocol()) || "file".equals(url.getProtocol())); + URL url1 = b.getResource("data/resource1"); + assertEquals("Wrong protocol used: " + url1, "custom", url1.getProtocol()); + assertEquals("Wrong content found.", "CUSTOM_CONTENT", readURL(url1)); + } + + private String readURL(URL url) { + StringBuffer sb = new StringBuffer(); + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); + try { + for (String line = reader.readLine(); line != null;) { + sb.append(line); + line = reader.readLine(); + if (line != null) + sb.append('\n'); + } + } finally { + reader.close(); + } + } catch (IOException e) { + fail("Unexpected exception reading url: " + url.toExternalForm(), e); //$NON-NLS-1$ + } + return sb.toString(); } } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/bundlefile/BundleFile.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/bundlefile/BundleFile.java index e4ddde7fc..4bdde96df 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/bundlefile/BundleFile.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/bundlefile/BundleFile.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2013 IBM Corporation and others. + * Copyright (c) 2004, 2015 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -119,7 +119,14 @@ abstract public class BundleFile { abstract public boolean containsDir(String dir); /** - * Returns a URL to access the contents of the entry specified by the path + * Returns a URL to access the contents of the entry specified by the path. + * This method first calls {@link #getEntry(String)} to locate the entry + * at the specified path. If no entry is found {@code null} is returned; + * otherwise {@link #createResourceURL(BundleEntry, Module, int, String)} + * is called in order to create the URL. Subclasses should not override + * this method. Instead the methods {@link #getEntry(String)} and/or + * {@link #createResourceURL(BundleEntry, Module, int, String)} may be + * overriden to augment the behavior. * @param path the path to the resource * @param hostModule the host module * @param index the resource index @@ -129,6 +136,18 @@ abstract public class BundleFile { BundleEntry bundleEntry = getEntry(path); if (bundleEntry == null) return null; + return createResourceURL(bundleEntry, hostModule, index, path); + } + + /** + * Creates a URL to access the content of the specified entry + * @param bundleEntry the bundle entry + * @param hostModule the host module + * @param index the resource index + * @param path + * @return a URL to access the contents of the specified entry + */ + protected URL createResourceURL(BundleEntry bundleEntry, Module hostModule, int index, String path) { long hostBundleID = hostModule.getId(); path = fixTrailingSlash(path, bundleEntry); try { diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/bundlefile/BundleFileWrapper.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/bundlefile/BundleFileWrapper.java index bf4a3a020..c26dbb5f6 100755 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/bundlefile/BundleFileWrapper.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/storage/bundlefile/BundleFileWrapper.java @@ -87,7 +87,8 @@ public class BundleFileWrapper extends BundleFile { } @Override - public URL getResourceURL(String path, Module hostModule, int index) { - return bundleFile.getResourceURL(path, hostModule, index); + protected URL createResourceURL(BundleEntry bundleEntry, Module hostModule, int index, String path) { + return bundleFile.createResourceURL(bundleEntry, hostModule, index, path); } + } |