Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » OSGi Services query fails(Monitor class works when launched from IDE but not at Export)
OSGi Services query fails [message #825802] Wed, 21 March 2012 09:19 Go to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 362
Registered: July 2009
Senior Member
hi guys!

i wrote a little helper class, which allows me to monitor all my osgi services provided by my application.

so when i launch my Application from within my IDE, everything is working fine and all the services get listed.

but when i export my application and launch this application i run into an nullpointer exception (@see // refs is null here!!)

can someone explain me why this happens?
and/or how to fix it?

thanks in advance
lumo

import java.util.HashMap;

public class ServiceMonitor {
	public static final String SERVICERUNNING = "service_running";
	public static final String SERVICESTOPPED = "service_stopped";

	private static final boolean DEBUG = false;
	private static ServiceMonitor instance;
	private HashMap<String, Object> services;
	private BundleContext context;
	private String[] infos;

	private ServiceMonitor() {
		// construct the singleton instance
	}
	
	public static final ServiceMonitor getInstance() {
		if (instance == null) {
			instance = new ServiceMonitor();
		}
		return instance;
	}

	public static void start(final BundleContext context) throws Exception {
		System.err.println(ServiceMonitor.class.getName() + " started.");
		System.out.println("context = " + context);
		if (getInstance().context == null) {
			getInstance().context = context;
			getInstance().services = new HashMap<String, Object>();
		}
		if (DEBUG) {
			System.out.println(String.format(
					"ServiceMonitor is NOW ready @ %s", instance.context));
		}
	}

	public static void stop() {
	}

	public static String[] getServices() {
		final String filter = "(objectclass=lumo.osgi.service.*)";
		System.out.println("getInstance().context = " + getInstance().context);
		if (getInstance().infos == null) {
			try {
				
				final ServiceReference<?>[] refs = getInstance().context
						.getAllServiceReferences(null, filter);
// refs is null here!!
				System.out.println("refs == null " + (refs == null));
				getInstance().infos = new String[refs.length];
				for (int i = 0; i < refs.length; i++) {
					String name = (String) refs[i]
							.getProperty("component.name");
					name = name.substring(name.lastIndexOf(".") + 1);
					System.err.println(String.format(
							"Service %d [%s] of %d Services checked.", i + 1,
							name, refs.length));
					final String clazz = (String) refs[i]
							.getProperty("component.name");
					if (!getInstance().services.containsKey(clazz)) {
						getInstance().services.put(clazz,
								getInstance().context.getService(refs[i]));
					}
					getInstance().infos[i] = clazz;
				}
			} catch (final InvalidSyntaxException e) {
				e.printStackTrace();
			}
		}
		return getInstance().infos;
	}

	public static boolean isServiceRunning(final Class<?> clazz) {
		final String[] infos = getServices();
		for (final String info : infos) {
			if (info.equals(clazz.getName())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * alias for getService
	 * 
	 * @param clazz
	 * @return
	 * @see #getService(Class)
	 */
	public static Object addService(final Class<?> clazz) {
		return getService(clazz);
	}

	/**
	 * this function automatically adds a service if its not there...
	 * 
	 * @param clazz
	 * @return
	 */
	public static Object getService(final Class<?> clazz) {
		if (clazz != null) {
			if (!getInstance().services.containsKey(clazz.getName())) {
				final ServiceReference<?> ref = getInstance().context
						.getServiceReference(clazz.getName());
				if (ref == null) {
					return null;
				} else {
					getInstance().services.put(clazz.getName(),
							getInstance().context.getService(ref));
					return getInstance().services.get(clazz.getName());
				}
			}
			return getInstance().services.get(clazz.getName());
		}
		return null;
	}

	/**
	 * true, if the service got removed, otherwise false (even if class == null)<br>
	 * not working yet
	 * 
	 * @param clazz
	 * @return
	 */
	public static Boolean removeService(final Class<?> clazz) {
		if (clazz != null) {
			boolean result = false;
			if (getInstance().services.containsKey(clazz.getName())) {
				final ServiceReference<?> ref = getInstance().context
						.getServiceReference(clazz.getName());
				if (ref == null) {
					return Boolean.valueOf(false);
				} else {
					result = getInstance().context.ungetService(ref);
					getInstance().services.remove(clazz.getName());
					if (DEBUG) {
						System.out.println(String.format("%s got %s removed.",
								clazz.getSimpleName(), result ? "successfully"
										: "not"));
					}
					return result;
				}
			}
			if (DEBUG) {
				System.out
						.println(String.format("%s got %s removed.", clazz
								.getSimpleName(), result ? "successfully"
								: "not"));
			}
			return result;
		}
		return Boolean.valueOf(false);
	}
}


UPDATE: i now removed this class from my project and replaced it by normal code (without static references.)
same result! the services are not found when i exported the product, but it's working fine when i launch the product from ide!

UPDATE2: it seems the services are simply not starting in the exported product. although i defined em Quote:
enabled="true" immediate="true"
in the xml...

[Updated on: Wed, 21 March 2012 13:01]

Report message to a moderator

Re: OSGi Services query fails [message #825932 is a reply to message #825802] Wed, 21 March 2012 12:37 Go to previous messageGo to next message
Thomas Watson is currently offline Thomas WatsonFriend
Messages: 437
Registered: July 2009
Senior Member
Quote:

// final ServiceReference<?>[] refs = getInstance().context
// .getServiceReferences(Constants.OBJECTCLASS, filter);
// refs is null here!!


I don't understand how this would ever return non-null. You are asking for all services that match the filter (objectclass=lumo.osgi.service.*) AND were registered under the class name "objectClass". I am assumeing Constants.OBJECTCLASS is from the OSGi constants class org.osgi.framework.Constants

Quote:

UPDATE2: it seems the services are simply not starting in the exported product. although i defined em Quote:

enabled="true" immediate="true"

in the xml...


When exported, is org.eclipse.equinox.ds activated BEFORE your code runs to make sure your service components are allowed to be registered?

HTH

Tom.
Re: OSGi Services query fails [message #825946 is a reply to message #825932] Wed, 21 March 2012 13:01 Go to previous messageGo to next message
Ludwig Moser is currently offline Ludwig MoserFriend
Messages: 362
Registered: July 2009
Senior Member
thanks for your reply Tom,

for your first quote, this code is commented out (forgot to delete it from the quote)
so it should not affect anything (cant remember but somewhere i read that you can also pass the superclass of the bundle, where it stated out that this constatnt.objectclass would return all bundles... - but its not in use anyway)
the // refs is null here!! was meant for the not commented out code Smile

i have the two plugins
org.eclipse.core.runtime
org.eclipse.equinox.ds
as autostart and starlevel (default) 0

how can i ensure the ds is activated before my code?

UPDATE: i switched eclipse version since my last export (where the osgi services were running fine) and when i compare my config ini's

the old one is including the org.eclipse.equinox.ds as osgi.bundle
whie the new one does NOT, BUT it says: equinox.ds=true

i am currently using eclipse 3.7, last export was with 3.6 and the plugins were created with eclipse 3.4 (but this will not make any difference i guess...)

[Updated on: Wed, 21 March 2012 13:12]

Report message to a moderator

Re: OSGi Services query fails [message #895603 is a reply to message #825946] Fri, 13 July 2012 18:12 Go to previous message
Terry Jahelka is currently offline Terry JahelkaFriend
Messages: 23
Registered: February 2012
Junior Member
I am having a similar issue with the EventAdmin service, did you resolve this and if so would you mind sharing your solution?
Previous Topic:NPE when deploying OSGi app with p2 director
Next Topic:What is implemented from OSGi spec v5 ?
Goto Forum:
  


Current Time: Mon Nov 24 04:22:41 GMT 2014

Powered by FUDForum. Page generated in 0.01897 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software