diff options
author | Thomas Watson | 2012-09-06 13:39:29 +0000 |
---|---|---|
committer | Thomas Watson | 2012-09-06 13:39:29 +0000 |
commit | 1d5e88b1dcfca7ff764ce0e9741b1e432b320617 (patch) | |
tree | 555fc9c6eb500a0ee26a617318380e3c38c8a19a | |
parent | 46a0ef83496c84f5e7c36bd5ddf755c235ec9a5e (diff) | |
download | rt.equinox.framework-1d5e88b1dcfca7ff764ce0e9741b1e432b320617.tar.gz rt.equinox.framework-1d5e88b1dcfca7ff764ce0e9741b1e432b320617.tar.xz rt.equinox.framework-1d5e88b1dcfca7ff764ce0e9741b1e432b320617.zip |
Bug 344111 - IllegalStateException in org.eclipse.osgi.framework.internal.protocol.MultiplexingURLStreamHandler.toExternalFormv20120906-133929I20120917-0800I20120916-2000I20120911-1000
7 files changed, 185 insertions, 20 deletions
diff --git a/bundles/org.eclipse.osgi.tests/.classpath b/bundles/org.eclipse.osgi.tests/.classpath index 728b8a713..6fbd14253 100644 --- a/bundles/org.eclipse.osgi.tests/.classpath +++ b/bundles/org.eclipse.osgi.tests/.classpath @@ -111,6 +111,7 @@ <classpathentry kind="src" output="bundle_tests/test.bug306181b" path="bundles_src/test.bug306181b"/> <classpathentry kind="src" output="bundle_tests/test.logging.a" path="bundles_src/test.logging.a"/> <classpathentry kind="src" output="bundle_tests/console.test" path="bundles_src/console.test"/> + <classpathentry kind="src" output="bundle_tests/geturl" path="bundles_src/geturl"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="output" path="bin"/> diff --git a/bundles/org.eclipse.osgi.tests/build.properties b/bundles/org.eclipse.osgi.tests/build.properties index 083368e49..9e2786250 100644 --- a/bundles/org.eclipse.osgi.tests/build.properties +++ b/bundles/org.eclipse.osgi.tests/build.properties @@ -237,7 +237,8 @@ source.bundle_tests/test.logging.a.jar = bundles_src/test.logging.a/ manifest.bundle_tests/test.logging.a.jar = META-INF/MANIFEST.MF source.bundle_tests/console.test.jar = bundles_src/console.test/ manifest.bundle_tests/console.test.jar = META-INF/MANIFEST.MF - +source.bundle_tests/geturl.jar = bundles_src/geturl/ +manifest.bundle_tests/geturl.jar = META-INF/MANIFEST.MF jars.compile.order = bundle_tests/ext.framework.b.jar,\ osgitests.jar,\ @@ -349,4 +350,5 @@ jars.compile.order = bundle_tests/ext.framework.b.jar,\ bundle_tests/test.bug306181a.jar,\ bundle_tests/test.bug306181b.jar,\ bundle_tests/test.logging.a.jar,\ - bundle_tests/console.test.jar + bundle_tests/console.test.jar,\ + bundle_tests/geturl.jar diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/geturl/META-INF/MANIFEST.MF b/bundles/org.eclipse.osgi.tests/bundles_src/geturl/META-INF/MANIFEST.MF new file mode 100644 index 000000000..381e708d1 --- /dev/null +++ b/bundles/org.eclipse.osgi.tests/bundles_src/geturl/META-INF/MANIFEST.MF @@ -0,0 +1,9 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: get url test +Bundle-SymbolicName: geturl +Bundle-Version: 1.0.0 +Bundle-Activator: geturl.Activator +Import-Package: + org.osgi.framework + diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/geturl/geturl/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/geturl/geturl/Activator.java new file mode 100644 index 000000000..665c8e65e --- /dev/null +++ b/bundles/org.eclipse.osgi.tests/bundles_src/geturl/geturl/Activator.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2010, 2012 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package geturl; + +import java.net.MalformedURLException; +import java.net.URL; +import java.security.PrivilegedAction; +import java.util.Dictionary; +import java.util.Hashtable; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +public class Activator implements BundleActivator { + + public void start(BundleContext context) throws Exception { + final URL url = (URL) System.getProperties().get("test.url"); + final String urlSpec = (String) System.getProperties().get("test.url.spec"); + Dictionary props = new Hashtable(); + props.put("test.url", url); + context.registerService(PrivilegedAction.class, new PrivilegedAction() { + + public Object run() { + try { + throw new RuntimeException("Expected to fail to create: " + new URL(urlSpec)); + } catch (MalformedURLException e) { + // expected; the parseURL will cause this to fail + } + try { + new URL(url.getProtocol(), url.getHost(), url.getFile()); + } catch (MalformedURLException e) { + // unexpected; the handler does not get involved and we have a multiplexor cached + throw new RuntimeException("Could not create URL from parts: " + url); + } + url.toExternalForm(); + return Boolean.TRUE; + + } + }, props); + } + + public void stop(BundleContext context) throws Exception { + // TODO Auto-generated method stub + + } + +} diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java index 8e6da60af..b8abe9595 100644 --- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java +++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2011 IBM Corporation and others. + * Copyright (c) 2008, 2012 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 @@ -11,7 +11,8 @@ package org.eclipse.osgi.tests.bundles; import java.io.*; -import java.net.URL; +import java.net.*; +import java.security.PrivilegedAction; import java.util.*; import java.util.jar.*; import junit.framework.Test; @@ -28,6 +29,7 @@ import org.osgi.framework.wiring.*; import org.osgi.service.packageadmin.ExportedPackage; import org.osgi.service.packageadmin.PackageAdmin; import org.osgi.service.startlevel.StartLevel; +import org.osgi.service.url.*; public class SystemBundleTests extends AbstractBundleTests { public static Test suite() { @@ -1142,6 +1144,96 @@ public class SystemBundleTests extends AbstractBundleTests { assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox2.getState()); //$NON-NLS-1$ } + class TestHandler extends AbstractURLStreamHandlerService { + + public URLConnection openConnection(URL u) throws IOException { + throw new IOException(); + } + + } + + public void testURLMultiplexing01() throws BundleException { + // create multiple instances of Equinox to test + File config1 = OSGiTestsActivator.getContext().getDataFile(getName() + "_1"); + Properties configuration1 = new Properties(); + configuration1.put(Constants.FRAMEWORK_STORAGE, config1.getAbsolutePath()); + Equinox equinox1 = new Equinox(configuration1); + try { + equinox1.start(); + } catch (BundleException e) { + fail("Unexpected exception in init()", e); //$NON-NLS-1$ + } + // should be in the STARTING state + assertEquals("Wrong state for SystemBundle", Bundle.ACTIVE, equinox1.getState()); //$NON-NLS-1$ + + File config2 = OSGiTestsActivator.getContext().getDataFile(getName() + "_2"); //$NON-NLS-1$ + Properties configuration2 = new Properties(); + configuration2.put(Constants.FRAMEWORK_STORAGE, config2.getAbsolutePath()); + Equinox equinox2 = new Equinox(configuration2); + try { + equinox2.start(); + } catch (BundleException e) { + fail("Unexpected exception in init()", e); //$NON-NLS-1$ + } + // should be in the STARTING state + assertEquals("Wrong state for SystemBundle", Bundle.ACTIVE, equinox2.getState()); //$NON-NLS-1$ + + BundleContext systemContext1 = equinox1.getBundleContext(); + assertNotNull("System context is null", systemContext1); //$NON-NLS-1$ + BundleContext systemContext2 = equinox2.getBundleContext(); + assertNotNull("System context is null", systemContext2); //$NON-NLS-1$ + + assertNotSame(systemContext1, systemContext2); + + // register a protocol hander in the "root" framework + Dictionary props = new Hashtable(); + props.put(URLConstants.URL_HANDLER_PROTOCOL, getName().toLowerCase()); + ServiceRegistration handlerReg = OSGiTestsActivator.getContext().registerService(URLStreamHandlerService.class, new TestHandler(), props); + try { + URL baseTestUrl = new URL(getName().toLowerCase(), "", "/test/url"); + System.getProperties().put("test.url", baseTestUrl); + System.setProperty("test.url.spec", baseTestUrl.toExternalForm()); + } catch (MalformedURLException e) { + fail("Unexpected url exception.", e); + } + + Bundle geturlBundle = systemContext1.installBundle(installer.getBundleLocation("geturl")); + geturlBundle.start(); + PrivilegedAction geturlAction = (PrivilegedAction) systemContext1.getService(systemContext1.getServiceReference(PrivilegedAction.class)); + try { + geturlAction.run(); + } catch (Exception e) { + fail("Unexpected exception", e); + } + + // put the framework 1 back to the RESOLVED state + try { + equinox1.stop(); + } catch (BundleException e) { + fail("Unexpected error stopping framework", e); //$NON-NLS-1$ + } + try { + equinox1.waitForStop(10000); + } catch (InterruptedException e) { + fail("Unexpected interrupted exception", e); //$NON-NLS-1$ + } + assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox1.getState()); //$NON-NLS-1$ + + // put the framework 2 back to the RESOLVED state + try { + equinox2.stop(); + } catch (BundleException e) { + fail("Unexpected erorr stopping framework", e); //$NON-NLS-1$ + } + try { + equinox2.waitForStop(10000); + } catch (InterruptedException e) { + fail("Unexpected interrupted exception", e); //$NON-NLS-1$ + } + assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox2.getState()); //$NON-NLS-1$ + handlerReg.unregister(); + } + public void testUUID() { File config1 = OSGiTestsActivator.getContext().getDataFile(getName() + "_1"); //$NON-NLS-1$ Map configuration1 = new HashMap(); diff --git a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/protocol/MultiplexingURLStreamHandler.java b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/protocol/MultiplexingURLStreamHandler.java index 4bc71a844..e3bb90f89 100644 --- a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/protocol/MultiplexingURLStreamHandler.java +++ b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/protocol/MultiplexingURLStreamHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2010 Cognos Incorporated, IBM Corporation and others. + * Copyright (c) 2006, 2012 Cognos Incorporated, 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 @@ -28,8 +28,9 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { private static Field handlerField; private static boolean methodsInitialized = false; - private String protocol; - private StreamHandlerFactory factory; + private final String protocol; + private final StreamHandlerFactory factory; + private final URLStreamHandler authorized; private static synchronized void initializeMethods(StreamHandlerFactory factory) { if (methodsInitialized) @@ -80,14 +81,15 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { methodsInitialized = true; } - public MultiplexingURLStreamHandler(String protocol, StreamHandlerFactory factory) { + public MultiplexingURLStreamHandler(String protocol, StreamHandlerFactory factory, URLStreamHandler authorized) { this.protocol = protocol; this.factory = factory; + this.authorized = authorized; initializeMethods(factory); } protected URLConnection openConnection(URL url) throws IOException { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { return (URLConnection) openConnectionMethod.invoke(handler, new Object[] {url}); @@ -104,7 +106,7 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { } protected boolean equals(URL url1, URL url2) { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { return ((Boolean) equalsMethod.invoke(handler, new Object[] {url1, url2})).booleanValue(); @@ -119,7 +121,7 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { } protected int getDefaultPort() { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { return ((Integer) getDefaultPortMethod.invoke(handler, (Object[]) null)).intValue(); @@ -134,7 +136,7 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { } protected InetAddress getHostAddress(URL url) { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { return (InetAddress) getHostAddressMethod.invoke(handler, new Object[] {url}); @@ -149,7 +151,7 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { } protected int hashCode(URL url) { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { return ((Integer) hashCodeMethod.invoke(handler, new Object[] {url})).intValue(); @@ -164,7 +166,7 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { } protected boolean hostsEqual(URL url1, URL url2) { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { return ((Boolean) hostsEqualMethod.invoke(handler, new Object[] {url1, url2})).booleanValue(); @@ -197,7 +199,7 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { } protected boolean sameFile(URL url1, URL url2) { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { return ((Boolean) sameFileMethod.invoke(handler, new Object[] {url1, url2})).booleanValue(); @@ -212,7 +214,7 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { } protected void setURL(URL arg0, String arg1, String arg2, int arg3, String arg4, String arg5, String arg6, String arg7, String arg8) { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { // set the real handler for the URL @@ -230,7 +232,7 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { } protected String toExternalForm(URL url) { - URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(protocol); + URLStreamHandler handler = findAuthorizedURLStreamHandler(protocol); if (handler != null) { try { return (String) toExternalFormMethod.invoke(handler, new Object[] {url}); @@ -244,4 +246,8 @@ public class MultiplexingURLStreamHandler extends URLStreamHandler { throw new IllegalStateException(); } + private URLStreamHandler findAuthorizedURLStreamHandler(String requested) { + URLStreamHandler handler = factory.findAuthorizedURLStreamHandler(requested); + return handler == null ? authorized : handler; + } } diff --git a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/protocol/StreamHandlerFactory.java b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/protocol/StreamHandlerFactory.java index 3e38687f7..0c4677539 100644 --- a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/protocol/StreamHandlerFactory.java +++ b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/protocol/StreamHandlerFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2012 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 @@ -111,8 +111,9 @@ public class StreamHandlerFactory extends MultiplexingFactory implements URLStre return null; // let the VM handle it URLStreamHandler result = null; if (isMultiplexing()) { - if (findAuthorizedURLStreamHandler(protocol) != null) - result = new MultiplexingURLStreamHandler(protocol, this); + URLStreamHandler authorized = findAuthorizedURLStreamHandler(protocol); + if (authorized != null) + result = new MultiplexingURLStreamHandler(protocol, this, authorized); } else { result = createInternalURLStreamHandler(protocol); } |