diff options
author | Thomas Watson | 2021-09-07 21:25:00 +0000 |
---|---|---|
committer | Thomas Watson | 2021-09-08 13:30:45 +0000 |
commit | 9b837b517d9829e774b69706736d593bdef66022 (patch) | |
tree | 0331458948e1e2b0895849224bda8e3ff8fca696 | |
parent | 1a27b3f9af6883206220e793b7ef71af6613dbab (diff) | |
download | rt.equinox.framework-9b837b517d9829e774b69706736d593bdef66022.tar.gz rt.equinox.framework-9b837b517d9829e774b69706736d593bdef66022.tar.xz rt.equinox.framework-9b837b517d9829e774b69706736d593bdef66022.zip |
Bug 575815 - Improve message about service object not instance of toI20210919-1800I20210919-0120I20210918-1800I20210917-1800I20210917-0000I20210916-2150I20210916-1800I20210915-1800I20210915-0700I20210915-0510I20210915-0150I20210914-1800I20210913-2050I20210911-1800I20210910-1800I20210909-1800I20210908-1800
include the object classname
Change-Id: I9295dd17ab1a47f1785f3abbcb3f49c8db6424c0
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/equinox/rt.equinox.framework/+/185120
Tested-by: Equinox Bot <equinox-bot@eclipse.org>
3 files changed, 58 insertions, 4 deletions
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/serviceregistry/ServiceRegistryTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/serviceregistry/ServiceRegistryTests.java index 49afd98e5..2e4a99f62 100644 --- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/serviceregistry/ServiceRegistryTests.java +++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/serviceregistry/ServiceRegistryTests.java @@ -17,12 +17,22 @@ import java.util.Dictionary; import java.util.Hashtable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import junit.framework.Test; import junit.framework.TestSuite; import org.eclipse.osgi.tests.OSGiTestsActivator; import org.eclipse.osgi.tests.bundles.AbstractBundleTests; import org.eclipse.osgi.tests.util.MapDictionary; -import org.osgi.framework.*; +import org.osgi.framework.Bundle; +import org.osgi.framework.Constants; +import org.osgi.framework.FrameworkEvent; +import org.osgi.framework.FrameworkListener; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceFactory; +import org.osgi.framework.ServiceListener; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; public class ServiceRegistryTests extends AbstractBundleTests { public static Test suite() { @@ -532,6 +542,42 @@ public class ServiceRegistryTests extends AbstractBundleTests { } } + public void testWrongServiceFactoryObject() throws InterruptedException { + AtomicReference<String> errorMsg = new AtomicReference<>(); + CountDownLatch gotEvent = new CountDownLatch(1); + FrameworkListener fwkListener = (e) -> { + if (e.getType() == FrameworkEvent.ERROR && e.getThrowable() != null) { + errorMsg.set(e.getThrowable().getMessage()); + gotEvent.countDown(); + } + }; + ServiceRegistration<Runnable> reg = OSGiTestsActivator.getContext().registerService(Runnable.class, + new ServiceFactory() { + + @Override + public Object getService(Bundle bundle, ServiceRegistration registration) { + return "Wrong object!!"; + } + + @Override + public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) { + } + + }, null); + OSGiTestsActivator.getContext().addFrameworkListener(fwkListener); + try { + ServiceReference<Runnable> ref = reg.getReference(); + Runnable service = OSGiTestsActivator.getContext().getService(ref); + assertNull(service); + gotEvent.await(30, TimeUnit.SECONDS); + assertNotNull(errorMsg.get()); + assertTrue("Wrong error message: " + errorMsg.get(), errorMsg.get().contains(String.class.getName())); + } finally { + OSGiTestsActivator.getContext().removeFrameworkListener(fwkListener); + reg.unregister(); + } + } + private void clearResults(boolean[] results) { for (int i = 0; i < results.length; i++) results[i] = false; diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceFactoryUse.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceFactoryUse.java index 374ee21f3..eb441dea5 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceFactoryUse.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceFactoryUse.java @@ -20,7 +20,11 @@ import org.eclipse.osgi.internal.debug.Debug; import org.eclipse.osgi.internal.framework.BundleContextImpl; import org.eclipse.osgi.internal.messages.Msg; import org.eclipse.osgi.util.NLS; -import org.osgi.framework.*; +import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkEvent; +import org.osgi.framework.ServiceException; +import org.osgi.framework.ServiceFactory; +import org.osgi.framework.ServiceRegistration; /** * This class represents the use of a service by a bundle. One is created for each @@ -239,7 +243,11 @@ public class ServiceFactoryUse<S> extends ServiceUse<S> { if (debug.DEBUG_SERVICES) { Debug.println("Service object is not an instanceof " + invalidService); //$NON-NLS-1$ } - ServiceException se = new ServiceException(NLS.bind(Msg.SERVICE_FACTORY_NOT_INSTANCEOF_CLASS_EXCEPTION, factory.getClass().getName(), invalidService), ServiceException.FACTORY_ERROR); + ServiceException se = new ServiceException( + NLS.bind(Msg.SERVICE_FACTORY_NOT_INSTANCEOF_CLASS_EXCEPTION, + new Object[] { factory.getClass().getName(), service.getClass().getName(), + invalidService }), + ServiceException.FACTORY_ERROR); context.getContainer().getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, registration.getBundle(), se); return null; } diff --git a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/messages/ExternalMessages.properties b/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/messages/ExternalMessages.properties index d3b57bdb1..9b5cdfb45 100644 --- a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/messages/ExternalMessages.properties +++ b/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/messages/ExternalMessages.properties @@ -23,7 +23,7 @@ MANIFEST_IOEXCEPTION=An error occurred while reading the manifest file. SERVICE_FACTORY_EXCEPTION=Exception in {0}.{1}() SERVICE_FACTORY_RECURSION=Recursive ServiceFactory call in {0}.{1}() SERVICE_NOT_INSTANCEOF_CLASS_EXCEPTION=The service object is not an instance of the service class {0} -SERVICE_FACTORY_NOT_INSTANCEOF_CLASS_EXCEPTION={0}.getService() returned a service object that is not an instance of the service class {1} +SERVICE_FACTORY_NOT_INSTANCEOF_CLASS_EXCEPTION={0}.getService() returned a service object of type {1} that is not an instance of the service class {2} SERVICE_OBJECT_NULL_EXCEPTION={0}.getService() returned a null service object SERVICE_ARGUMENT_NULL_EXCEPTION=The service parameter is null SERVICE_OBJECTS_UNGET_ARGUMENT_EXCEPTION=The service parameter was not provided by this object |