[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.rcp] Re: OSGI/RCP Errors

It looks like the HelloService hasn't been created yet.

This is one of the interesting features of OSGi services: they might not
be there.

Consider instead using a ServiceTracker. The ServiceTracker will be
notified via the addingService method when a service of the specified
type becomes available. Using a service tracker, you don't have to worry
about timing. 

Also, assuming that you really want to do something far more interesting
than just say hello, using a ServiceTracker will move the execution of
the code in the service out of the start(BundleContext) method which
should make startup more efficient.

helloTracker = new ServiceTracker(context, HelloService.class.getName(),
null) {
  public Object addingService(ServiceReference reference) {
    Object service = super.addingService(reference);
    HelloService helloService = (HelloService)service;
    System.out.println(helloService.sayHello());
  }
helloTracker.open();

Be sure to close the tracker in the stop(BundleContext) method.

HTH,

Wayne

On Tue, 2008-05-13 at 22:57 +0000, Shaun wrote:
> Hello,
> 
> I have an issue with calling an OSGI service. Using this activator code:
> 
> package com.javaworld.sample.helloworldrcp;
> 
> import org.eclipse.jface.resource.ImageDescriptor;
> import org.eclipse.ui.plugin.AbstractUIPlugin;
> import org.osgi.framework.BundleContext;
> import org.osgi.framework.ServiceReference;
> 
> import com.javaworld.sample.helloservice.HelloService;
> 
> /**
>  * The activator class controls the plug-in life cycle
>  */
> public class RCPActivator extends AbstractUIPlugin {
> 	public static final String PLUGIN_ID = 
> "com.javaworld.sample.helloworldRCP";
> 	private static RCPActivator plugin;
> 
> 	public RCPActivator() {
> 	}
> 
> 	ServiceReference helloServiceReference;
> 
> 	public void start(BundleContext context) throws Exception {
> 		super.start(context);
> 		helloServiceReference = context.getServiceReference(HelloService.class
> 				.getName());
> 		HelloService helloService = (HelloService) context
> 				.getService(helloServiceReference);
> 		System.out.println(helloService.sayHello());
> 		plugin = this;
> 	}
> 
> 	public void stop(BundleContext context) throws Exception {
> 		super.stop(context);
> 		context.ungetService(helloServiceReference);
> 		plugin = null;
> 	}
> 
> 	public static RCPActivator getDefault() {
> 		return plugin;
> 	}
> 
> 	public static ImageDescriptor getImageDescriptor(String path) {
> 		return imageDescriptorFromPlugin(PLUGIN_ID, path);
> 	}
> }
> 
> I can run this project as an OSGI framework and the calls to HelloService 
> work correctly.
> 
> If I run this same exact code as an Eclipse Application (which it is an 
> RCP application with a view), the OSGI calls crash giving me this error.
> 
> !ENTRY org.eclipse.osgi 4 0 2008-05-13 15:49:03.220
> !MESSAGE Application error
> !STACK 1
> org.eclipse.core.runtime.CoreException: Plug-in 
> com.javaworld.sample.helloworldRCP was unable to load class 
> com.javaworld.sample.helloworldrcp.Application.
> 	atorg.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.throwException(RegistryStrategyOSGI.java:180)
> 	atorg.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:162)
> 	atorg.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:788)
> 	atorg.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243)
> 	atorg.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:51)
> 	atorg.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:165)
> 	atorg.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:106)
> 	atorg.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:76)
> 	atorg.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:363)
> 	atorg.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:176)
> 	atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 	atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 	atjava.lang.reflect.Method.invoke(Method.java:597)
> 	atorg.eclipse.equinox.launcher.Main.invokeFramework(Main.java:508)
> 	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
> 	at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
> 	at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
> org.eclipse.core.runtime.CoreException[1]: 
> org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter$TerminatingClassNotFoundException: 
> An error occurred while automatically activating bundle 
> com.javaworld.sample.helloworldRCP (6).
> 	atorg.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:125)
> 	atorg.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:417)
> 	atorg.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:189)
> 	atorg.eclipse.osgi.framework.internal.core.BundleLoader.findLocalClass(BundleLoader.java:340)
> 	atorg.eclipse.osgi.framework.internal.core.BundleLoader.findClassInternal(BundleLoader.java:408)
> 	atorg.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:369)
> 	atorg.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:357)
> 	atorg.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:83)
> 	at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
> 	at 
> org.eclipse.osgi.framework.internal.core.BundleLoader.loadClass(BundleLoader.java:289)
> 	atorg.eclipse.osgi.framework.internal.core.BundleHost.loadClass(BundleHost.java:227)
> 	atorg.eclipse.osgi.framework.internal.core.AbstractBundle.loadClass(AbstractBundle.java:1269)
> 	atorg.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:160)
> 	atorg.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:788)
> 	atorg.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243)
> 	atorg.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:51)
> 	atorg.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:165)
> 	atorg.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:106)
> 	atorg.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:76)
> 	atorg.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:363)
> 	atorg.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:176)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 	atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 	atjava.lang.reflect.Method.invoke(Method.java:597)
> 	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:508)
> 	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
> 	at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
> 	at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
> Caused by: org.osgi.framework.BundleException: Exception in 
> com.javaworld.sample.helloworldrcp.RCPActivator.start() of bundle 
> com.javaworld.sample.helloworldRCP.
> 	at 
> org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:1018)
> 	at 
> org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:974)
> 	at 
> org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:346)
> 	at 
> org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:260)
> 	at 
> org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:400)
> 	at 
> org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:111)
> 	... 28 more
> Causedby:java.lang.NullPointerExceptionatorg.eclipse.osgi.framework.internal.core.BundleContextImpl.getService(BundleContextImpl.java:880)
> 	at 
> com.javaworld.sample.helloworldrcp.RCPActivator.start(RCPActivator.java:27)
> 	at 
> org.eclipse.osgi.framework.internal.core.BundleContextImpl$2.run(BundleContextImpl.java:999)
> 	at java.security.AccessController.doPrivileged(Native Method)
> 	at 
> org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:993)
> 	... 33 more
> 
> I member of my team with the same configuration has it working on his 
> machine, we can't seem to pinpoint the problem though.
> 
> Any suggestions would be helpful.
> 
> -Shaun
>